Intermittent Index Exceeding Matrix Dimensions Error

2 views (last 30 days)
Hello,
I have an image processing code that intermittently produces an error and I cannot figure out why it happens. I want to first say this is going to be a wall of text and I will do my best to explain my code and when the error occurs. I apologize if it is confusing, and thank you for reading.
The code counts the number of droplets and creates a .txt file which includes the number, location, and area of each droplet. It works chronologically as follows:
  • The image is imported and converted to grayscale
  • roipoly is used to draw a polygon which contains the droplets of interest
  • Burn the created mask from roipoly into the image and convert to black and white
  • Use the command bwboundaries to find the boundaries of the droplets within the mask
  • With the results of this command, everything is found and an output file is generated
These steps usually work, however, there is sometimes an issue which arises mostly when I am analyzing an image with a large amount of droplets. I pass the results from bwboundaries into a for loop in order to find their locations and area. This is done by using the following block of code where bwimg is my black and white image which has edges of the drawn polygon mask. (I left comments in the code to help you read what it's doing)
[B,L,N,A]=bwboundaries(bwimg);
for i=1+N:length(B)
perpixel(i-N)=length(B{i,1}); %The amount of pixels in each droplet perimeter
xloc=B{i}(:,2); %x-locations of each pixel perimeter in the cell array B
xmean(i-N)=mean(xloc); %Array of mean x-location for each droplet
yloc=B{i}(:,1); %y-locations of each pixel perimeter in the cell array B
ymean(i-N)=mean(yloc); %Array of mean y-locations for each droplet
count(i-N)=i-N; %Counter (array of the number of droplets)
ploc(i-N,:)=[xmean(i-1),ymean(i-1)]; %Location of each pixel (x,y) in pixels
parea=regionprops(~bwimg,'Area');
parea2=struct2cell(parea);
area(i-N)=scale^2*parea2{i-1}; %Area of the droplets in mm^2
end
Sometimes (definitely more so when I have an image with a large number of droplets), the code stops and produces this error:
Index exceeds matrix dimensions.
Error in BloodspatterAnalysisCode (line 144)
area(i-N)=scale^2*parea2{i-1}; %Area of the droplets in mm^2
Sometimes, it doesn't get to that point in the code and stops even sooner at the line which finds the mean x-locations with the same error. That looks something like this:
Index exceeds matrix dimensions.
xmean(i-N)=mean(xloc); %Array of mean x-location for each droplet
Any idea why this is happening? I suspect it has something to do with either how the function mean calculates mean values, or how struct2cell works, but their respective help files haven't brought anything to my attention. I've been working around the issue whenever it arises by simply choosing a smaller polygon window (and therefore less droplets) but it gets to be a pain when I have >5000 droplets.
Any idea as to why my errors occur would be much appreciated, thanks for your help,
Patrick

Answers (1)

Star Strider
Star Strider on 18 Aug 2015
My guess is that the RHS in both assignments is returning a vector rather than a scalar in some situations. A quick fix (that you would have to deal with later) would be to convert ‘area’ and ‘xmean’ to cells, for instance:
area{i-N} = ...
Also, area is a MATLAB plot function. Using function names as variable names can cause problems, so it’s best to change the name to something else, such as ‘areax’.
  2 Comments
Patrick Comiskey
Patrick Comiskey on 18 Aug 2015
Ok, I'll try the cell conversion. Good call on the variable area, I forgot that it was a built in function, I'll have to change that.Thanks for your help, I'll have to run the code a bunch more to see if these fixes help.
Star Strider
Star Strider on 18 Aug 2015
My pleasure.
The benefit to the cell assignments are that they will also tell you the sizes of the individual cells, so you know by displaying them (or hovering over them with the mouse) which ones are vectors. That can help you troubleshoot your code.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!