OpenCV从c++到C的转换

OpenCV conversion from C++ to C

本文关键字:转换 c++ OpenCV      更新时间:2023-10-16

我有一个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 foundn");
        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=%fnr",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

line 1224 at /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

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