How to change the contrast and brightness of an image saved as pixel values

I have an image that is stored as an array of pixel values. I want a brightness or contrast filter to be applied to this image. Is there an easy way or algorithm that I can use to achieve this.

Here is my code ...

   PlanarImage img=JAI.create("fileload","C:\\aimages\\blue_water.jpg");
   BufferedImage image = img.getAsBufferedImage();

   int w = image.getWidth();
   int h = image.getHeight();
   int k = 0;

   int[] sbins = new int[256];
   int[] pixel = new int[3];

   Double d = 0.0;
   Double d1;
   for (int x = 0; x < bi.getWidth(); x++) {
       for (int y = 0; y < bi.getHeight(); y++) {
           pixel = bi.getRaster().getPixel(x, y, new int[3]);
           k = (int) ((0.2125 * pixel[0]) + (0.7154 * pixel[1]) + (0.072 * pixel[2]));
           sbins[k]++;
       }
   }
+3
source share
1 answer

My suggestion was to use Java's built-in methods to adjust brightness and contrast, rather than trying to adjust pixel values ​​on my own. It looks pretty easy by doing something like this ...

float brightenFactor = 1.2f

PlanarImage img=JAI.create("fileload","C:\\aimages\\blue_water.jpg");
BufferedImage image = img.getAsBufferedImage();

RescaleOp op = new RescaleOp(brightenFactor, 0, null);
image = op.filter(image, image);

- . 120% (.. 20% )

. ... BufferedImage Java

. ... http://www.java2s.com/Code/Java/Advanced-Graphics/BrightnessIncreaseDemo.htm

+3

All Articles