Home  >  Article  >  Backend Development  >  How to read pixel values ​​from multi-channel image in OpenCV using C++?

How to read pixel values ​​from multi-channel image in OpenCV using C++?

王林
王林forward
2023-09-08 20:13:10957browse

How to read pixel values ​​from multi-channel image in OpenCV using C++?

We declared three variables, namely 'blue_Channel', 'green_channel' and 'red_channel'. The purpose of these variables is to hold pixel values. We are using these variables in the 'for loop'. Then, we declare a matrix called 'color_Image_Matrix'.

The syntax of this method is as follows:

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

We use a BGR image. It has three channels. These channels maintain a specific order, color_image_Matrix.at (i, j) represents the pixel value at position (i, j), and [0] represents the first channel. For example, if we write this line of code as follows:

blue_Channel=color_image_Matrix.at<Vec3b> (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<Vec3b>(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;
}

Output

Image read successfully...

This program will take a few minutes to run. It reads each pixel value from a different channel. That's why it takes a few minutes for the full results to appear.

The above is the detailed content of How to read pixel values ​​from multi-channel image in OpenCV using C++?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete