Using OPENCV to invoke camera problems under Win7 system

Source: Internet
Author: User
Tags rounds

When I try to use OPENCV to call the camera to do the center position detection of the circle in the picture, I find that the program can successfully invoke the integrated camera of the note, but cannot invoke the external USB camera. (Used for OpenCV1.0 + VC6.0)

The following programs are used:

#include "Cv.h"
#include "highgui.h"
#include "cvcam.h"
#include "iostream"
using namespace Std;

int main (int argc,char* argv[])
{
cvcapture* cap;
Cap=cvcapturefromcam (0);
Cap=cvcreatecameracapture (1); Use this function the same as the function above
if (!CAP)
{
cout<< "Create Camera capture Error" <<endl;
System ("pause");
Exit (-1);
}
Cvnamedwindow ("Example", 1);
iplimage* img;


Sleep (2000);

while (1)
{
Img=cvqueryframe (CAP);//capture video frame of camera and decode operation
Img=cvloadimage ("f:\\360 cloud disk \ \ Picture \\20083281447418_2.jpg"); Read pictures directly
if (!img)
Break
if (IMG->ORIGIN==IPL_ORIGIN_TL)//If the picture origin is in the upper-left corner, flip it along the x-axis so that the lower-left corner
{
Cvflip (IMG,IMG);
}
Cvshowimage ("Example", IMG);
Cvwaitkey (30);

Iplimage*gray =cvcreateimage (Cvgetsize (IMG), 8, 1);
Cvmemstorage*storage = cvcreatememstorage (0);
Cvcvtcolor (Img,gray,cv_bgr2gray);
Cvsmooth (gray,gray,cv_gaussian,5,15);

Cvseq*circles = Cvhoughcircles (gray,storage,cv_hough_gradient,2,gray->height/4,200,100);
int i;
for (i=0; i< circles->total; i++)
{
Float*p = (float*) Cvgetseqelem (circles,i);

Cvcircle (IMG, Cvpoint (Cvround (p[0]), Cvround (P[1])), 3, Cv_rgb (0,255,0),-1, 8, 0);
Cvcircle (IMG, Cvpoint (Cvround (p[0)), Cvround (P[1]), Cvround (p[2)), Cv_rgb (255,0,0), 3, 8, 0);
cout<< "Center coordinate x=" <<cvround (p[0]) <<endl<< "Center coordinate y=" <<cvround (p[1]) <<endl;
cout<< "radius =" <<cvround (p[2]) <<endl;
}
cout<< "Number of rounds =" <<circles->total<<endl;
Cvwaitkey (10);
}
Cvreleasecapture (&CAP);
Cvdestroyallwindows ();
Cvreleaseimage (&IMG);
return 0;
}

About the parameters of the function Cap=cvcapturefromcam (0) in the program, some people think that the set to 0 o'clock call is integrated camera, set to 1 when the call is a USB camera, only one camera is set to-1;

However, experiments on this machine found that the integration camera was called when set to 0 and 1, and the call error "Create Camera capture Error" is displayed when set to 1;

There are currently two main solutions

1, using VS2010 + OpenCV2.3.1 configuration display can call the USB camera, successful when the support SSE4.3 string.

In addition, someone on the Internet may be using the Win7 for reasons that do not support OpenCV1.0, instead using an XP system to solve the problem.   But I tried on the XP system is not successful, indicating that system compatibility is not the root of the problem. It is more likely that the previous OPENCV low version could not support a higher version of the USB camera.

2, using DirectShow can be directly on the Win7 + VC6.0 + OpenCV1.0 on the display of the USB camera,

The following programs are used:

#include "cxcore.h"
#include "cvcam.h"
#include "Windows.h"
#include "Cv.h"
#include "highgui.h"
#include "iostream"
using namespace Std;

iplimage* image;
void callback (iplimage* image);
int main ()
{
int ncams = Cvcamgetcamerascount ();//Returns the number of cameras that can be accessed
int width=640;
int height=480;

HWND Mywin;
Setting System Properties

Cvcamsetproperty (0, cvcam_prop_enable, cvcamtrue); Enable video
0: To set the camera number of the property, the second parameter is the property name;
The third parameter is a pointer to the set value

Cvcamsetproperty (0, Cvcam_prop_render, cvcamtrue); Enable video

Mywin is the type of window HWND

Mywin = (HWND) cvgetwindowhandle ("Cvcam window");
Cvcamsetproperty (0, Cvcam_prop_window, &mywin); Selects a window

Cvcamsetproperty (0,cvcam_rndwidth,&width);
Cvcamsetproperty (0,cvcam_rndheight,&height);
Video rendering

The callback function will process each frame
Cvcamsetproperty (0, Cvcam_prop_callback, CALLBACK);

Cvcaminit ();
Cvcamstart ();
Iplimage*gray =cvcreateimage (cvgetsize (image), 8, 1);
Cvmemstorage*storage = cvcreatememstorage (0);
Cvcvtcolor (Image,gray,cv_bgr2gray);
Cvsmooth (gray,gray,cv_gaussian,5,15);

Cvseq*circles = Cvhoughcircles (gray,storage,cv_hough_gradient,2,gray->height/4,200,100);
int i;
for (i=0; i< circles->total; i++)
{
Float*p = (float*) Cvgetseqelem (circles,i);

Cvcircle (Image, Cvpoint (Cvround (p[0)), Cvround (P[1])), 3, Cv_rgb (0,255,0),-1, 8, 0);
Cvcircle (Image, Cvpoint (Cvround (p[0)), Cvround (P[1]), Cvround (p[2)), Cv_rgb (255,0,0), 3, 8, 0);
cout<< "Center coordinate x=" <<cvround (p[0]) <<endl<< "Center coordinate y=" <<cvround (p[1]) <<endl;
cout<< "radius =" <<cvround (p[2]) <<endl;
}
cout<< "Number of rounds =" <<circles->total<<endl;
Cvwaitkey (100);
Now the program starts to work
Cvwaitkey (0);
Cvcamstop ();
Cvcamexit ();
return 0;
}

callback function
void callback (iplimage* image)
{
Cvcampause ();
Cvcamgetproperty (0, "Raw_image", &image);
Cvcamresume ();
}

But the last paragraph of the program in the image of each frame of the address is not clear is not *image, so there is still no center position detection of the program section added.

DirectShow is a new generation of COM (Component Object Model)-based streaming media processing developed by Microsoft on the basis of ActiveMovie and video for Windows, published with the DirectX development package. OPENCV is a computer vision processing library, which is widely used in the research and application of computer vision. OpenCV can use DirectShow to capture camera video, which is better than using the OPENCV capture video data function. This is my personal opinion.

Using OPENCV to invoke camera problems under Win7 system

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.