OpenCV程序无法连接到网络摄像头/摄像头

时间:2013-10-13 02:57:10

标签: c opencv video camera

我正在尝试了解OpenCV,并在youtube上找到了一个非常好的教程,但每当我被告知在Visual Studio上“选择一个视频设备”时,会弹出一个R6010错误,我必须不断中止该程序。这是源代码。我正在使用OpenCV 2.2,2010 Visua Studio,相关的摄像头是HP TrueVision HD。谢谢!

//tracker1

#include<opencv\cvaux.h>
#include<opencv\highgui.h>
#include<opencv\cxcore.h>

#include<stdio.h>
#include<stdlib.h>

int main(int argc, char* []){

    CvSize size640x480 = cvSize(640, 480);

    CvCapture* p_capWebCam; //assign a webcam (later)

    IplImage* p_imgOriginal; //image given by the webcam
    IplImage* p_imgProcessed; //webcam image processed

    CvMemStorage* p_strStorage; //passing stored variables

    CvSeq* p_seqCircles;

    float* p_fltXYRadius; //3 points, 0 = X, 1 = Y, 2 = Radius

    int i; //looping integer

    char charCheckForEscKey;

    p_capWebCam = cvCaptureFromCAM(0);

    if(p_capWebCam == NULL){
        printf("error, webcam not found");
        getchar();
        return(-1);
    }

    cvNamedWindow("original", CV_WINDOW_AUTOSIZE);
    cvNamedWindow("processed", CV_WINDOW_AUTOSIZE);

    p_imgProcessed = cvCreateImage(size640x480, IPL_DEPTH_8U, 1);

    while(1){
        p_imgOriginal = cvQueryFrame(p_capWebCam); //get frame of webcame
        if(p_capWebCam == NULL){ //when the frame is not attained
            printf("error, no frames were attained");
            getchar();
            break;
        }

        cvInRangeS(p_imgOriginal, CV_RGB(175,0,0), CV_RGB(256, 100, 100), p_imgProcessed);

        p_strStorage = cvCreateMemStorage(0);

        cvSmooth(p_imgProcessed, p_imgProcessed, CV_GAUSSIAN, 9 ,9);

        p_seqCircles = cvHoughCircles(p_imgProcessed, p_strStorage, CV_HOUGH_GRADIENT, 2, p_imgProcessed->height/4, 100, 50, 10, 400);

        for(i=0; i<p_seqCircles->total; i++){
            p_fltXYRadius = (float*)cvGetSeqElem(p_seqCircles, i);
            printf("ball position x = %f, y = %f, r = %f \n", p_fltXYRadius[0], p_fltXYRadius[1], p_fltXYRadius[2]);

            cvCircle(p_imgOriginal, cvPoint(cvRound(p_fltXYRadius[0]), cvRound(p_fltXYRadius[1])), 3, CV_RGB(0, 255, 0), CV_FILLED);

            cvCircle(p_imgOriginal, cvPoint(cvRound(p_fltXYRadius[0]), cvRound(p_fltXYRadius[1])), cvRound(p_fltXYRadius[2]), CV_RGB(255, 0, 0), 3);
        }

        cvShowImage("original", p_imgOriginal);
        cvShowImage("processed", p_imgProcessed);

        cvReleaseMemStorage(&p_strStorage);

        charCheckForEscKey = cvWaitKey(10);
        if(charCheckForEscKey == 27) break;
    }

    cvReleaseCapture(&p_capWebCam);
    cvDestroyWindow("original");
    cvDestroyWindow("processed");


}

0 个答案:

没有答案