How to replace "getsnapshot" with the button on my webcam?
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I need to capture a picture from my webcam, but I want to do it with an external trigger. The preferred trigger is the webcam button, but I'm open for other suggestions.
I know how to open the preview window and use the getsnapshot to get a snapshot.
The code is as follows:
%%initially
clear
clc
close all
%%display image options
imaqhwinfo
%%define webcam and open camera window
vid = videoinput('winvideo', 1, 'RGB24_320x240');
preview(vid);
%%get snapshot
data = getsnapshot(vid);
What I want to do is substitute the getsnapshot(vid) with an endless loop that checks if I pressed the button. When that condition is satisfied, the loop breaks, and I continue with proccessing my image.
0 Commenti
Risposta accettata
Jiro Doke
il 30 Gen 2011
I'm not sure if there's an easy way of capturing the state of the button on the webcam. You have to see if your webcam provides an API for you to access such information. One way is to use a figure window and make use of mouse clicks with WindowButtonDownFcn and WindowButtonUpFcn (these are figure properties). You can use WindowButtonUpFcn to start acquisition and WindowButtonDownFcn to stop it. Here's an example. Put everything in one file and run it. You'll see a stream of numbers while you have your mouse down in the figure.
%%Main
function mainFunction
% Set up a figure with WindowButtonDownFcn and WindowButtonUpFcn
figure('WindowButtonDownFcn', @buttonPressFcn, ...
'WindowButtonUpFcn', @buttonUpFcn);
%%Button Down Function
function buttonPressFcn(obj, edata)
% On mouse down, set APPDATA "buttonpressed" to true
setappdata(0, 'buttonpressed', true);
% Check the APPDATA "buttonpressed". Break if false
while getappdata(0, 'buttonpressed')
disp(rand)
drawnow;
end
%%Button Up Function
function buttonUpFcn(obj, edata)
% On mouse up, set APPDATA "buttonpressed" to false
setappdata(0, 'buttonpressed', false);
Alternatively, you can create a push button or a toggle button ( uicontrol ) for doing the same thing.
3 Commenti
Più risposte (2)
Siddharth Shankar
il 30 Gen 2011
What you are asking for is a "hardware trigger". You could check if your VIDEOINPUT object supports hardware triggers by using the TRIGGERINFO command. For ex:
triggerinfo(vid)
Most webcams I have worked with only have "immediate" as the trigger type.
One possibility is to connect a debouncer switch to your system via the serial port, or maybe a data acquisition card. You can then have a call to GETSNAPSHOT as soon as you detect a switch press. You can read more about trigger types for image acquisition devices here: Comparison of Trigger Types.
0 Commenti
David Tarkowski
il 18 Feb 2011
Webcams don't expose access to their hardware buttons in a consistent manner, so there is no good way for us to expose those buttons as hardware trigger inputs.
I think that Jiro's suggestion of using a uicontrol is probably the best solution. In the callback for the uicontrol you would call getsnapshot and then call your processing routine.
0 Commenti
Vedere anche
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!