Error using == Matrix dimensions must agree.

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

Does the error message not indicate the line which caused it?

Sign in to comment.

 Accepted Answer

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

Thank you Sir, It works! Much appreciated :-)
Guillaume
Guillaume on 14 Oct 2014
Edited: Guillaume on 14 Oct 2014
Good. if you're happy with the answer, then click on the green Accept this answer
what if I want to substract Areal from idx.
can I do like this:
osteocytter = minus(ismember(Areal , idx));
Areal and Idx have to different size evry time. I suppose to do the same. (use ismember)???
I'm afraid I don't understand what you're trying to do here.
Areal is an array of labelised objects area, while idx is the integer label of the objects that have an area greater than 500. Why would you want to subtract one from the other?
I will find osteocytes that are less than 500.
Areal contains all ostecytes in image, while idx contains osteocytes greater than 500. If I substract idx from Areal, I will get all the other osteocytes that are smaller than 500.
my whole code:
[Filename, Pathname] = uigetfile(...
{'*.bmp;*.jpg;*.tif;*.png;*.gif','Image files (*.bmp;*.jpg;*.tif;*.png;*.gif)';...
'*.*','All file (*.*)'},...
'Select a file to open');
if ~isequal(Filename,0)
FileName = fullfile(Pathname,Filename);
else
error('User pressed cancel.');
end
im = imread(FileName);
figure (1)
imagesc(im);
I = imcrop;
imagesc(I);
level = graythresh(I);
BW = ~im2bw (I, level);
BW = imclearborder(BW);
BW = bwareaopen(BW,50);
BW = imfill(BW,'holes');
L = bwlabeln(BW);
s = regionprops(L,'Area');
Areal=[s.Area];
idx = find(Areal >= 500);
BW(ismember(L, idx)) = 0;
figure (2)
imagesc(BW);
x = size(Areal) - size(idx);
Antalosteocytter = x(1,2);
This will remove the bits of Areal that correspond to what you've removed from your labelled image:
Areal(Areal >= 500) = [];
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)

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];

Tags

Asked:

on 14 Oct 2014

Commented:

on 14 Oct 2014

Community Treasure Hunt

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

Start Hunting!