Heim  >  Artikel  >  Backend-Entwicklung  >  Wie lese ich Pixelwerte aus einem Mehrkanalbild in OpenCV mit C++?

Wie lese ich Pixelwerte aus einem Mehrkanalbild in OpenCV mit C++?

王林
王林nach vorne
2023-09-08 20:13:10819Durchsuche

Wie lese ich Pixelwerte aus einem Mehrkanalbild in OpenCV mit C++?

Wir haben drei Variablen deklariert, nämlich „blue_Channel“, „green_channel“ und „red_channel“. Der Zweck dieser Variablen besteht darin, Pixelwerte zu speichern. Wir verwenden diese Variablen in der „for-Schleife“. Dann deklarieren wir eine Matrix namens „color_Image_Matrix“.

Die Syntax dieser Methode lautet wie folgt:

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

Wir verwenden ein BGR-Bild. Es verfügt über drei Kanäle. Diese Kanäle behalten eine bestimmte Reihenfolge bei, color_image_Matrix.at (i, j) repräsentiert den Pixelwert an Position (i, j) und [0] repräsentiert den ersten Kanal. Wenn wir diese Codezeile beispielsweise wie folgt schreiben:

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

Das bedeutet, dass die Variable „blue_Channel“ den Pixelwert des ersten Kanals bei (30, 35) hat. So können wir auf die Pixelwerte zugreifen OpenCV.

Das folgende Programm liest Pixelwerte verschiedener RGB-Bilder und zeigt die Pixelwerte der verschiedenen Kanäle in einem Konsolenfenster an.

Beispiel

#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;
}

Ausgabe

Image read successfully...

Die Ausführung dieses Programms dauert einige Minuten. Es liest jeden Pixelwert von einem anderen Kanal. Aus diesem Grund dauert es einige Minuten, bis die vollständigen Ergebnisse angezeigt werden.

Das obige ist der detaillierte Inhalt vonWie lese ich Pixelwerte aus einem Mehrkanalbild in OpenCV mit C++?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Dieser Artikel ist reproduziert unter:tutorialspoint.com. Bei Verstößen wenden Sie sich bitte an admin@php.cn löschen