帮我写基本的灰度变换函数增强图像java的代码
时间: 2023-06-04 08:08:02 浏览: 41
好的,以下是基本的灰度变换函数增强图像java代码:
```java
import java.awt.image.BufferedImage;
public class ImageProcessor {
public static void enhanceImage(BufferedImage image, int contrast, int brightness) {
int width = image.getWidth();
int height = image.getHeight();
for(int x = 0; x < width; x++) {
for(int y = 0; y < height; y++) {
int rgb = image.getRGB(x, y);
int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = rgb & 0xFF;
// Apply contrast and brightness to each pixel
red = applyContrastAndBrightness(red, contrast, brightness);
green = applyContrastAndBrightness(green, contrast, brightness);
blue = applyContrastAndBrightness(blue, contrast, brightness);
int newRGB = (red << 16) | (green << 8) | blue;
image.setRGB(x, y, newRGB);
}
}
}
private static int applyContrastAndBrightness(int color, int contrast, int brightness) {
color = ((color - 128) * contrast) / 128 + 128;
color += brightness;
if(color < 0) {
color = 0;
} else if(color > 255) {
color = 255;
}
return color;
}
}
```
您可以通过传递一个BufferedImage对象、对比度值和亮度值来调用enhanceImage函数,以增强图像。