Error using == Matrix dimensions must agree.

1 view (last 30 days)
I'm using the code below to find values, that is smaller than 500, but I get this error:
"Error using == Matrix dimensions must agree".
any one knows how to fix this problem?
thanks.
my code:
// BW is an image that I'm working on.
L = bwlabeln(BW);
s = regionprops(L);
Areal=[s.Area];
[~,dx] = find(Areal >= 500);
BW(L == dx)=0;
figure (5)
imagesc(BW)
  1 Comment
Adam
Adam on 14 Oct 2014
Does the error message not indicate the line which caused it?

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 14 Oct 2014
The error is because L is the size of the image and dx is any size up to the number of connected components in your image. The two sizes are never going to be the same, hence you can't use == for that.
First, your find should be:
idx = find(Area >= 500);
To then remove all the objects in BW that have an area greater than 500, use ismember:
BW(ismember(L, idx)) = 0;
  7 Comments
Iain
Iain on 14 Oct 2014
This will remove the bits of Areal that correspond to what you've removed from your labelled image:
Areal(Areal >= 500) = [];
Guillaume
Guillaume on 14 Oct 2014
Medhi,
No. Area contains the area of all osteocytes, while idx contains the label number of all osteocytes with an area greater than 500. The two things represent completely different concept.
To get the label number of the osteocytes smaller than 500, you can either
1) reverse the find criteria
idxsmall = find(Areal < 500);
2) compute the set difference (not subtract) between all the labels and idx
idxsmall = setdiff(idx, 1:max(L(:)));

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 14 Oct 2014
You could try this:
% Prior to here, use your same code to get BW. Then...
labeledImage = bwlabeln(BW);
s = regionprops(labeledImage,'Area');
allAreas = [s.Area];
% Do the size filtering.
keeperIndexes = find(allAreas <= 500); % These are the ones that we want!
filteredLabeledImage = ismember(labeledImage, keeperIndexes);
% Apply a variety of pseudo-colors to the remaining regions and display them
coloredLabelsImage = label2rgb (filteredLabeledImage , 'hsv', 'k', 'shuffle');
% Display the pseudo-colored image.
imshow(coloredLabelsImage);
% Now measure again on the new labeled image to get areas
% of only those blobs > 50 and <= 500 pixels in area.
s = regionprops(filteredLabeledImage,'Area');
allAreas = [s.Area];

Categories

Find more on Images in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!