2011-05-10 27 views
5

Bradski說:「當你想刪除一個序列時,你可以使用cvClearSeq(),這是一個清除序列中所有元素的例程。」如何刪除OpenCV中的cvseq?

但是,此功能不會將內存存儲區中分配的塊返回到存儲區或系統。

他說:「如果您想要爲其他目的檢索該內存,則必須通過cvClearMemStore()清除內存存儲」。

這個功能似乎並不存在:

error C3861: 'cvClearMemStore': identifier not found 

在勘誤的書,它指出:「‘cvClearMemStore’應該是‘cvClearMemStorage’」不過這個功能需要一個指向CvMemStorage,不CvSeq。

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

任何想法?

回答

8

我相信,他的意思cvClearMemStorage()

清除存儲器。這是唯一的方法(!!!)(除了cvRestoreMemStoragePos) 重用爲存儲分配的內存 - cvClearSeq,cvClearSet ... 不釋放任何內存。 子存儲在清除時將所有塊返回給父級。

文本從頭複製:核心/ core_c.h

你可以從錯誤中告訴,要傳遞了錯誤的數據類型此功能。 Check the docs這些功能知道如何調用它們。

我會嘗試用下面的代碼來說明:

// 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的。我發現this one相當有趣。有一個鏈接到帖子底部的源代碼。

2

它看起來像CVMemStorage結構被用作基本的底層結構來創建存儲空間的空間。

它說約CvMemStorage:

存儲器存儲是用於存儲動態地 不斷增長的數據結構的低電平 結構如 序列,等高線,圖, 細分等

你的struct 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對象存儲。

它在this頁面上得到了很好的解釋。

+0

謝謝。我也這麼想。但是,當我調用cvClearMemStorage時出現以下問題:http://stackoverflow.com/questions/5930985/cvcreatememstorage-not-working-in-second-object-instance – kylestephens 2011-05-10 14:36:52

+0

+1:感謝您的解釋!所以,CvSeq只是訪問mem stoware中正確元素的一種方式。 – 2013-05-30 20:30:58