`find` function turn out a error Matrix dimensions must agree

조회 수: 5 (최근 30일)
Ziming Zhong
Ziming Zhong 2017년 11월 3일
댓글: Ziming Zhong 2017년 11월 3일
I use some codes from the image processing toolbox tutorial . I convert my pictures from RGB to HSV, and find the threshold there and convert the image to BW accordingly. Then the later codes for labelling is more or less the same as the tutorials. When it comes to the `find` function, it ran well in the beginning. but then when I change the threshold value when converting from RGB to HSV, then it generate the error:
Matrix dimensions must agree.
Error in code (line 21)
keeperIndexes=find(allowabelCircularityIndexes & allowabelAreaIndexes)
I don't understand why. The threshold should have no effect on the `find` function, right?
Here is my code:
%reads the image
I = imread('test5.jpg')
%convert to hsv
hsvIm = rgb2hsv(I) % convert the image to hsv format
hist(hsvIm)
imshow(hsvIm)
%thresholding on the hue space
bwIm = hsvIm(:,:,1) < 0.15 %some use 0.15 as threshold
imshow(bwIm)
bw=imfill(bwIm,'holes') %fill the hole inside the colony
imshow(bw)
%find connected components
labeledbw = bwlabel(bw,8)
bwMeasurement=regionprops(labeledbw,'all')
%choose only components with enough circularaties
allAreas = [bwMeasurement.Area]
allPerims = [bwMeasurement.Perimeter]
circularity = (allPerims .^ 2) ./ (4 * pi * allAreas) %calculate the circularity of each components
allowabelCircularityIndexes= circularities<1
allowabelAreaIndexes= allAreas>50
keeperIndexes=find(allowabelCircularityIndexes & allowabelAreaIndexes)
keeperComponents=ismember(labeledbw, keeperIndexes)
labelKeeperComponents=bwlabel(keeperComponents,8)
imshow(labelKeeperComponents)
imshowpair(labelKeeperComponents,hsvIm,'montage')
Could somebody help me?
PS: I also have a question in the last call `imshowpair`: why does the image labelKeeperComponents always appear as dark in the left and bright on the right (seems like grey colour filling the image from the left to the right)? But if I call this `imshow(labelKeeperComponents)`, the image is normal and correct without any grey colour falling.

채택된 답변

Stephen23
Stephen23 2017년 11월 3일
편집: Stephen23 2017년 11월 3일
You define a variable named circularity, but then try to use one called circularities. I suspect that this is mistake, and that these should be the same variable.
If this is a script and you previously defined circularities then this value will continue to exist in the base workspace, and quite possibly have a different size that whatever the current image has.
  댓글 수: 1
Ziming Zhong
Ziming Zhong 2017년 11월 3일
oh so embarrassed to put this kind of error here... Thank you very much!!

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

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by