在 C++ 中读取图像的像素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1918263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:35:20  来源:igfitidea点击:

Reading Pixels of Image in C++

c++image-processing

提问by Ahmad Farid

How to open and read the pixels of an image in c++? Read them in the form of X, Y and to know the color.

如何在 C++ 中打开和读取图像的像素?以 X、Y 的形式阅读它们并了解颜色。

采纳答案by rem7

If you are going to be working with images you should look into the OpenCV library, it has pretty much everything you need to work with images.

如果您要处理图像,您应该查看 OpenCV 库,它几乎拥有处理图像所需的一切。

OpenCV 2.0 came out a couple of months ago and its very friendly with C++.

OpenCV 2.0 几个月前发布,它对 C++ 非常友好。

回答by Alex Jasmin

Reading the color of a pixel from an image file using the Magick++library

使用Magick++库从图像文件中读取像素的颜色

#include <Magick++.h>
#include <iostream>

using namespace Magick;
using namespace std;

int main(int argc, char **argv) {
 try {
  InitializeMagick(*argv);
  Image img("C:/test.bmp");
  ColorRGB rgb(img.pixelColor(0, 0));  // ie. pixel at pos x=0, y=0
  cout << "red: " << rgb.red();
  cout << ", green: " << rgb.green();
  cout << ", blue: " << rgb.blue() << endl;
 }
  catch ( Magick::Exception & error) {
  cerr << "Caught Magick++ exception: " << error.what() << endl;
 }
 return 0;
}

回答by horseyguy

Use a library like DevIL (http://openil.sourceforge.net/). DevIL will load the image data into an array and you can access the raw pixel data using a function like ilGetPixels() or so.

使用像 DevIL ( http://openil.sourceforge.net/)这样的库。DevIL 会将图像数据加载到数组中,您可以使用 ilGetPixels() 等函数访问原始像素数据。

DevIL also has OpenGL support.

DevIL 也有 OpenGL 支持。

回答by Laserallan

Either you use an existing library or you write your own code. Generally the first approach is better since an image file format is often more complex than it seems on the surface and you may end up with upside down images or incorrect color component order if you are not careful.

要么使用现有的库,要么编写自己的代码。通常第一种方法更好,因为图像文件格式通常比表面看起来更复杂,如果不小心,最终可能会得到颠倒的图像或不正确的颜色分量顺序。

Depending on your requirements you may also need to take capabilities of the format in consideration. If support for high dynamic range is interesting, OpenEXR is a great choice, if it's not it's probably just inconvenient since it does not have as big support among other programs as for example PNG.

根据您的要求,您可能还需要考虑格式的功能。如果对高动态范围的支持很有趣,OpenEXR 是一个不错的选择,如果不是,它可能只是不方便,因为它在其他程序中没有像 PNG 那样大的支持。

Here are two libraries I often turn to when needing to read and write images: libpng, OpenExr

以下是我在需要读写图像时经常使用的两个库: libpngOpenExr

回答by Niki Yoshiuchi

BMPs are incredibly simply. Uncompressed BMPs consist of a header, some information about the BMP, the color palette (if applicable) and then the bitmap data, pixel by pixel. Writing your own bitmap parser is a fun exercise although there is a lot of extra work handling all of the features (8-bit, RLE compression, etc).

BMP 非常简单。未压缩的 BMP 由标题、有关 BMP 的一些信息、调色板(如果适用)和位图数据组成,逐个像素。编写您自己的位图解析器是一项有趣的练习,尽管处理所有功能(8 位、RLE 压缩等)还有很多额外的工作要做。

Your best bet is to use a library. Image Magickhas a C library that will allow you to open just about any image format and access the pixels. SDL_imageis another library that is very easy to use and SDL can easily be used with OpenGL.

最好的办法是使用图书馆。 Image Magick有一个 C 库,可以让您打开几乎任何图像格式并访问像素。 SDL_image是另一个非常易于使用的库,SDL 可以很容易地与 OpenGL 一起使用。

What image format you should use will depend upon the application. JPGs have pretty good compression, but the compression is LOSSY, meaning that you lose detail. If the image has text, or has large areas of solid colors or edges (like a comic) this is bad, you will get noticeable artifacting. For photos, JPGs are generally fine. PNGs are a nice alternative, they are compressed but the compression is LOSSLESS. JPGs will generally be smaller than PNGs, both will be smaller than BMPs.

您应该使用哪种图像格式取决于应用程序。JPG 具有很好的压缩效果,但压缩是有损的,这意味着您会丢失细节。如果图像有文本,或者有大面积的纯色或边缘(如漫画),这很糟糕,你会得到明显的伪像。对于照片,JPG 通常很好。PNG 是一个不错的选择,它们被压缩但压缩是无损的。JPG 通常比 PNG 小,两者都比 BMP 小。