FreeImage: Get pixel color

风流意气都作罢 提交于 2019-12-04 21:48:33

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.

sinner

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!

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