5 ways to save Matlab image

Transfer from this blog: https: //blog.csdn.net/holybin/article/details/39502077, and I added some experimental results, as well as their own.

1, using the function imwrite

The image is img, it may be used imwrite (img, 'result.jpg'); this method to save the image size and the display size is the same. Sample sizes of the image and the original image obtained by the following method; The following is a method to save the image by


We note that was saved with the picture imwrite only the picture itself, nothing else.


2, directly save as

The figure in the menu using the file-> saveas-> Choose Save form (which may be saved as fig, eps, jpeg, gif, png, bmp format). This drawback is saved as image sharpness great sacrifice.

3, copy and paste

In the figure using the menu edit-> copyfigure, this time the image is copied to the clipboard. Note that the "copy options" To select "Bitmap".

4, with the saveas command

Provide direct saveas Matlab function may specify a block diagram of the figure or image is stored in simulink. saveas format is: saveas (gca, filename, fileformat), wherein the three parameters:

(1) gca: graphic handle if the graphics window title bar is "Figure 3", the handle is 3; gcf can be directly used to get the current window handle.

(2) filename: single-quoted string, specify the file name

(3) fileformat: single quotes string specified storage format

example:


    
    
  1. SaveAs (GCF, 'save.jpg' );% Save the current window image
  2. SaveAs ( 2 , 'save.jpg' );% Save Figure 2 window image

The following are results of using saveas:


    We can see the saved saveas result, white edge, save the following print function result is also have a white border. If you want to generate images for subsequent image processing, it is recommended to use imwrite function, otherwise, in what functions can be saved.

5, print function

print operation of the printer function, immediately after use using the print function plot function to save the image. The print format is: print (figure_handle, fileformat, filename), wherein the three parameters:

(1) figure_handle: graphic handle if the graphics window title bar is "Figure 3", the handle is 3; can also directly get the current window handle with gcf

(2) fileformat: single quotes string that specifies the storage format:

png format: '-dpng'

jpeg format: '-djpeg',

tiff format: '-dtiff'

bmp format: '-dbitmap'

gif format: '- dgif'

emf lossless format: '- dmeta'

(3) filename: filename

Example 1: displaying an image and saved


    
    
  1. x = -pi: 2 * pi / 1000 : ft;
  2. and = cos ( x );
  3. Plot ( X , Y ); Print (GCF, '-djpeg' , 'abc.jpg' )% draw an image and save it as jpg format

Example 2: save image is not displayed directly


    
    
  1. x = -pi: 2 * pi / 1000 : ft;
  2. set(figure( 1), 'visible', 'off');
  3. Plot ( X , SiN ( X )); Print (GCF, '-dpng' , 'abc.png' )% saved image is not displayed directly png format
Released nine original articles · won praise 13 · views 9512

Transfer from this blog: https: //blog.csdn.net/holybin/article/details/39502077, and I added some experimental results, as well as their own.

1, using the function imwrite

The image is img, it may be used imwrite (img, 'result.jpg'); this method to save the image size and the display size is the same. Sample sizes of the image and the original image obtained by the following method; The following is a method to save the image by


We note that was saved with the picture imwrite only the picture itself, nothing else.


2, directly save as

The figure in the menu using the file-> saveas-> Choose Save form (which may be saved as fig, eps, jpeg, gif, png, bmp format). This drawback is saved as image sharpness great sacrifice.

3, copy and paste

In the figure using the menu edit-> copyfigure, this time the image is copied to the clipboard. Note that the "copy options" To select "Bitmap".

4, with the saveas command

Provide direct saveas Matlab function may specify a block diagram of the figure or image is stored in simulink. saveas format is: saveas (gca, filename, fileformat), wherein the three parameters:

(1) gca: graphic handle if the graphics window title bar is "Figure 3", the handle is 3; gcf can be directly used to get the current window handle.

(2) filename: single-quoted string, specify the file name

(3) fileformat: single quotes string specified storage format

example:


  
  
  1. SaveAs (GCF, 'save.jpg' );% Save the current window image
  2. SaveAs ( 2 , 'save.jpg' );% Save Figure 2 window image

The following are results of using saveas:


    We can see the saved saveas result, white edge, save the following print function result is also have a white border. If you want to generate images for subsequent image processing, it is recommended to use imwrite function, otherwise, in what functions can be saved.

5, print function

print operation of the printer function, immediately after use using the print function plot function to save the image. The print format is: print (figure_handle, fileformat, filename), wherein the three parameters:

(1) figure_handle: graphic handle if the graphics window title bar is "Figure 3", the handle is 3; can also directly get the current window handle with gcf

(2) fileformat: single quotes string that specifies the storage format:

png format: '-dpng'

jpeg format: '-djpeg',

tiff format: '-dtiff'

bmp format: '-dbitmap'

gif format: '- dgif'

emf lossless format: '- dmeta'

(3) filename: filename

Example 1: displaying an image and saved


  
  
  1. x = -pi: 2 * pi / 1000 : ft;
  2. and = cos ( x );
  3. Plot ( X , Y ); Print (GCF, '-djpeg' , 'abc.jpg' )% draw an image and save it as jpg format

Example 2: save image is not displayed directly


  
  
  1. x = -pi: 2 * pi / 1000 : ft;
  2. set(figure( 1), 'visible', 'off');
  3. Plot ( X , SiN ( X )); Print (GCF, '-dpng' , 'abc.png' )% saved image is not displayed directly png format

Guess you like

Origin blog.csdn.net/ABV09876543210/article/details/104827853