如何使用C++在OpenCV中从多通道图像中读取像素值?

2023年 9月 8日 102.2k 0

如何使用C++在OpenCV中从多通道图像中读取像素值?

我们声明了三个变量,分别是'blue_Channel'、'green_channel'和'red_channel'。这些变量的目的是保存像素值。我们在'for循环'中使用了这些变量。然后,我们声明了一个名为'color_Image_Matrix'的矩阵。

这个方法的语法如下:

blue_Channel = color_image_Matrix.at(i, j)[0];

登录后复制

我们使用了一张BGR图像。它有三个通道。这些通道维护特定的顺序,color_image_Matrix.at (i, j) 表示位于(i, j)位置的像素值,[0]表示第一个通道。例如,如果我们将这行代码写成如下形式:

blue_Channel=color_image_Matrix.at (30, 35) [0];

登录后复制

It means the variable 'blue_Channel' will have the first channel's pixel value located at(30, 35). This is how we can access the pixel values using OpenCV.

The following program reads pixel values of different RGB images and displays the different channel pixel's value in a console window.

Example

#include
#include
using namespace std;
using namespace cv;
int main() {
int blue_Channel;
int green_Channel;
int red_Channel;
Mat color_image_Matrix; //Declaring a matrix to load the image//
color_image_Matrix = imread("colors.jpg"); //loading image in the matrix//
//Beginning of for loop to read pixel values of blue channel//
for (int i = 0; i < color_image_Matrix.rows; i++)//loop for rows// {
for (int j = 0; j < color_image_Matrix.cols; j++) {
//loop for columns//
blue_Channel = color_image_Matrix.at(i, j)[0];
//To read the value of first channel.Here the blue channel is first channel//
cout

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论