OpenCV绘图轮廓不起作用

OpenCV drawContours not working

本文关键字:不起作用 轮廓 绘图 OpenCV      更新时间:2023-10-16

我正在尝试使用C++绘制图像的轮廓。它给出错误:

cv::cvarrTOMat中的参数错误(未知数组类型)。

所以我换了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;
}

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

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

此外,findContours期望的是二进制图像,而不是灰度图像。你可以添加类似的东西:

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

然后调用CCD_ 3对CCD_。

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

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 );