Image Processing: Zoom in, process image, zoom out

20 views (last 30 days)
I am writing a GUI in which a user can apply some basic image processing (adjust brightness, contrast and thresholding) to an image file. Right now, I'm trying to figure out how to do a simple three step operation like:
1) Zoom in on image
2) process image (i.e. thresholding), while zoomed
3) Zoom out of image
I already managed to program steps 1) and 2), and it works quite good. Unfortunately I can't zoom out of the image after processing it. This is the code:
1) Zoom in
function zoomIn_OnCallback(hObject, eventdata, handles)
% hObject handle to zoomIn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global origXLim origYLim
zoomIn = zoom;
% save original axes limits
origXLim = get(handles.procAxes,'XLim');
origYLim = get(handles.procAxes,'YLim');
% get axes limits after zooming
set(zoomIn,'ActionPostCallback',@mypostcallback);
function mypostcallback(obj,evd)
global newXLim newYLim
newXLim = get(evd.Axes,'XLim');
newYLim = get(evd.Axes,'Ylim');
2) Image processing (here thresholding with a slider)
% --- Executes on slider movement.
function threshSlider_Callback(hObject, eventdata, handles)
% hObject handle to threshSlider (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
global newXLim newYLim
% get slider value
value = get(hObject, 'Value');
% display Slider value
set(handles.threshValue, 'String', num2str(value));
% thersholding (selfwritten function)
handles.newImg = threshold(handles.procImg, value, handles.color);
% display on axes for processed image
axes(handles.procAxes);
imshow(handles.newImg);
set(handles.procAxes, 'XLim', newXLim);
set(handles.procAxes, 'YLim', newYLim);
% update handles structure
guidata(hObject, handles);
I actually know, that I can't zoom out to the original size of the image, because I have set the limits of the axes that display the image to the new values 'newXLim' and 'newYLim'. But I just don't know an other way to keep the "zoomed in" state while applying the thresholding to the image. Is there maybe another way to do this?

Accepted Answer

Walter Roberson
Walter Roberson on 22 Sep 2015
Instead of using imshow at that point, update the Cdata property of the image object. You should not need to set the axis xlim or ylim when you do that.
  2 Comments
Phillip Probst
Phillip Probst on 22 Sep 2015
Thank you very much, Walter!
This actually solves everything and shortens my code. Just perfect!
Best Regards
Phillip
Maria Avila
Maria Avila on 18 Dec 2017
I have been trying to do a very similar program to yours and I have been having troubles with the zoom. I have been able to perform zoom on a set of images with the code you gave, but I have been having troubles understanding what do you mean by "update the Cdata property of the image object". How can I do that?

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 22 Sep 2015
Attached is the Mathworks demo for zooming.

Categories

Find more on Visual Exploration in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!