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 comentario

Adam
Adam el 14 de Oct. de 2014
Does the error message not indicate the line which caused it?

Iniciar sesión para comentar.

 Respuesta aceptada

Guillaume
Guillaume el 14 de Oct. de 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 comentarios

mehdi
mehdi el 14 de Oct. de 2014
Thank you Sir, It works! Much appreciated :-)
Guillaume
Guillaume el 14 de Oct. de 2014
Editada: Guillaume el 14 de Oct. de 2014
Good. if you're happy with the answer, then click on the green Accept this answer
mehdi
mehdi el 14 de Oct. de 2014
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)???
Guillaume
Guillaume el 14 de Oct. de 2014
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?
mehdi
mehdi el 14 de Oct. de 2014
Editada: Image Analyst el 14 de Oct. de 2014
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(:)));

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 14 de Oct. de 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];

Categorías

Más información sobre Read, Write, and Modify Image en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 14 de Oct. de 2014

Comentada:

el 14 de Oct. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by