1

我创建了一个简单的 GUI 来预览网络摄像头流并从中获取快照。为此,我在轴上创建了显示视频,一个按钮(pushbutton1)开始预览,一个按钮(pushbutton2)获取快照。以下是这两个按钮的代码。

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)
axes(handles.axes1);
vidObj = videoinput('winvideo',1);
videoRes = get(vidObj, 'VideoResolution');
numberOfBands = get(vidObj, 'NumberOfBands');
handleToImage = image( zeros([videoRes(2), videoRes(1), numberOfBands], 'uint8') );
preview(vidObj, handleToImage);


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
a=getsnapshot(get(axes,'Children'));
imshow(a);

在 pushbutton2_Callback 中,我试图获得轴的孩子,即。vidObj. 但这给了我错误??? Undefined function or method 'getsnapshot' for input arguments of type 'double'.。为什么它返回双类型而不是子对象vidObj?如何修复它并获取快照?还有其他更好的方法吗?(我刚开始学习 GUI。)谢谢。

4

1 回答 1

2

将变量声明为全局变量的更好替代方法是使用handles结构来共享数据。GUIDE 已经使用这种结构来存储所有 GUI 组件的句柄。只需将您的数据作为字段添加到此结构中,该结构将传递给所有回调函数。

所以在第一个回调中:

function pushbutton1_Callback(hObject, eventdata, handles)
    %# ... your existing code ...

    %# store video object in handles, and persist
    handles.vidObj = vidObj;
    guidata(hObject,handles)
end

然后在第二个中,您可以从handles结构中检索视频对象:

function pushbutton2_Callback(hObject, eventdata, handles)
    frame = getsnapshot(handles.vidObj);
    imshow(frame);
end
于 2012-06-09T16:47:04.360 回答