对于cvStartReadSeq函数官方文档说明如下:
Initializes the process of sequential reading from a sequence.
-
C: void cvStartReadSeq(const
CvSeq* seq, CvSeqReader* reader, int reverse=0 ) -
Parameters: - seq – Sequence
- reader – Reader state; initialized by the function
- reverse – Determines the direction of the sequence traversal. If reverse is
0, the reader is positioned at the first sequence element; otherwise it is positioned at the last element.
The function initializes the reader state. After that, all the sequence elements from the first one down to the last one can be read by subsequent calls of the macro CV_READ_SEQ_ELEM( read_elem, reader ) in
the case of forward reading and by using CV_REV_READ_SEQ_ELEM(read_elem, reader ) in
the case of reverse reading. Both macros put the sequence element to read_elem and move the reading pointer toward the next element. A circular structure
of sequence blocks is used for the reading process, that is, after the last element has been read by the macro CV_READ_SEQ_ELEM , the first element is read
when the macro is called again. The same applies to CV_REV_READ_SEQ_ELEM . There is no function to finish the reading process, since it neither changes the
sequence nor creates any temporary buffers. The reader field ptrpoints to the current element of the sequence that is to be read next. The code below demonstrates
how to use the sequence writer and reader.
相关说明:
- 序列
- reader
- 读取部分的状态; 由该函数初始化
- reverse
- 决定遍历序列的方向。如果 reverse 为0,则读取顺序被定位从序列头部元素开始,否则从尾部开始读取
seq
函数 cvStartReadSeq 初始化读取部分的状态。毕竟,顺序读取可通过调用宏 CV_READ_SEQ_ELEM( read_elem, reader ),逆序读取可通过调用宏CV_REV_READ_SEQ_ELEM( read_elem, reader )。这两个宏都将序列元素读进read_elem中, 并将指针移到下一个元素。下面代码显示了如何去使用reader 和 writer.
// 轮廓描绘.cpp : 定义控制台应用程序的入口点。 // /*================================================================== 名称:轮廓 时间:2013.07.27 说明:把加载图像进行二值化,寻找轮廓,再描绘轮廓 ===================================================================*/ #include "stdafx.h" #include "cv.h" #include "highgui.h" #include "cxcore.h" using namespace std; int _tmain(int argc, _TCHAR* argv[]) { int i = 0; int mode = CV_RETR_EXTERNAL; //获取轮廓的模式 int contours_num = 0; //图像中获取轮廓的数目 CvMemStorage *storage = cvCreateMemStorage(0); //创建并声明一个内存,提取轮廓时用的 //-------------------------图像二值化-----------------------------// IplImage *src; //声明一个图像指针 src = cvLoadImage("37.png", 0); //用来加载图像 cvThreshold(src, src, 128, 255, CV_THRESH_BINARY); //把加载图像二值化 cvNamedWindow("二值化图像"); //创建显示图像窗口 cvMoveWindow("二值化图像", 60, 60); //设置显示窗口的位置 cvShowImage("二值化图像", src); //图像显示 cvWaitKey(1000); //-------------------------寻找轮廓-------------------------------// CvSeq *contour = 0; //声明一个序列指针,用来存储第一个外接轮廓 contours_num = cvFindContours(src, storage, &contour, sizeof(CvContour), CV_RETR_TREE, CV_CHAIN_APPROX_NONE); //寻找轮廓函数 printf("输出轮廓数目:%d\n", contours_num);//输出轮廓数目 //-------------------------绘制轮廓------------------------------// CvSeqReader reader; //读序列 int count = 0; if(contour!=0) { count = contour->total; //获取轮廓点数 cout<<"count="<<count<<endl; //输出点数 } cvStartReadSeq(contour, &reader, 0);//初始化序列中的读取过程 CvPoint pt1; //声明一个二维坐标点 IplImage* img = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 3); //声明一个大小与输入图像一样,无符号3信道图像指针 cvNamedWindow("绘制图像", 1); cvShowImage("绘制图像", img); for(i = 0; i< count; i++) { CV_READ_SEQ_ELEM(pt1, reader); //顺序把点读入pt1中 cvCircle(img, pt1, 4, CV_RGB(255, 0, 0)); //绘制圆点来构成轮廓 cvShowImage("绘制图像", img); cvWaitKey(5); } cvWaitKey(0); //销毁窗口与释放所有内存 cvDestroyAllWindows(); cvReleaseImage(&src); cvReleaseImage(&img); return 0; }英文参考website:英文参考网址