Error in batch image processing: Index exceeds the number of array elements. Index must not exceed 0.

I am trying to process a batch of images. I can do the function just fine in the regular code but need to apply it to lots of pictures. I keep getting this error and am confused because I did exactly what matlab told me to do. The error is "Index exceeds the number of array elements. Index must not exceed 0."
I will need to draw a region of interest on each picture and then take the average rgb values from that, then calculate the blue index. (All which I can do).
This is the output to the command window i'm getting:
>> jullly13
Index exceeds the number of array elements. Index must not exceed 0.
Error in jullly13 (line 26)
im = varargin{1};
This is my code to open the processor (which works).
clear all
%new code july 13
%create image datastore
dataDir=pwd
imds=imageDatastore(dataDir,FileExtensions='.jpg');
imgs=readall(imds);
%load datastore into batch processor
imageBatchProcessor(imds)
This is my code to process the images in the batch processor (the one I need help on. )
function results = myimfcn(varargin)
%Image Processing Function
%
% VARARGIN - Can contain up to two inputs:
% IM - First input is a numeric array containing the image data.
% INFO - Second input is a scalar structure containing information about
% the input image source.
%
% INFO can be used to obtain metadata about the image read.
% To apply a batch function using the INFO argument, you must select the
% Include Image Info check box in the app toolstrip.
%
% RESULTS - A scalar struct with the processing results.
%
%
%
%--------------------------------------------------------------------------
% Auto-generated by imageBatchProcessor App.
%
% When used by the App, this function will be called for each input image
% file automatically.
%
%--------------------------------------------------------------------------
% Input parsing------------------------------------------------------------
im = varargin{1};
if nargin == 2
info = varargin{2};
disp(info);
end
% Replace the sample below with your code----------------------------------
%create file name and get the image info
fileName=info.Filename
imginfo=rawinfo(fileName)
%select region of interest in the image
h=drawrectangle('Position',[852,670,756,376],'StripeColor', 'b') %selects rectangle
roi=createMask(h)
%find average rgb values for pixels in region of interest
[row,col]=find(mask);
RGBpixels=impixel(I,col,row);
pos=h.Position
%get average rgb values from region of interest in each image, print those
%along with blue index values
avgrgb=mean(RGBpixels)
fprintf('The average Red, Green, and Blue values of the ROI are %6.3f, %6.3f, and %6.3f.\n',avgrgb)
%calculate blue index for each image
%%blueindex=((red+green+blue)/blue)
%%fprintf('The blue index of the roi is %6.3f',blueindex) will
%%calculate later
%--------------------------------------------------------------------------

답변 (1개)

The error is being generated against line 26 of jullly13.m which you do not show us the source code for.
You show us the source code for myimfcn(), which is a function that needs to be invoked with exactly two parameters, the first of which must exist but is otherwise ignored, and the second of which must be a struct or object with field or property Filename . The function must be called outside of any context that needs a return value, because the function fails to assign anything to the return parameter named results
It looks like you stored myimfcn inside of jully13.m and then tried to run the function without providing any input parameters.
You should probably be running the script instead, and the script should probably be changed to configure myimfcn as the name of the reading function for the imds .

댓글 수: 2

The jully13 is the second one I pasted.
What do you mean by running the script? And How would I add the input parameters. I'm just confused because I thought the only thing I had to do with the code was to paste my code where it says to.
Thanks!
Above, you showed some code preceeded by
This is my code to open the processor (which works).
The code that was posted immediately after that is the script that you have to run -- after editting it to set @myimfcn as the image reading function in the imageDataStore call.
(It is recommended that you store the myimfcn function in myimfcn.m rather than in jully13.m to avoid confusion.)

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2023년 7월 13일

댓글:

2023년 7월 14일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by