0
votes

I am trying to draw contours of an image using C++. It's giving error:

Bad Arguement(Unknown array type) in cv::cvarrTOMat.

So i switched to python and it's working fine there. Please help me figure out what's wrong with the code.

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

2 Answers

1
votes

You must initialize the input image to drawContours:

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

Also, findContours expects a binary image, not a grayscale image. You can add something similar to:

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

before calling findContours to binarize dst2.

-1
votes

That isn't the way to draw the contours. Please follow the piece of code to run.

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