c++ - 这个opencv代码有什么问题

标签 c++ opencv

我正在尝试编写一个代码,在其中我可以检测除正方形之外的不同几何形状,我发现:this answer here答案是用 python 给出的,我尝试用 c++ 编写,但我的程序崩溃了,知道我做错了什么:

int main (){
    cv::Mat img = cv::imread("src.jpg",0);
    cv::Mat image ;
    
    std::vector<std::vector<cv::Point>> contours;
    //std::vector<std::vector<cv::Point2f>> hiararchy;
    
        cv::threshold(img,img,127,255,CV_THRESH_BINARY_INV);
        cv::findContours(img,contours,/*hiararchy,*/CV_RETR_EXTERNAL,CV_RETR_CCOMP );
    std::vector<cv::Point2f> approx; 
    for ( int i=0; i<contours.size();i++){
        cv::approxPolyDP(cv::Mat(contours[i]),approx,cv::arcLength(cv::Mat(contours[i]),true)*0.02,true);
        
    }
            
    cv::waitKey(0);
    
    return 0;
    
}

我已经调试了程序,它在cv::approxPolyDP函数中崩溃了!

**更新** 在 C. Canberk Ba​​cı 的建议之后,我更改了 for l

for ( int i=0; i<contours.size();i++){
    cv::Mat m(contours[i]);
    cv::approxPolyDP(m,approx,cv::arcLength(m,true)*0.02,true);
    
}

但变化不大 预先感谢您的帮助!

最佳答案

明白了:

int main (){
    cv::Mat img = cv::imread("src.jpg",0);
    cv::Mat image ;

    std::vector<std::vector<cv::Point>> contours;
    //std::vector<std::vector<cv::Point2f>> hiararchy;

        cv::threshold(img,img,127,255,CV_THRESH_BINARY_INV);
        cv::findContours(img,contours,/*hiararchy,*/CV_RETR_EXTERNAL,CV_RETR_CCOMP );
        std::vector<cv::Point> approx;   // this should be  1D
    for ( int i=0; i<contours.size();i++){

        cv::approxPolyDP(cv::Mat(contours[i]),approx,(cv::arcLength(cv::Mat(contours[i]),true)*0.02),true);

    }

    cv::waitKey(0);

再次感谢您的帮助

关于c++ - 这个opencv代码有什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17716255/

相关文章:

c++ - 使用 NetBeans [C++] 制作安装程序

c# - 我是否需要在 C# 中使用来自非托管 C++ dll 的原始数据类型执行 MarshalAs?

python - 使用 Python 和 Numpy 创建原始图像的图像 block (m*n)

c++ - 错误 : QueryFrame not a member of cv

c++ - OpenCV:RNG 生成相同的数字

c++ - 在 Direct3D 11 中绘制具有不同数量基元的对象

c++ - 从插槽访问中央小部件时出现段错误

c# - 在 OOXML 中使用大二进制段的问题

python - 使用 OpenCV Python 读取视频

opencv - 在 OpenCV 中识别手绘形状