【问题标题】:Open cv finding convex hullOpencv寻找凸包
【发布时间】:2015-07-10 22:35:24
【问题描述】:

OpenCV 中的以下代码用于检测黄色球并绘制其凸包。该代码虽然没有给出任何编译错误,但在输出窗口中给出了以下错误。 我使用最大面积函数来避免较小的不需要的轮廓。 错误是

断言失败 in cv::drawContours ,文件 C:Buildsmasters..(某些路径),第 2299 行****

#include <opencv\cv.h>
    #include <opencv2\highgui\highgui.hpp>
    #include<opencv\cvaux.h>
    #include<opencv\cxcore.h>
    #include <opencv2\imgproc\imgproc.hpp>
    #include <iostream>
    #include<conio.h>
    #include <stdlib.h>



    using namespace cv;
    using namespace std;

    int main(){


        Mat img, frame, img2, img3;
        double maxarea = 0;
        int lrgctridx; //largest contour index
        VideoCapture cam(0);
        while (true){
            cam.read(frame);
            cvtColor(frame, img, CV_BGR2HSV);
            //thresholding 
            inRange(img, Scalar(0, 143, 86), Scalar(39, 255, 241), img2);



            //finding contours
            vector<vector<Point>> Contours;
            vector<Vec4i> hier;
            //morphological transformations
            erode(img2, img2, getStructuringElement(MORPH_RECT, Size(3, 3)));
            erode(img2, img2, getStructuringElement(MORPH_RECT, Size(3, 3)));

            dilate(img2, img2, getStructuringElement(MORPH_RECT, Size(8, 8)));
            dilate(img2, img2, getStructuringElement(MORPH_RECT, Size(8, 8)));


            //finding the contours required
            findContours(img2, Contours, hier, CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE, Point(0, 0));



            //finding the contour of largest area and storing its index
            for (int i = 0; i < Contours.size(); i++)
            {
            double a=contourArea(Contours[i]);
            if (a> maxarea)
            {
                maxarea = a;
             lrgctridx=i;
            }

            }
            //convex hulls
            vector<vector<Point> >hull(Contours.size());
            for (int i = 0; i < Contours.size(); i++)
            {
                convexHull(Contours[i], hull[i], false);
            }
            //REQUIRED contour is detected,then draw a convex hull
            if (maxarea!=0)
           drawContours(frame, hull, lrgctridx, Scalar(255, 255, 255), 1, 8, vector<Vec4i>(), 0, Point());



            imshow("output", frame);
            char key = waitKey(33);
            if (key == 27) break;



        }









    }

任何帮助将不胜感激。提前致谢!

【问题讨论】:

    标签: c++ opencv


    【解决方案1】:

    您应该在每次迭代时重置 lrgctridx

    假设您在时间“t”找到了一个计数,并且您设置了lrgctridx = 1;。在时间“t+1”,您没有找到任何轮廓,因此 Contourshull 的大小将为 0,但您正在尝试访问位置 1。

    只需将lrgctridx = 0 放在for 循环之前。 maxarea 也一样。

    lrgctridx = 0;
    maxarea = 0;
    for (int i = 0; i < Contours.size(); i++)
    {
    ....
    

    现在你画轮廓的条件没问题,但你最好换成

    if(!Contours.empty()) {
        drawContours(...);
        ....
    

    【讨论】:

    • 在时间 =t+1 假设没有轮廓。lrgctrdx 的值为 0 是否仍会绘制轮廓。如果我正确,唯一阻止的是区域!= 0 部分?如果我删除那行代码似乎可以正常工作..我不明白
    • @SridharThiagarajan 是的,您还应该将 maxarea = 0 与 lrgctridx 一起重置,这样您就不会调用 drawContours。或者只是if(!Contours.empty()) { drawContours...
    • @SridharThiagarajan 很高兴它有所帮助。通常,始终初始化变量并进行检查以防止出现极限情况。
    • 酷..如果我删除该区域,代码的工作原理相同!=0 条件..为什么会这样??
    • @SridharThiagarajan 我想如果hull 是空的,你什么都不会画
    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2012-08-02
    • 1970-01-01
    • 1970-01-01
    • 2012-01-05
    • 2020-06-04
    • 2013-02-20
    相关资源
    最近更新 更多