Jake
Jake

Reputation: 3330

Matlab imwrite() quality

I'm very new to Matlab, though I know a few other programming languages, so please forgive me if this is something simple. I have not been able to find any answers to this, either on StackOverflow or elsewhere.

I produce a figure using the following code:

 figure(6),imageplot(P); drawnow;

Which looks like this: enter image description here

I then save this image to my computer using the following commands:

imwrite(P, 'images/plot.png');

And the resulting image is tiny, and missing some of the color information:

enter image description here

If, however, I utilize the save function in the open figure (image #1) and save it manually, I get exactly what I want, which is that exact image stored on my computer.

enter image description here

How would I program that? I assumed that imwrite() would just write the image directly, but apparently I'm doing something wrong. Any advice? Perhaps it has something to do with the imageplot command? I cannot seem to get that to work in imwrite.

Update: Based on the comments below, I have begun using "imresize" with the "nearest" option. This scales the image properly, but the resulting image is still curiously darker (and therefore has less information) than if I hit the "save" button in the figure.

Image saved from figure:

enter image description here

Image using "imresize" with "nearest" option:

enter image description here

Upvotes: 2

Views: 7406

Answers (2)

Martin J.H.
Martin J.H.

Reputation: 2205

The command you need for the "Save As..." functionality of figures is called "print". I often use

print(gcf, '-dpng', 'some_filename.png')

or

print(gcf, '-depsc', 'some_filename.eps','-r0')

to save a figure as it is shown on screen. The format png offers a small filesize and excellent quality, and it is understood by most image viewers and browsers. The eps format is a vector format, which I use for printig. The '-r0' option specifies "use the same size as given by the screen resolution" for the vector format properties.

Upvotes: 2

Roney Michael
Roney Michael

Reputation: 3994

The MATLAB imwrite command saves exactly the number of pixels as specified in your image matrix. This is the actual result of your computation; the reason the output is "tiny" is because it is supposed to be. To make it larger, would be to simply scale/zoom it as required.

The save figure option however does something quite different: it rasterizes the the output you obtain in the figure window and gives you the option for saving it as an image. This is evident in the fact that when you do so, you obtain a white background in addition to your result which is really just the grey background you see before you save it; this can be adjusted by resizing the figure window before utilizing the save option.

If you're looking to simply make the output figure larger, I would recommend using something along the lines of the imresize command.

Say, if you want the default size to be twice the size of the real result, simply use:

imresize(P, 2.0);

For more options, try help imresize.

Upvotes: 3

Related Questions