在Matlab中从网络摄像头获取快照 [英] Getting snapshot from webcam in Matlab
问题描述
我创建了一个简单的GUI来预览摄像头流并从中获取快照.为此,我在轴上创建了用于显示视频的图像,一个按钮(pushbutton1)开始预览,一个按钮(pushbutton2)获取快照.以下是这两个按钮的代码.
I have created a simple GUI to preview webcam stream and to get snapshot from it. For this I have created on axes to show video, one push button(pushbutton1) to start preview, one push button(pushbutton2) to get snapshot. Following is the code for these two push buttons.
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'.
.为什么要重用double类型而不是子对象vidObj
?
如何修复并获取快照?
还有其他更好的方法吗?
(我刚刚开始学习GUI.)
谢谢.
In pushbutton2_Callback I am trying to get child of axes ie. vidObj
. But this gives me error ??? Undefined function or method 'getsnapshot' for input arguments of type 'double'.
. Why is it returing double type instead of child object vidObj
?
How can I fix it and get snapshot?
Is there any other better way?
(I just started learning GUI.)
Thanks.
推荐答案
对于全局声明变量而言,更好的替代方法是将handles
结构用于
A better alternative to declaring your variables global, is to use the handles
structure for sharing data. GUIDE already uses this structure to store handles to all GUI components. Simply add your data as a field to this structure that gets passed around to all callback functions.
因此在第一个回调中:
function pushbutton1_Callback(hObject, eventdata, handles)
%# ... your existing code ...
%# store video object in handles, and persist
handles.vidObj = vidObj;
guidata(hObject,handles)
end
然后在第二个中,您可以从handles
结构中检索视频对象:
Then in the second, you can retrieve the video object from the handles
structure:
function pushbutton2_Callback(hObject, eventdata, handles)
frame = getsnapshot(handles.vidObj);
imshow(frame);
end
这篇关于在Matlab中从网络摄像头获取快照的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!