i want convert image gray scale image pixel intensities between 0-255.
i able convert images gray scale images following java method.
public void converttograyscale(bufferedimage bufimage, int imgwidth, int imgheight) { (int w = 0; w < imgwidth; w++) { (int h = 0; h < imgheight; h++) { color color = new color(bufimage.getrgb(w, h)); int colavgval = ((color.getred() + color.getgreen() + color.getblue()) / 3); color avg = new color(colavgval, colavgval, colavgval); bufimage.setrgb(w, h, avg.getrgb()); system.out.println(avg.getrgb()); } } }
"system.out.println(avg.getrgb());" used see pixel intensities all grey levels minus values , not between 0-255.
am doing wrong ? how convert image gray scale image pixel intensities between 0-255.
thanks
color.getrgb()
not return value 0..255, returns integer composited of red, green , blue values, including alpha value. presumably, alpha value 0xff
, makes any combined color end 0xffrrggbb
, or, got, huge negative number when written in decimals.
to see "gray" level assigned, check colavgval
.
note better formula convert between rgb , grayscale use pal/ntsc conversion:
gray = 0.299 * red + 0.587 * green + 0.114 * blue
because "full blue" should darker in grayscale "full red" , "full green".
note: if use formula directly, watch out floating point rounding errors. in theory, should not return value outside of 0..255
gray
; in practice, it will. test , clamp result.
another option not require testing-and-clamping per pixel, use integer-only version:
gray = (299 * red + 587 * green + 114 * blue)/1000;
which should work small rounding error.
Comments
Post a Comment