c++image-processingrgbpixelgetpixel

How to get all the pixel data (RGB) of an image through c++


From a JPG or PNG file, I want pixel data in the form of 2D array.
What is the procedure to do it OR which libraries can do that task?


Solution

  • Try openCV library. Here is it's site that you can download and install it. Here is the code that does what you want:

    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/core/core.hpp>
    #include <iostream>
    using namespace cv;
    using namespace std;
    int main()
    {
    Mat image = imread("C:/.../image1.jpg");
    Vec3b buf;
    
    for(int i = 0; i < image.rows; i++)
        for(int j = 0; j < image.cols; j++)
        {
            buf = image.at<Vec3b>(i,j);
            array_B[i][j] = buf[0];
            array_G[i][j] = buf[1];
            array_R[i][j] = buf[2];
        }
    
    //imwrite("C:/.../image2.jpg",image3);
    imshow("Image",image);
    waitKey(0);
    }