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

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

立即学习“C++免费学习笔记(深入)”;

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

Example

#include#includeusing 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 << "Value of pixel of blue channel" << "(" << i << "," << j << ")" << "="            << blue_Channel << endl; //showing the values in console window//      }   }   //End of for loop to read pixel values of blue channel//   //Beginning of for loop to read pixel values of green 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// {         green_Channel = color_image_Matrix.at(i, j)[1];         //To read the value of first channel.Here the green channel is first channel//         cout << "Value of pixel of green channel" << "(" << i << ","            << j << ")" << "=" << blue_Channel << endl;//showing the values in console window//      }   }   //End of for loop to read pixel values of green channel//   //Beginning of for loop to read pixel values of red 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// {         red_Channel = color_image_Matrix.at(i, j)[2];         //To read the value of first channel.Here the red channel is first channel//         cout << "Value of pixel of red channel" << "(" << i << "," <<            j << ")" << "=" << blue_Channel << endl; //showing the values in console window//      }   }   //End of for loop to read pixel values of red channel//   if (waitKey(0)==27);      cout << "Image read successfully…!";      return 0;}

输出

Image read successfully...

这个程序运行需要几分钟时间。它从不同的通道读取每个像素值。这就是为什么显示完整结果需要几分钟的原因。

以上就是如何使用C++在OpenCV中从多通道图像中读取像素值?的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1444694.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月17日 21:47:56
下一篇 2025年12月17日 21:48:08

相关推荐

发表回复

登录后才能评论
关注微信