how to avoid the error "matrix dimensions must agree" when i try to save the image?
Mostra commenti meno recenti
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
Più risposte (1)
Image Analyst
il 21 Giu 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 Commenti
Manoj Kumar
il 21 Giu 2014
Image Analyst
il 21 Giu 2014
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?
dpb
il 21 Giu 2014
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
Image Analyst
il 21 Giu 2014
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?
dpb
il 21 Giu 2014
...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?
Image Analyst
il 21 Giu 2014
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.
Manoj Kumar
il 23 Giu 2014
Image Analyst
il 23 Giu 2014
I don't think that will compress the image. Why do you think it does?
Manoj Kumar
il 23 Giu 2014
Image Analyst
il 23 Giu 2014
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!
Manoj Kumar
il 25 Giu 2014
dpb
il 25 Giu 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.
Categorie
Scopri di più su Convert Image Type in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!