How to replace "getsnapshot" with the button on my webcam?

5 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Jiro Doke
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
Jiro Doke
Jiro Doke il 1 Feb 2011
I updated my answer. Hope that helps.
matar maoz
matar maoz il 4 Feb 2011
this is a good way to check if there is a common signal recognition for webcam and mousebutton.
--UNFORTUNATELLY--
IT DIDNT WORK FOR MINE
I am thinking of another way achive my goal.
I think about using an automatic, in coded, trigger.
It will take periodicly a series of snapshots from the camera.
When Ill pull the (mechanical) trigger, it will shut down power to the camera, and that would be the sighn to MATLAB to save the last snapshot took.
a bit of a hard work, but seems promising.
Can I do that?

Accedi per commentare.

Più risposte (2)

Siddharth Shankar
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.

David Tarkowski
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.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by