我是OpenCV的新手,我在使用它时遇到了一些问题。在cvFindContours中使用CvSeq时发生内存泄漏
目前我正在研究二进制分区树(BPT)算法。基本上我需要将图像分成许多区域,并基于某些参数。 2个地区将合并并形成1个新地区,由这2个地区组成。
我设法通过使用cvWatershed得到初始区域。我还创建了一个矢量来存储这些区域,每个区域位于1个矢量块中。但是,当我尝试将轮廓信息移入矢量时,出现内存泄漏。它说,内存泄漏。
for (int h = 0; h <compCount; h++) // compCount - Amount of regions found through cvWaterShed
{
cvZero(WSRegion); // clears out an image, used for painting
Region.push_back(EmptyNode); // create an empty vector slot
CvScalar RegionColor = colorTab[h]; // the color of the region in watershed
for (int i = 0; i <WSOut->height; i++)
{
for (int j = 0; j <WSOut->width; j++)
{
CvScalar s = cvGet2D(WSOut, i, j); // get pixel color in watershed image
if (s.val[0] == RegionColor.val[0] && s.val[1] == RegionColor.val[1] && s.val[2] == RegionColor.val[2])
{
cvSet2D(WSRegion, i, j, cvScalarAll(255)); // paint the pixel to white if it has the same color with the region[h]
}
}
}
MemStorage = cvCreateMemStorage(); // create memory storage
cvFindContours(WSRegion, MemStorage, &contours, sizeof(CvContour), CV_RETR_LIST);
Region[h].RegionContour = cvCloneSeq(contours); // clone and store in vector Region[h]
Region[h].RegionContour->h_next = NULL;
}
难道我能解决这个问题吗?或者有什么替代方案,我不需要为每个区域矢量创建新的内存存储空间?预先感谢您
我将创建内存存储语句移至for循环之外。 它可以工作,但是当我释放内存时,最后一个矢量块的RegionContour也被释放了。任何建议,以避免这一点? – 2011-12-26 05:07:00
针对您的新问题编辑回复。希望能帮助到你。 – Adrian 2012-01-03 07:40:33