【OpenCV】訪問影象畫素值並修改--IplImage
阿新 • • 發佈:2019-02-11
1.IplImage的結構:
2.訪問影象畫素值,並且可以通過該方式改變影象的畫素值typedef struct _IplImage { int nSize; /* IplImage大小 */ int ID; /* 版本 (=0)*/ int nChannels; /* 大多數OPENCV函式支援1,2,3 或 4 個通道 */ int alphaChannel; /* 被OpenCV忽略 */ int depth; /* 畫素的位深度: IPL_DEPTH_8U, IPL_DEPTH_8S, IPL_DEPTH_16U, IPL_DEPTH_16S, IPL_DEPTH_32S, IPL_DEPTH_32F and IPL_DEPTH_64F 可支援 */ char colorModel[4]; /* 被OpenCV忽略 */ char channelSeq[4]; /* 同上 */ int dataOrder; /* 0 - 交叉存取顏色通道, 1 - 分開的顏色通道. cvCreateImage只能建立交叉存取影象 */ int origin; /* 0 - 頂—左結構, 1 - 底—左結構 (Windows bitmaps 風格) */ int align; /* 影象行排列 (4 or 8). OpenCV 忽略它,使用 widthStep 代替 */ int width; /* 影象寬畫素數 */ int height; /* 影象高畫素數*/ struct _IplROI *roi;/* 影象感興趣區域. 當該值非空只對該區域進行處理 */ struct _IplImage *maskROI; /* 在 OpenCV中必須置NULL */ void *imageId; /* 同上*/ struct _IplTileInfo *tileInfo; /*同上*/ int imageSize; /* 影象資料大小(在交叉存取格式下imageSize=image->height*image->widthStep),單位位元組*/ char *imageData; /* 指向排列的影象資料 */ int widthStep; /* 排列的影象行大小,以位元組為單位 */ int BorderMode[4]; /* 邊際結束模式, 被OpenCV忽略 */ int BorderConst[4]; /* 同上 */ char *imageDataOrigin; /* 指標指向一個不同的影象資料結構(不是必須排列的),是為了糾正影象記憶體分配準備的 */ } IplImage;
IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1); uchar* data=(uchar *)img->imageData; int step = img->widthStep/sizeof(uchar); uchar* tmp; for(int i=0;i<img->height;i++) for(int j=0;j<img->width;j++) *tmp=data[i*step+j];
修改畫素示例:
判斷畫素示例:uchar* data = (uchar *)src->imageData; int step = src->widthStep / sizeof(uchar); data[i*step + j] = 255;
if (((uchar*)(iplBinImg->imageData + j*iplBinImg->widthStep)[i]) != ((uchar*)(iplBinImg->imageData + j*iplBinImg->widthStep)[i + 1]))