Drawing contours retrieved from the binary image

最后都变了- 提交于 2019-12-11 11:39:39

问题


I want use findContours with the binary image, but the callback function causes an error:

Invalid address specified to RtlFreeHeap

when returning.

When i want to use clear() to free the vector<vector<Point> > value, it causes the same exception and the code crashed in free.c at the line:

if (retval == 0) errno = _get_errno_from_oserr(GetLastError());

For example:

void onChangeContourMode(int, void *)
{
    Mat m_frB = imread("3.jpg", 0);
    vector<vector<Point>> contours
    vector<Vec4i> hierarchy;
    findContours(m_frB, contours, hierarchy, g_contour_mode, CV_CHAIN_APPROX_SIMPLE);
    for( int idx = 0 ; idx >= 0; idx = hierarchy[idx][0] )
    drawContours( m_frB, contours, idx, Scalar(255,255,255), 
    CV_FILLED, 8, hierarchy );
    imshow( "Contours", m_frB );
}

Can anyone help me? Thank you very much!


回答1:


Mat m_frB = imread("3.jpg", CV_LOAD_IMAGE_GRAYSCALE);

loads 3.jpg as a 8bpp grayscale image, so it's not binary image. It is specific for findContours function that "non-zero pixels are treated as 1’s. Zero pixels remain 0’s, so the image is treated as binary". Also note that this "function modifies the image while extracting the contours".

The actual problem here is that although the destination image is 8bpp, you should make sure that it has 3 channels by using CV_8UC3 before you draw RGB contours into it. Try this:

// find contours:
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(m_frB, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);

// draw contours:
Mat imgWithContours = Mat::zeros(m_frB.rows, m_frB.cols, CV_8UC3);
RNG rng(12345);
for (int i = 0; i < contours.size(); i++)
{
    Scalar color = Scalar(rng.uniform(50, 255), rng.uniform(50,255), rng.uniform(50,255));
    drawContours(imgWithContours, contours, i, color, 1, 8, hierarchy, 0);
}
imshow("Contours", imgWithContours);


来源:https://stackoverflow.com/questions/15406609/drawing-contours-retrieved-from-the-binary-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!