0

I am looking to replace pixels in an image that are black to some degree (semi-black) to become fully black.

The method to do this is setRGB(int x, int y, int rgb). I know this. What I do not know is how to detect pixels that are semi-black.

I have tried (i is a BufferedImage):

final int rgb = i.getRGB(x, y);
if (rgb == -16777216) {
    i.setRGB(x, y, -16777216);
}

To do this, but it only replaces the pixels that are pure black with pure black.

I have also tried dimming the image, but that does not work either.

Any ideas on how I test for generic blackness?

My goal: the image I am reading is thin text. I wish to make this bolder text by this.

4

1 回答 1

4

The integer that you receive represents the combined red, green, blue and alpha values. Essentially, you need to:

  • break that integer down into its component red, green, blue values
  • from those values, assess the overall "brightness" of the pixel

As a rough implementation, you could do something like this:

int pixVal = ... getRGB() as you have
int red = (pixVal >>> 16);
int green = (pixVal >>> 8) & 0xff;
int blue = pixVal & 0xff;
int brightness = (red + green + blue) / 3;
if (brightness < 16) {
  // pixel is black
}

Now, the value 16 is a rough value: ideally, you would tailor this to the particular image.

Purists might also whinge that the perceived "brightness" of a pixel isn't literally the mean of the red/green/blue pixels (because the human eye is not equally sensitive to these components). But that's the rough idea to work from.

于 2012-05-05T01:44:52.767 回答