OpenCV訪問畫素點的灰度值
阿新 • • 發佈:2019-02-18
1.Mat矩陣數值的儲存方式
這裡以指標的方式訪問影象素為例
(1)單通道
定義一個單通道影象:
cv::Mat img_1 = (320, 640, CV_8UC1, Scalar(0));
對於單通道M(i,j)即為第i行j列的其灰度值;程式中表示為:
img_1.ptr<uchar>(i)[j];
(2)多通道
這裡以RGB影象為例,每一個子列依次為B、G、R,,第一個分量是藍色,第二個是綠色,第三個是紅色。
定義一個3通道BGR影象:
cv::Mat img_1 = (320, 640, CV_8UC3, Scalar(0, 0 ,0));
對於多通道M(i,j*3)即為第i行j列的B通道其灰度值,M(i,j*3+1) 即為第i行j列的G通道其灰度值,M(i,j*3+1) 即為第i行j列的B通 道其灰度值;程式中表示為:
第i行j列的B通道其灰度值:
img_1.ptr<uchar>(i)[j*3];
第i行j列的G通道其灰度值:
img_1.ptr<uchar>(i)[j*3+1];
第i行j列的R通道其灰度值:
img_1.ptr<uchar>(i)[j*3+2];
2.示例程式,以三種方法(指標,at,迭代器)
////獲得影象畫素值 #include <iostream> #include <vector> #include <algorithm> #include <opencv2/core/core.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/opencv.hpp> using namespace std; using namespace cv; void get_setImagePixel3(char *imagePath, int x, int y) { Mat image = imread(imagePath, 1); //得寬高 int w = image.cols; int h = image.rows; int channels = image.channels(); if (channels == 1) { //得到初始位置的迭代器 Mat_<uchar>::iterator it = image.begin<uchar>(); //得到終止位置的迭代器 Mat_<uchar>::iterator itend = image.end<uchar>(); int pixel = *(it + y * w + x); cout << "灰度影象,處的灰度值為" << pixel << endl; } else { //得到初始位置的迭代器 Mat_<Vec3b>::iterator it = image.begin<Vec3b>(); //得到終止位置的迭代器 Mat_<Vec3b>::iterator itend = image.end<Vec3b>(); //讀取 it = it + y * w + x; int b = (*it)[0]; cout << b << endl; int g = (*it)[1]; cout << g << endl; int r = (*it)[2]; cout << r << endl; //設定畫素值 (*it)[0] = 255; (*it)[1] = 255; (*it)[2] = 255; } imshow("cc", image); } int main() { vector<int> v = {1,2,3,4,6}; cout << "*********通過指標訪問畫素的灰度值********************" << endl; //通過指標訪問畫素的灰度值 //單通道 Mat img1(20, 30, CV_32FC1, Scalar(0)); img1.ptr<float>(19)[25] = 23456.1789; cout << "img(19,25):" << img1.ptr<float>(19)[25] <<endl; //多通道,建立一個B、G、R通道灰度值為0的影象,影象大小20*30,每個灰度值為16位無符號2進製表示 Mat img2(20, 30, CV_16UC3, Scalar(0, 0, 0)); cout << "img(1,2):" << int(img1.ptr<uchar>(19)[25]) << endl; Mat img = imread("test1.jpg"); int numRow = img.rows; int numCol = img.cols; int numCol_channel = img.cols*img.channels(); cout << "numRow:" << numRow << endl; cout << "numCol:" << numCol << endl; cout << "numCol_channel:" << numCol_channel << endl; cout << "45行,483列B通道灰度值" << int(img.ptr<uchar>(45)[483*3]) << endl; cout << "45行,483列G通道灰度值" << int(img.ptr<uchar>(45)[483*3+1]) << endl; cout << "45行,483列R通道灰度值" << int(img.ptr<uchar>(45)[483*3+2]) << endl; Mat img_B(numRow, numCol, CV_8UC3, Scalar(0, 0, 0)); Mat img_G(numRow, numCol, CV_8UC3, Scalar(0, 0, 0)); Mat img_R(numRow, numCol, CV_8UC3, Scalar(0, 0, 0)); for (int i = 0; i < numRow; i++) { for (int j = 0; j < numCol; j++) { img_B.ptr<uchar>(i)[j*3] = img.ptr<uchar>(i)[j*3]; img_G.ptr<uchar>(i)[j*3+1] = img.ptr<uchar>(i)[j*3+1]; img_R.ptr<uchar>(i)[j*3+2] = img.ptr<uchar>(i)[j*3+2]; } } imshow("img", img); imshow("img_B", img_B); imshow("img_G", img_G); imshow("img_R", img_R); cout << endl; cout << endl; cout << endl; cout << "*********at只適合灰度值為8位的影象********************" << endl; //注意:at只適合灰度值為8位的影象 //單通道 Mat img3(20, 30, CV_8UC1, Scalar(0)); cout << "img(7,8)" << int(img3.at<uchar>(7, 8)) << endl; //多通道 Mat img4(20, 30, CV_8UC3, Scalar(0)); //BGR通道 cout << "B通道灰度值" << int(img4.at<Vec3b>(3, 4)[0]) << endl; cout << "G通道灰度值" << int(img4.at<Vec3b>(3, 4)[1]) << endl; cout << "R通道灰度值" << int(img4.at<Vec3b>(3, 4)[2]) << endl; waitKey(0); system("pause"); return 0; }