Here's my problem: I have an axes object in a figure.
The user can draw a bunch of points on the image.
Up to this point, everything is working as it should.
Now the user shall be able to zoom, draw some points on the zoomed image, and revert back to the original image once he/she has done.
I have a function updateVisualization that is called every time there is a change on the axes made by the user.
In this function, I tried to use
zoom(h, 'reset');
imagesc(updated_img);
zoom(h, 'out');
This solution keeps the zoom every the user does a modification :-) however, once the user is done, he/she cannot go back to the original zoom level.
What should I do?
% Saving zoom level
xlim = get(handles.axes, 'XLim');
ylim = get(handles.axes, 'YLim');
% Displaying image
imagesc(im, 'Parent', handles.axes, 'ButtonDownFcn', #axes_ButtonDownFcn);
% Setting saved zoom level.
set(handles.axes, 'XLim', xlim);
set(handles.axes, 'YLim', ylim);
Related
have an image that I would like to zoom in/out when I left/Right-click a point on the image but I do not want to do it using the magnifying glass. Basically, I need a script for what I said above. I have come up with the following script but it only zooms in/out to the center not to the position that I click on.
And if you are asking why I am getting the clicked position using ginput the answer is, I am planning to use this script to edit a binary image! I have removed the lines corresponding to the binary image editing part to avoid any confusion.
hFi= figure; imshow(e);
button=0;
while button~=2
% Get the mouse position on the axes (needed for binary image editing) and button number
[y,x,button]=ginput(1);
% Get the mouse position on the figure
position=get(hFi,'CurrentPoint')
% Set 'CurrentPoint' to the mouse position that was just captured
set(hFi,'CurrentPoint',position)
% Determine if it is a zoom-in or zoom-out
if button==1
zoom(2);
elseif button==3
zoom(0.5);
end
end
I think the command set() does not do anything here but I saw someone suggested that in a forum. Sorry if that looks dumb!
I am assuming you have a matrix as you are asking for the y and x values.
In this way a simple answer would be to display a smaller, or a larger range of values.
Let A be your matrix.
A=rand(1000,1000);
figure,
h=imagesc(A)
axis off
axis image
colormap('gray')
A_range=size(A);
A_zoom=1;
while true
[y,x,button]=ginput(1)
A_center=[x,y];
if button==1
A_zoom=A_zoom/2;
elseif button==3
A_zoom=A_zoom*2;
end
axis([max(x-A_range(1)*A_zoom,1), min(x+A_range(1)*A_zoom,A_range(1)), ...
max(y-A_range(2)*A_zoom,1), min(y+A_range(2)*A_zoom,A_range(2))])
end
Alright, after playing around with imshow and reading through the documentation on internet, I finally came up with what I had been looking for. Thanks to everyone who suggested some ideas, especially ASantosRibeiro who gave me the idea of changing the axis limits.
Basically, I used a combination of commands zoom and xlim/ylim. Therefore, the final code would look like:
hFi= figure; h=imshow(e);
button=0;
zlvl=1;
xl = get(gca,'xlim');
xlen = size(e,2);
yl = get(gca,'ylim');
ylen = size(e,1);
while button~=2
% Get the mouse position on the axes (needed for binary image editing) and button number
[y,x,button]=ginput(1);
% Determine if it is a zoom-in or zoom-out
if button==1
zlvl = zlvl*2;
zoom(2);
elseif button==3
zlvl = zlvl/2;
if zlvl<1, zlvl=1; end % No zoom level smaller than 1
zoom(0.5);
end
% Change the axis limits to where the mouse click has occurred
% and make sure that the display window is within the image dimensions
xlimit = [x-xlen/zlvl/2+0.5 x+xlen/zlvl/2+0.5];
if xlimit(1)<0.5, xlimit=[0.5 xlen/zlvl+0.5]; end
if xlimit(2)>0.5+xlen, xlimit=[xlen-xlen/zlvl+0.5 xlen+0.5]; end
xlim(xlimit);
ylimit = [y-ylen/zlvl/2+0.5 y+ylen/zlvl/2+0.5];
if ylimit(1)<=0.5, ylimit=[0.5 ylen/zlvl+0.5]; end
if ylimit(2)>=0.5+ylen, ylimit=[ylen-ylen/zlvl+0.5 ylen+0.5]; end
ylim(ylimit);
end
This code lets you zoom in/out when you use ginput and you need to use imshow to keep the aspect ratio of image fixed. This becomes very helpful when you need to read coordinates of image pixels and zoom in/out as well. It zooms in when the user left-clicks and zooms out when the user left-clicks. If the middle button is clicked the while loop ends. Other buttons can be added to perform desired operations. For example, I added a few lines of code to edit binary images.
I would like to know your thoughts and if there is any way to make this code more efficient.
I would like to add code in my function to zoom around a point, while keeping the option to un-zoom manually later.
I can use set(gca,'xlim',[x1 x2]), but if I later try to get back to the original zoom using the magnifier glass, the axes are "stuck".
Maybe not as elegant as you want, but you can store the original axis limits:
plot(rand(100));
xlim_orig = xlim();
ylim_orig = ylim();
% Zoom to point ...
% Do other stuff
% Zoom back out
xlim(xlim_orig)
ylim(ylim_orig)
Once you have "reset" the axes limits, the manual zoom and pan tools will work as expected.
I have a 2D plot with many data elements on it covering a extensive area. Although all the data is necessary, I am usually interested on a small element of the plot.
I would like to programmatically focus the view on that element of interest, while allowing to use the zoom tool ((-) in the GUI) to fastly go back to a wider perspective.
It is easy to use set(gca, 'xlim', [limitsXOfSmallElement]) and set(gca, 'ylim', [limitsYOfSmallElement]) to set axis limits so that the small element is in focus, but this makes impossible to use the GUI (-) zoom tool to go back to the general view without manually resetting back axis limits to original values.
My intuition is that this could be solved by controlling camera properties (CameraPosition, CameraTarget and/or CameraViewAngle), but when I apply them, posterior uses of the GUI zoom tool have weird effects on the axis, as changing its position and size on the figure.
Is there a good method for setting the fragment of the 2D canvas that is displayed in the axis?
Consider the following example:
function example_zoom
%# some plot
plot(1:10)
hAx = gca;
%# save original axis limits
setappdata(hAx, 'limits',get(gca,{'XLim','YLim'}))
%# create custom toolbar button
[X,map] = imread(fullfile(toolboxdir('matlab'),'icons','view_zoom_out.gif'));
icon = ind2rgb(X,map);
uipushtool('CData',icon, 'ClickedCallback',{#click_cb,hAx});
%# zoom
uiwait(msgbox('Zooming now, click button to reset', 'modal'))
set(gca, 'XLim',[3 7], 'YLim',[2 9])
%zoom on
end
function click_cb(o,e, hAx)
%# restore original axis limits
limits = getappdata(hAx, 'limits');
set(hAx, 'XLim',limits{1}, 'YLim',limits{2})
end
The idea is to create your own toolbar button that restores the axis limits to their original values.
There is a axes named image in which I show the image when the user presses the browse button.
imshow(orgImg, 'Parent', handles.image);
Then I do image processing stuff.
There is a clear button to clear that image shown in image axes after done all the processing.
I used cla(handles.image,'reset');
This clear the image from axes. But, it displays the XTick and YTick as 0, 0.5, 1, 1.5 and so on and also XColor and YColor as black.
I don't want XTick and YTick values to be displayed on axes and also color should be white. But I need to display the axes without above values. Now it shows the axes with above values.
How can I remove those properties?
After you clear the image, issue this command
set(gca,'xtick',[],'ytick',[],'Xcolor','w','Ycolor','w')
you can replace gca with your current handle.
The easiest solution may actually be to leave out the 'reset' argument to CLA:
cla(handles.image);
This will have the effect of clearing the image object from the axes, but leaving the axes settings unchanged (i.e. the axes will still be invisible).
I have a project of "Optical Character Recognition" in MATLAB and i need your help:
how do i recognize when the user press the mouse on an image?
i trying to do this with ButtonDownFcn but even when i just printing message
the message is not printed.
i want to allow the user to select the license plate from the image.
how can i do this and save the Pixels of the selected area?
thanks in advance.
Addressing your two questions:
I'm guessing that you are trying to set the 'ButtonDownFcn' of the figure window, which won't work how you expect it to. If you want to do something when the user clicks on an image, you should make sure that you're setting the 'ButtonDownFcn' of the image, and not the figure window or the axes object. Note this line in the figure property documentation (emphasis added by me):
Executes whenever you press a mouse button while the pointer is in the figure window, but not over a child object (i.e., uicontrol, uipanel, axes, or axes child).
This is why you have to set a 'ButtonDownFcn' for each object you want it to work for. Setting it for the figure window won't make it work automatically for each object in the figure. Here's an example that sets the 'ButtonDownFcn' for the figure and an image object:
img = imread('peppers.png'); %# Load a sample image
hFigure = figure; %# Create a figure window
hImage = image(img); %# Plot an image
set(hFigure,'ButtonDownFcn',... %# Set the ButtonDownFcn for the figure
#(s,e) disp('hello'));
set(hImage,'ButtonDownFcn',... %# Set the ButtonDownFcn for the image
#(s,e) disp('world'));
Notice how clicking inside and outside the image displays a different message, since each calls the 'ButtonDownFcn' for a different object. Notice also that if you click on the tick mark label of one of the axes, nothing is displayed. This is because the axes object has its own 'ButtonDownFcn', which wasn't set to anything.
If you have access to the Image Processing Toolbox you can use the function IMFREEHAND to have the user draw an ROI (region of interest) in the image. Then you can use the createMask method to create a binary mask of the image with ones for pixels inside the ROI and zeroes for pixels outside the ROI.