如何使用 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