绘制从二值图像中检索到的轮廓

Drawing contours retrieved from the binary image

本文关键字:轮廓 检索 二值图像 绘制      更新时间:2023-10-16

我想使用findContours与二进制图像,但回调函数导致错误:

RtlFreeHeap指定的地址无效

当返回

当我想使用clear()来释放vector<vector<Point> >值时,它会导致相同的异常,代码在free.c中崩溃:

if (retval == 0) errno = _get_errno_from_oserr(GetLastError());
例如:

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

有人能帮我吗?非常感谢!

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

3.jpg加载为8bpp灰度图像,因此它不是二值图像。特定于findContours函数,"非零像素被视为1。零像素仍然是0,因此图像被视为二进制""。还要注意,这个"函数在提取轮廓时修改图像"

这里的实际问题是,虽然目标图像是8bpp,你应该确保它有3个通道使用CV_8UC3之前,你绘制RGB轮廓到它。试试这个:

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