【问题标题】:Converting grayscale image to color将灰度图像转换为彩色
【发布时间】:2011-04-21 03:08:28
【问题描述】:

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

我认为这将使用ColorMatrix 类,但我不知道如何使用它来实现这一点。

【问题讨论】:

    标签: android graphics


    【解决方案1】:

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

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

    您可以只使用 int[] 并执行双重 for 循环将所有灰度值复制到 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 类,您也必须处理必须将 GrayScale 位复制到 [r g b a] 值的问题。 ColorMatrix 真正用于从 ARGB 到 ARGB 的缩放/转换。

    【讨论】:

    猜你喜欢
    • 2011-12-28
    • 1970-01-01
    • 2016-01-11
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多