MATLAB: How to avoid the error “matrix dimensions must agree” when i try to save the image

matrix dimensions

I performed two opertaions, im2bw and rgb2gray with the help of push buttons.
I get the converted images to be displayed in the axes2. I want to save the image that is in the axes, it may be gray image or bw image, which ever operation is recently done…
I used the following code:
global grayimage
global bwimage
im2=getimage(handles.axes2);
if im2==grayimage
im3 = im2;
else if im2==bwimage
im3=im2;
end
end
imwrite(im3, outputFullFileName);
I am facing error as
Error using == Matrix dimensions must agree.
Error in manual>savefinal_Callback (line354)if im2==grayimage
i don't want to use f=getframe(handles.axes2); because the image quality gets reduced.
Any suggestions please…

Best Answer

Error in manual>savefinal_Callback (line354)if im2==grayimage
The problem is you're trying to compare what looks to be a global scalar constant to the image returned that is an array. You've got to somewhere/how else determine the type of the image in a logical to do the test.
BTW, in the code snippet
if im2==grayimage
im3 = im2;
elseif im2==bwimage
im3=im2;
end
There's no point in the two IF clauses, anyway, it sets im3 to im2 either way. The only alternate path is that neither is true in which case there is no apparent value for im3 and the later reference to it would seemingly fail.