Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

การอ่านค่าพิกเซลจากภาพหลายช่องสี (Multichannel) ใน OpenCV ด้วย C++

โจทย์

เราได้ประกาศตัวแปรสามตัวคือ 'blue_Channel', 'green_channel' และ 'red_channel' เป้าหมายของตัวแปรเหล่านี้คือเก็บค่าพิกเซล เราใช้ตัวแปรเหล่านี้ภายใน 'for loops' จากนั้นประกาศเมทริกซ์ชื่อ 'color_Image_Matrix'

ไวยากรณ์ของเมธอดนี้คือ:

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

เราใช้ภาพ BGR ซึ่งมี 3 ช่องสี ช่องสีเหล่านี้มีลำดับเฉพาะ color_image_Matrix.at<Vec3b> (i, j) แทนค่าพิกเซลที่อยู่ที่ (i, j) และ [0] แทนช่องสีแรก ตัวอย่างเช่น หากเราเขียนบรรทัดดังนี้:

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

หมายความว่าตัวแปร 'blue_Channel' จะมีค่าพิกเซลของช่องสีแรกที่อยู่ที่ (30, 35) นี่คือวิธีที่เราสามารถเข้าถึงค่าพิกเซลได้โดยใช้ OpenCV

โปรแกรมต่อไปนี้อ่านค่าพิกเซลของภาพ RGB ต่างๆ และแสดงค่าพิกเซลของช่องสีต่างๆ ในหน้าต่างคอนโซล

ตัวอย่าง

#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 << ")" << "=" << green_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 << ")" << "=" << red_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...

โปรแกรมนี้ใช้เวลาหลายนาทีในการรัน มันอ่านค่าพิกเซลจากช่องสีต่างๆ นั่นคือเหตุผลที่มันใช้เวลาสองสามนาทีในการแสดงผลลัพธ์ครบถ้วน