在 MATLAB GUI 中同时与 2 个图形交互
Simultaneous interaction with 2 figures in MATLAB GUI
我正在 MATLAB 中编写一个 GUI(指南),其中将向用户显示来自一系列图像(但每张都有一点漂移)的 2 张图像(两张图像在单个 gui window 中并排放置) ) 并将被允许进入 select 个感兴趣的区域。
我希望用户select在图 1 中工作,同时突出显示图 2 中 select 编辑的区域,这样更容易判断感兴趣的特征是否偏离了selected区域与否。怎么做?
我正在使用以下对 select 的回答和裁剪感兴趣的区域(仅供参考):
crop image with fixed x/y ratio
这是一种使用 imrect
及其 addNewPositionCallback
方法来实现的方法。检查 here 以获取可用方法列表。
在下图中我创建了2个轴。左边是原始图像,右边是 "modified" 图像。通过按下按钮,调用 imrect
并且 addNewPositionCallback
方法执行一个名为 GetROIPosition
的函数,该函数用于获取 imrect
定义的矩形的位置。同时,在第 2 个轴中,绘制一个矩形,其位置与第 1 个轴中的位置相同。更花哨的是,您可以使用 setConstrainedPosition
强制将矩形包围在给定的轴中。我会让你做的:)
这是带有 2 个屏幕截图的完整代码:
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
按下按钮后的图形:
移动矩形后:
耶!希望有帮助!请注意,由于您使用的是 GUIDE,因此回调的语法看起来会有些不同,但想法完全相同。
我正在 MATLAB 中编写一个 GUI(指南),其中将向用户显示来自一系列图像(但每张都有一点漂移)的 2 张图像(两张图像在单个 gui window 中并排放置) ) 并将被允许进入 select 个感兴趣的区域。
我希望用户select在图 1 中工作,同时突出显示图 2 中 select 编辑的区域,这样更容易判断感兴趣的特征是否偏离了selected区域与否。怎么做?
我正在使用以下对 select 的回答和裁剪感兴趣的区域(仅供参考): crop image with fixed x/y ratio
这是一种使用 imrect
及其 addNewPositionCallback
方法来实现的方法。检查 here 以获取可用方法列表。
在下图中我创建了2个轴。左边是原始图像,右边是 "modified" 图像。通过按下按钮,调用 imrect
并且 addNewPositionCallback
方法执行一个名为 GetROIPosition
的函数,该函数用于获取 imrect
定义的矩形的位置。同时,在第 2 个轴中,绘制一个矩形,其位置与第 1 个轴中的位置相同。更花哨的是,您可以使用 setConstrainedPosition
强制将矩形包围在给定的轴中。我会让你做的:)
这是带有 2 个屏幕截图的完整代码:
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
按下按钮后的图形:
移动矩形后:
耶!希望有帮助!请注意,由于您使用的是 GUIDE,因此回调的语法看起来会有些不同,但想法完全相同。