FreeImage: Get pixel color

╄→гoц情女王★ 提交于 2019-12-06 15:33:47

问题


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!



来源:https://stackoverflow.com/questions/7645168/freeimage-get-pixel-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!