c++ - OpenCV 从 C++ 到 C 的转换

标签 c++ c opencv

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

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

最佳答案

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

cvScalar

cvNamedWindow

cvSmooth

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

参见this question了解更多信息。

关于c++ - OpenCV 从 C++ 到 C 的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15647058/

相关文章:

c - 将 select() 与管道一起使用

opencv - 对象的 3D 重建和运动

c++ - move 语义 : invalid conversion from `type&&` to `type` . 模板:将未知参数传递给重载函数

c++ - 如何使用 keystates SDL 检查 key 是否已被释放

c - 这是对该程序输出的正确解释吗?

c - 将 TotalView 调试器附加到变量

opencv - 构建OpenCV 2.4.10 + Windows 7 + Visual Studio 2013,指导和错误解决

python - Opencv (Python) - 脑肿瘤的异常形状 Blob 检测

c++ - 在 C++ STL 中使用 auto 关键字

c++ - 'undefined reference to' 使用 Android Studio NDK 的函数