开发者

Converting grayscale image to color

开发者 https://www.devze.com 2023-02-27 04:08 出处:网络
What is the best way to convert a grayscal开发者_如何学Ce image (1-pixel/byte) to a color bitmap - using a color space (RGB in form of a byte array)?

What is the best way to convert a grayscal开发者_如何学Ce image (1-pixel/byte) to a color bitmap - using a color space (RGB in form of a byte array)?

I think this would use ColorMatrix class, but I cannot figure out how to use it achieve this.


The algorithm for converting from Gray -> RGB is outlined (in my favorite) OpenCV docs:

RGB->Gray is a weighted sum of R,G,B. Gray->RGB is a straight copy from Gray Value = R,G,B values (no scaling).

You could just use a int[] and do a double for loop to copy all Gray Values to the RGB array, you could then use Canvas.drawBitmap(int[],width,offset,height,offset,RGB) to draw onto a Canvas.

//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, ...)

Even if used the ColorMatrix calss you have to deal with having to copy the GrayScale bits into the [r g b a] value. ColorMatrix is really for scaling/converting from ARGB to ARGB.

0

精彩评论

暂无评论...
验证码 换一张
取 消