日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

OpenCV访问像素点的灰度值

發布時間:2023/12/2 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 OpenCV访问像素点的灰度值 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

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;}

?

總結

以上是生活随笔為你收集整理的OpenCV访问像素点的灰度值的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。