gpt4 book ai didi

opencv - 在 cvFindContours 中使用 CvSeq 时内存泄漏

转载 作者:太空宇宙 更新时间:2023-11-03 21:38:31 25 4
gpt4 key购买 nike

我是 OpenCV 的新手,在使用它时遇到了一些问题。

目前我正在研究二叉分区树 (BPT) 算法。基本上我需要根据一些参数将图像分成许多区域。 2个区域将合并形成1个新区域,由这2个区域组成。

我设法通过使用 cvWatershed 获得初始区域。我还创建了一个向量来存储这些区域,每个区域都在 1 个向量 block 中。但是,当我尝试将轮廓信息移动到向量中时,出现内存泄漏。它说,内存泄漏。

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

有什么办法可以解决这个问题吗?或者是否有任何替代方案,我不需要为每个区域向量创建一个新的内存存储?提前谢谢你

最佳答案

你应该在循环之前只创建一次内存存储,cvFindContours 可以使用它,在循环之后你应该释放存储:

void cvReleaseMemStorage(CvMemStorage** storage)

您还可以在这里查看 CvMemStorage 规范: http://opencv.itseez.com/modules/core/doc/dynamic_structures.html?highlight=cvreleasememstorage#CvMemStorage

编辑:

你的下一个问题是 cvCloneSeq()。以下是它的一些规范:

CvSeq* cvCloneSeq(const CvSeq* seq, CvMemStorage* storage=NULL )
Parameters:

seq – Sequence
storage – The destination storage block to hold the new sequence header and the copied data, if any. If it is NULL, the function uses the storage block containing the input sequence.

如您所见,如果您不指定不同的内存存储,它会将序列克隆到与输入相同的内存块中。当您在循环后释放内存存储时,您也释放了最后一个轮廓,它是您插入列表的克隆。

关于opencv - 在 cvFindContours 中使用 CvSeq 时内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8598353/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com