首页 文章

OpenCV drawContours无法正常工作

提问于
浏览
0

我试图用C绘制图像的轮廓 . 它给出了错误:

cv :: cvarrTOMat中的Bad Arguement(未知数组类型) .

所以我切换到python,它在那里工作正常 . 请帮我弄清楚代码有什么问题 .

int main()
{
    Mat src, dst,dst2,dst3;
    src = imread("imagedata\\result0.jpg", 1);
    resize(src, dst, Size(), 0.09, 0.09, INTER_LINEAR);
    string s = "cropped.jpg";
    imwrite(s , dst);
    cvtColor(dst, dst2, CV_RGB2GRAY);
    imwrite("cropped2.jpg", dst2);
    vector< vector<Point> > contours;
    vector<Vec4i> hierarchy;
    findContours(dst2, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
    drawContours(dst3, contours, -1 , (128,255,0), 3);//, hierarchy);
    imwrite("cropped3.jpg", dst3);
    return 0;
}

2 回答

  • -1

    您必须将输入图像初始化为 drawContours

    dst3 = dst.clone(); // initialize dst3
    drawContours(dst3, contours, -1, (128, 255, 0), 3);
    

    此外, findContours 需要二进制图像,而不是灰度图像 . 您可以添加类似于的内容:

    threshold(dst2, dst2, 127, 255, THRESH_BINARY);
    

    在调用 findContours 以二进制化 dst2 之前 .

  • 1

    这不是绘制轮廓的方法 . 请按照这段代码运行 .

    threshold(dst2, dst2, 100, 255, THRESH_BINARY);
    findcontours()
      for( int i = 0; i< contours.size(); i++ )
         {
           Scalar color = Scalar( 255,255,255);
           drawContours( dst3, contours, i, color, 2, 8, hierarchy, 0, Point() );
         }
    
     namedWindow( "Contours", CV_WINDOW_AUTOSIZE );
     imshow( "Contours", drawing );
    

相关问题