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

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...

 Risposta accettata

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 Commenti

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);
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.

Accedi per commentare.

Più risposte (1)

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 Commenti

it is working well, but the problem is when i save using getimage(handles.axes2), the image is getting compressed and its quality is reducing. Is there any other way for this?
thanks...
It should not unless (gasp!) you're saving as JPG format. A BMP, PNG, or TIFF format image should be the same as the original. If not, create a simple script that demonstrates what you're seeing.
Check the size of the image returned by getimage(). Is it the same size as your original image, or is it the size of the displayed image.
im2 = getimage(handles.axes2);
[rows1, columns1, numberOfColorChannels1] = size(originalImage);
[rows2, columns2, numberOfColorChannels2] = size(im2);
Is rows1 the same as rows2?
If you want to check the class for curiosity sake, use isa()
Reason you're IA and I'm dpb... :) I've done so little image processing and don't have the Toolbox so wasn't aware that getimage would return other than double to be the "trick" for determining type... could've looked it up I suppose... :)
Manoj could check for the size disparity as
if size(originalImage,1)~=size(im2,1)
error('Image2 NOT same size as Original')
end
Actually I've never used the function. Never needed to. I always have the image I stuff into an axes already, so why do I need to extract it back out of the axes when I already still have the original?
...so why do I need to extract it back out of the axes when I already still have the original?
I suppose the original could've been munged on, though, maybe, so this would be retrieving the modified version?
Well you could put it in there, then change the variable, and then get back out the first/original version from the axes but I think this would be poor programming practice. I think usually people do it because the image is not in scope in the function they need it in, and found the recommended methods in the FAQ too confusing so they just yank it out of the axes. Not really how I'd do it though.
Hi Image Analyst and dpb, The size of both the images is same,,,, One more question to ask you is, how to save the image in .tif without compression. Here i used as
[~,baseFileName,~]=fileparts(baseFileName);
outputBaseFileName = sprintf('%s_manual.tif', baseFileName);
outputFullFileName = fullfile(outputFolder, outputBaseFileName);
imwrite(im2, outputFullFileName);
when i put as .tif, it is getting saved with compression due to which the size is getting reduced.
Thank you...
I don't think that will compress the image. Why do you think it does?
Hi, I did in this way,
im2=getimage(handles.axes2);
figure;imshow(grayimage);%%it opens a figure and i saved in .tif with no compression
imwrite(im2, outputFullFileName);
So, i checked the size of the two files, the image saved using figure is of nearly 2.5MB and the image saved using imwrite is of 250KB.
how can i rectify this?
You only showed saving the actual image itself using imwrite(). Show the other way. Show how you "saved the image using figure". If you saved an entire figure window, say using export_fig(), then you're saving something entirely different!
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.
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.

Accedi per commentare.

Categorie

Scopri di più su Convert Image Type in Centro assistenza e File Exchange

Richiesto:

il 20 Giu 2014

Commentato:

dpb
il 25 Giu 2014

Community Treasure Hunt

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

Start Hunting!

Translated by