代码之家  ›  专栏  ›  技术社区  ›  Harry

开放式小时循环

  •  2
  • Harry  · 技术社区  · 15 年前

    IM使用XCODE和C++

    我已经从 OpenCV documentation :

    #include <cv.h>
    #include <highgui.h>
    #include <math.h>
    
    using namespace cv;
    
    int main(int argc, char** argv)
    {
        Mat img, gray;
        if( argc != 2 && !(img=imread(argv[1], 1)).data)
            return -1;
        cvtColor(img, gray, CV_BGR2GRAY);
        // smooth it, otherwise a lot of false circles may be detected
        GaussianBlur( gray, gray, Size(9, 9), 2, 2 );
        vector<Vec3f> circles;
        HoughCircles(gray, circles, CV_HOUGH_GRADIENT,
                     2, gray->rows/4, 200, 100 );
        for( size_t i = 0; i < circles.size(); i++ )
        {
             Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
             int radius = cvRound(circles[i][2]);
             // draw the circle center
             circle( img, center, 3, Scalar(0,255,0), -1, 8, 0 );
             // draw the circle outline
             circle( img, center, radius, Scalar(0,0,255), 3, 8, 0 );
        }
        namedWindow( "circles", 1 );
        imshow( "circles", img );
        return 0;
    }
    

    然后修改如下:

    int main(int argc, char** argv)
    {
        VideoCapture cap(0);
        if(!cap.isOpened())
            return -1;
        namedWindow( "circles", 1 );
    
        Mat img, gray;
        for( ;; )
        {
    
            cap >> img;
            vector<Vec3f> circles;      
            cvtColor(img, gray, CV_BGR2GRAY);
            GaussianBlur(gray, gray, Size(7,7), 1.5, 1.5);
            HoughCircles(img, circles, CV_HOUGH_GRADIENT, 2, img->rows/4, 200, 100 );
            imshow( "circles", img );
            if(waitKey(30) >= 0) break;
        }
    
        return 0;
    }
    

    在这两种情况下都会得到错误:“->”的基操作数具有非指针类型“cv::mat” 然后我将->替换为。还有一个错误。这与我从文档中复制的代码相同。

    我的理论是,这种情况发生是因为它没有得到和形象或其他什么。但当我把小时圈的代码拿出来的时候,相机运行正常。

    有什么想法吗??

    2 回复  |  直到 14 年前
        1
  •  0
  •   karlphillip    15 年前

    都是编译错误吗?使用时会出现什么错误 . 访问 IMG 成员?我相信你应该 使用 -gt; 在这种情况下,从 IMG 不是指向mat的指针。

    根据 API reference manual (你应该拿些战利品):

    void HoughCircles(Mat& image, vector<Vec3f>& circles, int method, double dp, double minDist, double param1=100, double param2=100, int minRadius=0, int maxRadius=0);
    

    必须传递对象的内存地址 IMG 圈子 而不是通过对象本身。

    你应该这样做:

    HoughCircles(&img, &circles, CV_HOUGH_GRADIENT, 2, img.rows/4, 200, 100 );
    
        2
  •  2
  •   DigitalMonk    15 年前

    首先,你 不能 传递地址 img circles . HoughCircles接受引用,而不是指针。如果你试图通过 &img &circles ,您将收到新的令人兴奋的类型错误。您的原始代码在这方面是正确的。

    如果 函数接受指针,它的声明应该是:

    void HoughCircles(Mat * image, vector<Vec3f> * circles, ...);
    

    (这不是——我只对其他被 & C++函数声明中的语法

    第二, -> 不会工作,因为 Mat 是一个对象,而不是指针。我只是有点惊讶原始文档是错误的。OpenCV的C++接口(如果你正在使用的话) cv::Mat )是一种新的东西,而且东西还在转变。这可能是从C API文档复制/粘贴的, -gt; 错过了。

    img.rows 应该工作(并且为我工作)。您为提供的错误 IMG行 这个案子并不是一个完全的错误,所以我不知道为什么它是一个投诉。

    我现在唯一看到的是 dp 参数是 double ,你通过了一个 int . 我曾经使用过的任何C++编译器都会为你做上转换,但是如果你的编译器处于完全偏执狂模式,你可以尝试改变2到2。(我真的,真的怀疑这是问题所在,但是如果没有关于错误的更多细节,我不明白为什么会出现编译时错误)

    但是,在运行时,它会爆炸,因为你要经过你原来基于BGR的小时圈 IMG 而不是通过单一通道 gray .