opencv中mat,,cvmat,,Iplimage構(gòu)造體定義以及格式互相轉(zhuǎn)換opencv中mat,cvmat,,Iplimage結(jié)構(gòu)體定義以及格式互相轉(zhuǎn)換
opencv中常見的與圖像操作有關(guān)的數(shù)據(jù)容器有Mat,,cvMat和IplImage,這三種類型都可以代表和顯示圖像,,但是,,Mat類型側(cè)重于計(jì)算,數(shù)學(xué)性較高,,openCV對(duì)Mat類型的計(jì)算也進(jìn)行了優(yōu)化,。而CvMat和IplImage類型更側(cè)重于“圖像”,opencv對(duì)其中的圖像操作(縮放,、單通道提取,、圖像閾值操作等)進(jìn)行了優(yōu)化。在opencv2.0之前,,opencv是完全用C實(shí)現(xiàn)的,,但是,IplImage類型與CvMat類型的關(guān)系類似于面向?qū)ο笾械睦^承關(guān)系,。實(shí)際上,,CvMat之上還有一個(gè)更抽象的基類----CvArr,這在源代碼中會(huì)常見,。 1. IplImage opencv中的圖像信息頭,,該結(jié)構(gòu)體定義: View
Code
typedef struct _IplImage { int nSize; int ID; int nChannels; int alphaChannel; int depth; char colorModel[4]; char channelSeq[4]; int dataOrder; int origin; int align; int width; int height; struct _IplROI *roi; struct _IplImage *maskROI; void *imageId; struct _IplTileInfo *tileInfo; int imageSize; char *imageData; int widthStep; int BorderMode[4]; int BorderConst[4]; char *imageDataOrigin; } IplImage; dataOrder中的兩個(gè)取值:交叉存取顏色通道是顏色數(shù)據(jù)排列將會(huì)是BGRBGR...的交錯(cuò)排列。分開的顏色通道是有幾個(gè)顏色通道就分幾個(gè)顏色平面存儲(chǔ),。roi是IplROI結(jié)構(gòu)體,,該結(jié)構(gòu)體包含了xOffset,yOffset,height,width,coi成員變量,,其中xOffset,yOffset是x,y坐標(biāo),coi代表channel of interest(感興趣的通道),,非0的時(shí)候才有效,。訪問圖像中的數(shù)據(jù)元素,分間接存儲(chǔ)和直接存儲(chǔ),,當(dāng)圖像元素為浮點(diǎn)型時(shí),,(uchar *) 改為 (float *): View Code
IplImage* img=cvLoadImage("lena.jpg", 1); CvScalar s; s=cvGet2D(img,i,j); cvSet2D(img,i,j,s); IplImage* img; //malloc memory by cvLoadImage or cvCreateImage for(int row = 0; row < img->height; row++) { for (int col = 0; col < img->width; col++) { b = CV_IMAGE_ELEM(img, UCHAR, row, col * img->nChannels + 0); g = CV_IMAGE_ELEM(img, UCHAR, row, col * img->nChannels + 1); r = CV_IMAGE_ELEM(img, UCHAR, row, col * img->nChannels + 2); } } IplImage* img; //malloc memory by cvLoadImage or cvCreateImage uchar b, g, r; // 3 channels for(int row = 0; row < img->height; row++) { for (int col = 0; col < img->width; col++) { b = ((uchar *)(img->imageData + row * img->widthStep))[col * img->nChannels + 0]; g = ((uchar *)(img->imageData + row * img->widthStep))[col * img->nChannels + 1]; r = ((uchar *)(img->imageData + row * img->widthStep))[col * img->nChannels + 2]; } } 初始化使用IplImage *,是一個(gè)指向結(jié)構(gòu)體IplImage的指針: IplImage * cvLoadImage(constchar * filename, int//load images from specified image IplImage * cvCreateImage(CvSize size, int depth, int channels); //allocate memory 2.CvMat 首先,,我們需要知道,,第一,在OpenCV中沒有向量(vector)結(jié)構(gòu),。任何時(shí)候需要向量,,都只需要一個(gè)列矩陣(如果需要一個(gè)轉(zhuǎn)置或者共軛向量,則需要一個(gè)行矩陣),。第二,,OpenCV矩陣的概念與我們?cè)诰€性代數(shù)課上學(xué)習(xí)的概念相比,更抽象,,尤其是矩陣的元素,,并非只能取簡(jiǎn)單的數(shù)值類型,可以是多通道的值,。CvMat 的結(jié)構(gòu): typedef struct CvMat { int type; int step; int* refcount; union { uchar* ptr; short* s; int* i; float* fl; double* db; } data; union { int rows; int height; }; union { int cols; int width; }; } CvMat; 創(chuàng)建CvMat數(shù)據(jù): View Code
CvMat * cvCreateMat(int rows, int cols, int type); CV_INLine CvMat cvMat((int rows, int cols, int type, void* data CV_DEFAULT); CvMat * cvInitMatHeader(CvMat * mat, int rows, int cols, int type, void * data CV_DEFAULT(NULL), int step CV_DEFAULT(CV_AUTOSTEP)); 對(duì)矩陣數(shù)據(jù)進(jìn)行訪問: cvmSet( CvMat* mat, int row, int col, double value); cvmGet( const CvMat* mat, int row, int col ); CvScalar cvGet2D(const CvArr * arr, int idx0, int idx1); //CvArr只作為函數(shù)的形參void cvSet2D(CvArr* arr, int idx0, int idx1, CvScalar value); CvMat * cvmat = cvCreateMat(4, 4, CV_32FC1); cvmat->data.fl[row * cvmat->cols + col] = (float)3.0; CvMat * cvmat = cvCreateMat(4, 4, CV_64FC1); cvmat->data.db[row * cvmat->cols + col] = 3.0; CvMat * cvmat = cvCreateMat(4, 4, CV_64FC1); CV_MAT_ELEM(*cvmat, double, row, col) = 3.0; if (CV_MAT_DEPTH(cvmat->type) == CV_32F) CV_MAT_ELEM_CN(*cvmat, float, row, col * CV_MAT_CN(cvmat->type) + ch) = (float)3.0; // ch為通道值 if (CV_MAT_DEPTH(cvmat->type) == CV_64F) CV_MAT_ELEM_CN(*cvmat, double, row, col * CV_MAT_CN(cvmat->type) + ch) = 3.0; // ch為通道值 for (int row = 0; row < cvmat->rows; row++) { p = cvmat ->data.fl + row * (cvmat->step / 4); for (int col = 0; col < cvmat->cols; col++) { *p = (float) row + col; *(p+1) = (float)row + col + 1; *(p+2) = (float)row + col + 2; p += 3; } } CvMat * vector = cvCreateMat(1,3, CV_32SC2);CV_MAT_ELEM(*vector, CvPoint, 0, 0) = cvPoint(100,100); CvMat * vector = cvCreateMat(1,3, CV_64FC4);CV_MAT_ELEM(*vector, CvScalar, 0, 0) = CvScalar(0, 0, 0, 0); CvMat* M1 = cvCreateMat(4,4,CV_32FC1); CvMat* M2; M2=cvCloneMat(M1);
3.Mat Mat是opencv2.0推出的處理圖像的新的數(shù)據(jù)結(jié)構(gòu),,現(xiàn)在越來越有趨勢(shì)取代之前的cvMat和lplImage,相比之下Mat最大的好處就是能夠更加方便的進(jìn)行內(nèi)存管理,,不再需要程序員手動(dòng)管理內(nèi)存的釋放,。opencv2.3中提到Mat是一個(gè)多維的密集數(shù)據(jù)數(shù)組,可以用來處理向量和矩陣,、圖像,、直方圖等等常見的多維數(shù)據(jù)。 class CV_EXPORTS Mat { public: int flags;(Note :目前還不知道flags做什么用的) int dims; int rows,cols; uchar *data; int * refcount; ... }; 從以上結(jié)構(gòu)體可以看出Mat也是一個(gè)矩陣頭,,默認(rèn)不分配內(nèi)存,,只是指向一塊內(nèi)存(注意讀寫保護(hù))。初始化使用create函數(shù)或者M(jìn)at構(gòu)造函數(shù),,以下整理自opencv2.3.1 Manual: Mat(nrows, ncols, type, fillValue]); M.create(nrows, ncols, type); 例子: Mat M(7,7,CV_32FC2,Scalar(1,3)); M.create(100, 60, CV_8UC(15)); int sz[] = {100, 100, 100}; Mat bigCube(3, sz, CV_8U, Scalar:all(0)); double m[3][3] = {{a, b, c}, {d, e, f}, {g, h, i}}; Mat M = Mat(3, 3, CV_64F, m).inv(); Mat img(Size(320,240),CV_8UC3); Mat img(height, width, CV_8UC3, pixels, step); IplImage* img = cvLoadImage("greatwave.jpg", 1); Mat mtx(img,0); // convert IplImage* -> Mat; 訪問Mat的數(shù)據(jù)元素: Mat M; M.row(3) = M.row(3) + M.row(5) * 3; Mat M1 = M.col(1); M.col(7).copyTo(M1); Mat M; M.at<double>(i,j); M.at(uchar)(i,j); Vec3i bgr1 = M.at(Vec3b)(i,j) Vec3s bgr2 = M.at(Vec3s)(i,j) Vec3w bgr3 = M.at(Vec3w)(i,j) double sum = 0.0f; for(int row = 0; row < M.rows; row++) { constdouble * Mi = M.ptr<double>(row); for (int col = 0; col < M.cols; col++) sum += std::max(Mi[j], 0.); } double sum=0; MatConstIterator<double> it = M.begin<double>(), it_end = M.end<double>(); for(; it != it_end; ++it) sum += std::max(*it, 0.); Mat可進(jìn)行Matlab風(fēng)格的矩陣操作,,如初始化的時(shí)候可以用initializers,zeros(), ones(), eye().
除以上內(nèi)容之外,Mat還有有3個(gè)重要的方法: View Code
Mat mat = imread(const String* filename); // 讀取圖像 imshow(conststring frameName, InputArray mat); // 顯示圖像 imwrite (conststring& filename, InputArray img); //儲(chǔ)存圖像
4. CvMat, Mat, IplImage之間的互相轉(zhuǎn)換 IpIImage -> CvMat CvMat matheader; CvMat * mat = cvGetMat(img, &matheader); CvMat * mat = cvCreateMat(img->height, img->width, CV_64FC3); cvConvert(img, mat) IplImage -> Mat Mat::Mat(const IplImage* img, bool copyData=false); 例子: IplImage* iplImg = cvLoadImage("greatwave.jpg", 1); Mat mtx(iplImg); Mat -> IplImage Mat M IplImage iplimage = M; CvMat -> Mat Mat::Mat(const CvMat* m, bool copyData=false); Mat -> CvMat 例子(假設(shè)Mat類型的imgMat圖像數(shù)據(jù)存在): CvMat cvMat = imgMat;/*Mat -> CvMat, 類似轉(zhuǎn)換到IplImage,,不復(fù)制數(shù)據(jù)只創(chuàng)建矩陣頭 |
|