简体   繁体   English

如何加载图像并遍历其像素?

[英]How can I load an image and iterate through its pixels?

My question is pretty simple, suppose I have an image and I want to load it into my program so that I can get the color of each pixel.我的问题很简单,假设我有一个图像并且我想将它加载到我的程序中,以便我可以获得每个像素的颜色。 How could I do that?我怎么能那样做? In a nutshell I want a method that gives me the color of a pixel.简而言之,我想要一种给我像素颜色的方法。

struct color{
    double r, g, b;
    color(){}
    color(float red, float green, float blue){r = red; g = green; b = blue;}
};

color GetPixel(string imageName, int x, int y){
    //if(x < 0 || x >= width of image) return color(0,0,0);
    //if(y < 0 || y >= height of image) return color(0,0,0);
    //do stuff
    return colorForPixelXY;
}

Preferably I would like to do it natively, without using any external libs.最好我想在本地进行,而不使用任何外部库。

The STB image library is a single file, header only "library" that you can include and use in your project scott free. STB 图像库是单个文件,header 只是一个“库”,您可以在项目中免费包含和使用它。 It's sort of an industry standard for cases like yours.对于像您这样的情况,这是一种行业标准。 Highly recommended.强烈推荐。

https://github.com/nothings/stb https://github.com/nothings/stb

That said, reading a PNM/PPM file is very easy thing to do in C++ as it is basically a text file.也就是说,在 C++ 中读取 PNM/PPM 文件非常容易,因为它基本上是一个文本文件。 Any image package can take your BMP,JPG and save it to PNM that you can even include in your executable binary.任何图像 package 都可以获取您的 BMP、JPG 并将其保存到 PNM,您甚至可以将其包含在可执行二进制文件中。

read PPM file and store it in an array; 读取 PPM 文件并将其存储在数组中; coded with C 编码为 C

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM