如何通过 c++ 获取图像的所有像素数据 (RGB)

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

本文关键字:像素数 像素 数据 RGB c++ 何通过 获取 图像      更新时间:2023-10-16

从JPG或PNG文件中,我想要2D数组形式的像素数据。
执行该任务的过程是什么,或者哪些库可以完成该任务?

尝试 openCV 库。这是您可以下载并安装它的网站。下面是执行所需操作的代码:

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