c++ opencv 动态内存
1.CvMemStorage定义动态内存存储器
内存存储器是一个用来存储诸如序列、轮廓、图形和子划分等动态增长数据结构的底层结构
2.示例
CvMemStorage *mems = cvCreateMemStorage(100);
CvMemStoragePos mempos;
cvSaveMemStoragePos(mems, &mempos);//保存地址
//cvRestoreMemStoragePos(mems, &mempos);
printf("%x %d\n", mempos.top, mempos.free_space);
CvMemStorage *mems1 = cvCreateChildMemStorage(mems);//创建子块
CvMemStoragePos mempos1;
cvSaveMemStoragePos(mems1, &mempos1);
printf("%x %d\n", mempos1.top, mempos1.free_space);
char strInfo[] = {"123123123"};
CvString str = cvMemStorageAllocString(mems, strInfo, strlen(strInfo));
printf("%s %d\n", str.ptr, str.len);
cvReleaseMemStorage((CvMemStorage**)&mems);
3.CvSeq是所有OpenCV动态数据结构的基础。序列有两种类型:稠密序列和稀疏序列。
(1).稠密序列都派生于CvSeq,用来代表可扩展的一维数组------向量、栈、队列和双端队列等,数据间不存在空隙,如果从序列中删除元素或将新元素插入到序列中,则此元素后的相关元素会被移动
(2).稀疏序列都派生于CvSet,是由节点组成的序列,每个节点或者被占用空间或者是空,由flags标志指定。
4.标准的序列元素类型:
#define CV_SEQ_ELTYPE_POINT CV_32SC2 /**< (x,y) */
#define CV_SEQ_ELTYPE_CODE CV_8UC1 /**< freeman code: 0..7 */
#define CV_SEQ_ELTYPE_GENERIC 0
#define CV_SEQ_ELTYPE_PTR CV_USRTYPE1
#define CV_SEQ_ELTYPE_PPOINT CV_SEQ_ELTYPE_PTR /**< &(x,y) */
#define CV_SEQ_ELTYPE_INDEX CV_32SC1 /**< #(x,y) */
#define CV_SEQ_ELTYPE_GRAPH_EDGE 0 /**< &next_o, &next_d, &vtx_o, &vtx_d */
#define CV_SEQ_ELTYPE_GRAPH_VERTEX 0 /**< first_edge, &(x,y) */
#define CV_SEQ_ELTYPE_TRIAN_ATR 0 /**< vertex of the binary tree */
#define CV_SEQ_ELTYPE_CONNECTED_COMP 0 /**< connected component */
#define CV_SEQ_ELTYPE_POINT3D CV_32FC3 /**< (x,y,z) */
5.序列操作示例
CvMemStorage * mems = cvCreateMemStorage(0);
//CvSeq *seq = cvCreateSeq(CV_32FC1, sizeof(CvSeq), sizeof(float), mems);
CvSeq *seq = cvCreateSeq(CV_64FC1, sizeof(CvSeq), sizeof(double), mems);
cvSetSeqBlockSize(seq, 500);
for (double i = 500; i >= 0; i -= 1){
cvSeqPush(seq, &i);
}
double fNum[1024 * 10] = {0};
cvCvtSeqToArray(seq, fNum, CV_WHOLE_SEQ);
for (int i = 0; i < seq->total; i++){
printf("%f ", fNum[i]);
}
printf("\n");
cvSeqPop(seq);
printf("%d\n", seq->total);
double dbData = 1230;
cvSeqPushFront(seq, &dbData);
printf("%d\n", seq->total);
cvSeqPopFront(seq);
printf("%d\n", seq->total);
double fArr[] = {1,2,3,4,5,6,7,8,9,10};
cvSeqPushMulti(seq, fArr, 18, 0);
printf("%d\n", seq->total);
cvSeqInsert(seq, 5, &dbData);
printf("%d\n", seq->total);
cvSeqRemove(seq, 5);
printf("%d\n", seq->total);
for (int i = 0; i < seq->total; i++){//打印index
signed char *pT = cvGetSeqElem(seq, i);
int p = cvSeqElemIdx(seq, pT);
printf("%d\n", p);
}
CvMemStorage *mem = cvCreateMemStorage(0);
CvSeq *seq3 = cvSeqSlice(seq, CvSlice(0, seq->total), mem, seq->total);
printf("seq->total = %d\n", seq->total);
printf("seq3->total = %d\n", seq3->total);
CvSeq *seq4 = cvCloneSeq(seq);//拷贝到另外一个序列
printf("seq4->total = %d\n", seq4->total);
cvSeqRemoveSlice(seq, CvSlice(0, 1));//从序列中删除数据
printf("%d\n", seq->total);
cvSeqInsertSlice(seq, seq->total, seq3);//在序列seq中插入一个数组
cvCvtSeqToArray(seq, fNum, CV_WHOLE_SEQ);
for (int i = 0; i < seq->total; i++){
printf("%f ", fNum[i]);
}
printf("\n");
cvSeqInvert(seq);//逆序操作
//cvSeqSort(seq, CmpFunc, NULL);
CvSeqWriter seqWriter;
cvStartAppendToSeq(seq, &seqWriter);
cvStartWriteSeq(CV_64FC1, sizeof(CvSeq), sizeof(double), mem, &seqWriter);
cvEndWriteSeq(&seqWriter);
cvClearSeq(seq);
cvReleaseMemStorage(&mems);
版权声明:本文为博主原创文章,未经博主允许不得转载。