opencv學習筆記(八)IplImage* 訪問圖像像素的值


  opencv2.1版本之前使用IplImage*數據結構來表示圖像,2.1之后的版本使用圖像容器Mat來存儲。IplImage結構體如下所示。

 1 typedef struct _IplImage  
 2     {  
 3         int  nSize;         /* IplImage大小 */  
 4         int  ID;            /* 版本 (=0)*/  
 5         int  nChannels;     /* 大多數OPENCV函數支持1,2,3 或 4 個通道 */  
 6         int  alphaChannel;  /* 被OpenCV忽略 */  
 7         int  depth;         /* 像素的位深度: IPL_DEPTH_8U, IPL_DEPTH_8S, IPL_DEPTH_16U, 
 8                                IPL_DEPTH_16S, IPL_DEPTH_32S, IPL_DEPTH_32F and IPL_DEPTH_64F 可支持 */  
 9         char colorModel[4]; /* 被OpenCV忽略 */  
10         char channelSeq[4]; /* 同上 */  
11         int  dataOrder;     /* 0 - 交叉存取顏色通道, 1 - 分開的顏色通道. 
12                                cvCreateImage只能創建交叉存取圖像 */  
13         int  origin;        /* 0 - 頂—左結構, 
14                                1 - 底—左結構 (Windows bitmaps 風格) */  
15         int  align;         /* 圖像行排列 (4 or 8). OpenCV 忽略它,使用 widthStep 代替 */  
16         int  width;         /* 圖像寬像素數 */  
17         int  height;        /* 圖像高像素數*/  
18         struct _IplROI *roi;/* 圖像感興趣區域. 當該值非空只對該區域進行處理 */  
19         struct _IplImage *maskROI; /* 在 OpenCV中必須置NULL */  
20         void  *imageId;     /* 同上*/  
21         struct _IplTileInfo *tileInfo; /*同上*/  
22         int  imageSize;     /* 圖像數據大小(在交叉存取格式下imageSize=image->height*image->widthStep),單位字節*/  
23         char *imageData;  /* 指向排列的圖像數據 */  
24         int  widthStep;   /* 排列的圖像行大小,以字節為單位 */  
25         int  BorderMode[4]; /* 邊際結束模式, 被OpenCV忽略 */  
26         int  BorderConst[4]; /* 同上 */  
27         char *imageDataOrigin; /* 指針指向一個不同的圖像數據結構(不是必須排列的),是為了糾正圖像內存分配准備的 */  
28     }  
29     IplImage;  

1、使用指針遍歷圖像像素

(1)單通道字節型圖像像素訪問

 1 /*
 2 @author:CodingMengmeng
 3 @theme:Read the image pixel values
 4 @time:2017-3-16 11:27:31
 5 @blog:http://www.cnblogs.com/codingmengmeng/
 6 */
 7 #include <cv.h>  
 8 #include <highgui.h>  
 9 using namespace std;
10 using namespace cv;
11 int main(void)
12 {
13     IplImage* imgSrc = cvLoadImage("./inputData\\shuke1.jpg",0);
14     uchar* pixel = new uchar;
15     for (int i = 0; i < imgSrc->height; i++)
16     {
17         for (int j = 0; j < imgSrc->width; j++)
18         {
19             pixel = (uchar*)(imgSrc->imageData + i*imgSrc->widthStep+j);
20             cout << "pixel=" <<(*pixel)+0<< endl;//+0隱式轉換為整型,否則會打印出字符
21         }
22     }
23     delete pixel;
24 return 0; 25 }

輸出結果是0-255灰度級的灰度值。

其中(uchar*)(imgSrc->imageData + i*imgSrc->widthStep+j)的具體含義:

a)imgSrc->imageData指向圖像第一行的首地址,i是指當前像素點所在的行,widthStep是指圖像每行所占的字節數;所以imgSrc->imageData + i*imgSrc->widthStep表示該像素點所在行的首地址;j表示當前像素點所在列,所以imgSrc->imageData + i*imgSrc->widthStep+j即表示該像素點的地址。而因為IplImage->ImageData 的默認類型是 char 類型,所以再對圖像像素值進行操作時,要使用強制類型轉換為unsigned char,再對其進行處理。否則,圖像像素值中,會有負值出現。

(b)widthStep表示存儲一行像素需要的字節數

一個m*n的單通道字節型圖像,其imageData排列如下:

因為opencv分配的內存是按4字節對齊的,所以widthStep必須是4的倍數,如果8U圖像寬度為3,那么widthStep是4,加一個字節補齊。這個圖像的一行需要4個字節,只使用前3個,最后一個空在那兒不用。也就是一個寬3高3的圖像的imageData數據大小為4*3=12字節。

 

(2)三通道字節型圖像像素訪問

多通道字節型圖像的imageData排列如下:

其中(Bi,Bj)(Gi,Gj)(Ri,Rj)表示圖像(i,j)處BGR分量的值。

 1 /*
 2 //@author:CodingMengmeng
 3 //@theme:Read the image pixel values
 4 //@time:2017-3-16 11:59:17
 5 //@blog:http://www.cnblogs.com/codingmengmeng/
 6 */
 7 #include <cv.h>  
 8 #include <highgui.h>  
 9 using namespace std;
