2011-10-04 201 views
0

我正在寫一個小應用程序,它讀取圖像中每個像素的顏色並將其寫入文件。首先我是用Python做的,但是在大圖片上它太慢了。然後我發現了可以使用的FreeImage庫,但我無法理解如何使用GetPixelColor方法。 您能否提供一個關於如何獲得顏色的例子,例如像素[50:50]? 這裏是關於GetPixelColor的信息:http://freeimage.sourceforge.net/fnet/html/13E6BB72.htm。 非常感謝!FreeImage:獲取像素顏色

+0

如果可以使用DevIL或CImg來完成,那也不錯。 – ghostmansd

回答

4

隨着使用FreeImagePlus一個24位或32位的圖像,得到了像素在COORDS 50,50應該是這樣的:

fipImage input; 
RGBQUAD pixel; 

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

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

請注意,在FreeImage的原產地是左下角,所以y值大概會需要通過如上所述從圖像高度減去y來反轉。

0

要從輸入圖像獲取像素顏色:img,從函數調用讓我們說:void read_image(const char* img)請按照下面的代碼片段。

這裏是上面read_image函數的代碼片斷:

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

可變color將包含圖像像素的顏色。可以按如下方式提取的RGB值:

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

希望它能幫助!