如何在OpenCV中删除cvseq

How do you delete a cvseq in OpenCV?

本文关键字:删除 cvseq OpenCV      更新时间:2023-10-16

Bradski指出:"当你想要删除一个序列时,你可以使用cvClearSeq(),一个清除序列所有元素的例程。

但是,此函数不会将内存存储中分配的块返回到存储或系统。

他说:"如果你想出于其他目的检索该内存,你必须通过cvClearMemStore()清除内存存储"。

此函数似乎不存在:

error C3861: 'cvClearMemStore': identifier not found

在本书的勘误表中,它指出:"'cvClearMemStore'应该是'cvClearMemStorage'",但这个函数需要一个指向CvMemStorage的指针,而不是CvSeq。

error C2664: 'cvClearMemStorage' : cannot convert parameter 1 from 'CvSeq *' to 'CvMemStorage *' 

有什么想法吗?

我相信他的意思是cvClearMemStorage()

清除内存存储。这是唯一的方法(!!)(除了cvRestoreMemStoragePos) 重用为存储分配的内存 - cvClearSeq,cvClearSet ... 不要释放任何内存。 子存储会在清除时将所有块返回给父存储。

从标题复制的文本:core/core_c.h

从错误中可以看出,您将错误的数据类型传递给此函数。查看这些函数的文档以确切了解如何调用它们。

我将尝试使用以下代码进行说明:

// Create variable to hold the memory allocated for calculations
CvMemStorage* storage = 0;
// Allocate the memory storage. Param 0 will set the block size to a default value - currently it is about 64K.
storage = cvCreateMemStorage(0);
IplImage* gray = NULL; 
// <insert code to load a gray image here>
/* Retrieves contours from the binary image and returns the number of retrieved contours.
 * cvFindContours will scan through the image and store connected contours in "storage".
 * "contours" will point to the first contour detected.
 */
CvSeq* contours = 0;
cvFindContours(gray, storage, &contours );
// Clear the memory storage which was used before
cvClearMemStorage(storage);
// Release memory
cvReleaseMemStorage(&storage);
有一些

教程展示了cvSeq的使用。我发现这个很有趣。帖子底部有一个指向源代码的链接。

看起来CVMemStorage结构被用作基本的低级结构来创建存储事物的空间。

它说关于CvMemStorage:

内存存储是低级 用于动态存储的结构 不断增长的数据结构,例如 序列、轮廓、图形、 细分等

您的结构 cvSeq 包含一个指向CVMemStorage的指针,用于告诉它存储的位置。

#define CV_SEQUENCE_FIELDS() 
    int flags; /* micsellaneous flags */ 
    int header_size; /* size of sequence header */ 
    struct CvSeq* h_prev; /* previous sequence */ 
    struct CvSeq* h_next; /* next sequence */ 
    struct CvSeq* v_prev; /* 2nd previous sequence */ 
    struct CvSeq* v_next; /* 2nd next sequence */ 
    int total; /* total number of elements */ 
    int elem_size;/* size of sequence element in bytes */ 
    char* block_max;/* maximal bound of the last block */ 
    char* ptr; /* current write pointer */ 
    int delta_elems; /* how many elements allocated when the sequence grows
                  (sequence granularity) */ 
    CvMemStorage* storage; /* where the seq is stored */   //HERE!!! pointer to storage
    CvSeqBlock* free_blocks; /* free blocks list */ 
    CvSeqBlock* first; /* pointer to the first sequence block */
typedef struct CvSeq
{
    CV_SEQUENCE_FIELDS()
} CvSeq;

因此,您需要调用 CvClearMemStorage 并将指向 cvSeq 对象CvMemStorage的指针传递给它,这将清除CvSeq对象的存储。

此页面上的解释非常好。