OpenCVが新しく、使用中に問題が発生しました。cvFindContoursでCvSeqを使用中にメモリリークが発生する
現在、私はバイナリパーティションツリー(BPT)アルゴリズムで作業しています。基本的には、画像を多くの領域に分割し、いくつかのパラメータに基づいて分割する必要があります。 2つの領域がマージされ、これらの2つの領域からなる1つの新しい領域を形成する。
私は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も解放されました。これを避けるための提案はありますか? –
新しい問題の応答を編集しました。それが役に立てば幸い。 – Adrian