Display image using GUI MATLAB in a specific region - image

I'm looking for how can I display an image in my GUI in a specific region of my interface.

In GUIDE, you can draw axes into the GUI. Then, in a callback function, you can plot an image into the axes.
Personally, I would rather not have the image inside the GUI, because it makes it harder to resize everything properly, and if you want to have a closer look at the image, or capture it to paste into another application, having the figure as part of the GUI can be inconvenient. Thus, I prefer to have the image open in a separate figure window.

Try the following code:
function movingimage
%# Plotting a figure
fig1=figure('Name','Plotting an image',...
'Unit','normalized', 'Position',[.1 .1 .8 .8]);
uicontrol(fig1,'Style','text','Unit','Normalized',...
'Position',[.9 .85 .1 .07],'String','Press the button below to move the image location.');
uicontrol(fig1,'Style','pushbutton','Unit','Normalized',...
'Position',[.9 .8 .05 .05],'String','Move','Callback',{#action_Callback});
%# Say, you wish to plot an image of relative dimension (.3 x .3) to the figure.
xdim=.3; ydim=.3;
%# Image's movable range in x is (1 - xdim)
dx=1-xdim;
%# Image's Movable range in y is (1 - ydim)
dy=1-ydim;
%# considering the size of the image...
pos = [.5*dx .5*dy xdim ydim]; %# Initial location of the image is at the center of the figure.
ax1 = axes('position',pos);
img = load('mandrill');
image(img.X)
colormap(img.map);axis off;axis equal;
function action_Callback(hObj,eventdata)
pos=[rand(1)*dx rand(1)*dy xdim ydim];
set(ax1,'position',pos);
end
end

The most direct and easy way I find to do this is to use the Axis component as shown in this tutorial:
http://www.aboutcodes.com/2012/06/how-to-display-image-in-gui-using.html

Related

Writing a greyscale video using Videowriter/avifile

I am writing a function that generates a movie mimicking a particle in a fluid. The movie is coloured and I would like to generate a grayscaled movie for the start. Right now I am using avifile instead of videowriter. Any help on changing this code to get grayscale movie? Thanks in advance.
close all;
clear variables;
colormap('gray');
vidObj=avifile('movie.avi');
for i=1:N
[nx,ny]=coordinates(Lx,Ly,Nx,Ny,[x(i),-y(i)]);
[xf,yf]=ndgrid(nx,ny);
zf=zeros(size(xf))+z(i);
% generate a frame here
[E,H]=nfmie(an,bn,xf,yf,zf,rad,ns,nm,lambda,tf_flag,cc_flag);
Ecc=sqrt(real(E(:,:,1)).^2+real(E(:,:,2)).^2+real(E(:,:,3)).^2+imag(E(:,:,1)).^2+imag(E(:,:,2)).^2+imag(E(:,:,3)).^2);
clf
imagesc(nx/rad,ny/rad,Ecc);
writetif(Ecc,i);
if i==1
cl=caxis;
else
caxis(cl)
end
axis image;
axis off;
frame=getframe(gca);
cdata_size = size(frame.cdata);
data = uint8(zeros(ceil(cdata_size(1)/4)*4,ceil(cdata_size(2)/4)*4,3));
data(1:cdata_size(1),1:cdata_size(2),1:cdata_size(3)) = [frame.cdata];
frame.cdata = data;
vidObj = addframe(vidObj,frame);
end
vidObj = close(vidObj);
For your frame data, use rgb2gray to convert a colour frame into its grayscale counterpart. As such, change this line:
data(1:cdata_size(1),1:cdata_size(2),1:cdata_size(3)) = [frame.cdata];
To these two lines:
frameGray = rgb2gray(frame.cdata);
data(1:cdata_size(1),1:cdata_size(2),1:cdata_size(3)) = ...
cat(3,frameGray,frameGray,frameGray);
The first line of the new code will convert your colour frame into a single channel grayscale image. In colour, grayscale images have all of the same values for all of the channels, which is why for the second line, cat(3,frameGray,frameGray,frameGray); is being called. This stacks three copies of the grayscale image on top of each other as a 3D matrix and you can then write this frame to your file.
You need to do this stacking because when writing a frame to file using VideoWriter, the frame must be colour (a.k.a. a 3D matrix). As such, the only workaround you have if you want to write a grayscale frame to the file is to replicate the grayscale image into each of the red, green and blue channels to create its colour equivalent.
BTW, cdata_size(3) will always be 3, as getframe's cdata structure always returns a 3D matrix.
Good luck!

Print image to pdf without margin using Matlab

I'm trying to use the answers I found in these questions:
How to save a plot into a PDF file without a large margin around
Get rid of the white space around matlab figure's pdf output
External source
to print a matlab plot to pdf without having the white margins included.
However using this code:
function saveTightFigure( h, outfilename, orientation )
% SAVETIGHTFIGURE(H,OUTFILENAME) Saves figure H in file OUTFILENAME without
% the white space around it.
%
% by ``a grad student"
% http://tipstrickshowtos.blogspot.com/2010/08/how-to-get-rid-of-white-margin-in.html
% get the current axes
ax = get(h, 'CurrentAxes');
% make it tight
ti = get(ax,'TightInset');
set(ax,'Position',[ti(1) ti(2) 1-ti(3)-ti(1) 1-ti(4)-ti(2)]);
% adjust the papersize
set(ax,'units','centimeters');
pos = get(ax,'Position');
ti = get(ax,'TightInset');
set(h, 'PaperUnits','centimeters');
set(h, 'PaperSize', [pos(3)+ti(1)+ti(3) pos(4)+ti(2)+ti(4)]);
set(h, 'PaperPositionMode', 'manual');
set(h, 'PaperPosition',[0 0 pos(3)+ti(1)+ti(3) pos(4)+ti(2)+ti(4)]);
% save it
%saveas(h,outfilename);
if( orientation == 1)
orient portrait
else
orient landscape
end
print( '-dpdf', outfilename );
end
Results in this output:
As you can see the 'PaperSize' seems to be set not properly. Any idea of possible fixes?
NOTE
If I change the orientation between landscape and portrait the result is the same, simply the image is chopped in a different way.
However if I save the image with the saveas(h,outfilename); instruction the correct output is produced.
Why is this? And what is the difference between the two saving instructions?
Alltogether the answers you mentioned offer a lot of approaches, but most of them didn't worked for me neither. Most of them screw up your papersize when you want to get the tight inset, the only which worked for me was:
set(axes_handle,'LooseInset',get(axes_handle,'TightInset'));
I finally wrote a function, where I specify the exact height and width of the output figure on paper, and the margin I want (or just set it to zero). Be aware that you also need to pass the axis handle. Maybe this functions works for you also.
function saveFigure( fig_handle, axes_handle, name , height , width , margin)
set(axes_handle,'LooseInset',get(axes_handle,'TightInset'));
set(fig_handle, 'Units','centimeters','PaperUnits','centimeters')
% the last two parameters of 'Position' define the figure size
set(fig_handle,'Position',[-margin -margin width height],...
'PaperPosition',[0 0 width+margin height+margin],...
'PaperSize',[width+margin height+margin],...
'PaperPositionMode','auto',...
'InvertHardcopy', 'on',...
'Renderer','painters'... %recommended if there are no alphamaps
);
saveas(fig_handle,name,'pdf')
end
Edit: if you use painters as renderer saveas and print should produce similar results. For jpegs print is preferable as you can specify the resolution.

How to expand the pixel coordinates in matlab?

I want develop an matlab's application that can show the bounding box to the object in the image.
I have detected the object, and cropped it.
And now, for the boundng box, i just have to add 10 in all my pixel.
For exmpl:
x=x+10;
y=y+10;
w=w+10;
h=h+10;
I use imcrop function.
But the problem is that i dont understand how to get the pixel's coordinates from imcrop.
[I_crop, I_rect]=imcrop(ImSeq(:,:,1),[])
I_rect=floor(I_rect);
final_rect=I_rect;
for t=1:NumImages
cur_r=final_rect(2);
cur_c=final_rect(1);
for r= cur_r -10:cur_r+10
for c=cur_c-10:cur_c+10
temp= abs(I_crop-ImSeq(r:r+I_rect(4),c:c+I_rect(3),t));
what is final_rect(2), final_rect(1), I_rect(4) and I_rect(3)?
How i can get the coordinates of x,y,w,h of the cropped image??
Thanks
In [I2 rect] = imcrop(I), rect is the cropping rectangle, a four-element position vector. Within the original image, the cropped area is defined by:
rect(2) the current row
rect(1) the current column
rect(3) is the width
rect(4) the height.

Mapping image into cylinder or sphere shape?

So lets say I have black & white image that is read with imread() command and saved into matrix A.
I want to output/graph this matrix A image in a cylinder shape. I know how to draw a cylinder in MATLAB, but I do not have a clue what I should do if I want to put image on a cylinder or draw image in cylinder shape. Any help will be appreciated. Thank you.
I found this site from googling.
http://www.flashandmath.com/advanced/rolls/cylin.html
This is exactly what I want to do, but I need to do this in MATLAB.
The technique is called texture mapping. This is a code example from surface function (R2011b):
load clown
surface(peaks,flipud(X),...
'FaceColor','texturemap',...
'EdgeColor','none',...
'CDataMapping','direct')
colormap(map)
view(-35,45)
This example loads RGB image from "peppers.png" and maps it onto cylinder:
imgRGB = imread('peppers.png');
[imgInd,map] = rgb2ind(imgRGB,256);
[imgIndRows,imgIndCols] = size(imgInd);
[X,Y,Z] = cylinder(imgIndRows,imgIndCols);
surface(X,Y,Z,flipud(imgInd),...
'FaceColor','texturemap',...
'EdgeColor','none',...
'CDataMapping','direct')
colormap(map)
view(-35,45)
Things are even simpler with the warp function (comes with Image Processing toolbox) as natan suggested:
imgRGB = imread('peppers.png');
[imgRows,imgCols,imgPlanes] = size(imgRGB);
[X,Y,Z] = cylinder(imgRows,imgCols);
warp(X,Y,Z,imgRGB);

Matlab: How can I display several outputs in the same image?

Let's say my image is img=zeros(100,100,3), my outputs are several ellipse which i get using a created function [ret]=draw_ellipse(x,y,a,b,angle,color,img), I can display one ellipse using imshow(ret).For the moment, I'm trying to show serval ellipse in the image. But i don't know how to code it. will ‘for loop’ work or I need to hold them?
If this is related to what you were doing in your previous question, then what you need to do is to pass the result of one iteration as input to the next.
So assuming that the function [ret]=draw_ellipse(x,y,a,b,angle,color,img) you mentioned takes as input an image img and returns the same image with an ellipse drawn on it, you could do this:
%# ellipses parameters
%#x = {..}; y = {..};
%#a = {..}; b = {..};
%#angle = {..}; color = {..};
img = zeros(200,100,'uint8'); %# image to start with
for i=1:10
img = draw_ellipse(x{i},y{i}, a{i},b{i}, angle{i}, color{i}, img);
end
imshow(img)
I'm a bit unsure of what you want. You want to show several ellipse in one image, like plotting several graphs with hold on?
There is no equivalent command for images, but a simple solution is to add the ellipses into one image and show that one:
several_ellipse = ellipse1 + ellipse2 + ellipse3;
imshow(several_ellipse)
Presumably you want to pass ret as the final input to the next call to draw_ellipse.

Resources