(1) programming with USB camera in MATLAB environment
The image acquisition toolbox in MATLAB provides us with the necessary functions, and we can call them directly. It mainly introduces how to use the toolbox to program the USB2.0 camera.
Tag: USB camera MATLAB
The image acquisition toolbox in MATLAB provides us with the necessary functions, so we can call them directly. In this post, we will briefly introduce how to use this toolbox to program the USB2.0 camera.
Let's get down to the truth. But remember that you have to install a Pc camera to do the following. If you want to install a camera for the first time, you 'd better restart the PC. Otherwise, you may not be able to recognize the camera.
In the whole process, we need to do the following:
1. query the specific parameters of usb2.0camera (imaqhwinfo)
2. Create a video input object (videoinput)
3. Image preview and display (preview, stoppreview, closepreview, and image)
4. Get the video image (getsnapshot)
5. Get and set the image retrieval device (get and set)
6. disable a video object (delete)
Before getting started, I would like to explain the following terms in the Image Acquisition toolbox:
Image Acquisition devices, such as cameras and scanners
Image Acquisition adapter: The main purpose is to transmit information between devices through MATLAB and image retrieval.
ROI: region of interest-of-interest
Let's talk about a few common functions. Here we just describe their functions. For details about how to use the help system
Getselectedsource
Imaqfind
Isvalid
Peekdata
Getdata
Imaqmontage
Query the specific parameters of usb2.0camera
To program a camera, we must know its related parameters. Of course, we can query the merchant's manual, but it's not tiring.
The MATLAB Image Acquisition Toolbox provides imaqhwinfo () For me to obtain the hardware information from the installed image on the PC.
If no parameter is input, a struct is returned, which contains the adapter and Matlab-related version information in the system (this is usually used for the first time)
> Info = imaqhwinfo
Info =
Installedadaptors: {'coreco' 'winvideo'} % two adapters are installed on my PC.
MATLAB version: '7.6 (r2008a )'
Toolboxname: 'image acquisition toolbox'
Toolboxversion: '3. 1 (r2008a )'
Copy code
When a parameter is input, a struct containing the data of the specified adapter is returned.
> Win_info = imaqhwinfo ('winvideo') % let's take a look at the specific parameters of the second adapter.
Win_info =
Adaptordllname: [1x81 char] % absolute path of the adapter DLL file
Adaptordllversion: '3. 1 (r2008a) '% adapter DLL file version
Adaptorname: 'winvideo' % adapter name
Deviceids: {[1]} % device ID, which is frequently used.
Deviceinfo: [1x1 struct] % device information, which is mainly used to obtain parameters of the device.
For more information, see, which of the following information does this image obtain? ==================
> Win_info.deviceids
Ans =
[1]
> Dev_win_info = win_info.deviceinfo
Dev_win_info =
Defaultformat: 'rgb24 _ 320x240' % get the default format of the image
Devicefilesupported: 0
Devicename: 'usb Pc camera p227' % device name
DeviceID: 1% device number
Objectconstructor: 'videoinput ('winvideo', 1) '% object construction method, which is mostly the same
Supportedformats: {1x12 cell} % supported formats of the obtained images. Generally, there are many formats. The above defaultformat is only the default format.
% ======================================================== See the Image Acquisition Device Support the image format
> Dev_win_info.supportedformats % you can see that the camera on my PC supports the following 12 picture formats
Ans =
Columns 1 through 5
'I420 _ 160x120 ''i420 _ 176x144' 'i420 _ 320x240' 'i420 _ 352x288 ''i420 _ 640x2017'
Columns 6 through 9
'Rgb24 _ 1280x960' 'rgb24 _ 160x120 ''rgb24 _ 176x144' 'rgb24 _ 320x240'
Columns 10 through 12
'Rgb24 _ 352x288' 'rgb24 _ 640x0000' 'rgb24 _ 800x600'
Video preview, collection, and storage
(1) create a video input object
OBJ = videoinput (adaptorname, DeviceID, Format)
Adaptorname: the name of the adapter. You can use the imaqhwinfo function without parameters for the first time.
DeviceID: The device ID. You can use the imaqhwinfo function to obtain the device ID for the first time.
Format: video collection format, which can be obtained through supportedformats of deviceinfo. If this parameter is not specified, the default format is used.
> OBJ = videoinput ('winvideo', 1, 'rgb24 _ 320x240') % here we use the default video collection format
Summary of video input object using 'usb pccamera p227 '.
Acquisition source (s): input1 isavailable.
Acquisition parameters: 'input1' is the current selected source.
10 frames pertrigger using the selected source.
'Rgb24 _ 320x240' video data to be logged upon start.
Grabbing first ofevery 1 frame (s ).
Log Data to 'memory 'on trigger.
Trigger parameters: 1 'immediate' trigger (s) on start.
Status: Waiting for start.
0 frames acquiredsince starting.
0 frames availablefor getdata.
(2) open the video preview window.
Himage = Preview (OBJ, himage)
OBJ: video collection object
Himage: The handle corresponding to the video preview window. That is to say, you can preview the video in the specified handle object. This parameter can be left blank.
You can use the colsepreview and stoppreview functions to close and stop the preview window.
Vidres = get (OBJ, 'videoresolution ');
Nbands = get (OBJ, 'numberofbands ');
Figure () % specifies the figure displayed in the preview form
Axes () % specifies the coordinate system displayed in the preview window
Himage = image (zeros (vidres (2), vidres (1), nbands ));
Preview (OBJ, himage );
(3) image capture, display, and storage
% Frame is the matrix of H x W x B. H Image Height, specified by roiposition; W image width, specified by roiposition; B cable number, specified by numberofbands
Frame = getsnapshot (OBJ); % get a video frame
Imshow (FRAME); % displays the obtained Frame
Imwrite(frame,'snap.jpg ', 'jpg ');
(2)
MATLAB camera data collection
Www.diybl.com time: 2010-07-10 Author: Network Editor: huyang629 CLICK: 24 [comment]
1. query the specific parameters of usb2.0camera. Enter imaqinfo = imaqhwinfo Response Information Installedadaptors: {'winvideo '} MATLAB version: '7. 1 (r14sp3 )' Toolboxname: 'image acquisition toolbox' Toolboxversion: '1. 9 (r14sp3 )' Enter imaqinfo. installedadaptors Response Information Ans =
'Winvideo' Enter winvideoinfo = imaqhwinfo ('winvideo ') Response Information Winvideoinfo =
Adaptordllname: [1x76 char] Adaptordllversion: '1. 9 (r14sp3 )' Adaptorname: 'winvideo' Deviceids: {[1]} Deviceinfo: [1x1 struct] Enter winvideoinfo. deviceinfo Response Information
Ans =
Defaultformat: 'yuy2 _ 160x120' Devicefilesupported: 0 Devicename: 'usb video device' DeviceID: 1 Objectconstructor: 'videoinput ('winvideo', 1 )' Supportedformats: {1X5 cell} Enter device1 = winvideoinfo. deviceinfo (1) Response Information
Device1 =
Defaultformat: 'yuy2 _ 160x120' Devicefilesupported: 0 Devicename: 'usb video device' DeviceID: 1 Objectconstructor: 'videoinput ('winvideo', 1 )' Supportedformats: {1X5 cell} Enter device1.devicename Response Information Ans =
USB video device Enter device1.deviceid Response Information
Ans =
1 Enter device1.defaultformat Response Information Ans =
Yuy2_160x120 Enter device1.supportedformats Response Information Columns 1 through 4
'Yuy2 _ 160x120 ''yuy2 _ 176x144' 'yuy2 _ 320x240 ''yuy2 _ 352x288'
Column 5
'Yuy2 _ 640x' 2. Enter the following code to obtain the default camera capture window for preview. OBJ = videoinput ('winvideo', 1 ); Preview (OBJ ); 3. Enter the following code: % Access an image acquisition device. Vidobj = videoinput ('winvideo', 1, 'yuy2 _ 640x '); Sources = vidobj. source; Whos sources
Set (vidobj, 'selectedsourcename', 'input1 '); Sources Selectedsrc = getselectedsource (vidobj ); Get (selectedsrc ); Delete (vidobj ); Clear vidobj;
Response Information Name size bytes class
Sources 1x1 726 videosource object
Grand total is 30 elements using 726 bytes
Display summary for video source object:
Index: sourcename: selected: 1 'input1'
General settings: Parent = [1x1 videoinput] Selected = on Sourcename = input1 Tag = Type = videosource
Device specific properties: Backlightcompensation = on Brightness =-16 Contrast = 120 Fig = 30.0000 Gamma = 60 Hue = 0 Saturation = 40 Sharpness = 3
4. Enter the following code: CLC; CLF; Clear all; Imaqmem (30000000); % applied for memory space % Adaptor: an interface between MATLAB and a video device. The main purpose is to transmit information.
Vid = videoinput ('winvideo', 1, 'yuy2 _ 640x '); Preview (VID ); Start (VID ); H = figure ('numbertitle', 'off', 'name', 'video ',... 'Menubar ', 'none', 'color', 'C ',... 'Position', [0, 0, 1, 1], 'visible ', 'on'); % create a window Set (H, 'doublebuffer', 'on', 'outerposition', get (0, 'screensize ')); H1 = axes ('position', [0.02, 0.1, 0.4, 0.8], 'parent', H); % create a display window Hold on; Axis off; While ishandle (h) % determines whether an image object handle is valid A = getsnapshot (VID); % capture image Flushdata (VID); % clear all data of the Data retrieval engine, set the property samplesavailable to 0 Imshow (a); % display image Drawnow; % update images in real time End; Delete (VID ); |