如何使用 C++ 在 OpenCV 中讀取單通道影像的畫素值?
數字影像由畫素組成。使用 OpenCV 可以輕鬆讀取畫素值。但是,如果我們想要獲取畫素值,我們必須單獨處理單個通道。
此處,我們在名為“cimage”的矩陣中載入影像,然後使用 'cvtColor(cimage, img, COLOR_BGR2GRAY); ' 轉換為影像,並將其儲存在名為“img”的矩陣中。
以下程式讀取影像的畫素值,並在控制檯視窗中顯示值。
示例
#include<iostream>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main() {
int x;//Declaring an integer variable to hold values of pixels//
Mat cimage = imread("colors.jpg");//loading an image//
Mat img;//Declaring an empty matrix to store converted image//
cvtColor(cimage, img, COLOR_BGR2GRAY);//Converting loaded image to grayscale image//
for (int i = 0; i < img.rows; i++)//loop for rows// {
for (int j = 0; j < img.cols; j++)//loop for columns// {
x = (int)img.at<uchar>(i, j);//storing value of (i,j) pixel in variable//
cout << "Value of pixel" << "(" << i << "," << j << ")" << "=" << x << endl;//showing the values in console window//
}
}
imshow("Show", img);//showing the image//
waitKey();//wait for keystroke from keyboard//
return 0;
}輸出

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式語言
C++
C#
MongoDB
MySQL
Javascript
PHP