Computer >> Máy Tính >  >> Lập trình >> C ++

Làm cách nào để đọc giá trị pixel từ hình ảnh đa kênh trong OpenCV bằng C ++?

Chúng tôi đã khai báo ba biến có tên-'blue_Channel ',' green_channel 'và' red_channel '. Mục tiêu của các biến này là lưu các giá trị pixel. Chúng tôi đã sử dụng các biến này bên trong 'vòng lặp for'. Sau đó, chúng tôi khai báo một ma trận có tên 'color_Image_Matrix'.

Cú pháp của phương thức này là:

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

Chúng tôi đã sử dụng hình ảnh BGR. Nó có ba kênh. Các kênh này duy trì trình tự cụ thể, color_image_Matrix.at (i, j) đại diện cho các giá trị pixel nằm tại (i, i) và [0] đại diện cho kênh đầu tiên. Ví dụ, nếu chúng ta viết dòng như sau:

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

Nó có nghĩa là biến 'blue_Channel' sẽ có giá trị pixel của kênh đầu tiên nằm ở (30, 35). Đây là cách chúng tôi có thể truy cập các giá trị pixel bằng OpenCV.

Chương trình sau đây đọc các giá trị pixel của các hình ảnh RGB khác nhau và hiển thị giá trị pixel của kênh khác nhau trong cửa sổ bảng điều khiển.

Ví dụ

#include<iostream>
#include<opencv2/highgui/highgui.hpp>
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<Vec3b>(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<Vec3b>(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;
}

Đầu ra

Image read successfully...

Chương trình này mất vài phút để chạy. Nó đọc từng giá trị pixel từ các kênh khác nhau. Đó là lý do tại sao phải mất vài phút để hiển thị kết quả hoàn chỉnh.