2015-03-13 40 views
0

我正在用MATLAB編寫一個圖形用戶界面(GUI),用戶將從一系列圖像中顯示2個圖像(兩個圖像並排放置在單個GUI窗口中)一點點),並將被允許選擇感興趣的區域。在MATLAB GUI中同時與2個數字交互

我希望用戶選擇工作在圖像1中,同時突出顯示圖像2中的選定區域,以便更容易判斷感興趣的特徵是否偏離了選定區域。怎麼做?

我使用以下的答案來選擇和感興趣的作物面積(僅供參考): crop image with fixed x/y ratio

回答

0

這是一種使用imrect及其addNewPositionCallback方法來做到這一點。檢查here獲取可用方法的列表。

在下圖中,我創建了2個軸。左邊是原始圖像,右邊是「修改」圖像。通過按下按鈕,調用imrectaddNewPositionCallback方法執行一個函數,稱爲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 

按下按鈕後,數字:

enter image description here

並圍繞移動矩形後:

enter image description here

耶!希望有所幫助!注意,既然你使用了GUIDE,回調的語法看起來有點不同,但這個想法完全一樣。

+0

它工作!!!!!!完全合作!不知道爲什麼addNewPositionCallback()的記錄太差。即使搜索谷歌沒有鏈接到anyMatlab文檔,但在matlab中央的一些問題。只是一個簡單的問題,定義一個像這樣的函數是什麼意思 - someRandomFunction(〜)?我的意思是'〜'是什麼意思? – ipcamit 2015-03-13 18:14:49

+0

太棒了!是的,我同意關於這個文檔沒有真正提供:)至於你的問題,這意味着該函數沒有任何輸入參數。你可以省略它,但這是一個習慣寫這樣的哈哈。 – 2015-03-13 18:15:54

+0

啊!無論如何回到編碼然後!非常感謝! – ipcamit 2015-03-13 18:17:05