转自: http://wiki.opencv.org.cn/index.php/OpenCV_%E7%BC%96%E7%A8%8B%E7%AE%80%E4%BB%8B%EF%BC%88%E7%9F%A9%E9%98%B5/%E5%9B%BE%E5%83%8F/%E8%A7%86%E9%A2%91%E7%9A%84%E5%9F%BA%E6%9C%AC%E8%AF%BB%E5%86%99%E6%93%8D%E4%BD%9C%EF%BC%89
目录[隐藏]
|
by Gary R. Bradski, Vadim Pisarevsky, and Jean-Yves Bouguet, Springer, 1st ed. (June, 2006). chenyusiyuan: 补充以下书籍
by Gary Bradski & Adrian Kaehler, O'Reilly Media, 1 st ed. (September, 2008).
作者:刘瑞祯 于仕琪,北京航空航天大学出版社,出版日期:200706
cvActionTargetMod(...)
Action = 核心功能(core functionality) (e.g. set, create) Target = 目标图像区域(target image area) (e.g. contour, polygon) Mod = (可选的)调整语(optional modifiers) (e.g. argument type)
CV_(S|U|F)C
S = 符号整型 U = 无符号整型 F = 浮点型
E.g.: CV_8UC1 是指一个8位无符号整型单通道矩阵, CV_32FC2是指一个32位浮点型双通道矩阵.
IPL_DEPTH_(S|U|F)
E.g.: IPL_DEPTH_8U 图像像素数据是8位无符号整型. IPL_DEPTH_32F图像像素数据是32位浮点型.
#include#include #include #include #include // 一般不需要,cv.h 内已包含该头文件
g++ hello-world.cpp -o hello-world \ -I /usr/local/include/opencv -L /usr/local/lib \ -lm -lcv -lhighgui -lcvaux
其中
/usr/local/include/opencv 为opencv头文件目录. /usr/local/lib为库文件目录 -lcv -lhighgui -lcvaux为链接库文件参数.
不同linxu发行版库文件路径可能会有所不同,x86_64版本与i386版本路径也可能有所区别
例如fedora 17 x86_64下该命令为:
g++ hello-world.cpp -o hello-world \ -I /usr/include/opencv \ -L /usr/lib64 \ -lm -lopencv_core -lopencv_highgui
在Visual Studio的‘选项’和‘项目’中设置好OpenCV相关文件的路径。
//
// hello-world.cpp
//
// 该程序从文件中读入一幅图像,将之反色,然后显示出来.
//
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
IplImage* img = 0;
int height,width,step,channels;
uchar *data;
int i,j,k;
if(argc<2){
printf("Usage: main \n\7" );
exit(0);
}
// load an image
img=cvLoadImage(argv[1]);
if(!img){
printf("Could not load image file: %s\n",argv[1]);
exit(0);
}
// get the image data
height = img->height;
width = img->width;
step = img->widthStep;
channels = img->nChannels;
data = (uchar *)img->imageData;
printf("Processing a %dx%d image with %d channels\n",height,width,channels);
// create a window
cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);
cvMoveWindow("mainWin", 100, 100);
// invert the image
// 相当于 cvNot(img);
// IplImage *pDstImg = cvCreateImage(cvGetSize(img),img->depth,img->nChannels);
// cvNot(img, pDstImg);
for(i=0;i<height;i++) for(j=0;j<width;j++) for(k=0;k<channels;k++)
data[i*step+j*channels+k]=255-data[i*step+j*channels+k];
// show the image
cvShowImage("mainWin", img );
// wait for a key
cvWaitKey(0);
// release the image
cvReleaseImage(&img );
return 0;
}
cvNamedWindow("win1", CV_WINDOW_AUTOSIZE); cvMoveWindow("win1", 100, 100); // offset from the UL corner of the screen
IplImage* img=0; img=cvLoadImage(fileName, CV_LOAD_IMAGE_COLOR); if(!img) printf("Could not load image file: %s\n",fileName);
cvShowImage("win1",img);
该函数可以在上面建立的窗口(win1)中显示彩色或灰度的字节型/浮点型图像。字节型图像像素值范围为[0-255];浮点型图像像素值范围为[0-1]。彩色图像的三色元素按BGR(蓝-绿-红)顺序存储。
cvDestroyWindow("win1");
cvResizeWindow("win1",100,100); // new width/heigh in pixels
void mouseHandler(int event, int x, int y, int flags, void* param) { switch(event){ case CV_EVENT_LBUTTONDOWN: if(flags & CV_EVENT_FLAG_CTRLKEY) printf("Left button down with CTRL pressed\n"); break; case CV_EVENT_LBUTTONUP: printf("Left button up\n"); break; } }
x,y: 相对于左上角的像素坐标
event: CV_EVENT_LBUTTONDOWN, CV_EVENT_RBUTTONDOWN, CV_EVENT_MBUTTONDOWN, CV_EVENT_LBUTTONUP, CV_EVENT_RBUTTONUP, CV_EVENT_MBUTTONUP, CV_EVENT_LBUTTONDBLCLK, CV_EVENT_RBUTTONDBLCLK, CV_EVENT_MBUTTONDBLCLK, CV_EVENT_MOUSEMOVE:
flags: CV_EVENT_FLAG_CTRLKEY, CV_EVENT_FLAG_SHIFTKEY, CV_EVENT_FLAG_ALTKEY, CV_EVENT_FLAG_LBUTTON, CV_EVENT_FLAG_RBUTTON, CV_EVENT_FLAG_MBUTTON
mouseParam=5; cvSetMouseCallback("win1",mouseHandler,&mouseParam); //第三个参数可以设置为NULL
int key; key=cvWaitKey(10); // wait 10ms for input
int key; key=cvWaitKey(0); // wait indefinitely for input
while(1){ key=cvWaitKey(10); if(key==27) break; switch(key){ case 'h': ... break; case 'i': ... break; } }
void trackbarHandler(int pos) { printf("Trackbar position: %d\n",pos); }
int trackbarVal=25; int maxVal=100; cvCreateTrackbar("bar1", "win1", &trackbarVal ,maxVal , trackbarHandler);
int pos = cvGetTrackbarPos("bar1","win1");
cvSetTrackbarPos("bar1", "win1", 25);
(译注:OpenCV 1.1、1.2或2.0版本中各数据结构的结构体元素有所调整,以下仅作参考)
IplImage |-- int nChannels; // 颜色通道数目 (1,2,3,4) |-- int depth; // 像素的位深: | // IPL_DEPTH_8U, IPL_DEPTH_8S, | // IPL_DEPTH_16U,IPL_DEPTH_16S, | // IPL_DEPTH_32S,IPL_DEPTH_32F, | // IPL_DEPTH_64F |-- int width; // 图像宽度(像素为单位) |-- int height; // 图像高度 |-- char* imageData; // 图像数据指针 | // 注意彩色图像按BGR顺序存储数据 |-- int dataOrder; // 0 - 将像素点不同通道的值交错排在一起,形成单一像素平面 | // 1 - 把所有像素同通道值排在一起,形成若干个通道平面,再把平面排列起来 | // cvCreateImage 只能创建像素交错排列式的图像 |-- int origin; // 0 – 像素原点为左上角, | // 1 – 像素原点为左下角 (Windows bitmaps style) |-- int widthStep; // 相邻行的同列点之间的字节数 width * nChannels * depth / 8 |-- int imageSize; // 图像的大小(字节为单位) = height*widthStep |-- struct _IplROI *roi;// 图像的感兴趣区域(ROI). ROI非空时对图像的 | // 处理仅限于ROI区域. |-- char *imageDataOrigin; // 图像数据未对齐时的数据原点指针 | // (需要正确地重新分配图像内存 ) | // (needed for correct image deallocation) |-- int align; // 图像数据的行对齐: 4 or 8 byte alignment | // OpenCV 中无此项,采用widthStep代替 |-- char colorModel[4]; // 颜色模型 – OpenCV中忽略此项
CvMat // 2D 矩阵 |-- int type; // 元素类型 (uchar,short,int,float,double) 与标志 |-- int step; // 整行长度字节数 |-- int rows, cols; // 行、列数 |-- int height, width; // 矩阵高度、宽度,与rows、cols对应 |-- union data; |-- uchar* ptr; // data pointer for an unsigned char matrix |-- short* s; // data pointer for a short matrix |-- int* i; // data pointer for an integer matrix |-- float* fl; // data pointer for a float matrix |-- double* db; // data pointer for a double matrix
CvMatND // N-维矩阵 |-- int type; // 元素类型 (uchar,short,int,float,double) 与标志 |-- int dims; // 矩阵维数 |-- union data; | |-- uchar* ptr; // data pointer for an unsigned char 矩阵 | |-- short* s; // data pointer for a short matrix | |-- int* i; // data pointer for an integer matrix | |-- float* fl; // data pointer for a float matrix | |-- double* db; // data pointer for a double matrix | |-- struct dim[]; // 各维信息 |-- size; // 元素数目 |-- step; // 元素间距(字节为单位)
CvSparseMat // N-维稀疏矩阵
CvArr* // 仅作为函数定义的参数使用, // 表明函数可以接受不同类型的矩阵作为参数, // 例如:IplImage*, CvMat* 甚至是 CvSeq*. // 矩阵的类型通过矩阵头的前4个字节信息来确定
CvScalar |-- double val[4]; //4D 向量
初始化函数:
CvScalar s = cvScalar(double val0, double val1=0, double val2=0, double val3=0); // Example: CvScalar s = cvScalar(20.0); s.val[0]=20.0;
注意该初始化函数的函数名与对应的结构体名称几乎同名,差别仅在于函数名第一个字母是小写的,而结构体名第一个字母是大写的。它并不是一个 C++ 构造函数。(译注:类似的还有 cvMat 与 CvMat、cvPoint 与 CvPoint 等等)
CvPoint p = cvPoint(int x, int y); CvPoint2D32f p = cvPoint2D32f(float x, float y); CvPoint3D32f p = cvPoint3D32f(float x, float y, float z); //E.g.: p.x=5.0; p.y=5.0;
CvSize r = cvSize(int width, int height); CvSize2D32f r = cvSize2D32f(float width, float height);
CvRect r = cvRect(int x, int y, int width, int height);
IplImage* cvCreateImage(CvSize size, int depth, int channels);
IPL_DEPTH_16S, IPL_DEPTH_32S, IPL_DEPTH_32F, IPL_DEPTH_64F
示例:
// Allocate a 1-channel byte image IplImage* img1=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1); // Allocate a 3-channel float image IplImage* img2=cvCreateImage(cvSize(640,480),IPL_DEPTH_32F,3);
IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1); cvReleaseImage(&img);
IplImage* img1=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1); IplImage* img2; img2=cvCloneImage(img1); // 注意通过cvCloneImage得到的图像 // 也要用 cvReleaseImage 释放,否则容易产生内存泄漏
void cvSetImageROI(IplImage* image, CvRect rect); void cvResetImageROI(IplImage* image); CvRect cvGetImageROI(const IplImage* image);
大多数OpenCV函数都支持 ROI, region of interests,设置了ROI之后,函数对图片的处理只会应用到ROI区域上.
void cvSetImageCOI(IplImage* image, int coi); // 0=all int cvGetImageCOI(const IplImage* image);
大多数OpenCV函数不支持 COI.
IplImage* img=0; img=cvLoadImage(fileName); if(!img) printf("Could not load image file: %s\n",fileName);
支持的图像格式: BMP, DIB, JPEG, JPG, JPE, PNG, PBM, PGM, PPM, SR, RAS, TIFF, TIF
OpenCV默认将读入的图像强制转换为一幅三通道彩色图像. 不过可以按以下方法修改读入方式:
img=cvLoadImage(fileName,flag);
flag: >0 将读入的图像强制转换为一幅三通道彩色图像 =0 将读入的图像强制转换为一幅单通道灰度图像 <0 读入的图像通道数与所读入的文件相同.
if(!cvSaveImage(outFileName,img)) printf("Could not save: %s\n", outFileName);
保存的图像格式由 outFileName 中的扩展名确定.
IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1); CvScalar s; s=cvGet2D(img,i,j); // get the (j,i) pixel value, 注意cvGet2D与cvSet2D中坐标参数的顺序与其它opencv函数坐标参数顺序恰好相反.本函数中i代表y轴,即height;j代表x轴,即width. printf("intensity=%f\n",s.val[0]); s.val[0]=111; cvSet2D(img,i,j,s); // set the (j,i) pixel value
IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_32F,3); CvScalar s; s=cvGet2D(img,i,j); // get the (j,i) pixel value printf("B=%f, G=%f, R=%f\n",s.val[0],s.val[1],s.val[2]); s.val[0]=111; s.val[1]=111; s.val[2]=111; cvSet2D(img,i,j,s); // set the (j,i) pixel value
IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1); ((uchar *)(img->imageData + i*img->widthStep))[j]=111;
IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3); ((uchar *)(img->imageData + i*img->widthStep))[j*img->nChannels + 0]=111; // B ((uchar *)(img->imageData + i*img->widthStep))[j*img->nChannels + 1]=112; // G ((uchar *)(img->imageData + i*img->widthStep))[j*img->nChannels + 2]=113; // R
IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_32F,3); ((float *)(img->imageData + i*img->widthStep))[j*img->nChannels + 0]=111; // B ((float *)(img->imageData + i*img->widthStep))[j*img->nChannels + 1]=112; // G ((float *)(img->imageData + i*img->widthStep))[j*img->nChannels + 2]=113; // R
IplImage* img = cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1); int height = img->height; int width = img->width; int step = img->widthStep; uchar* data = (uchar *)img->imageData; data[i*step+j] = 111;
IplImage* img = cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3); int height = img->height; int width = img->width; int step = img->widthStep; int channels = img->nChannels; uchar* data = (uchar *)img->imageData; data[i*step+j*channels+k] = 111;
IplImage* img = cvCreateImage(cvSize(640,480),IPL_DEPTH_32F,3); int height = img->height; int width = img->width; int step = img->widthStep; int channels = img->nChannels; float * data = (float *)img->imageData; data[i*step+j*channels+k] = 111;
template<class T> class Image { private: IplImage* imgp; public: Image(IplImage* img=0) {imgp=img;} ~Image(){imgp=0;} void operator=(IplImage* img) {imgp=img;} inline T* operator[](const int rowIndx) { return ((T *)(imgp->imageData + rowIndx*imgp->widthStep));} }; typedef struct{ unsigned char b,g,r; } RgbPixel; typedef struct{ float b,g,r; } RgbPixelFloat; typedef Image<RgbPixel> RgbImage; typedef Image<RgbPixelFloat> RgbImageFloat; typedef Image<unsigned char> BwImage; typedef Image<float> BwImageFloat;
IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1); BwImage imgA(img); imgA[i][j] = 111;
IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3); RgbImage imgA(img); imgA[i][j].b = 111; imgA[i][j].g = 111; imgA[i][j].r = 111;
IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_32F,3); RgbImageFloat imgA(img); imgA[i][j].b = 111; imgA[i][j].g = 111; imgA[i][j].r = 111;
cvConvertImage(src, dst, flags=0);
src = float/byte grayscale/color image dst = byte grayscale/color image flags = CV_CVTIMG_FLIP (垂直翻转图像) CV_CVTIMG_SWAP_RB (置换 R 和 B 通道)
// Using the OpenCV conversion: cvCvtColor(cimg,gimg,CV_BGR2GRAY); // cimg -> gimg // Using a direct conversion: for(i=0;i<cimg->height;i++) for(j=0;j<cimg->width;j++) gimgA[i][j]= (uchar)(cimgA[i][j].b*0.114 + cimgA[i][j].g*0.587 + cimgA[i][j].r*0.299);
cvCvtColor(src,dst,code); // src -> dst
code = CV_2 / = RGB, BGR, GRAY, HSV, YCrCb, XYZ, Lab, Luv, HLS
e.g.: CV_BGR2GRAY, CV_BGR2HSV, CV_BGR2Lab
// 在点 (100,100) 和 (200,200) 之间绘制一矩形,边线用红色、宽度为 1 cvRectangle(img, cvPoint(100,100), cvPoint(200,200), cvScalar(0,0,255), 1);
// 圆心为(100,100)、半径为20. 圆周绿色、宽度为1 cvCircle(img, cvPoint(100,100), 20, cvScalar(0,255,0), 1);
// 在 (100,100) 和 (200,200) 之间、线宽为 1 的绿色线段 cvLine(img, cvPoint(100,100), cvPoint(200,200), cvScalar(0,255,0), 1);
CvPoint curve1[]={10,10, 10,100, 100,100, 100,10}; CvPoint curve2[]={30,30, 30,130, 130,130, 130,30, 150,10}; CvPoint* curveArr[2]={curve1, curve2}; int nCurvePts[2]={4,5}; int nCurves=2; int isCurveClosed=1; int lineWidth=1; cvPolyLine(img,curveArr,nCurvePts,nCurves,isCurveClosed,cvScalar(0,255,255),lineWidth); void cvPolyLine( CvArr* img, CvPoint** pts, int* npts, int contours, int is_closed, CvScalar color, int thickness=1, int line_type=8, int shift=0 ); img 图像。 pts 折线的顶点指针数组。 npts 折线的定点个数数组。也可以认为是pts指针数组的大小 contours 折线的线段数量。 is_closed 指出多边形是否封闭。如果封闭,函数将起始点和结束点连线。 color 折线的颜色。 thickness 线条的粗细程度。 line_type 线段的类型。参见cvLine。 shift 顶点的小数点位数
cvFillPoly(img,curveArr,nCurvePts,nCurves,cvScalar(0,255,255)); cvFillPoly用于一个单独被多边形轮廓所限定的区域内进行填充。函数可以填充复杂的区域,例如,有漏洞的区域和有交叉点的区域等等。 void cvFillPoly( CvArr* img, CvPoint** pts, int* npts, int contours,CvScalar color, int line_type=8, int shift=0 ); img 图像。 pts 指向多边形的数组指针。 npts 多边形的顶点个数的数组。 contours 组成填充区域的线段的数量。 color 多边形的颜色。 line_type 组成多边形的线条的类型。 shift 顶点坐标的小数点位数。
CvFont font; double hScale=1.0; double vScale=1.0; int lineWidth=1; cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX|CV_FONT_ITALIC, hScale,vScale,0,lineWidth); cvPutText (img,"My comment",cvPoint(200,400), &font, cvScalar(255,255,0));
其它可用的字体类型有: CV_FONT_HERSHEY_SIMPLEX, CV_FONT_HERSHEY_PLAIN, CV_FONT_HERSHEY_DUPLEX, CV_FONT_HERSHEY_COMPLEX, CV_FONT_HERSHEY_TRIPLEX, CV_FONT_HERSHEY_COMPLEX_SMALL, CV_FONT_HERSHEY_SCRIPT_SIMPLEX, CV_FONT_HERSHEY_SCRIPT_COMPLEX,
CvMat* cvCreateMat(int rows, int cols, int type);
type: 矩阵元素类型. 按CV_(S|U|F)C 方式指定. 例如: CV_8UC1 、CV_32SC2. 示例:
CvMat* M = cvCreateMat(4,4,CV_32FC1);
CvMat* M = cvCreateMat(4,4,CV_32FC1); cvReleaseMat(&M);
CvMat* M1 = cvCreateMat(4,4,CV_32FC1); CvMat* M2; M2=cvCloneMat(M1);
double a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; CvMat Ma=cvMat(3, 4, CV_64FC1, a); //等价于: CvMat Ma; cvInitMatHeader(&Ma, 3, 4, CV_64FC1, a);
CvMat* M = cvCreateMat(4,4,CV_32FC1); cvSetIdentity(M); // does not seem to be working properly
cvmSet(M,i,j,2.0); // Set M(i,j) t = cvmGet(M,i,j); // Get M(i,j)
CvMat* M = cvCreateMat(4,4,CV_32FC1); int n = M->cols; float *data = M->data.fl; data[i*n+j] = 3.0;
CvMat* M = cvCreateMat(4,4,CV_32FC1); int step = M->step/sizeof(float); float *data = M->data.fl; (data+i*step)[j] = 3.0;
double a[16]; CvMat Ma = cvMat(3, 4, CV_64FC1, a); a[i*4+j] = 2.0; // Ma(i,j)=2.0;
CvMat *Ma, *Mb, *Mc; cvAdd(Ma, Mb, Mc); // Ma+Mb -> Mc cvSub(Ma, Mb, Mc); // Ma-Mb -> Mc cvMatMul(Ma, Mb, Mc); // Ma*Mb -> Mc
CvMat *Ma, *Mb, *Mc; cvMul(Ma, Mb, Mc); // Ma.*Mb -> Mc cvDiv(Ma, Mb, Mc); // Ma./Mb -> Mc cvAddS(Ma, cvScalar(-10.0), Mc); // Ma.-10 -> Mc
double va[] = {1, 2, 3}; double vb[] = {0, 0, 1}; double vc[3]; CvMat Va=cvMat(3, 1, CV_64FC1, va); CvMat Vb=cvMat(3, 1, CV_64FC1, vb); CvMat Vc=cvMat(3, 1, CV_64FC1, vc); double res=cvDotProduct(&Va,&Vb); // 向量点乘: Va . Vb -> res cvCrossProduct(&Va, &Vb, &Vc); // 向量叉乘: Va x Vb -> Vc
注意在进行叉乘运算时,Va, Vb, Vc 必须是仅有3个元素的向量.
CvMat *Ma, *Mb; cvTranspose(Ma, Mb); // 转置:transpose(Ma) -> Mb (注意转置阵不能返回给Ma本身) CvScalar t = cvTrace(Ma); // 迹:trace(Ma) -> t.val[0] double d = cvDet(Ma); // 行列式:det(Ma) -> d cvInvert(Ma, Mb); // 逆矩阵:inv(Ma) -> Mb
CvMat* A = cvCreateMat(3,3,CV_32FC1); CvMat* x = cvCreateMat(3,1,CV_32FC1); CvMat* b = cvCreateMat(3,1,CV_32FC1); cvSolve(&A, &b, &x); // solve (Ax=b) for x
CvMat* A = cvCreateMat(3,3,CV_32FC1); CvMat* E = cvCreateMat(3,3,CV_32FC1); CvMat* l = cvCreateMat(3,1,CV_32FC1); cvEigenVV(A, E, l); // l = A 的特征值(递减顺序) // E = 对应的特征向量 (行向量)
(7) 奇异值分解(SVD):====
CvMat* A = cvCreateMat(3,3,CV_32FC1); CvMat* U = cvCreateMat(3,3,CV_32FC1); CvMat* D = cvCreateMat(3,3,CV_32FC1); CvMat* V = cvCreateMat(3,3,CV_32FC1); cvSVD(A, D, U, V, CV_SVD_U_T|CV_SVD_V_T); // A = U D V^T
标志位使矩阵U或V按转置形式返回 (若不转置可能运算出错).
CvCapture* capture = cvCaptureFromCAM(0); // capture from video device #0
CvCapture* capture = cvCaptureFromAVI("infile.avi");
IplImage* img = 0; if(!cvGrabFrame(capture)){ // capture a frame printf("Could not grab a frame\n\7"); exit(0); } img=cvRetrieveFrame(capture); // retrieve the captured frame
若要从多个摄像头中同步捕捉画面,则须首先从每个摄像头中抓取一帧,紧接着要将被捕捉的帧画面恢复到一个IplImage*型图像中。(译注:这一过程其实可以用 cvQueryFrame() 函数一步完成)
cvReleaseCapture(&capture);
注意由视频流捕捉器得到的图像是由捕捉器分配和释放内存的,不需要单独对图像进行释放内存的操作。
cvQueryFrame(capture); // 在读取视频流信息前,要先执行此操作 int frameH = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT); int frameW = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH); int fps = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FPS); int numFrames = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_COUNT);
统计总帧数仅对视频文件有效,但似乎不太准确(译注:也许OpenCV2.0中此问题已解决)
float posMsec = cvGetCaptureProperty(capture, CV_CAP_PROP_POS_MSEC); int posFrames = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_POS_FRAMES); float posRatio = cvGetCaptureProperty(capture, CV_CAP_PROP_POS_AVI_RATIO);
所抓取的帧的位置有三种表达方式:距离第一帧画面的时间间隔(毫秒为单位), 或者距离第一帧画面(序列号为0)的序列数. 第三种方式是按相对比率,第一帧的相对比率为0,最后一帧的相对比率为1. 此方式仅对读取视频文件时有效.
// start capturing from a relative position of 0.9 of a video file cvSetCaptureProperty(capture, CV_CAP_PROP_POS_AVI_RATIO, (double)0.9);
注意此方法定位并不准确。
CvVideoWriter *writer = 0; int isColor = 1; int fps = 25; // or 30 int frameW = 640; // 744 for firewire cameras int frameH = 480; // 480 for firewire cameras writer=cvCreateVideoWriter("out.avi",CV_FOURCC('P','I','M','1'), fps,cvSize(frameW,frameH),isColor);
其它的编码器代号包括: CV_FOURCC('P','I','M','1') = MPEG-1 codec CV_FOURCC('M','J','P','G') = motion-jpeg codec (does not work well) CV_FOURCC('M', 'P', '4', '2') = MPEG-4.2 codec CV_FOURCC('D', 'I', 'V', '3') = MPEG-4.3 codec CV_FOURCC('D', 'I', 'V', 'X') = MPEG-4 codec CV_FOURCC('U', '2', '6', '3') = H263 codec CV_FOURCC('I', '2', '6', '3') = H263I codec CV_FOURCC('F', 'L', 'V', '1') = FLV1 codec 若编码器代号为 -1,则运行时会弹出一个编码器选择框.
IplImage* img = 0; int nFrames = 50; for(i=0;i<nFrames;i++){ cvGrabFrame(capture); // capture a frame img=cvRetrieveFrame(capture); // retrieve the captured frame // img = cvQueryFrame(capture); cvWriteFrame(writer,img); // add the frame to the file }
要查看所抓取到的帧画面,可以在循环中加入以下语句:
cvShowImage("mainWin", img); key=cvWaitKey(20); // wait 20 ms
注意 cvWaitKey 参数应该不小于 20 ms,否则画面的显示可能出错.
cvReleaseVideoWriter(&writer);
By Gady Agam 2006-03-31 翻译:chenyusiyuan 2010-1-26