如何更改存储为像素值的图像的对比度和亮度

Jay*_*Jay 4 java bufferedimage image-processing

我有一个存储为像素值数组的图像。我希望能够对此图像应用亮度或对比度滤镜。有什么简单的方法或算法可用来实现这一目标。

这是我的代码...

   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]++;
       }
   }
Run Code Online (Sandbox Code Playgroud)

wat*_*ios 5

我的建议是使用Java的内置方法来调整亮度和对比度,而不是尝试自己调整像素值。像这样做似乎很容易...

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);
Run Code Online (Sandbox Code Playgroud)

浮点数是亮度的百分比。在我的示例中,它将亮度提高到现有值的120%(即比原始图像亮20%)

看到这个链接有一个类似的问题... 在Java中调整BufferedImage的亮度和对比度

请参阅此链接以获取示例应用程序... http://www.java2s.com/Code/Java/Advanced-Graphics/BrightnessIncreaseDemo.htm