David Gunawan
David Gunawan

Reputation: 41

Matlab saving picture

I want to save a picture using this code, but when using the code to save a picture the size of the picture will be different with manual Save As.

Is there another method to save which is the same as the manual Save As method using code?

I had a try with this code (the data name: ffout2):

imagesc(fftout2); figure(gcf);
saveas(gcf,[pwdpath '\Image\' ClassName '\',WavClass(WavClassIndex).name '.jpg'], 'jpg');

or with:

print -djpeg 'FIG1.jpg';

Both codes export file all in 1201 x 901 , 24 bit (colour)

But using manual Save As, the size will be 561 x 420 , 24 bit (colour)

Upvotes: 4

Views: 1656

Answers (2)

chappjc
chappjc

Reputation: 30579

You request having code that saves a file with the same dimensions as that saved via the manual "Save As" menu. To get the same size as with the "Save As" menu, you have to use the getframe and frame2im commands as follows,

F = getframe(gcf);
[im,map] = frame2im(F);
if isempty(map)
    imwrite(im,'SaveAsFig.jpg');
else
    imwrite(im,map,'SaveAsFig.jpg');
end

If you use saveas or print, you will likely get a file with different dimensions. Similarly for just imwrite(fftout2), which stores a file that will have the native matrix size rather than the "Save As" size. The operation of getframe ensures the file resolution reflects what you have on screen. From the documentation:

Resolution of Captured Frames

The resolution of the framed image depends on the size of the axes in pixels when getframe is called. As the getframe command takes a snapshot of the screen, if the axes is small in size (e.g.,because you have restricted the view to a window within the axes), getframe captures fewer screen pixels, and the captured image might have poor resolution if enlarged for display.

EDIT:

To get the same effect with the print command you need to specify screen resolution with the -r switch as described in this MathWorks guide to figure settings. The -r0 switch gives you the default resolution, but not necessarily the resolution Save As gives you. For example:

print -djpeg -r0 'FIG1.jpg'

The resolution used by Save As and getframe/frame2im differ from the automatic resolution used here. Interestingly, I tried to find a -r that gave the me same resolution returned by the Save As menu (and getframe), but was unable to find a suitable -r. The closes was -r87 for my 1600x1200 monitor. Nothing gave an exact match to the screen grab saved by getframe. Perhaps its best just to use getframe if you just want to get what is on the screen without worrying about print resolution. DPI is kind of a silly concept anyway unless you are printing or always expecting to display the file on a monitor of the same size.

Upvotes: 1

anandr
anandr

Reputation: 1662

Your code

imagesc(fftout2);
figure(gcf);
saveas(gcf,[pwdpath '\Image\' ClassName '\',WavClass(WavClassIndex).name '.jpg'], 'jpg');

or

print -djpeg 'FIG1.jpg';

save MATLAB figure as it is presented on your screen with axes, tick labels, titles, etc.

But this code

imwrite(fftout2,'File.jpg');

writes pure image data to file. Both ways are correct, depending oon what is your goal.

EDIT1

After the answer by @chappjc and his comment, I did some tests and it seems that all these ways may be tuned to provide the same image size even the print function with proper DPI settings. Check this code

img = rand(100,200,3);
figure;                                         % size is 560 x 420 pixels
imagesc(img);
get(gcf,'Position')
F = getframe(gcf);
size(F.cdata)
[im,map] = frame2im(F);
if isempty(map)
    imwrite(im,'SaveAsFig_RGB.png');            %  560 x  420 image
else
    imwrite(im,map,'SaveAsFig_WithCMAP.png');   %  
end
print(gcf, '-djpeg',       'FIG_000.jpg');      % 1200 x  900 image
print(gcf, '-djpeg','-r70','FIG_070.jpg');      %  560 x  420 image
print(gcf, '-djpeg','-r72','FIG_072.jpg');      %  576 x  432 image
print(gcf, '-djpeg','-r96','FIG_096.jpg');      %  768 x  576 image
get(0,'ScreenPixelsPerInch')                    % gives 96

Funny thing is that to get the save image size I had to put 70 dpi (Win7 + R2001b), which is different from the screen DPI resolution (96 dpi) MATLAB stores in root object (see get(0))

Upvotes: 4

Related Questions