2015-10-26 73 views
0

我有一个png格式的图像目录,它们的编码使用固定的颜色索引。我有兴趣生成灰度值与索引本身相对应的图像,而不是该索引的颜色。将颜色索引图像转换为索引

例如,假设颜色索引具有条目索引3 - >(255,0,0)(红色)。我想用灰度值3替换RGB图像中(255,0,0)的每个实例。

我的第一个想法是1)将反转颜色索引硬编码到查找表中2)加载图像,3)迭代像素,根据查找表进行替换。

问题在于硬编码查找表。我可以得到它(通过imagemagick识别),但那很乏味。有没有任何图书馆可以为我做这件事?我正在寻找1)cmd线转换或2)获取像素颜色索引的代码库。

回答

1

如果你使用GD在PHP中打开一个palettised图像,但是不小心忘了告诉GD它是堆栈的,你实际上会得到蓝色像素的调色板索引。因此,您可以利用该功能创建一个灰度图像,方法是创建一个与您的原始图像大小相同的真彩色图像,然后分别为红色,绿色和蓝色通道分别写入三次调色板索引。这是很容易,它的声音,这里是代码:

#!/usr/local/bin/php 
<?php 
// Read in a palettised image 
$im = imagecreatefrompng("pal.png"); 

// Create output image same size 
$out = imagecreatetruecolor(imagesx($im), imagesy($im)); 

for ($x = 0; $x < imagesx($im); $x++) { 
    for ($y = 0; $y < imagesy($im); $y++) { 
     $pixel = imagecolorat($im, $x, $y); 
     $index = $pixel & 0xFF; 
     $outCol = imagecolorallocate($out,$index,$index,$index); 
     imagesetpixel($out,$x,$y,$outCol); 
     // printf("[%d,%d] Palette index:%d\n",$x,$y,$index); 
    } 
} 
imagepng($out,"result.png"); 
?> 

所以,如果我创建ImageMagick的一个3像素palettised像这样的:

convert xc:red xc:lime xc:blue +append pal.png 

,并检查它的调色板这样

identify -verbose pal.png | more 
    Image: pal.png 
     Format: PNG (Portable Network Graphics) 
     Mime type: image/png 
     Class: PseudoClass 
     Geometry: 3x1+0+0 
     Units: Undefined 
     Type: Palette     <--- it has a palette 
     Endianess: Undefined 
     Colorspace: sRGB 
     Depth: 8/1-bit 
     Channel depth: 
     red: 1-bit 
     green: 1-bit 
     blue: 1-bit 
     Channel statistics: 
     Pixels: 3 
     Red: 
      min: 0 (0) 
      max: 255 (1) 
      mean: 85 (0.333333) 
      standard deviation: 120.208 (0.471405) 
      kurtosis: -1.5 
      skewness: 0.707107 
      entropy: 0.918296 
     Green: 
      min: 0 (0) 
      max: 255 (1) 
      mean: 85 (0.333333) 
      standard deviation: 120.208 (0.471405) 
      kurtosis: -1.5 
      skewness: 0.707107 
      entropy: 0.918296 
     Blue: 
      min: 0 (0) 
      max: 255 (1) 
      mean: 85 (0.333333) 
      standard deviation: 120.208 (0.471405) 
      kurtosis: -1.5 
      skewness: 0.707107 
      entropy: 0.918296 
     Image statistics: 
     Overall: 
      min: 0 (0) 
      max: 255 (1) 
      mean: 85 (0.333333) 
      standard deviation: 120.208 (0.471405) 
      kurtosis: -1.5 
      skewness: 0.707107 
      entropy: 0.918296 
     Colors: 3 
     Histogram: 
      1: ( 0, 0,255) #0000FF blue 
      1: ( 0,255, 0) #00FF00 lime 
      1: (255, 0, 0) #FF0000 red 
     Colormap entries: 4 
     Colormap:          <--- here is the palette 
      0: (255, 0, 0) #FF0000 red 
      1: ( 0,255, 0) #00FF00 lime 
      2: ( 0, 0,255) #0000FF blue 
      3: (255,255,255) #FFFFFF white 

然后在运行PHP脚本后检查result.png,它现在看起来像这样 - 即灰度和颜色匹配前面的索引。

identify -verbose result.png 
Image: result.png 
    Format: PNG (Portable Network Graphics) 
    Mime type: image/png 
    Class: DirectClass 
    Geometry: 3x1+0+0 
    Units: Undefined 
    Type: Grayscale    <--- it is now greyscale, no palette 
    Endianess: Undefined 
    Colorspace: sRGB 
    Depth: 8-bit 
    Channel depth: 
    gray: 8-bit 
    Channel statistics: 
    Pixels: 3 
    Gray: 
     min: 0 (0) 
     max: 2 (0.00784314) 
     mean: 1 (0.00392157) 
     standard deviation: 0.816497 (0.00320195) 
     kurtosis: -1.5 
     skewness: 0 
     entropy: 1 
    Colors: 3 
    Histogram: 
     1: ( 0, 0, 0) #000000 gray(0) 
     1: ( 1, 1, 1) #010101 gray(1) 
     1: ( 2, 2, 2) #020202 gray(2) 

注意,如果原始图像有极少数的调色板项,输出图像将是黑暗的,所以你会想对比度拉伸或正常化它...