Opencv得到圖像(IplImage)中的像素

IplImage結構來自於 Intel Image Processing Library(是其本身所具有的)。OpenCV 只支持其中的一個子集:

來自於http://wiki.opencv.org.cn/index.php/Cxcore%e5%9f%ba%e7%a1%80%e7%bb%93%e6%9e%84

typedef struct _IplImage
    {
        int  nSize;         //   IplImage大小,=sizeof(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]; /*    被OpenCV忽略           */
        int  dataOrder;     /* 0 - 交叉存取顏色通道,對三通道RGB圖像,像素存儲順序爲BGR BGR BGR ... BGR;
                                     1 - 分開的顏色通道,對三通道RGB圖像,像素存儲順序爲RRR...R GGG...G BBB...B。
                                  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


單通道圖像訪問像素值:


重要的兩個元素是:char *imageData以及widthStep。imageData存放圖像像素數據,而widStep類似CvMat中的step,表示以字節爲單位的行數據長度。

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



示例代碼:


#include <stdio.h>
#include <iostream>


#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"


using namespace std;
using namespace cv;


int main()
{
//加載一張圖片(彩色圖,8位3通道)
IplImage* queryImg = cvLoadImage("x1.jpg");

//轉換爲灰度圖(8位單通道)
IplImage* queryGrey = cvCreateImage(cvGetSize(queryImg), IPL_DEPTH_8U, 1);//創建目標圖像  
cvCvtColor(queryImg, queryGrey, CV_BGR2GRAY);//cvCvtColor(src,des,CV_BGR2GRAY)

//求灰度圖像的像素值


//第一種:如果我們要遍歷圖像中的元素,只需:

        uchar* tmp = new uchar[307200];
for (int i = 0; i < queryGrey->height; i++)
{
for (int j = 0; j < queryGrey->width; j++){
*tmp = ((uchar *)(queryGrey->imageData + i*queryGrey->widthStep))[j];
cout << "第i行第j列的圖像灰度值: " << (float)(*tmp) << ",";
}

}


//第二種:這種直接訪問的方法速度快,但容易出錯,我們可以通過定義指針來訪問。即:

        uchar* data = (uchar *)queryGrey->imageData;
int step = queryGrey->widthStep / sizeof(uchar);
uchar* tmp = new uchar[307200];
for (int i = 0; i < queryGrey->height; i++)
{
for (int j = 0; j < queryGrey->width; j++)
{
*tmp = data[i*step + j];
cout << "第i行第j列的圖像灰度值: " << (float)(*tmp) << ",";
}

}


return 0;
}


三通道圖像訪問像素值:




其中(Bi,Bj)(Gi,Gj)(Ri,Rj)表示圖像(i,j)處BGR分量的值。使用指針的遍歷方法如下:


示例代碼:


#include <stdio.h>
#include <iostream>


#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"


using namespace std;
using namespace cv;


int main()
{
//加載一張圖片(彩色圖,8位3通道)
IplImage* queryImg = cvLoadImage("x1.jpg");

//求彩色圖像(3通道)的像素值

        //以下是兩種不同的方法,選一種即可:


//第一種:如果我們要遍歷圖像中的元素,只需:
uchar *b = new uchar[307200];//初始化,不然會報錯
uchar *g = new uchar[307200];
uchar *r = new uchar[307200];


for (int i = 0; i<2/*queryImg->height*/; i++)
for (int j = 0; j <2/* queryImg->width*/; j++){
*b = ((uchar *)(queryImg->imageData + i*queryImg->widthStep + j*queryImg->nChannels))[0];
*g = ((uchar *)(queryImg->imageData + i*queryImg->widthStep + j*queryImg->nChannels))[1];
*r = ((uchar *)(queryImg->imageData + i*queryImg->widthStep + j*queryImg->nChannels))[2];
}



//第二種:指針的方式
uchar* data = (uchar *)queryImg->imageData;
int step = queryImg->widthStep / sizeof(uchar);
int channels = queryImg->nChannels;
for (int i = 0; i<2/*queryImg->height*/; i++)
for (int j = 0; j<2/*queryImg->width*/; j++){
*b = data[i*step + j*channels + 0];
*g = data[i*step + j*channels + 1];
*r = data[i*step + j*channels + 2];
cout << "b通道的圖像灰度值: " << (float)(*b) << ","<<endl;
}


return 0;
}

ps:*如果要修改某像素值,則直接賦值。


3.使用cvGet2D()函數訪問:



3.1 單通道:


#include <stdio.h>
#include <iostream>


#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"


using namespace std;
using namespace cv;


int main()
{
//加載一張圖片(彩色圖,8位3通道)
IplImage* queryImg = cvLoadImage("x1.jpg");

//轉換爲灰度圖(8位單通道)
IplImage* queryGrey = cvCreateImage(cvGetSize(queryImg), IPL_DEPTH_8U, 1);//創建目標圖像  
cvCvtColor(queryImg, queryGrey, CV_BGR2GRAY);//cvCvtColor(src,des,CV_BGR2GRAY)


//求灰度圖像(單通道)的像素值
double tmp;
for (int i = 0; i<queryGrey->height; i++)
for (int j = 0; j < queryGrey->width; j++){
tmp = cvGet2D(queryGrey, i, j).val[0];
cout << "第i行第j列的像素值爲: " << tmp << endl;
}



return 0;
}

3.1 三通道:


#include <stdio.h>
#include <iostream>


#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"


using namespace std;
using namespace cv;


int main()
{
//加載一張圖片(彩色圖,8位3通道)
IplImage* queryImg = cvLoadImage("x1.jpg");



//求彩色圖像(3通道)的像素值


double tmpb, tmpg, tmpr;
for (int i = 0; i<queryImg->height; i++)
for (int j = 0; j<queryImg->width; j++){
tmpb = cvGet2D(queryImg, i, j).val[0];
tmpg = cvGet2D(queryImg, i, j).val[1];
tmpr = cvGet2D(queryImg, i, j).val[2];
cout << "第i行第j列的B通道的圖像像素值: " << tmpb << ",";

}


return 0;
}






參考網站:

http://blog.csdn.net/xiaowei_cqu/article/details/7557063

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章