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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10105521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:38:35  来源:igfitidea点击:

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

javaimage-processingbufferedimage

提问by Jay

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]++;
       }
   }

回答by wattostudios

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...

我的建议是使用 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);

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)

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

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

有关类似问题,请参阅此链接... 在 Java 中调整 BufferedImage 的亮度和对比度

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

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