首頁 > 後端開發 > C++ > 如何使用C++在OpenCV中從多通道影像中讀取像素值?

如何使用C++在OpenCV中從多通道影像中讀取像素值?

王林
發布: 2023-09-08 20:13:10
轉載
980 人瀏覽過

如何使用C++在OpenCV中從多通道影像中讀取像素值?

我們宣告了三個變量,分別是'blue_Channel'、'green_channel'和'red_channel'。這些變數的目的是保存像素值。我們在'for迴圈'中使用了這些變數。然後,我們聲明了一個名為'color_Image_Matrix'的矩陣。

這個方法的語法如下:

blue_Channel = color_image_Matrix.at<Vec3b>(i, j)[0];
登入後複製

我們使用了一個BGR映像。它有三個通道。這些通道維護特定的順序,color_image_Matrix.at (i, j) 表示位於(i, j)位置的像素值,[0]表示第一個通道。例如,如果我們將這行程式碼寫成如下形式:

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 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.

##r#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;
}
登入後複製

這個程式運行需要幾分鐘。它從不同的通道讀取每個像素值。這就是為什麼顯示完整結果需要幾分鐘的原因。

以上是如何使用C++在OpenCV中從多通道影像中讀取像素值?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板