ipcamit
ipcamit

Reputation: 372

Simultaneous interaction with 2 figures in MATLAB GUI

I am writing a GUI in MATLAB (guide) where user will be shown 2 images(both images are positioned side by side in single gui window) from a series of images (but each drifted little bit) and will be allowed to select area of interest.

I want user to select working are in image 1 while simultaneously highlighting the selected area in image 2, so that it is easier to judge whether the feature of interest has drifted out of selected area or not. How to do that?

I am using following answer to select and crop area of interest(just FYI): crop image with fixed x/y ratio

Upvotes: 0

Views: 497

Answers (1)

Benoit_11
Benoit_11

Reputation: 13945

Here is a way to do it using imrect and its addNewPositionCallback method. Check here for a list of available methods.

In the following figure I create 2 axes. On the left that's the original image and on the right that's the "modified" image. By pressing the pushbutton, imrect is called and the addNewPositionCallback method executes a function, called GetROIPosition that is used to get the position of the rectangle defined by imrect. At the same time, in the 2nd axes, a rectangle is drawn with the same position as that in the 1st axes. To be even more fancy you can use the setConstrainedPosition to force the rectangle to be enclosed in a given axes. I'll let you do it :) Here is the whole code with 2 screenshots:

function SelectROIs(~)
%clc
clear
close all

%//=========================
%// Create GUI components

hfigure = figure('Position',[300 300 900 600],'Units','Pixels');

handles.axesIm1 = axes('Units','Pixels','Position',[30,100,400 400],'XTick',[],'YTIck',[]);
handles.axesIm2 = axes('Units','Pixels','Position',[460,100,400,400],'XTick',[],'YTIck',[]);

handles.TextaxesIm1 = uicontrol('Style','Text','Position',[190 480 110 20],'String','Original image','FontSize',14);
handles.TextaxesIm2 = uicontrol('Style','Text','Position',[620 480 110 20],'String','Modified image','FontSize',14);

%// Create pushbutton and its callback
handles.SelectROIColoring_pushbutton = uicontrol('Style','pushbutton','Position',[380 500 120 30],'String','Select ROI','FontSize',14,'Callback',@(s,e) SelectROIListCallback);

%// ================================
%/ Read image and create 2nd image by taking median filter
handles.Im = imread('coins.png');

[Height,Width,~] = size(handles.Im);
handles.ModifIm = medfilt2(handles.Im,[3 3]);

imshow(handles.Im,'InitialMagnification','fit','parent',handles.axesIm1);
imshow(handles.ModifIm,'InitialMagnification','fit','parent',handles.axesIm2);


guidata(hfigure,handles);
%%
%// Pushbutton's callback. Create a draggable rectangle in the 1st axes and
%a rectangle in the 2nd axes. Using the addNewPositionCallback method of
%imrect, you can get the position in real time and update that of the
%rectangle.

    function SelectROIListCallback(~)

        hfindROI = findobj(handles.axesIm1,'Type','imrect');
        delete(hfindROI);

        hROI = imrect(handles.axesIm1,[Width/4  Height/4 Width/2 Height/2]); % Arbitrary size for initial centered ROI.

        axes(handles.axesIm2)
        rectangle('Position',[Width/4  Height/4 Width/2 Height/2],'EdgeColor','y','LineWidth',2);

        id = addNewPositionCallback(hROI,@(s,e) GetROIPosition(hROI));

    end

%// Function to fetch current position of the moving rectangle.
    function ROIPos = GetROIPosition(hROI)

        ROIPos = round(getPosition(hROI));

        axes(handles.axesIm2)

        hRect = findobj('Type','rectangle');
        delete(hRect)
        rectangle('Position',ROIPos,'EdgeColor','y','LineWidth',2);
    end

end

The figure after pressing the button:

enter image description here

And after moving the rectangle around:

enter image description here

Yay! Hope that helps! Nota that since you're using GUIDE the syntax of the callbacks will look a bit different but the idea is exactly the same.

Upvotes: 1

Related Questions