我想使用 findContours
与二值图像,但回调函数导致错误:
Invalid address specified to 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,但在将 RGB 轮廓绘制到其中之前,您应该使用 CV_8UC3
确保它具有 3 个 channel 。试试这个:
// 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);
关于c++ - 绘制从二值图像中检索到的轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15406609/