10 using namespace cv;
11 int main(void)
12 {
13     IplImage* imgSrc = cvLoadImage("./inputData\\shuke1.jpg");
14     uchar* b_pixel = new uchar;
15     uchar* g_pixel = new uchar;
16     uchar* r_pixel = new uchar;
17     for (int i = 0; i < imgSrc->height; i++)
18     {
19         for (int j = 0; j < imgSrc->width; j++)
20         {
21             b_pixel = (uchar*)(imgSrc->imageData + i*imgSrc->widthStep + (j*imgSrc->nChannels + 0));
22             g_pixel = (uchar*)(imgSrc->imageData + i*imgSrc->widthStep + (j*imgSrc->nChannels + 1));
23             r_pixel=(uchar*)(imgSrc->imageData + i*imgSrc->widthStep + (j*imgSrc->nChannels + 2));
24             cout << "b_pixel=" << *b_pixel+0 << endl;
25             cout << "g_pixel=" << *g_pixel+0 << endl;
26             cout << "r_pixel=" << *r_pixel+0 << endl;
27             cout << "/********************************************/" << endl;
28         }
29     }
30     delete b_pixel;
31     delete g_pixel;
32     delete r_pixel;
33     return 0;
34 }

 

運行結果:

 

2、使用cvGet2D()函數訪問

cvGet*D系列函數可以用來返回特定位置的數組元素(一般使用cvGet2D),原型如下:

1 CvScalar cvGet1D( const CvArr* arr, int idx0 );  
2 CvScalar cvGet2D( const CvArr* arr, int idx0, int idx1 );  
3 CvScalar cvGet3D( const CvArr* arr, int idx0, int idx1, int idx2 );  
4 CvScalar cvGetND( const CvArr* arr, int* idx );  

(1)單通道圖像像素訪問

 1 /*
 2 @author:CodingMengmeng
 3 @theme:Read the image pixel values
 4 @time:2017-3-16 15:12:57
 5 @blog:http://www.cnblogs.com/codingmengmeng/
 6 */
 7 #include <cv.h>  
 8 #include <highgui.h>  
 9 using namespace std;
10 using namespace cv;
11 int main(void)
12 {
13     IplImage* imgSrc = cvLoadImage("./inputData\\shuke1.jpg",0);
14     CvScalar pixel_v;
15     /*
16     CvScalar是一個可以用來存放4個double數值的數組
17     一般用來存放像素值(不一定是灰度值哦)的,最多可以存放4個通道的
18     如何賦值:
19     a) 存放單通道圖像中像素:cvScalar(255);
20     b) 存放三通道圖像中像素:cvScalar(255,255,255);
21     c)只使用第一個通道,val[0]=val0;等同於cvScalar(val0,0,0,0);
22     */
23     for (int i = 0; i < imgSrc->height; i++)
24     {
25         for (int j = 0; j < imgSrc->width; j++)
26         {
27             pixel_v = cvGet2D(imgSrc, i, j);
28             cout << "pixel=" << pixel_v.val[0] << endl;
29         }
30     }
31     return 0;
32 }

注意:

對於圖像中的某一像素點 P(x, y), 在我們正常的坐標系中,x代表其橫坐標,y代表其縱坐標;而在opencv的函數cvGet2D()的函數原型是 : CvScalar cvGet2D (const CvArr * arr, int idx0, int idx1); 函數返回的是一個CvScalar 容器,其參數中也有兩個方向的坐標,但跟我們平常習慣的坐標不一樣的是,idx0代表是的行,即高度,對應於我們平常坐標系的y, idx1代表的是列,即寬度,對應於我們平常坐標系的x使用時請千萬別弄反,否則容易出現溢出引發異常。

(2)多通道字節型/浮點型圖像像素訪問

 1 /*
 2 @author:CodingMengmeng
 3 @theme:Read the image pixel values
 4 @time:2017-3-16 15:18:29
 5 @blog:http://www.cnblogs.com/codingmengmeng/
 6 */
 7 #include <cv.h>  
 8 #include <highgui.h>  
 9 using namespace std;
10 using namespace cv;
11 int main(void)
12 {
13     IplImage* imgSrc = cvLoadImage("./inputData\\shuke1.jpg");
14     CvScalar pixel_v;
15     /*
16     CvScalar是一個可以用來存放4個double數值的數組
17     一般用來存放像素值(不一定是灰度值哦)的,最多可以存放4個通道的
18     如何賦值:
19     a) 存放單通道圖像中像素:cvScalar(255);
20     b) 存放三通道圖像中像素:cvScalar(255,255,255);
21     c)只使用第一個通道,val[0]=val0;等同於cvScalar(val0,0,0,0);
22     */
23     for (int i = 0; i < imgSrc->height; i++)
24     {
25         for (int j = 0; j < imgSrc->width; j++)
26         {
27             pixel_v = cvGet2D(imgSrc, i, j);
28             cout << "b_pixel=" << pixel_v.val[0] << endl;//B分量
29             cout << "g_pixel=" << pixel_v.val[1] << endl;//G分量
30             cout << "r_pixel=" << pixel_v.val[2] << endl;//R分量
31             cout << "/********************************************/" << endl;
32         }
33     }
34     return 0;
35 }

運行結果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM