简体   繁体   中英

Display Bitmap in gray scale of one color space component value only

I would like to know how can I display color Bitmap in gray-scale. It must be done for each color space components separately. Probably it is not clear, therefore I uploaded images to visualize it: Grey-scale images visualisation These Bitmaps are displayed after color conversion into one color space component: B, G, R, S (blue, green red, saturation). I want to repeat it in Android. I tried something like this:

case 'blue': pixels[i]=Color.argb(alpha,0,0,blue);

But it does not work at all. How to remove unnecessary components and present a Bitmap in gray-scale with a value of one color space component?

You won't be able to use ColorMatrixColorFilter to display the saturation because the formula can't be turned into a matrix, and it's probably easier to use the same technique for all the components rather than handling that as a special case.

Your included code will produce a blue bitmap, to display the blue component in grey scale you need to copy it to the red and green channels also:

case 'blue': pixels[i]=Color.argb(alpha,blue,blue,blue);

Likewise for red:

case 'red': pixels[i]=Color.argb(alpha,red,red,red);

and green:

case 'green': pixels[i]=Color.argb(alpha,green,green,green);

For the saturation you will need to compute it. Here is one formula:

case 'saturation':         
     int rgbMax = red > green ? (red > blue ? red : blue) : (green > blue ? green : blue);
     if(rgbMax == 0)
     {
         pixels[i]=Color.argb(alpha,0,0,0);
     }
     else
     {
         int rgbMin = red < green ? (red < blue ? red : blue) : (green < blue ? green : blue);
         int saturation = (255 * (rgbMax - rgbMin)) / rgbMax;
         pixels[i]=Color.argb(alpha,saturation,saturation,saturation);
     }

If you are not satisfied with the result then refer to the formula for HSV .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM