使用MATLAB控制笔记本电脑上的摄像头驱动程序

function liveVideoTemplate()
% liveVideoTemplate - Template for running a video stream into Matlab.
%
% This function can be altered for general-purpose use of streaming video
% into Matlab from an attached camera. 
%
% Helpful functions - You may find the following functions helpful.
%
%   % To get information about hardware devices, use:
%   h = imaqhwinfo  
%
%   % Look at the installed adaptors from h (using the above command). You
%   %   can find hardware components (cameras) that Matlab knows about 
%   %   using:
%   h = imaqhwinfo('ADAPTOR')
%   eg: h = imaqhwinfo('winvideo')
%   
%   % From here, look at h.DeviceIDs; this field will list all devices that
%   %   can be used with the associated adaptor (a dll file). To see 
%   %   information about a particular device, look at:
%   h.DeviceInfo(DeviceID)
%   eg: h.DeviceInfo(1)
%   
%   % Of important note is the field SupportedFormats, ie:
%   h.DeviceInfo(1).SupportedFormats
%   
%   % This field lists all the potential formats that the connected camera
%   %   can return. These formats typically look something like 
%   %   'FORMAT_ImgWdthxImgHght'. Eg 'MJPG_1280x720' will return RGB images 
%   %   with a resolution of 1280x720. Common formats include H264, MJPG, 
%   %   and YUY2. 
%   %
%   %   NOTE: as far as I know streaming H.264 format is NOT supported 
%   %   (even if a camera lists the format and Matlab allows you to set up 
%   %   the object, the returned images will not be converted to an rgb 
%   %   format Matlab recognizes and your image will look like noise).
%
%   % To connect a camera device to Matlab you create a videoinput object:
%   vid = videoinput('ADAPTOR', 'DeviceID', 'FORMAT');
%   eg: vid = videoinput('winvideo', 1, 'YUY2_640x480');
%
%   % You can see the available properties of our video object with:
%   get(vid)
%
%   % Of note are the camera properties which can be found with:
%   vid.Source
%
%   % You can see if the camera object is working with:
%   preview(vid)
%   
%   % To get a single frame from the camera, use the getsnapshot command:
%   img = getsnapshot(vid);
%   imshow(img)
%   
%   % If your camera returns YUY2 you can set the video object to return 
%   %   rgb images with the following command:
%   set(vid, 'ReturnedColorSpace', 'rgb');
%
%   % To stream video you will want to put the video object into manual 
%   %   mode and then 'start' it. Connecting to the camera takes some 
%   %   overhead so we just want to connect once; the camera will start 
%   %   streaming video (without Matlab saving it) and then when we request
%   %   a frame Matlab can just get the most recent image from the stream:
%   triggerconfig(vid,'manual');
%   start(vid)
%
%   % Now you can get and display images as desired:
%   tic
%   while toc<10
%       img = getsnapshot(vid);
%       imagesc(img); axis off
%   end
%
%   % After you are done you will want to 'stop' the video object. This 
%   %   will allow other programs to use the camera.
%   stop(vid)
%
%   % If you lose scope of your video object (i.e. a function you have 
%   %   using the video object crashes) you don't need to close Matlab in 
%   %   order to get control of your video object again. You can use 
%   %   imaqfind to find it:
%   vid = imaqfind;
%
%   % Finally, to clear out a video object, use the following command:
%   delete(vid)

% Delete any previous [video] objects (necessary if this program previously
%   crashed):
delete(imaqfind);

% Set up video object. Note: to change to a different camera (or camera
%   setup) change the following line:
vid = videoinput('winvideo',1,'YUY2_640x480');

% Set the video object to always return rgb images:
set(vid, 'ReturnedColorSpace', 'rgb');

% Put the video trigger into 'manual', this starts streaming the video
% without saving it. We can then request frames at will while only having
% to run the startup overhead this one time.
triggerconfig(vid,'manual');
start(vid)

% Initialize frame counter and fps variable
counter = 0;
fps = 0;

% Set the total runtime in seconds 
runtime = 15;

% Initialize figure to display video
h = figure(1);

% Start the timer and start keeping track of the time at the beginning of
% every 10 frames
tic
timeTracker = toc;

% We run a while loop to get and display a frame from the camera. The while
% loop runs for <runtime> seconds.
while toc < runtime 
  % Compute the frame rate averaged over the last 10 frames
   if counter==10
       counter = 0;
       fps = 10/(toc-timeTracker);
       timeTracker = toc;
   end
   counter = counter + 1;

   % Get a new frame from the camera
   img = getsnapshot(vid);

   % PUT IMAGE PROCESSING HERE
   %    You may put any type of image processing you want to do here.
   
   % Display image
   %    Note: use imagesc() instead of imshow() (it's faster).
   imagesc(img); axis off
   title(['FPS: ' sprintf('%2.1f', fps)])
   
end

% Stop the video stream
stop(vid)
C76

Guess you like

Origin blog.csdn.net/ccsss22/article/details/121804404