c++ - findcontours 断言失败

标签 c++ exception opencv inheritance

我是 C++ 和 opencv 的新手。我写了一个简单的程序,您可以在下面找到它,但是当我运行它时,我总是会抛出异常 findContours(img, ctr, CV_RETR_LIST, CV_CHAIN_APPROX_NONE)类型断言失败引发

OpenCV Error: Assertion failed (mtype == type0 || (CV_MAT_CN(mtype) == CV_MAT_CN (type0) && ((1 << type0) & fixedDepthMask) != 0)) in create, file C:\opencv\modu les\core\src\matrix.cpp, line 1466.

我需要一个表示单个轮廓并集成轮廓分析方法的类。我知道CONTOUR是关于 vector<Point> 的不同类型但由于它扩展了后者,所以不应该 CONTOUR也是vector<Point>输入(并且以同样的方式 vector<CONTOUR> 也是一个 vector< vector<Point> > )?我错了吗?

请注意,如果您声明CONTOUR作为派生自 vector<vector<Point>> 的类并声明Ctr在下面的代码中作为 CONTOUR对象代替 vector<CONTOUR>一切顺利。

非常感谢。

这是我的代码

#include "opencv2/opencv.hpp"

#include <vector>

using namespace cv;
using namespace std;

class CONTOUR : public vector<Point>
{
public:
    CONTOUR() : vector<Point>(){ };
    CONTOUR(const CONTOUR& orig) : vector<Point> (orig){ };
    virtual ~CONTOUR(){ };

    CONTOUR& operator=(const CONTOUR& rhs)
    {
        vector<Point> :: operator = (rhs);
        return *this;
    }

    CONTOUR& operator=(const vector<Point>& rhs)
    {
        vector<Point> :: operator = (rhs);
        return *this;
    }
};

/** @function main */
int main(int argc, char** argv)
{
    VideoCapture Camera;

    if(Camera.open(0))
    {
        Mat img;

        namedWindow("VIDEO", CV_WINDOW_AUTOSIZE);

        for(;;)
        {

            Camera >> img;

            if(!img.empty())
            {
                CONTOUR ctr;
                RNG n(12345);

                GaussianBlur(img, img, Size(5,5), 1.0, 1.0);
                cvtColor(img, img, CV_BGR2GRAY);
                Canny(img, img, 20, 80, 3);

                findContours(img, ctr, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);

                Mat shape = Mat::zeros( img.size(), CV_8UC3 );

                for( unsigned int i = 0; i< ctr.size(); i++ )
                {
                    Scalar color(n.uniform(0,255), n.uniform(0,255), n.uniform(0,255));
                    drawContours(shape, ctr, i, color, 1, 8);
                }

                imshow("VIDEO", shape);

                if(waitKey(30) >= 0)
                {
                    break;
                }
            }
        }
    }
    else
    {
        cout << "Camera not opened" << endl;
    }

    return 0;
}

最佳答案

首先,请允许我这样说:尝试以多态方式使用标准库容器是一个 Bad Idea .不要这样做。在您的情况下甚至没有必要。

解决您的问题很简单:免除 class CONTOUR并传递 vector<vector<cv::Point>> .这是因为 cv::findContours()要求您传递那个或等效的 cv::Mat .这是因为它使用代理类型作为只能从这些类型构造的参数,因此断言失败。如果要定义轮廓的速记,请使用 typedef std::vector<cv::Point> Contour , 而不是 #define CONTOUR .这为您带来了类型安全的好处。

此外,vector<CONTOUR>vector<vector<Point>> 类型相同.尽管CONTOUR继承自 vector<cv::Point> ,它们是不同的类型。因此,它们的 vector 也是不同的类型。 This answer也可能有助于理解这个问题。

此外,我注意到在您的代码中,CONTOUR源自 vector<cv::Point> .这个断言表明你需要一个 vector 的 vector :vector<vector<cv::Point>> .

关于c++ - findcontours 断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17797674/

相关文章:

c++ - 矩阵 vector 乘积,如 Eigen 中的乘法

c++ - Intel Advisor 赋予的这些功能是什么?

python - 如何获得图像凸出效果?

python - ImportError : numpy. core.multiarray 在使用 mod_wsgi 时导入失败

c++ - 有没有办法解决这个模板循环依赖

c++ - 如何让我的编译器更笨(错误的索引)?

python - "raise"和 "raise e"之间的区别?

java - Java中有没有一种方法可以使用Instrumentation来拦截对象的创建?

c# - 检查实例是否正确实例化的最佳方法

android - OpenCv 和 Android studio 1.3+ 使用新的 gradle - undefined reference