The OPENCV Chinese website has a tutorial on combining DirectShow and OpenCV to capture images, address: http://wiki.opencv.org.cn/index.php/%e4%bd%bf%e7%94%a8DirectShow% e9%87%87%e9%9b%86%e5%9b%be%e5%83%8f
However, this configuration is older, this article describes how to configure and modify the VS2012 and opencv2.4.9 based on this tutorial to complete the USB camera driver.
The blogger's USB drive-free camera is as follows:
at the end of the article there is a full code download address 1. Environment Configuration
The OPENCV Chinese website has a tutorial on combining DirectShow and OpenCV to capture images, address: http://wiki.opencv.org.cn/index.php/%e4%bd%bf%e7%94%a8DirectShow% e9%87%87%e9%9b%86%e5%9b%be%e5%83%8f
However, the description of the "Ccamerads Class call collection function described in this document can be returned directly to Iplimage, the use of more convenient, and integrated DirectShow, do not need to install a large directx/platform SDK" is not reliable, DirectShow Seems to have started to be eliminated by Microsoft, and finally existed in the DirectX 9.0b package years ago.
Note that there is no need to download DirectX 9.0 packages Here, the configuration process under VS2012 and opencv2.4.9 is described below. 1.1 Configuring the VS2012 and OPENCV environments
According to the popular online configuration can, such as http://www.sigvc.org/bbs/thread-529-1-1.html. After configuration, try running an open picture applet to detect if the OPENCV environment is configured successfully. 1.2 Configuring the DirectX Environment
Create a new project, configure the OPENCV environment, and then add the CameraDS.h and CameraDS.cpp files downloaded from the OpenCV Chinese web site to the project header and source files, respectively.
The VS2012 flagship version comes with the SDK, in C:\Program Files (x86) \microsoft Sdks\windows\v7.1a\include.
Open the Properties page of the established VS2012 project, locate the VC + + directory, add (Frameworksdkdir) include in the included directory, add (Frameworksdkdir) include in the Library directory, add ( Frameworksdkdir) Lib.
Found include "Qedit.h" error, because the current version has no qedit.h this header file, from the URL: http://download.csdn.net/download/bcj296050240/9651955 The file is downloaded in the header file of the project. 2. Running
After the environment is configured, you can use the OpenCV Chinese Internet download main.cpp run, the running process may encounter the const char* can not be converted, the code here can be removed.
The following is the main function I wrote, providing a USB camera to open, monitor, image capture function. 2.1 Viewing all camera states of the system (Initallcameras function)
The parameter is an object of the Ccamerads class. This function gets the number of cameras and displays the camera name. From the output we can find the number of the USB camera, in general the number is 1.
Get the currently available camera and turn on the USB camera
int Initallcameras (ccamerads &m_camds) {
///Just get the number of cameras
int m_icamcount = Ccamerads::cameracount ();
printf ("There is%d cameras.\n", m_icamcount);
if (M_icamcount = = 0)
{
return-1;
}
Get the name of all cameras for
(int i = 0; i < M_icamcount; i++)
{
char szcamname[1024];
int retval = M_camds.cameraname (i, Szcamname, sizeof (Szcamname));
if (retval >0)
{
printf ("Camera #%d ' s Name is '%s '. \ n", I, Szcamname);
}
else
{
printf ("Can not get Camera #%d ' s name.\n", i);
}
}
return m_icamcount;
}
The results of the operation are as follows:
There is 3 cameras.
Camera #0 ' s Name is ' Lenovo easycamera '.
Camera #1 ' s Name is ' 3D camera '.
Camera #2 ' s Name is ' Basler GenICam Source '.
As you can see from the running results, the number of 3D cameras used is 1. 2.2 Open USB Camera (Openusbcam function)
The function has four parameters, the first parameter is the object of the Ccamerads class, the Camnum is set to 1, which indicates that the USB camera to be opened now, not the computer comes with the camera. Camwidth and Camheight are set to the width and height of the image according to the camera they are using.
The code is as follows:
Open the USB camera!! must call the function
//Camnum = 1 before calling Camdisplay and Camcappic; The camera number is 1, which means that the USB camera
//Camwidth = 2560 is currently being used; Picture width
//camheight = 720; Picture height
int Openusbcam (ccamerads &m_camds, const int camnum=1, const int camwidth=2560, const int camheight = 720) {
//Get the number of currently available cameras
//In all cameras, General number 0 for PC with camera, number 1 for USB camera to use
int m_icamcount = Initallcameras (M_CAMDS) ;
if (M_icamcount = =-1) {
cout<< "no camera available!" Exit: " <<endl;
return-1;
}
Mat pframe;
Open the camera and take a picture
if ((!m_camds.opencamera (Camnum, False, Camwidth, camheight)) | | (M_camds.queryframe () = = NULL)) {
cout<< "#" <<camNum<< "number camera cannot be opened, program exits ...";
return-2;
}
return 1;
}
2.3 Real-time display (Camdisplay function)
The function has two parameters, the first parameter is the object of the Ccamerads class, the second parameter is the time, the default is to take a picture every 20ms.
During the display, press any key to exit. Can be modified according to their own requirements, such as change to press any key to save the current picture.
Displays the contents of the camera's field of view every 20 milliseconds display a frame
int camdisplay (ccamerads &m_camds, const int t=20) {
Mat pframe;
while (1)
{
//Get a frame
pframe = Mat (M_camds.queryframe ());
Display
imshow ("Camera", pframe);
Delay, if there is a key pressed, exit if
(Cv::waitkey (t)! =-1) break
;
}
M_camds.closecamera (); Do not call this function, Ccamerads will automatically turn off the camera
CV::d estroyallwindows ();
return 0;
}
2.4 Save Picture (Camcappic function)
The implementation obtains a frame picture to display and saves, the save path has the filename setting.
Grab the picture and save it to the filename path
int camcappic (ccamerads &m_camds, String filename= "Usbcam.jpg") {
Mat pframe;
Get a frame
pframe = Mat (M_camds.queryframe ());
Display
imshow ("Grab picture", pframe);
Waitkey (+);
Destroyallwindows ();
Grab picture save
imwrite (filename, pframe);
M_camds.closecamera (); Do not call this function, Ccamerads will automatically turn off the camera
return 0;
}
2.5 main Function
The order of invocation is to instantiate the object first and then turn on the camera (call the Openusbcam function).
After opening the camera, you can call different functions according to your own needs, if you want to display in real time, call the Camdisplay function, if you want to grab the image to save, call the Camcappic function.
Note that when these functions are called, the same Ccamerads object is used.
Note that the Closecamera () function is not easily called in the function, which releases the open USB camera. If you want to recapture the picture after you release it, you need to call the Openusbcam function again.
int main () {
ccamerads m_camds;
Turn on the USB camera
Openusbcam (M_CAMDS);
1.0 Display Camera Contents
cout<<endl<< "Show camera contents ..." <<endl;
Camdisplay (M_CAMDS);
cout<< "Show camera contents exit ..." <<endl<<endl;
2.0 Grab Picture Save
string filename = "usbcam.jpg";
cout<< "Grab picture save ..." <<endl;
Camcappic (M_CAMDS, filename);
cout<< "Grab picture Save Exit ... File path "<<filename<<endl;
cout<<endl<< "End ..." <<endl;
GetChar ();
return 0;
}
full code download address:
http://download.csdn.net/detail/bcj296050240/9652088