how to avoid the error "matrix dimensions must agree" when i try to save the image?

10 views (last 30 days)
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...

Accepted Answer

dpb
dpb on 20 Jun 2014
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.
  2 Comments
Manoj Kumar
Manoj Kumar on 20 Jun 2014
global grayimage
global bwimage
im2=getimage(handles.axes2); % the image which is in axes2 to be saved
if im2==grayimage
im3 = im2;
else
im3=im2;
end
end
imwrite(im3, outputFullFileName);
NOw i am facing error as Error using imwrite (line 421) Expected DATA to be nonempty.
Error in manual>savefinal_Callback (line 367) imwrite(saveImage, outputFullFileName);
dpb
dpb on 20 Jun 2014
Well, you've still not done anything to turn an image into a logical for the test and so the if clause isn't satisfied and so there isn't anything for im3.
The code just as well might be
im2=getimage(handles.axes2); % the image which is in axes2 to be saved
imwrite(im2, outputFullFileName);
One presumes you had some intent of determining whether is or isn't b/w or not or wouldn't have created the two logicals as globals but where/how that was to be done isn't shown.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 21 Jun 2014
You're saving it the same way regardless of what the class is. So simply do this:
im2 = getimage(handles.axes2);
imwrite(im2, outputFullFileName);
If you want to check the class for curiosity sake, use isa()
if isa(im2, 'logical')
% It's a binary image.
elseif is(im2, 'uint8')
% It's grayscale or color
if ndims(im2) == 2
% It's grayscale or indexed.
elseif ndims(im2) == 3
% It's color
end
end
But again, you don't need to do that just to save it.
  12 Comments
Manoj Kumar
Manoj Kumar on 25 Jun 2014
I used this command,
figure;imshow(grayimage);
so a figure pops up and I saved the image with the extension .tif with no compression. By comparing the image saved using the figure and imwrite(), I came to know that both the files sizes are different.
dpb
dpb on 25 Jun 2014
There is no 'save' operation shown there at all--if you did it manually by saving the whole figure window, then as IA says, that's a lot more than just the image.
So, you've saved two different things and drawn the wrong conclusion from the size difference.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!