MATLAB: Image Processing: Zoom in, process image, zoom out

guiimage processingImage Processing Toolboxzoom

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?

Best Answer

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.