Чтение пикселей изображения в [закрытом] C++

Как открыть и считать пиксели изображения в C++? Считайте их в форме X, Y и знать цвет.

13
задан Ahmad Farid 16 December 2009 в 22:28
поделиться

5 ответов

Если вы собираетесь работать с изображениями, вам следует заглянуть в библиотеку OpenCV, в ней есть почти все, что вам нужно для работы с изображениями.

OpenCV 2.0 вышел пару месяцев назад и очень дружелюбен с C ++.

4
ответ дан 1 December 2019 в 20:57
поделиться

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

#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;
}
10
ответ дан 1 December 2019 в 20:57
поделиться

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 also has OpenGL support.

5
ответ дан 1 December 2019 в 20:57
поделиться

Вы либо используете существующую библиотеку, либо пишете свой собственный код. Как правило, лучше использовать первый подход, поскольку формат файла изображения часто более сложен, чем кажется на первый взгляд, и вы можете получить перевернутые изображения или неправильный порядок компонентов цвета, если не будете осторожны.

В зависимости от ваших требований вы можете также необходимо учитывать возможности формата. Если интересна поддержка расширенного динамического диапазона, OpenEXR - отличный выбор, если нет » libpng , OpenExr

5
ответ дан 1 December 2019 в 20:57
поделиться

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).

Your best bet is to use a library. Image Magick has a C library that will allow you to open just about any image format and access the pixels. SDL_image is another library that is very easy to use and SDL can easily be used with 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.

4
ответ дан 1 December 2019 в 20:57
поделиться
Другие вопросы по тегам:

Похожие вопросы: