Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have an image that is stored as an array of pixel values. I want to be able to apply a brightness or contrast filter to this image. Is there any simple 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]++;
       }
   }
like image 672
Jay Avatar asked Apr 11 '12 12:04

Jay


People also ask

Which command can you use to adjust the brightness and contrast of an image?

Going to Image > Adjustments > Brightness/Contrast.

What determines the brightness of an image pixel?

It depends on your visual perception. Since brightness is a relative term, so brightness can be defined as the amount of energy output by a source of light relative to the source we are comparing it to. In some cases we can easily say that the image is bright, and in some cases, its not easy to perceive.


1 Answers

My suggestion would be to use the built-in methods of Java to adjust the brightness and contrast, rather than trying to adjust the pixel values yourself. It seems 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);

The float number is a percentage of the brightness. In my example it would increase the brightness to 120% of the existing value (ie. 20% brighter than the original image)

See this link for a similar question... Adjust brightness and contrast of BufferedImage in Java

See this link for an example application... http://www.java2s.com/Code/Java/Advanced-Graphics/BrightnessIncreaseDemo.htm

like image 192
wattostudios Avatar answered Nov 15 '22 03:11

wattostudios