引用不存在的字段处理matlab

时间:2017-09-09 07:54:09

标签: matlab matlab-guide

function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
f=imread('/Users/MoChutima/Desktop/WORK1:2560/ImageProcess/dip/dip/baboon.jpg');
Tscale = [handles.sx 0 0; 0 handles.sy 0; 0 0 1];
Trotation = [cos(handles.theta) sin(handles.theta) 0; -sin(handles.theta) cos(handles.theta) 0; 0 0 1];
Tshear = [1 handles.shx 0; handles.shy 1 0; 0 0 1];
T=Tscale*Trotation*Tshear;
tform=maketform('affine',T);
g=imtransform(f,tform,'bilinear');
imshow(g);

我有错误

Error in Workex63>pushbutton1_Callback (line 82)
Tscale = [handles.Sx 0 0; 0 handles.Sy 0; 0 0 1];

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

Error in Workex63 (line 42)
gui_mainfcn(gui_State, varargin{:});

Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)Workex63('pushbutton1_Callback',hObject,eventdata,guidata(hObject)) 

评估UIControl回调时出错

我在GUI中做几何,我想创建滑块和编辑文本以填充剪切X,Y刻度X,Y的数量,但现在我无法加载图片来处理。

由于

2 个答案:

答案 0 :(得分:0)

实际上,我认为你的控件(文本框,复选框等等)与它们的基础值混淆。

例如,我们假设handles.theta引用Slider控件,而handles.ssomething引用EditText控件,用户可以在其中插入数字值。如果您想检索它们的值并使用它们来处理您的计算,那么您必须这样做:

th = get(handles.theta,'Value');
ss = str2double(get(handles.ssomething,'String'));

或(它是相同的,但我更喜欢这种方法):

th = handles.theta.Value;
ss = str2double(handles.ssomething.String);

因此,要修复代码,首先从应用程序控件中检索所需的所有数值,然后继续计算:

function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

th = handles.theta.Value;
shx = str2double(handles.shx.String);
shy = str2double(handles.shy.String);
sx = str2double(handles.sx.String);
sy = str2double(handles.sy.String);

f = imread('myimage.jpg');
Tscale = [sx 0 0; 0 sy 0; 0 0 1];
Trotation = [cos(th) sin(th) 0; -sin(th) cos(th) 0; 0 0 1];
Tshear = [1 shx 0; shy 1 0; 0 0 1];
T=Tscale*Trotation*Tshear;
tform=maketform('affine',T);
g=imtransform(f,tform,'bilinear');
imshow(g);

请勿忘记通过EditTextCallback控件中执行完整性检查,以验证用户的输入(并防止插入错误的值)。这不是问题的一部分,但我很确定你会发现数百个谷歌搜索的例子。

答案 1 :(得分:0)

为时已晚?哈哈... 但很快,对于那些遇到相同情况的人。

我也遇到了这个错误..

但是..你可以尝试从 句柄.sx

to this..(edit1表示回调函数。或者其他编辑文本的回调函数)

handles.edit1 

示例...
之前

% ---------Edit1_Callback -- Sx ------------- %
sx = str2double(get(handles.sx, 'string'));

% ----------Edit2_Callback -- sy ------------ %
sy = str2double(get(handles.sy,'string'));

% ----------Edit3_Callback -- shx ------------ %
shx = str2double(get(handles.shx, 'string'));

% ----------Edit4_Callback -- shy ------------ %
shy = str2double(get(handles.shy, 'string'));

然后改成这个。

% ---------Edit1_Callback -- Sx ------------- %
sx = str2double(get(handles.edit1, 'string'));

% ----------Edit2_Callback -- sy ------------ %
sy = str2double(get(handles.edit2,'string'));

% ----------Edit3_Callback -- shx ------------ %
shx = str2double(get(handles.edit3, 'string'));

% ----------Edit4_Callback -- shy ------------ %
shy = str2double(get(handles.edit4, 'string'));

但是您必须从编辑文本函数的每个回调函数中删除此代码 这意味着 => handles.sx=str2double(get(hObject, 'String'));

这是我的几何变换代码

function pushbutton1_Callback(hObject, eventdata, handles)

% ---------Edit1 Sx ------------- %
sx = str2double(get(handles.edit1, 'string'));

% ----------Edit2 sy ------------ %
sy = str2double(get(handles.edit2,'string'));

% ----------Edit3 shx ------------ %
shx = str2double(get(handles.edit3, 'string'));

% ----------Edit4 shy ------------ %
shy = str2double(get(handles.edit4, 'string'));

% ------------Slider theta--------------- %
th = get(handles.slider1, 'value');
th = th * pi * 180;
%-------------------------------------%

f = imread('testImg.jpg');
Tscale = vertcat([sx 0 0], [0 sy 0], [0 0 1])
Trotation = vertcat([cos(th) sin(th) 0], [-sin(th) cos(th) 0], [ 0 0 1]);
Tshear = vertcat([1 shx 0], [shy 1 0], [0 0 1]);

T = Tscale * Trotation * Tshear;

% tform = maketform('testImg.jpg', T); %Err Unrecognized TRANSFORMTYPE: 'testImg.jpg'.

tform = affine2d(T); 
% ref...
% https://www.mathworks.com/help/images/ref/affine2d.html

g = imwarp(f, tform);

% ref...
% https://www.mathworks.com/help/images/ref/imwarp.html
imshow(g);