-2

我有一个需要转换为 C 的 C++ openCV 代码。但是我找不到指南来检查我应该使用什么而不是

cvScalar、cvNamedWindow、cvSmooth

另外,如果您在我的代码中发现任何其他问题,您能告诉我吗?

#include "cv.h"
#include "highgui.h"
#include "cxcore.h"
#include <stdio.h>


IplImage* Threshold(IplImage* src)
{

    IplImage* srcHSV = cvCreateImage(cvGetSize(src), 8, 3);
    cvCvtColor(src, srcHSV, CV_BGR2HSV);// Convert the BGR to HSV

    IplImage* Thresholdimg = cvCreateImage(cvGetSize(src), 8, 1); // create a Threshold image

    // HSV tresholding
cvInRangeS(srcHSV, CvScalar(7, 107, 219), CvScalar(11,148,255), Thresholdimg);

    cvSmooth( Thresholdimg, Thresholdimg, CV_GAUSSIAN, 9, 9 ); // Smoothing with a Gaussian filter (9*9 kernel)


    cvReleaseImage(&srcHSV);

    return Thresholdimg;
}



int main()
{

    CvCapture* capture = 0;
    capture = cvCaptureFromCAM(1);// 1 for usb webcam, 0 o/w.

    if(!capture)
    {
        printf("Can not capture,no device found\n");
        return -1;
    }

    // Windows for Threshold and Output
    cvNamedWindow("Tracking output");
    cvNamedWindow("Threshold");

    while(1) // infinite loop
    {

        IplImage* frame = 0;
        frame = cvQueryFrame(capture); // decompress the captured frame

        if(!frame)
            break;

        IplImage* finalthreshold = Threshold(frame);

        cvShowImage("Threshold", finalthreshold);//show the thresholded image

        CvMemStorage*  storage  = cvCreateMemStorage(0);// allocate memory to store the contour information

        CvSeq* circles = cvHoughCircles(finalthreshold, storage, CV_HOUGH_GRADIENT, 2, finalthreshold->height/4, 100, 40, 20, 200);



        int i;
        for (i = 0; i < circles->total; i++)
        {

            float *p = (float*)cvGetSeqElem(circles, i);
            printf("Ball! x=%f y=%f r=%f\n\r",p[0],p[1],p[2] );
            CvPoint center = cvPoint(cvRound(p[0]),cvRound(p[1]));
            CvScalar val = cvGet2D(finalthreshold, center.y, center.x);

            if (val.val[0] < 1) continue;
            cvCircle(frame,  center, 3,             CV_RGB(0,255,0), -1, CV_AA, 0);
            cvCircle(frame,  center, cvRound(p[2]), CV_RGB(255,0,0),  3, CV_AA, 0);
            cvCircle(finalthreshold, center, 3,             CV_RGB(0,255,0), -1, CV_AA, 0);
            cvCircle(finalthreshold, center, cvRound(p[2]), CV_RGB(255,0,0),  3, CV_AA, 0);
        }



         cvShowImage("Tracking output", frame);


         int c = cvWaitKey(27);
         if(c!=-1)
             break;


         cvReleaseImage(&finalthreshold);

    }


    cvReleaseCapture(&capture);
    return 0;
}

以下是错误:

root@ghostrider:/home/zero/Desktop/deneme opencv# opencv untitled.c 
compiling untitled.c
untitled.c: In function ‘Threshold’:
untitled.c:17:2: error: too few arguments to function ‘cvScalar’
In file included from /usr/local/include/opencv2/core/core_c.h:47:0,
                 from /usr/local/include/opencv/cv.h:63,
                 from untitled.c:1:
/usr/local/include/opencv2/core/types_c.h:1224:22: note: declared here
untitled.c:17:2: error: too few arguments to function ‘cvScalar’
In file included from /usr/local/include/opencv2/core/core_c.h:47:0,
                 from /usr/local/include/opencv/cv.h:63,
                 from untitled.c:1:
/usr/local/include/opencv2/core/types_c.h:1224:22: note: declared here
untitled.c:19:2: error: too few arguments to function ‘cvSmooth’
In file included from /usr/local/include/opencv/cv.h:65:0,
                 from untitled.c:1:
/usr/local/include/opencv2/imgproc/imgproc_c.h:81:13: note: declared here
untitled.c: In function ‘main’:
untitled.c:42:2: error: too few arguments to function ‘cvNamedWindow’
In file included from /usr/local/include/opencv/highgui.h:47:0,
                 from untitled.c:2:
/usr/local/include/opencv2/highgui/highgui_c.h:120:12: note: declared here
untitled.c:43:2: error: too few arguments to function ‘cvNamedWindow’
In file included from /usr/local/include/opencv/highgui.h:47:0,
                 from untitled.c:2:
/usr/local/include/opencv2/highgui/highgui_c.h:120:12: note: declared here
Output file => untitled

第 1224 行/usr/local/include/opencv2/core/types_c.h

CV_INLINE  CvScalar  cvScalar( double val0, double val1 CV_DEFAULT(0),
                               double val2 CV_DEFAULT(0), double val3 CV_DEFAULT(0))
4

1 回答 1

2

您提到的构造以您给出的名称存在于 C 中。您可以继续使用它们,使用 C API 而不是 C++ API。

cvScalar

cvNamedWindow

cvSmooth

编辑: 请注意,C 实现没有默认参数。您需要为每个函数提供所有参数,即使您只是粘贴默认参数。

有关更多信息,请参阅此问题

于 2013-03-26T20:55:45.657 回答