Matlab imwrite() quality - matlab

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:
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:
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.
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:
Image using "imresize" with "nearest" option:

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.

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.

Related

Copy figure in Matlab

I am asking for a solution to the blurred image I get when I use copy figure option in Matlab. For example, when I plot 3d figure in matlab I get the following image (It is the result of printscreen!)..
However, when I use the option copy figure I get a blurred image as the following image
.
How can I avoid this as I need to get high resolution image using copy figure option. Thank you in advance for any help you may lend.
By default, the figure is copied as an Enhanced Metafile (a color graphics format) which works for basic plots such as bar plots, line plots, and other 2D plots but may yield undesirable results for more complex plots as in your case. This is because the rendering of the Metafile content is controlled my Microsoft Word and it may render things differently than MATLAB.
If you need a high resolution image, I would recommend using the print command where you can specify the desired resolution (using the -r option). Then you can import the resulting image into Word
print(gcf, 'myfile.png', '-dpng', '-r300')
Alternately, you can use export_fig from the File Exchange which better preserves the appearance
Another option would be to change the format used by Copy Figure to tell it to use a bitmap instead
Or you can call Copy Figure programmatically and specify a different format (either a bitmap or PDF)
print(gcf, '-clipboard', '-dpdf')

Matlab: Export a figure made with myaa

I am trying to get better quality phase plots of complex functions made with the Complex Function Explorer of E. Wegert CFE. For this purpose I apply the Matlab anti-aliasing function myaa.m to the phase plots that are made with the CFEGUI.m. For the example screenshot of the result window below I used the setting myaa([8 8]) in the Matlab command window which means that the supersampling enlarge the figure 8x and then downscale it to 1/8 to get the original h x w.
As one can see the window of the figure has no operation icons or menu options for save or print. My question is what to do with such a figure (beside making screenshots)? Can I somehow use export-fig to save such a figure or load it in an image array and if yes, how?
It is also possible to use the setting myaa([8 1]) which results in a very large figure window that is larger than my screen (and has the unpleasant attribute that it can not be moved). It would be even better if such a whole figure could be saved (not only the visible part).
You can use getframe to grab current figure information in pixels.
However, while I do not know how this affects you, notice that MATLAB versions above 2014b (included) have already anti-aliasing. The code you linked seem to be from 2008, I am not 100% sure if it has become obsolete now.

Matlab figure contents lost while saving as eps/pdf

I have tried most of the usually used options (print(figr, '-depsc', 'cross_corr.eps');) to save a matlab figure as eps/pdf but each time I do it, the figure contents are saved partially and perhaps one quarter of it is lost.
I have shared the figure here: http://ge.tt/2ZrsdD02/v/0?c
Using the options such as the following save it completely but I prefer it save it directly inti eps/pdf:
print(figr, '-dpng', 'cross_corr.png');
The problem seems to be that you have very small values in the 'PaperPosition' property of the figure. Try changing them to the default ones,
set(figr, 'PaperPosition', [0.634517 6.34517 20.3046 15.2284])
and then apply your print(figr, '-depsc', 'cross_corr.eps').
I attach results on my computer without and with 'PaperPosition' correction (using GSview with bounding box showed):
Also, consider reducing font size to avoid overlapped text:
set(findobj('type','text'), 'Fontsize', 5)
If I first enlarge the figure size (by using the middle button on the top right), and then save it as eps, I get this: http://ge.tt/1Pv8YE02/v/0
The quality is very nice as compared to all other options and the content are also ok.
Its not possible to automate it through script?...

Distortion while saving matlab figure as a eps file

saveas(gcf,'result.pdf');
After I using above command to output my figure into a eps file, I got the following result.
However, the actual figure is like the following.
I have tried using other commands like
saveas(gcf, 'result.eps');
print -depsc myplot.eps
or even the 'export_fig' library,but still can't get correct figure output.
Does anyone know where is the key point of this problem? Thanks.
[ Update ]
Following dpwe's comment, after zooming in the figure, the result of .eps is like this
and the original figure is like this
Yes, it seems that they are much more similar!
The saveas function uses a default size for saving figures, I think it's something like 3/4 of your screen resolution. You can figure it out by looking at the number of pixels in the image (for a png anyway). If you run
set(gcf,'Position',[a b c d]);
saveas(gcf, 'result.eps');
to resize the figure to the size that saveas will use to save it before it saves it, that might help.

Matlab imshow() not showing the image properly

I have a simple code to show an image in Matlab. I use imread() to read it and imshow() to show it. the code it below, and the result in not shown properly. hope someone can help me.
img = imread('/home/samuelpedro/Desktop/API - Projecto/coimbra_aerea.jpg');
figure, imshow(img);
the resulting image is below.
also, if i choose to save it to file as a new jpg it is saved correctly.
UPDATE 1:
weirdly if i choose to show the axes in the preferences>image processing, it is corrected
Locking at your screen-shot, the x- and y-ticks are missing. They should appear in a standard-configuration of Matlab. Maybe something is just messed up in the Matlab-configuration. Try to do this with a clean new ~/.matlab folder (rename the old one before).
Alternatively ... again judging by your screen-shot, this looks like Ubuntu/Unity in the background. Unity needs acceleration (OpenGL), which can be randomly buggy for some Linux graphics drivers. You may want to try to launch matlab in a "clean" X-server (maybe the twm environment) to rule this out.
Save the image as an (uncompressed) bitmap (bmp) and read it with imread. If the jpg is messed up by the imread-routine, this should rule this out.
Last but not least, broken copy of your jpg on your disk, some flipped bits. Run md5sums on your file's copies.