[OpenCV3函數] —— at函數獲取像素點

(Mat).at<類型>(y,x)

頭文件:opencv2/imgproc/imgproc.hpp

**作用:**獲取圖像像素點值

opencv3中圖形存儲基本爲Mat格式,如果我們想獲取像素點的灰度值或者RGB值,可以通過image.at<uchar>(i,j)的方式輕鬆獲取。

Mat類中的at方法對於獲取圖像矩陣某點的RGB值或者改變某點的值很方便,對於單通道的圖像,則可以使用:

image.at<uchar>(i, j)

其中有一個要注意的地方是i對應的是點的y座標,j對應的是點的x座標,而不是我們習慣的(x,y)

來獲取或改變該點的值,而RGB通道的則可以使用:

image.at<Vec3b>(i, j)[0]  
image.at<Vec3b>(i, j)[1]  
image.at<Vec3b>(i, j)[2]

來分別獲取B、G、R三個通道的對應的值。下邊的代碼實現對圖像加椒鹽噪聲:

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace std;
using namespace cv;

void salt_noise( Mat image, int time );

int main ( int argc, char** argv )
{
    Mat image = imread("C:\\Users\\Administrator\\Desktop\\2-1.jpg",0); 
    if ( image.empty() ){
        cout << "Load image error" << endl;
        return -1;
    }
    salt_noise(image, 3000);
    namedWindow("image", 1);
    imshow("image", image);
    waitKey();
    return 0;
}

void salt_noise ( Mat image, int time )
{
    for (int k = 0; k < time; k++ ) //time is the number of noise you add
    {
        int i = rand() % image.rows;
        int j = rand() % image.cols;
        if (image.channels() == 1) //single channel
        {
            image.at<uchar>(i,j) = rand() % 255;
        }
        else if (image.channels() == 3) //RGB channel
        {
            image.at<Vec3b>(i, j)[0] = rand() % 255;
            image.at<Vec3b>(i, j)[1] = rand() % 255;
            image.at<Vec3b>(i, j)[2] = rand() % 255;
        }
    }
}

還有比較省時的方法使用Mat的模板子類Mat_<T>,,對於單通道的具體使用:

Mat_<uchar> img = image;  
img(i, j) = rand() % 255;

對於RGB通道的使用:

Mat_<Vec3b> img = image;  
img(i, j)[0] = rand() % 255;  
img(i, j)[1] = rand() % 255;  
mg(i, j)[2] = rand() % 255;

還可以用指針的方法遍歷每一像素:(耗時較小)

void colorReduce(Mat image, int div = 64)  
    {  
        int nrow = image.rows;  
        int ncol = image.cols*image.channels();  
        for (int i = 0; i < nrow; i++)  
        {  
            uchar* data = image.ptr<uchar>(i);//get the address of row i;  
            for (int j = 0; j < ncol; j++)  
            {  
                data[i] = (data[i] / div)*div ;  
            }  
        }  
    } 

我們要尤其注意OpenCV座標系與row&col的關係 (Mat::at(x,y)和Mat::at(Point(x, y))的區別)

直接給出對應關係吧

row == heigh == Point.y

col == width == Point.x

Mat::at(Point(x, y)) == Mat::at(y,x)

因爲還有點的座標,所以建議在訪問時都用Mat::at(Point(x, y))這種形式吧,免去了點座標和行列的轉換

詳細說明:

  1. 座標體系中的零點座標爲圖片的左上角,X軸爲圖像矩形的上面那條水平線;Y軸爲圖像矩形左邊的那條垂直線。該座標體系在諸如結構體Mat,Rect,Point中都是適用的。(雖然網上有學着說OpenCV中有些數據結構的座標原點是在圖片的左下角,但是我暫時還沒碰到過)。
  2. 在使用image.at(x1, x2)來訪問圖像中點的值的時候,x1並不是圖片中對應點的x軸座標,而是圖片中對應點的y座標。因此其訪問的結果其實是訪問image圖像中的Point(x2, x1)點,即與image.at(Point(x2, x1))效果相同。
  3. 如果所畫圖像是多通道的,比如說image圖像的通道數時n,則使用Mat::at(x, y)時,其x的範圍依舊是0到image的height,而y的取值範圍則是0到image的width乘以n,因爲這個時候是有n個通道,所以每個像素需要佔有n列。但是如果在同樣的情況下,使用Mat::at(point)來訪問的話,則這時候可以不用考慮通道的個數,因爲你要賦值給獲取Mat::at(point)的值時,都不是一個數字,而是一個對應的n維向量。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章