MATLAB: How can i capture image from webcome for recognization

capture image from webcame

Pls help me this, i have a webcame and i want to capture and save it in a folder for compare with the sample image is saved before.
Many thanks

Best Answer

%=====================================================================

function InitializeVideoCamera(handles)
% Initialize the video camera.
global vidobj; % Video camera object.


try
% Initialize Logitech webcam
vidobj = videoinput('winvideo', 1, 'RGB24_640x480');
if ~isempty(vidobj)
src = getselectedsource(vidobj);
vidobj.FramesPerTrigger = 1;
axes(handles.axesImage);
hImage = findobj(handles.axesImage, 'Type', 'image');
preview(vidobj, hImage);
% src.ZoomMode = 'manual';
% Turn on the live video preview. Display the bounding box over it if there is one selected.


TurnOnLiveVideo(handles);
end
% Turn on the live video preview. Display the bounding box over it if there is one selected.
TurnOnLiveVideo(handles);
catch ME
errorMessage = sprintf('Error in function logitech_webcam_OpeningFcn.\nNo Logitech webcam detected!\n\nError Message:\n%s', ME.message);
set(handles.txtInfo, 'string', errorMessage);
uiwait(warndlg(errorMessage));
end
%===================================================================== function snappedImage = SnapImage(handles)
% Declare imgOriginal. It might be filled with values here.
global imgOriginal; % Declare global so that other functions can see it, if they also declare it global.
global vidobj; % Video camera object.
if isempty(vidobj), return, end;
try
snappedImage = getsnapshot(vidobj);
axes(handles.axesImage);
hold off;
axis auto;
imshow(snappedImage, 'InitialMagnification', 'fit');
grayImage = rgb2gray(snappedImage);
% Just for fun, let's get its histogram.
[pixelCount grayLevels] = imhist(grayImage);
axes(handles.axesPlot);
bar(pixelCount);
title('Histogram of image');
xlim([0 grayLevels(end)]); % Scale x axis manually.
imgOriginal = snappedImage;
catch ME
errorMessage = sprintf('Error in function btnPreviewVideo_Callback.\nError Message:\n%s', ME.message);
set(handles.txtInfo, 'string', errorMessage);
msgboxw(errorMessage);
end
%=====================================================================
% Turn on the live video preview. Display the bounding box over it if there is one selected.
function TurnOnLiveVideo(handles)
global vidobj; % Video camera object.
% Bail out if there is no video object class instantiated.
if isempty(vidobj), return, end;
% Switch the current graphic axes to handles.axesImage.
% This is where we want the video to go.
axes(handles.axesImage);
% Reset image magnification. Required if you ever displayed an image
% in the axes that was not the same size as your webcam image.
hold off;
axis auto;
% Get the handle to the image in the axes.
hImage = findobj(handles.axesImage, 'Type', 'image');
% Turn on the live video.
preview(vidobj, hImage);
% Put hold on so that displaying our bounding box doesn't blow away the image.
hold on;
% Retrieve our x,y coordinates of the bounding box corners.
GetImageMask(handles);
% They have been previously set elsewhere as global variables.
global maskVerticesXCoordinates;
global maskVerticesYCoordinates;
if ~(isempty(maskVerticesXCoordinates) || isempty(maskVerticesYCoordinates))
% If the bounding box coordinates exist,
% plot the bounding box over the live video.
plot(maskVerticesXCoordinates, maskVerticesYCoordinates);
end
% stoppreview(vidobj);
return; % from TurnOnLiveVideo
Related Question