My first Matlab tips. We will create a GUI form that will preview and capture picture from webcam connected to your computer. Webcam preview and captured image will be shown on an axes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | % choose which webcam (winvideo-1) and which mode (YUY2_176x144) vid = videoinput('winvideo', 1, 'YUY2_176x144'); % only capture one frame per trigger, we are not recording a video vid.FramesPerTrigger = 1; % output would image in RGB color space vid.ReturnedColorspace = 'rgb'; % tell matlab to start the webcam on user request, not automatically triggerconfig(vid, 'manual'); % we need this to know the image height and width vidRes = get(vid, 'VideoResolution'); % image width imWidth = vidRes(1); % image height imHeight = vidRes(2); % number of bands of our image (should be 3 because it's RGB) nBands = get(vid, 'NumberOfBands'); % create an empty image container and show it on axPreview hImage = image(zeros(imHeight, imWidth, nBands), 'parent', handles.axPreview); % begin the webcam preview preview(vid, hImage); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | vid = videoinput('winvideo', 1, 'YUY2_176x144'); vid.FramesPerTrigger = 1; vid.ReturnedColorspace = 'rgb'; triggerconfig(vid, 'manual'); vidRes = get(vid, 'VideoResolution'); imWidth = vidRes(1); imHeight = vidRes(2); nBands = get(vid, 'NumberOfBands'); hImage = image(zeros(imHeight, imWidth, nBands), 'parent', handles.axPreview) preview(vid, hImage); % prepare for capturing the image preview start(vid); % pause for 3 seconds to give our webcam a "warm-up" time pause(3); % do capture! trigger(vid); % stop the preview stoppreview(vid); % get the captured image data and save it on capt1 variable capt1 = getdata(vid); % now write capt1 into file named captured.png imwrite(capt1, 'captured.png'); % just dialog that we are done capturing warndlg('Done!'); |
Things to remember:
UPDATE: since there are people that having difficulties implementing the code, you can download this .zip file so you can quickly try it on your own Matlab. (Please remember that this code is created using Matlab 2011a GUIDE, I don’t guarantee it will work on earlier version)
Download MATLAB demo codes to capture image from webcam
thank you very much, really help a lot.
thanks a lot, the code is very easy and focus….
Thank you, just I needed
Thank you very much.. I am just begginner with matlab and camera interfacing.. and this helps me lot…
I’m grateful to you! This was my homework and I have searching about three hours on the internet.
Thanks a lot.
i used it in my code, but suddenly it stopped working. I don’t know why
I’ve edited my post, please download my code (.zip) and try it. Let me know if there’s still error.
i keep getting an error regarding undefined variable or class “handles” and “handles.axPreview”. How do i solve this?
Thanks
post updated, please download my codes (.zip) file. you can try it.
Where are the zip files?
Read the whole post. At the end of the post, there is a big download button (“Download MATLAB demo codes to capture image from webcam“)
thanks dear..
it works.
thank you very much..it’s works..
but, how to use two types of axes
1 axes for preview
another axes for result of capturing
thanks a lot
That helped me really very much thanks a lot for your efforts you are good
))
Gracias!!! es lo que andaba buscando y lo explicas muy bien.