How to display multiple images in same axes in matlab GUI GUIDE? - matlab-guide

Suppose I have 5 images like im1, im2, im3, im4, im5 and imshow functions are
imshow(im1,'Parent',handles.axes1);
imshow(im2,'Parent',handles.axes1);
imshow(im3,'Parent',handles.axes1);
imshow(im4,'Parent',handles.axes1);
imshow(im5,'Parent',handles.axes1);
But it displays only the last image im5 and previous images im1, im2, im3, im4 are not diplayed. Where is the wrong ? Anyone can help ?

There is a command to retain old graphs in an axes.
handles.axes1; hold on;
You can disable it by using.
handles.axes1; hold off;

Related

Netlogo : Set Size and Position of drawing

I have created a Netlogo program in which I imported cars using following code:
import-drawing "F:\\BMW.png"
It imports the image as background and stretches the image in whole screen. I want to resize it, but cannot do it using set size 2 as its used for resizing of built in shapes (as far as I know). Can someone please guide me how can I resize my PNG image and also set it to a specific coordinates (say 0,14).
Any help will be appreciated. Thanks
It won't change anything for how your model works, so you can just use:
set-patch-size XXX
If you dont want to guess at what patch size you want, you can just drag the size smaller until it's about the size you want, then click on info for GUI and it will have a patch size there.
I'm not sure what you mean by resize your png however.

Matplotlib Plots Lose Transparency When Saving as .ps/.eps

I'm having an issue with attempting to save some plots with transparent ellipsoids on them if I attempt to save them with .ps/.eps extensions.
Here's the plot saved as a .png:
If I choose to save it as a .ps/.eps here is what it looks like:
How I got around this, was to use ImageMagick to convert the original png to a ps. The only problem is that the image in png format is about 90k, and it becomes just under 4M after conversion. This is not good since I have a lot of these images, and it will take too much time to compile my latex document. Does anyone have a solution to this?
The problem is that eps does not support transparencies natively.
There are few options:
rasterize the image and embed in a eps file (like #Molly suggests) or exporting to pdf and converting with some external tool (like gs) (which usually relies as well on rasterization)
'mimic' transparency, giving a colour that looks like the transparent one on a given background.
I discussed this for sure once on the matplotlib mailing list, and I got the suggestion to rasterize, which is not feasible as you get either pixellized or huge figures. And they don't scale very nicely when put into, e.g., a publication.
I personally use the second approach, and although not ideal, I found it good enough. I wrote a small python script that implements the algorithm from this SO post to obtain a solid RGB representation of a colour with a give transparency
EDIT
In the specific case of your plot try to use the zorder keyword to order the parts plotted. Try to use zorder=10 for the blue ellipse, zorder=11 for the green and zorder=12 for the hexbins.
This way the blue should be below everything, then the green ellipse and finally the hexbins. And the plot should be readable also with solid colors. And if you like the shades of blue and green that you have in png, you can try to play with mimic_alpha.py.
EDIT 2
If you are 100% sure that you have to use eps, there are a couple of workarounds that come to my mind (and that are definitely uglier than your plot):
Just draw the ellipse borders on top of the hexbins.
Get centre and amplitude of each hexagon, (possibly discard all zero bins) and make a scatter plot using the same colour map as in hexbin and adjusting the marker size and shape as you like. You might want to redraw the ellipses borders on top of that
Another alternative would be to save them to pdf
savefig('myfigure.pdf')
That works with pdflatex, if that was the reason why you needed to use eps and not svg.
You can rasterize the figure before saving it to preserve transparency in the eps file:
ax.set_rasterized(True)
plt.savefig('rasterized_fig.eps')
I had the same problem. To avoid rasterizing, you can save the image as a pdf and then run (on unixish systems at least) in a terminal:
pdftops -eps my.pdf my.eps
Which gives a .eps file as output.
I solved this by:
1) adding a set_rasterization_zorder(1) when defining the figure area:
fxsize=16
fysize=8
f = figure(num=None, figsize=(fxsize, fysize), dpi=180, facecolor='w',
edgecolor='k')
plt.subplots_adjust(
left = (18/25.4)/fxsize,
bottom = (13/25.4)/fysize,
right = 1 - (8/25.4)/fxsize,
top = 1 - (8/25.4)/fysize)
subplots_adjust(hspace=0,wspace=0.1)
#f.suptitle('An overall title', size=20)
gs0 = gridspec.GridSpec(1, 2)
gs11 = gridspec.GridSpecFromSubplotSpec(1, 1, subplot_spec=gs0[0])
ax110 = plt.Subplot(f, gs11[0,0])
f.add_subplot(ax110)
ax110.set_rasterization_zorder(1)
2) a zorder=0 in each alpha=anynumber in the plot:
ax110.scatter(xs1,ys1 , marker='o', color='gray' , s=1.5,zorder=0,alpha=0.3)#, label=label_bg)
and
3) finally a rasterized=True when saving:
P.savefig(str(PLOTFILENAME)+'.eps', rasterized=True)
Note that this may not work as expected with the transparent keyword to savefig because an RGBA colour with alpha<1 on transparent background will be rendered the same as the RGB colour with alpha=1.
As mentioned above, the best and easiest choice (if you do not want to loose resolution) is to rasterized the figure
f = plt.figure()
f.set_rasterized(True)
ax = f.add_subplot(111)
ax.set_rasterized(True)
f.savefig('figure_name.eps',rasterized=True,dpi=300)
This way, you can manage the size by dpi option as well. In fact, you can also play with the zorder below you want to apply the rasterization:
ax.set_rasterization_zorder(0)
Note: It is important to keep f.set_rasterized(True) when you use plt.subplot and plt.subplot2grid functions. Otherwise, label and tick area will not appear in the .eps file
My solution is to export the plot as .eps, load it up to Inkscape for example, then Ungroup the plot, select the object that I want to set the transparency and just edit the Opacity of the Fill in the "Fill and Stroke" tab.
You can save the file as .svg if you want to tweak it later, or export the image for a publication.
If you are writing the academic paper in latex, I would recommend you export the .pdf file rather than .eps. The .pdf format supports transparency perfectly and has good compression efficiency, and most importantly, can be easily edited in Adobe Illustrator.
If you wanna further edit the graph (NOT EDITING DATA! I MEAN, FOR GOOD-LOOKING), you could open the exported graph, in Adobe Acrobat - Edit - Copy elements into Adobe Illustrator. The two software can handle everything perfectly.
I work happily with this method. Everything clear, editable and small-size. Hope can help.

