美国伊力诺理工学院计算机科学系Gady Adam
翻译:Mensch
2006年11月22日
cvActionTarget[Mod](...) Action = 核心功能(例如 设定set, 创建create) Target = 操作目标 (例如 轮廓contour, 多边形polygon) [Mod] = 可选修饰词 (例如说明参数类型)
CV_<bit_depth>(S|U|F)C<number_of_channels> S = 带符号整数 U = 无符号整数 F = 浮点数 例: CV_8UC1 表示一个8位无符号单通道矩阵, CV_32FC2 表示一个32位浮点双通道矩阵.
IPL_DEPTH_<bit_depth>(S|U|F) 例: IPL_DEPTH_8U 表示一个8位无符号图像. IPL_DEPTH_32F 表示一个32位浮点数图像.
#include <cv.h> #include <cvaux.h> #include <highgui.h> #include <cxcore.h> // 不必要 - 该头文件已在 cv.h 文件中包含
g++ hello-world.cpp -o hello-world / -I /usr/local/include/opencv -L /usr/local/lib / -lm -lcv -lhighgui -lcvaux
注意在项目属性中设好OpenCV头文件以及库文件的路径.
//////////////////////////////////////////////////////////////////////// // // hello-world.cpp // // 一个简单的OpenCV程序 // 它从一个文件中读取图像,将色彩值颠倒,并显示结果. // //////////////////////////////////////////////////////////////////////// #include <stdlib.h> #include <stdio.h> #include <math.h> #include <cv.h> #include <highgui.h> 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 <image-file-name>/n/7"); exit(0); } // 载入图像 img=cvLoadImage(argv[1]); if(!img){ printf("Could not load image file: %s/n",argv[1]); exit(0); } // 获取图像数据 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); // 创建窗口 cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE); cvMoveWindow("mainWin", 100, 100); // 反色图像 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]; // 显示图像 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); // 以屏幕左上角为起点的偏移量
IplImage* img=0; img=cvLoadImage(fileName); if(!img) printf("Could not load image file: %s/n",fileName);
cvShowImage("win1",img);
可显示彩色或灰度的字节/浮点图像。 彩色图像数据认定为BGR顺序.
cvDestroyWindow("win1");
cvResizeWindow("win1",100,100); // 新的宽/高值(象素点)
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);
int key; key=cvWaitKey(10); // 输入等待10ms
int key; key=cvWaitKey(0); // 无限等待键盘输入
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);
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 - 彩色通道交叉存取 BGRBGRBGR, | // 1 - 彩色通道分隔存取 BBBGGGRRR | // 函数cvCreateImage只能创建交叉存取的图像 |-- int origin; // 0 - 起点为左上角, | // 1 - 起点为右下角(Windows位图bitmap格式) |-- int widthStep; // 每行图像数据所占字节大小 |-- int imageSize; // 图像数据所占字节大小 = 高度*每行图像数据字节大小 |-- struct _IplROI *roi;// 图像ROI. 若不为NULL则表示需要处理的图像 | // 区域. |-- char *imageDataOrigin; // 指针指向图像数据原点 | // (用来校准图像存储单元的重新分配) | |-- int align; // 图像行校准: 4或8字节校准 | // OpenCV不采用它而使用widthStep |-- char colorModel[4]; // 图像色彩模型 - 被OpenCV忽略
CvMat // 2维数组 |-- int type; // 元素类型(uchar,short,int,float,double) |-- int step; // 一行所占字节长度 |-- int rows, cols; // 尺寸大小 |-- int height, width; // 备用尺寸参照 |-- union data; |-- uchar* ptr; // 针对unsigned char矩阵的数据指针 |-- short* s; // 针对short矩阵的数据指针 |-- int* i; // 针对integer矩阵的数据指针 |-- float* fl; // 针对float矩阵的数据指针 |-- double* db; // 针对double矩阵的数据指针 CvMatND // N-维数组 |-- int type; // 元素类型(uchar,short,int,float,double) |-- int dims; // 数组维数 |-- union data; | |-- uchar* ptr; // 针对unsigned char矩阵的数据指针 | |-- short* s; // 针对short矩阵的数据指针 | |-- int* i; // 针对integer矩阵的数据指针 | |-- float* fl; // 针对float矩阵的数据指针 | |-- double* db; // 针对double矩阵的数据指针 | |-- 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);
举例:
CvScalar s = cvScalar(20.0); s.val[0]=10.0;
注意:初始化函数与数据结构同名,只是首字母小写. 它不是C++的构造函数.
CvPoint p = cvPoint(int x, int y); CvPoint2D32f p = cvPoint2D32f(float x, float y); CvPoint3D32f p = cvPoint3D32f(float x, float y, float z); 例如: 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); size: cvSize(width,height); depth: IPL_DEPTH_8U, IPL_DEPTH_8S, IPL_DEPTH_16U, IPL_DEPTH_16S, IPL_DEPTH_32S, IPL_DEPTH_32F, IPL_DEPTH_64F channels: 1, 2, 3 or 4. 注意数据为交叉存取.彩色图像的数据编排为b0 g0 r0 b1 g1 r1 ...
举例:
// 分配一个单通道字节图像 IplImage* img1=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1); // 分配一个三通道浮点图像 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);
void cvSetImageROI(IplImage* image, CvRect rect); void cvResetImageROI(IplImage* image); vRect cvGetImageROI(const IplImage* image); 大部分OpenCV函数都支持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); Supported image formats: BMP, DIB, JPEG, JPG, JPE, PNG, PBM, PGM, PPM, SR, RAS, TIFF, TIF
载入图像默认转为3通道彩色图像. 如果不是,则需加flag:
img=cvLoadImage(fileName,flag); flag: >0 载入图像转为三通道彩色图像 =0 载入图像转为单通道灰度图像 <0 不转换载入图像(通道数与图像文件相同).
if(!cvSaveImage(outFileName,img)) printf("Could not save: %s/n",outFileName);
输入文件格式由文件扩展名决定.
IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1); CvScalar s; s=cvGet2D(img,i,j); // get the (i,j) pixel value printf("intensity=%f/n",s.val[0]); s.val[0]=111; cvSet2D(img,i,j,s); // set the (i,j) pixel value
IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_32F,3); CvScalar s; s=cvGet2D(img,i,j); // get the (i,j) 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 (i,j) 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/sizeof(uchar); 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/sizeof(uchar); 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/sizeof(float); 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 (flip vertically) CV_CVTIMG_SWAP_RB (swap the R and B channels)
使用OpenCV转换函数:
cvCvtColor(cimg,gimg,CV_BGR2GRAY); // cimg -> gimg
直接转换:
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_<X>2<Y> <X>/<Y> = RGB, BGR, GRAY, HSV, YCrCb, XYZ, Lab, Luv, HLS e.g.: CV_BGR2GRAY, CV_BGR2HSV, CV_BGR2Lab
// 用宽度为1的红线在(100,100)与(200,200)之间画一长方体 cvRectangle(img, cvPoint(100,100), cvPoint(200,200), cvScalar(255,0,0), 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);
cvFillPoly(img,curveArr,nCurvePts,nCurves,cvScalar(0,255,255));
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));
Other possible fonts:
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_<bit_depth>(S|U|F)C<number_of_channels>. 例如: CV_8UC1 表示8位无符号单通道矩阵, CV_32SC2表示32位有符号双通道矩阵. 例程: 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); // 这里似乎有问题,不成功
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 end{verbatim}
注意 Va, Vb, Vc 在向量积中向量元素个数须相同.
CvMat *Ma, *Mb; cvTranspose(Ma, Mb); // transpose(Ma) -> Mb (不能对自身进行转置) 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 = 对应的特征向量 (每行)
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)){ // 抓取一帧 printf("Could not grab a frame/n/7"); exit(0); } img=cvRetrieveFrame(capture); // 恢复获取的帧图像
要从多个摄像头同时获取图像, 首先从每个摄像头抓取一帧. 在抓取动作都结束后再恢复帧图像.
cvReleaseCapture(&capture);
注意由设备抓取的图像是由capture函数自动分配和释放的. 不要试图自己释放它.
cvQueryFrame(capture); // this call is necessary to get correct // capture properties 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);
所有帧数似乎只与视频文件有关. 用摄像头时不对,奇怪!!!.
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, 只对视频文件有效.
// 从视频文件相对位置0.9处开始抓取 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则将打开一个编码选择窗口(windows系统下).
IplImage* img = 0; int nFrames = 50; for(i=0;i<nFrames;i++){ cvGrabFrame(capture); // 抓取帧 img=cvRetrieveFrame(capture); // 恢复图像 cvWriteFrame(writer,img); // 将帧添加入视频文件 }
若想在抓取中查看抓取图像, 可在循环中加入下列代码:
cvShowImage("mainWin", img); key=cvWaitKey(20); // wait 20 ms
若没有20[毫秒]延迟,将无法正确显示视频序列.
cvReleaseVideoWriter(&writer);