将灰度图像转换为颜色

时间:2011-04-20 18:45:04

标签: android graphics

使用色彩空间(字节数组形式的RGB)将灰度图像(1像素/字节)转换为彩色位图的最佳方法是什么?

我认为这会使用ColorMatrix类,但我无法弄清楚如何使用它来实现这一目标。

1 个答案:

答案 0 :(得分:5)

从格雷转换的算法 - > RGB概述(我最喜欢的)OpenCV docs:

RGB-> Gray是R,G,B的加权和。 Gray-> RGB是来自Gray Value = R,G,B值(无缩放)的直接拷贝。

您可以使用int []并执行双循环将所有灰度值复制到RGB数组,然后可以使用Canvas.drawBitmap(int [],width,offset,height,offset,RGB )绘制到画布上。

//color[], gray[]
for(int y = 0; y < getHeight(); y++){
    for(int x = 0; x < getWidth(); x++){
      color[x+y*w] = 0xff << 24 | gray[x+y*w] << 16 |  gray[x+y*w] << 8 | gray[x+y*w];
    }
}
// make sure to set hasAlpha to be false, or set the 0xff value to be alpha
//canvas.drawBitmap(color, ...)

即使使用了ColorMatrix calss,您也必须处理必须将GrayScale位复制到[r g b a]值中。 ColorMatrix非常适合从ARGB扩展/转换为ARGB。