mapped imshow does not work in subplot if different palette

I found that apparently, a mapped version of imshow function does not work in subplot. Is this by design?
The following script
rgb=imread('../FruitSample_small.png');
[ind,map]=rgb2ind(rgb,4);
figure
imshow(ind,map)
figure
subplot(5,1,1);
imshow(ind,map)
subplot(5,1,2);
imshow(ind==0)
subplot(5,1,3);
imshow(ind==1)
subplot(5,1,4);
imshow(ind==2)
subplot(5,1,5);
imshow(ind==3)
produces the following result
i.e. mapped version looks black. If I plot mapped version only (5 times) it looks ok. I.e. subsequent plotting apparently change the palette.
How to plot all these 5 images on same figure then?
Colormap is a property of the figure, not the axis. The second call to imshow resets the Colormap for the entire figure. Here is some more information and a Matlab file exchange solution to the problem. If you download the function described in that link, freezeColors, you can use it in your code like this.
rgb=imread('peppers.png'); % example image included in matlab
[ind,map]=rgb2ind(rgb,4);
figure
imshow(ind,map)
figure
subplot(5,1,1);
imshow(ind,map)
freezeColors % keep colors the same after other subplots are displayed
subplot(5,1,2);
imshow(ind==0)
freezeColors
subplot(5,1,3);
imshow(ind==1)
subplot(5,1,4);
imshow(ind==2)
subplot(5,1,5);
imshow(ind==3)
And the resulting second figure will look like this:

image is too big to fit in the screen (MATLAB)

I got an error in Matlab which is
Warning: Image is too big to fit on screen; displaying at 33%
and my source code for this part is this :
watermarked_image_uint8=uint8('watermarked_image');
%# write watermarked Image to file
imwrite(watermarked_image_uint8,'watermarked_image','jpeg');
%# display watermarked image figure(1)
imshow(watermarked_image), title('Watermarked_Image')
Can any one help me please to debug this warning?
It's not an error, just a warning that the resolution of the image you are showing is larger than the resolution of your Matlab window, so Matlab has to reduce the size of the image before displaying it.
It has nothing to do with your code, and it won't effect your results, so you can safely ignore it.
As Ghaul said, the warning is nothing to worry about. Use the InitialMagnification argument to imshow to make your image smaller, or turn the warning off, if it annoys you.
You should probably try to change the resolution of your image so that it would fit in your screen. To check for your screen resolution check this site :
http://www.whatismyscreenresolution.com/
Try using images with lower or the same resolution with your monitor. To change the resolution of your image you can use paint or any photo editors.
Hope it helps.
I guess you could do something like get first the size of the screen, create a figure and then set the your window size, for example:
plot_size = get(0,'ScreenSize');
fg = figure(1);
set(fg, 'Color', [1 1 1], 'Position', plot_size, 'Visible', 'on');
imshow(watermarked_image),
title('Watermarked_Image')
this is simply warning you are facing, so either identify the unique number of warning and then suppress it or you can use
imshow(watermarked_image, 'InitialMagnification', 50);
this will help you to reduce the size of your image and fit it on the screen.
I also found this error when running from the command line with the -nodisplay argument (what I really wanted was -nodesktop).

How to add image in MATLAB GUI?

I want to switch back and forth between two images, like blinking: 1 second for the first image and one second for second image.
I'm not totally sure of what you want to do (specifically what type of images you are trying to display), but here's some sample code that may do what you want:
image1 = imread('cameraman.tif'); % Load a test image
image2 = imread('circles.png'); % Load another test image
hAxes = gca; % Get a handle to the current axes
for iLoop = 1:5, % Loop five times
imshow(image1,'Parent',hAxes);
pause(1);
imshow(image2,'Parent',hAxes);
pause(1);
end
I used the general function IMSHOW, but this sometimes changes other properties of the figure/axes and that may not be to your liking (since you mention adding this to an existing GUI). You may want to use the IMAGE function instead. Also, instead of the for loop you could use a while loop that stops switching images when a condition is met (such as a button press).
How are your images stored in Matlab? As a matlab movie or a 3 or 4 dimensional matrix depending on if the images are color or grayscale. Also, if you have the image processing toolbox, implay and immovie. Another option assuming that your images are in a mxnx3xk (rgb color) or a mxnxk (gray scale) matrix. Then the following should work. Assuming the following
Img - matrix storing image data either with dimensions mxnx3xk
or mxnxk
handles.imageAxes -
handle for the axis you want to
display the image (set the tag of the
axis to imageAxes in GUIDE)
Now you can loop through Img
for i=1:k
% display the i^th image use `Img(:,:,i)` for a gray scale stack
image(Img(:,:,:,i),'parent',handles.imageAxes);
pause(1) % pause one second
end
that's it.

Resources