Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

image processing- zoom in

69 views
Skip to first unread message

maoz Shemesh

unread,
Jan 2, 2010, 10:13:04 AM1/2/10
to
hey,
I would like to know if there is an option to control the zoom function.
I want to be able to zoom in and out by clicking the mouse during the process.

thanking you in advance
Maoz

Matt Fetterman

unread,
Jan 2, 2010, 12:05:07 PM1/2/10
to
"maoz Shemesh" <maoz...@yahoo.com> wrote in message <hhnnq0$f6r$1...@fred.mathworks.com>...

On the top of the figure there is a row of tools including a magnifying glass with zoom and unzoom. Regards ! Matt

ImageAnalyst

unread,
Jan 2, 2010, 12:24:30 PM1/2/10
to
maoz Shemesh:

If you want to know how to do it in code, I have a GUI and I placed on
that a slider called sldZoom, and a label txtInfo. When the user
clicks the slider to change the zoom, it executes this code:

% --- Executes on slider movement.
function sldZoom_Callback(hObject, eventdata, handles)
% hObject handle to sldZoom (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range
of slider
zoomFactor = get(hObject,'Value');
axes(handles.axesImage);
zoom('out');
zoom(zoomFactor);
txtInfo = sprintf('Zoom Factor = %.2f (%d %%)\n\nOnce zoomed, you
can pan by clicking and dragging in the image.', zoomFactor, round
(zoomFactor * 100));
set(handles.txtInfo, 'String', txtInfo);
txtInfo = sprintf('Zoom Factor = %.2f\n\nOnce zoomed, you can pan by
clicking and dragging in the image.', zoomFactor);
set(handles.sldZoom, 'TooltipString', txtInfo);
txtZoom = sprintf('Zoom Factor = %.2f (%d %%)', zoomFactor, round
(zoomFactor * 100));
set(handles.txtZoom, 'String', txtZoom);
% if zoomFactor ~= 1
% else
% end
% Set up to allow panning of the image by clicking and dragging.
% Cursor will show up as a little hand when it is over the image.
set(handles.axesImage, 'ButtonDownFcn', 'disp(''This executes'')');
set(handles.axesImage, 'Tag', 'DoNotIgnore');
h = pan;
set(h, 'ButtonDownFilter', @myPanCallbackFunction);
set(h, 'Enable', 'on');
return;

% Sets up panning by clicking and dragging via the hand cursor.
function [flag] = myPanCallbackFunction(obj, eventdata)
% If the tag of the object is 'DoNotIgnore', then return true.
% Indicate what the target is
disp(['In myPanCallbackFunction, you clicked on a ' get(obj,'Type') '
object.'])
objTag = get(obj, 'Tag');
if strcmpi(objTag, 'DoNotIgnore')
flag = true;
else
flag = false;
end
return;

maoz Shemesh

unread,
Jan 2, 2010, 12:31:05 PM1/2/10
to
"Matt Fetterman" <mattin...@yahoo.com> wrote in message <hhnuc3$33q$1...@fred.mathworks.com>...

Hey Matt,
I know how to use it the way you described. I meant it to be controled by the user (as a part of the algorythm)

regards,
Maoz

ImageAnalyst

unread,
Jan 2, 2010, 12:43:02 PM1/2/10
to
On Jan 2, 12:31 pm, "maoz Shemesh" <maozen...@yahoo.com> wrote:
> Hey Matt,
> I know how to use it the way you described. I meant it to be controled by the user (as a part of the algorythm)
>
> regards,
> Maoz
----------------------------------------------------
And my code doesn't do that??? Then how, exactly, do you mean
"controled"? My code allows them to move a slider and the zoom is
controlled in the code by this call:
zoom(zoomFactor);
If you don't want to use a toolbar button, slider, or any other sort
of control, then can't you just build that call to zoom() in to your
"algorythm"?

maoz Shemesh

unread,
Jan 2, 2010, 12:51:03 PM1/2/10
to
Hey ImageAnalyst,
thanks for your answer!
I wrote my answer to Matt before I saw your message.
I checked your code and it's great for me.

Maoz

maoz Shemesh

unread,
Jan 3, 2010, 1:37:02 AM1/3/10
to
Hey ImageAnalyst,
I have a question about the code you added above...
it seem great however I really don't understand how to call this function correctly.
I will appreciate your help with it.

great day,
Maoz

ImageAnalyst

unread,
Jan 3, 2010, 10:35:50 AM1/3/10
to

---------------------------------
I used GUIDE. Just put a slider on the window with a tag "sldZoom"
and view the callback. Paste my code into the callback function, then
add the function myPanCallbackFunction to handle click-and-drag
panning, and you should be ready to go.

maoz Shemesh

unread,
Jan 3, 2010, 5:08:03 PM1/3/10
to
ImageAnalyst <imagea...@mailinator.com> wrote in message <a192f477-5737-4d78...@r10g2000vbn.googlegroups.com>...

Hey ImageAnalyst,
I tried to do what you told me but I still have a problem. I still get an error when I run it. I attached my code below. I will appreciate any help of yours:
function varargout = untitled(varargin)

gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @untitled_OpeningFcn, ...
'gui_OutputFcn', @untitled_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before untitled is made visible.
function untitled_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure


% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% varargin command line arguments to untitled (see VARARGIN)

handles.axesImage=imread('lena.gif');
imshow(handles.axesImage);


% Choose default command line output for untitled
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes untitled wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = untitled_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure


% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on slider movement.
function sldZoom_Callback(hObject, eventdata, handles)
% hObject handle to sldZoom (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider

zoomFactor = get(hObject,'Value');
axes(handles.axesImage);
zoom('out');
zoom(zoomFactor);

txtInfo = sprintf('Zoom Factor = %.2f (%d %%)\n\nOnce zoomed, you can pan by clicking and dragging in the image.', zoomFactor, round(zoomFactor * 100));


set(handles.txtInfo, 'String', txtInfo);
txtInfo = sprintf('Zoom Factor = %.2f\n\nOnce zoomed, you can pan by clicking and dragging in the image.', zoomFactor);
set(handles.sldZoom, 'TooltipString', txtInfo);

txtZoom = sprintf('Zoom Factor = %.2f (%d %%)', zoomFactor, round(zoomFactor * 100));


set(handles.txtZoom, 'String', txtZoom);
% if zoomFactor ~= 1
% else
% end
% Set up to allow panning of the image by clicking and dragging.
% Cursor will show up as a little hand when it is over the image.
set(handles.axesImage, 'ButtonDownFcn', 'disp(''This executes'')');
set(handles.axesImage, 'Tag', 'DoNotIgnore');
h = pan;
set(h, 'ButtonDownFilter', @myPanCallbackFunction);
set(h, 'Enable', 'on');

return;

% --- Executes during object creation, after setting all properties.
function sldZoom_CreateFcn(hObject, eventdata, handles)


% hObject handle to sldZoom (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB

% handles empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end

% Sets up panning by clicking and dragging via the hand cursor.
function [flag] = myPanCallbackFunction(obj, eventdata)
% If the tag of the object is 'DoNotIgnore', then return true.
% Indicate what the target is
disp(['In myPanCallbackFunction, you clicked on a ' get(obj,'Type') 'object.'])
objTag = get(obj, 'Tag');
if strcmpi(objTag, 'DoNotIgnore')
flag = true;
else
flag = false;
end
return;


% --- Executes during object creation, after setting all properties.
function axes1_CreateFcn(hObject, eventdata, handles)
% hObject handle to axes1 (see GCBO)


% eventdata reserved - to be defined in a future version of MATLAB

% handles empty - handles not created until after all CreateFcns called

% Hint: place code in OpeningFcn to populate axes1

thanks a lot,
Maoz

ImageAnalyst

unread,
Jan 3, 2010, 9:15:43 PM1/3/10
to
maoz Shemesh:
Just briefly scanning it, it looks more or less okay.
(Other than the function is called "untitled" but that could be okay
if you saved your m-file as untitled.m and saved your fig file as
untitled.fig, but that's a bad practice.)

Can you tell me the error message? Otherwise I'd have to build up the
GUI myself. I could maybe try that tomorrow (no time now) but since
it will be my first day back to work in 2 weeks I doubt I'll have
time.

You did put a slider on the GUI and changed the Tag property of it to
say sldZoom didn't you?

maoz Shemesh

unread,
Jan 4, 2010, 12:23:04 PM1/4/10
to
ImageAnalyst <imagea...@mailinator.com> wrote in message <020c6df5-361a-4690...@m16g2000yqc.googlegroups.com>...

Hey again,
my m-file and the flp are both called untitled (that's not my real code, just to fix this problem before...)
The error message I get is:

??? Error using ==> axes
Invalid object handle.

Error in ==> untitled>sldZoom_Callback at 64
axes(handles.axesImage);

Error in ==> gui_mainfcn at 96
feval(varargin{:});

Error in ==> untitled at 17
gui_mainfcn(gui_State, varargin{:});

??? Error while evaluating uicontrol Callback

when I run the program I can see the pic and the slider but when I click on the slider I get the message above.
thank a lot!!!

ImageAnalyst

unread,
Jan 4, 2010, 12:46:40 PM1/4/10
to
That doesn't make sense. If handles.axesImage were undefined, then
how were you able to do this:
handles.axesImage=imread('lena.gif');
imshow(handles.axesImage);

in the OpenFcn and get it to display the image?

Set a break point in the function at that line and see what the value
of handles is. It should be a structure with a bunch of fields in it,
one of which should be axesImage.

maoz Shemesh

unread,
Jan 5, 2010, 2:30:08 AM1/5/10
to
ImageAnalyst <imagea...@mailinator.com> wrote in message <a1d047be-56b3-46d1...@a21g2000yqc.googlegroups.com>...

Hey,
I set the break point the place you told me and the field axesImage exist in the handles structure and includes the image info.

ImageAnalyst

unread,
Jan 5, 2010, 6:20:37 AM1/5/10
to
OK, then you can not possibly be getting an error message like

??? Error using ==> axes
Invalid object handle.
because you say the object handle is valid.
If I were you, I'd check very carefully the spelling and
capitalization used throughout.

Steven Lord

unread,
Jan 5, 2010, 10:04:30 AM1/5/10
to

"ImageAnalyst" <imagea...@mailinator.com> wrote in message
news:bc5cfe8d-60a0-47c4...@a15g2000yqm.googlegroups.com...

Just because the handle exists in the handles structure doesn't necessarily
mean it's valid.


handles.axes1 = axes;
pause
close all
handles.axes1
axes(handles.axes1)


--
Steve Lord
sl...@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ


ImageAnalyst

unread,
Jan 5, 2010, 10:51:38 AM1/5/10
to
OK, you're right. He did something wrong though I'm not sure what it
was. I made a simple GUI to show how you can zoom with a slider and
pan with the hand cursor. To do this, make a GUI called zoomtest. On
it place
1. a slider with tag sldZoom and with min = 1, and value = 1, and max
= 24
2. a text label with tag txtInfo
3. a text label with tab txtZoom
4. an axes with tag axesImage

Then with this m-file, it will work as described:

function varargout = zoomtest(varargin)
% ZOOMTEST M-file for zoomtest.fig
% ZOOMTEST, by itself, creates a new ZOOMTEST or raises the
existing
% singleton*.
%
% H = ZOOMTEST returns the handle to a new ZOOMTEST or the handle
to
% the existing singleton*.
%
% ZOOMTEST('CALLBACK',hObject,eventData,handles,...) calls the
local
% function named CALLBACK in ZOOMTEST.M with the given input
arguments.
%
% ZOOMTEST('Property','Value',...) creates a new ZOOMTEST or
raises the
% existing singleton*. Starting from the left, property value
pairs are
% applied to the GUI before zoomtest_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property
application
% stop. All inputs are passed to zoomtest_OpeningFcn via
varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows
only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help zoomtest

% Last Modified by GUIDE v2.5 05-Jan-2010 10:39:03

% Begin initialization code - DO NOT EDIT


gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...

'gui_OpeningFcn', @zoomtest_OpeningFcn, ...
'gui_OutputFcn', @zoomtest_OutputFcn, ...


'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before zoomtest is made visible.
function zoomtest_OpeningFcn(hObject, eventdata, handles, varargin)


% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% varargin command line arguments to zoomtest (see VARARGIN)

% Choose default command line output for zoomtest
handles.output = hObject;
% Read in standard MATLAB demo image.
grayImage = imread('cameraman.tif');
axes(handles.axesImage);
imshow(grayImage, []);
title('Original Grayscale Image');

% Set up zoom slider
minZoom = get(handles.sldZoom, 'min')
maxZoom = get(handles.sldZoom, 'max')
set(handles.sldZoom, 'value', minZoom);
% set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes zoomtest wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.

function varargout = zoomtest_OutputFcn(hObject, eventdata, handles)

function [flag] = myPanCallbackFunction(obj, eventdata)


% If the tag of the object is 'DoNotIgnore', then return true.
% Indicate what the target is
disp(['In myPanCallbackFunction, you clicked on a ' get(obj,'Type') '
object.'])
objTag = get(obj, 'Tag');
if strcmpi(objTag, 'DoNotIgnore')
flag = true;
else
flag = false;
end
return;

% --- Executes during object creation, after setting all properties.

maoz Shemesh

unread,
Jan 5, 2010, 12:13:19 PM1/5/10
to
thank you so much!!!
it works and it is axacly what I've been looking for.

Maoz

Subha

unread,
Feb 6, 2012, 12:20:11 AM2/6/12
to
Hi ImageAnalyst,

Your code works perfectly. I need the same thing that zoom in and out the image but my image is updated every time. that is I am plotting for real time data and I created gui programmatically(not using guide). So how to inculde Zoom code without guide and also for moving image?

Please help me.


ImageAnalyst <imagea...@mailinator.com> wrote in message <96179dd2-0eef-4cac...@v25g2000yqk.googlegroups.com>...

GANESH Prabhakar

unread,
Feb 6, 2012, 9:25:10 PM2/6/12
to
"maoz shemesh" wrote in message <hhnnq0$f6r$1...@fred.mathworks.com>...
Make a new m-file and copy and paste the following code and run it, it ill ask for the image
select an image, then do what ever u want to do with ur mouse ,(zoom functionality)

function magnify(f1)

%magnify(f1)
%
% Figure creates a magnification box when under the mouse
% position when a button is pressed. Press '+'/'-' while
% button pressed to increase/decrease magnification. Press
% '>'/'<' while button pressed to increase/decrease box size.
% Hold 'Ctrl' while clicking to leave magnification on figure.

[baseFileName, folder] = uigetfile('*.*', 'Specify an image file');
fullImageFileName = fullfile(folder, baseFileName);
I=imread(fullImageFileName );

figure('Toolbar','none',...
'Menubar','none',...
'Name','My Image Compare Tool',...
'NumberTitle','off',...
'IntegerHandle','off');
hold off
imshow(I, [], 'InitialMagnification', 'fit');

if (nargin == 0), f1 = gcf; end;
set(f1, ...
'WindowButtonDownFcn', @ButtonDownCallback, ...
'WindowButtonUpFcn', @ButtonUpCallback, ...
'WindowButtonMotionFcn', @ButtonMotionCallback, ...
'KeyPressFcn', @KeyPressCallback);
return;

function ButtonDownCallback(src,eventdata)
f1 = src;
a1 = get(f1,'CurrentAxes');
a2 = copyobj(a1,f1);

set(f1, ...
'UserData',[f1,a1,a2], ...
'Pointer','fullcrosshair', ...
'CurrentAxes',a2);
set(a2, ...
'UserData',[2,0.2], ... %magnification, frame size
'Color',get(a1,'Color'), ...
'Box','on');
xlabel(''); ylabel(''); zlabel(''); title('');

set(a1, ...
'Color',get(a1,'Color')*0.95);
set(f1, ...
'CurrentAxes',a1);
ButtonMotionCallback(src);
return;

function ButtonUpCallback(src,eventdata)
H = get(src,'UserData');
f1 = H(1); a1 = H(2); a2 = H(3);
set(a1, ...
'Color',get(a2,'Color'));
set(f1, ...
'UserData',[], ...
'Pointer','arrow', ...
'CurrentAxes',a1);
if ~strcmp(get(f1,'SelectionType'),'alt'),
delete(a2);
end;
return;

function ButtonMotionCallback(src,eventdata)
H = get(src,'UserData');
if ~isempty(H)
f1 = H(1); a1 = H(2); a2 = H(3);
a2_param = get(a2,'UserData');
f_pos = get(f1,'Position');
a1_pos = get(a1,'Position');

[f_cp, a1_cp] = pointer2d(f1,a1);

set(a2,'Position',[(f_cp./f_pos(3:4)) 0 0]+a2_param(2)*a1_pos(3)*[-1 -1 2 2]);
a2_pos = get(a2,'Position');

set(a2,'XLim',a1_cp(1)+(1/a2_param(1))*(a2_pos(3)/a1_pos(3))*diff(get(a1,'XLim'))*[-0.5 0.5]);
set(a2,'YLim',a1_cp(2)+(1/a2_param(1))*(a2_pos(4)/a1_pos(4))*diff(get(a1,'YLim'))*[-0.5 0.5]);
end;
return;

function KeyPressCallback(src,eventdata)
H = get(gcf,'UserData');
if ~isempty(H)
f1 = H(1); a1 = H(2); a2 = H(3);
a2_param = get(a2,'UserData');
if (strcmp(get(f1,'CurrentCharacter'),'+') | strcmp(get(f1,'CurrentCharacter'),'='))
a2_param(1) = a2_param(1)*1.2;
elseif (strcmp(get(f1,'CurrentCharacter'),'-') | strcmp(get(f1,'CurrentCharacter'),'_'))
a2_param(1) = a2_param(1)/1.2;
elseif (strcmp(get(f1,'CurrentCharacter'),'<') | strcmp(get(f1,'CurrentCharacter'),','))
a2_param(2) = a2_param(2)/1.2;
elseif (strcmp(get(f1,'CurrentCharacter'),'>') | strcmp(get(f1,'CurrentCharacter'),'.'))
a2_param(2) = a2_param(2)*1.2;
end;
set(a2,'UserData',a2_param);
ButtonMotionCallback(src);
end;
return;

function [fig_pointer_pos, axes_pointer_val] = pointer2d(fig_hndl,axes_hndl)

if (nargin == 0), fig_hndl = gcf; axes_hndl = gca; end;
if (nargin == 1), axes_hndl = get(fig_hndl,'CurrentAxes'); end;

set(fig_hndl,'Units','pixels');

pointer_pos = get(0,'PointerLocation'); %pixels {0,0} lower left
fig_pos = get(fig_hndl,'Position'); %pixels {l,b,w,h}

fig_pointer_pos = pointer_pos - fig_pos([1,2]);
set(fig_hndl,'CurrentPoint',fig_pointer_pos);

if (isempty(axes_hndl)),
axes_pointer_val = [];
elseif (nargout == 2),
axes_pointer_line = get(axes_hndl,'CurrentPoint');
axes_pointer_val = sum(axes_pointer_line)/2;
end;
0 new messages