FreeImage: Get pixel color

2020-08-04 06:02发布

问题:

I'm writing a little application that reads color of each pixel in image and writes it to file. First I did it in Python, buit it's too slow on big images. Then I discovered FreeImage library, which I could use, but I can't understand how to use GetPixelColor method. Could you please provide an example on how to get color, for example, of pixel[50:50]? Here is information about GetPixelColor: http://freeimage.sourceforge.net/fnet/html/13E6BB72.htm. Thank you very much!

回答1:

With FreeImagePlus using a 24 or 32 bit image, getting the pixel at coords 50, 50 would look like this:

fipImage input;
RGBQUAD pixel;

input.load("myimage.png");
height = in.getHeight();

in.getPixelColor(50, height-1-50, &pixel);

Be aware that in FreeImage the origin is bottom left, so y values will probably need to be inverted by subtracting y from the image height as above.



回答2:

To get pixel color from an input image: img, from a function call let's say: void read_image(const char* img) follow the below code snippet.

Here is the code snippet for above read_image function:

FREE_IMAGE_FORMAT fif = FreeImage_GetFIFFromFilename(img); 
FIBITMAP *bmp = FreeImage_Load(fif, img);

unsigned width = FreeImage_GetWidth(bmp); 
unsigned height =  FreeImage_GetHeight(bmp); 
int bpp = FreeImage_GetBPP(bmp);

FIBITMAP* bitmap = FreeImage_Allocate(width, height, bpp);
RGBQUAD color; FreeImage_GetPixelColor(bitmap, x, y, &color);

variable color will contain the color of the image pixel. You can extract rgb values as follows:

float r,g,b;
r = color.rgbRed;
g = color.rgbGreen;
b = color.rgbBlue;

Hope it helps!