[go] using MATLAB for USB camera programming

Source: Internet
Author: User

http://blog.csdn.net/linxue968/article/details/6047376

1, query Usb2.0camera the specific parameters (Imaqhwinfo)

2. Create video input object (videoinput)

3, Image Preview and display (preview, Stoppreview, Closepreview and image)

4. Get video image (Getsnapshot)

5. Acquiring and setting the image acquisition device (get and set)

6. Close the video object (delete)

Before I explain it formally, I would like to explain the next few terms in the Image Acquisition Toolbox:

Image acquisition devices: such as cameras, scanners

Image Acquisition Adapter: The main purpose is to pass information between MATLAB and image acquisition devices by driving

Roi:region-of-interest area of Interest

In terms of a few commonly used functions, we're just explaining how it works, how to use the reference Help system

Getselectedsource

Imaqfind

IsValid

Peekdata

GetData

Imaqmontage

Give us a webcam we must know his relevant parameters before we can proceed with our programming. Of course we can check the Merchant handbook, but that tired is not tiring AH.

MATLAB's Image Acquisition Toolbox provides me with Imaqhwinfo () to get hardware information on the PC for mounting images

When there is no input parameter, a struct is returned, which contains the adapter and MATLAB-related version information that is present in the system (for the first time we generally use this)

Code:

>> Info=imaqhwinfo

info =

Installedadaptors: {' Coreco ' winvideo '}% here can see two adapters installed on my PC

Matlabversion: ' 7.6 (r2008a) '

Toolboxname: ' Image acquisition Toolbox '

Toolboxversion: ' 3.1 (r2008a) '

When there is an input parameter, returns a struct that contains the data information for the specified adapter

Code:

>> win_info=imaqhwinfo (' winvideo ')% we look at the specific parameters of the second adapter

Win_info =

Adaptordllname: [1x81 char]% Adapter DLL file absolute path

Adaptordllversion: ' 3.1 (r2008a) '% Adapter DLL file version

Adaptorname: ' WinVideo '%s adapter name

DeviceIDs: {[1]}% Device ID number, this we often need to use

DeviceInfo: [1x1 struct]% device information, here is mainly the image acquisition equipment, some parameters, more important

%==================== below we understand, this image to get the device exactly what is the details of it ====================

>> Win_info. DeviceIDs

Ans =

[1]

>> Dev_win_info=win_info. DeviceInfo

Dev_win_info =

DefaultFormat: ' rgb24_320x240 '% gets the default format of the picture

devicefilesupported:0

DeviceName: ' USB PC CAMERA P227 '% device name

deviceid:1% Device number

Objectconstructor: ' Videoinput (' WinVideo ', 1) '% object building method, this is mostly the same

Supportedformats: {1x12 cell}% gets the image support format, generally there are many kinds, the above defaultformat is only the default format

%================================== Look at Image capture device supported image formats ==================================

>> Dev_win_info. Supportedformats% 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_640x480 '

Columns 6 through 9

' rgb24_1280x960 ' rgb24_160x120 ' rgb24_176x144 ' rgb24_320x240 '

Columns through 12

' rgb24_352x288 ' rgb24_640x480 ' rgb24_800x600 '

Video preview, capture, and save

(1) Create a video input object

obj = Videoinput (Adaptorname,deviceid,format)

Adaptorname: Adapter name, the first time you can use the Imaqhwinfo function without parameters to get

DeviceID: The device ID number, which can be obtained through the Imaqhwinfo function for the first time

Format: Video capture formats, which can be obtained by DeviceInfo Supportedformats, using the default format if not filled in

Code:

>> obj = videoinput (' WinVideo ', 1, ' rgb24_320x240 ')% here we use the default video capture format

Summary of Video Input Object Using ' USB PC CAMERA P227 '.

Acquisition Source (s): INPUT1 is available.

Acquisition Parameters: ' INPUT1 ' is the current selected source.

Ten frames per trigger using the selected source.

' rgb24_320x240 ' video data to be logged upon START.

Grabbing first of every 1 frame (s).

Log data to ' memory ' on trigger.

Trigger parameters:1 ' immediate ' Trigger (s) on START.

Status:waiting for START.

0 frames acquired since starting.

0 frames available for GETDATA.

(2) Open the video preview window

Himage=preview (Obj,himage)

Obj: Video Capture Object

Himage: The corresponding handle to the video preview window, that is, to preview the video in the specified handle object, the parameter can be empty

The Colsepreview and Stoppreview functions can be used to close and stop the preview window

Code:

Vidres = Get (obj, ' videoresolution ');

Nbands = Get (obj, ' numberofbands ');

Figure ()% specifies the figure that the preview form displays

Axes ()% specifies the coordinate system that the preview window displays

Himage = Image (Zeros (Vidres (2), Vidres (1), nbands));

Preview (obj, himage);

(3) Image capture, display and save

Code:

%frame is the matrix of HXWXB. H image height, specified by roiposition; W image width, specified by roiposition; b cable number, specified by numberofbands

frame = Getsnapshot (obj);

Imshow (frame);

Imwrite (Fame, ' snap.jpg ', ' jpg ');

Experimental steps

1, query the specific parameters of Usb2.0camera

Input imaqinfo = Imaqhwinfo

return information

Installedadaptors: {' WinVideo '}

Matlabversion: ' 7.1 (R14SP3) '

Toolboxname: ' Image acquisition Toolbox '

Toolboxversion: ' 1.9 (R14SP3) '

Input Imaqinfo.installedadaptors

return information

Ans =

' WinVideo '

Input winvideoinfo = imaqhwinfo (' WinVideo ')

return information

Winvideoinfo =

Adaptordllname: [1x76 Char]

Adaptordllversion: ' 1.9 (R14SP3) '

Adaptorname: ' WinVideo '

DeviceIDs: {[1]}

DeviceInfo: [1x1 struct]

Enter Winvideoinfo. DeviceInfo

return 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)

return information

Device1 =

DefaultFormat: ' yuy2_160x120 '

devicefilesupported:0

DeviceName: ' USB video Device '

Deviceid:1

Objectconstructor: ' Videoinput (' WinVideo ', 1) '

Supportedformats: {1x5 cell}

Enter Device1. DeviceName

return information

Ans =

USB Video Device

Enter Device1. DeviceID

return information

Ans =

1

Enter Device1. DefaultFormat

return information

Ans =

yuy2_160x120

Enter Device1. Supportedformats

return information

Columns 1 through 4

' yuy2_160x120 ' yuy2_176x144 ' yuy2_320x240 ' yuy2_352x288 '

Column 5

' yuy2_640x480 '

2, the simplest collection experiment, enter the following code, you can get a preview of the default format of the Camera Capture window obj=videoinput (' WinVideo ', 1);

Preview (obj);

3. Enter the following code

Vidobj = Videoinput (' WinVideo ', 1, ' yuy2_640x480 ');

Sources = Vidobj. Source;

whos sources

Set (Vidobj, ' selectedsourcename ', ' input1 ');

Sources

SELECTEDSRC = Getselectedsource (vidobj);

Get (SELECTEDSRC);

Delete (vidobj);

Clear Vidobj;

return information

Name Size Bytes Class

Sources 1x1 726 VideoSource Object

Grand Total is elements using 726 bytes

Display Summary for Video Source Object:

Index:SourceName:Selected:

1 ' input1 ' on '

General Settings:

Parent = [1x1 videoinput]

Selected = On

SourceName = Input1tag =

Type = VideoSource

Device Specific Properties:

Backlightcompensation = On

brightness =-16

Contrast = 120

framerate = 30.0000

Gamma = 60

Hue = 0

saturation = 40

sharpness = 3

4. Enter the following code

CLC

ClF

Clear all;

Imaqmem (30000000); % Request Memory space

Interface between%adaptor:matlab and video devices, the main purpose is to transmit information

vid = Videoinput (' WinVideo ', 1, ' yuy2_640x480 ');

Preview (VID);

Start (vid);

H=figure (' Numbertitle ', ' off ', ' Name ', ' video ',...

' MenuBar ', ' none ', ' Color ', ' C ',...

' Position ', [0, 0, 1, 1], ' Visible ', ' on '); % New Window

Set (H, ' DoubleBuffer ', ' on ', ' Outerposition ', get (0, ' screensize '));

H1=axes (' Position ', [0.02, 0.1, 0.4, 0.8], ' Parent ', h); % New Display window

Hold on;

Axis off;

While Ishandle (h)% determines whether a valid image object handle

A=getsnapshot (VID); % capture Image

FlushData (VID); % Clear Data acquisition engine all data, set attribute samplesavailable to 0

Imshow (a); % Display Image

Drawnow; % real-time image update

End

Delete (vid);

[go] using MATLAB for USB camera programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.