2013-12-12 80 views
0

我在matlab上做了一個小小的界面,現在出現了問題。所以基本上,我有一個圖像和兩個按鈕,第一個是旋轉圖像(以ginput(2)取2個座標),第二個是裁剪圖像(也取兩個座標)。在matlab中使用ginput後更新img圖形用戶界面

我想在2 x和2 y時更新圖像。

所以,我在這裏宣佈我的形象:

function projetGUI_OpeningFcn(hObject, eventdata, handles, varargin) 

% Choose default command line output for projetGUI 
handles.output = hObject; 

% Update handles structure 
guidata(hObject, handles); 
axes(handles.axes1); 
img = imread('plaque3.png'); 
imshow(img) 

這是我的第一個按鈕:

function pushbutton1_Callback(hObject, eventdata, handles) 

[x,y] =ginput(2); 

angleRad = atan((y(2)-y(1))/(x(2)-x(1))); 
angleDeg = radtodeg(angleRad); 

img = imrotate(img, angleDeg); 
imshow(img); 
set (handles.text1, 'String' ,'Résults ') ; 

這是我的第二個按鈕:

function pushbutton2_Callback(hObject, eventdata, handles) 

[xcrop,ycrop] = ginput(2); 

largeur = xcrop(2) - xcrop(1); 
hauteur = ycrop(2) - ycrop(1); 

img = imcrop(img,[xcrop(1) ycrop(1) largeur hauteur]); 
imshow(img); 
set (handles.text1, 'String' ,'Results ') ; 

這是錯誤我得到了:

Undefined function or variable "img". 

Error in projetGUI>pushbutton1_Callback (line 91) 
img = imrotate(img, angleDeg); 

Error in gui_mainfcn (line 96) 
     feval(varargin{:}); 

Error in projetGUI (line 43) 
    gui_mainfcn(gui_State, varargin{:}); 

Error in 
@(hObject,eventdata)projetGUI('pushbutton1_Callback',hObject,eventdata,guidata(hObject)) 


Error while evaluating uicontrol Callback 

我敢肯定,問題出現是因爲我的圖像不是全局變量,但我沒有找到正確的方式來聲明它。

任何人都可以幫助我嗎?

非常感謝=) (順便說一句,對不起,我的英語,這不是我的母語)

回答

0

pushbutton1_CallbackprojetGUI_OpeningFcnpushbutton2_Callback嵌套函數?如果不是,那麼是的,img只存在於projetGUI_OpeningFcn的工作空間中,但不存在於其他兩個函數的工作空間中。

可能的解決方法是使用handles.img,而不是imgprojetGUI_OpeningFcnguidata(hObject, handles);末各功能的。然後該圖像可以通過handles.img等其他功能訪問。

查看documentation on guidata瞭解更多信息。

+0

非常感謝,它的工作原理! = d – tomatediabolik