JAVA完成对灰度图像的图像增强操作
时间: 2023-12-08 21:05:48 浏览: 21
对灰度图像进行图像增强可以通过以下步骤实现:
1. 读取灰度图像,将其转换为二维数组形式。
2. 对图像进行直方图均衡化操作,使得灰度级分布更加均匀。
3. 对图像进行滤波处理,可以采用中值滤波等方法,去除噪声。
4. 对图像进行边缘增强处理,可以采用拉普拉斯算子或者Sobel算子等方法进行卷积操作。
5. 对图像进行二值化处理,将图像转换为黑白形式,可以采用Otsu算法等方法进行阈值分割。
以下是JAVA代码实现:
```java
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class ImageEnhancement {
public static void main(String[] args) throws Exception {
// 读取灰度图像
BufferedImage image = ImageIO.read(new File("gray_image.jpg"));
// 直方图均衡化
int[] hist = new int[256];
int width = image.getWidth();
int height = image.getHeight();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int gray = image.getRGB(x, y) & 0xFF;
hist[gray]++;
}
}
for (int i = 1; i < 256; i++) {
hist[i] += hist[i - 1];
}
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int gray = image.getRGB(x, y) & 0xFF;
int newGray = hist[gray] * 255 / (width * height);
image.setRGB(x, y, (newGray << 16) | (newGray << 8) | newGray);
}
}
// 中值滤波
int[][] pixels = new int[3][3];
for (int y = 1; y < height - 1; y++) {
for (int x = 1; x < width - 1; x++) {
pixels[0][0] = (image.getRGB(x - 1, y - 1) & 0xFF);
pixels[0][1] = (image.getRGB(x - 1, y) & 0xFF);
pixels[0][2] = (image.getRGB(x - 1, y + 1) & 0xFF);
pixels[1][0] = (image.getRGB(x, y - 1) & 0xFF);
pixels[1][1] = (image.getRGB(x, y) & 0xFF);
pixels[1][2] = (image.getRGB(x, y + 1) & 0xFF);
pixels[2][0] = (image.getRGB(x + 1, y - 1) & 0xFF);
pixels[2][1] = (image.getRGB(x + 1, y) & 0xFF);
pixels[2][2] = (image.getRGB(x + 1, y + 1) & 0xFF);
int median = getMedian(pixels);
image.setRGB(x, y, (median << 16) | (median << 8) | median);
}
}
// 边缘增强
int[][] laplacian = {{-1, -1, -1}, {-1, 8, -1}, {-1, -1, -1}};
int[][] sobel_x = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}};
int[][] sobel_y = {{-1, -2, -1}, {0, 0, 0}, {1, 2, 1}};
int[][] operator = sobel_y; // 选择边缘算子
for (int y = 1; y < height - 1; y++) {
for (int x = 1; x < width - 1; x++) {
int sum = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int pixel = image.getRGB(x + j - 1, y + i - 1) & 0xFF;
sum += pixel * operator[i][j];
}
}
int gray = Math.min(Math.max(sum, 0), 255);
image.setRGB(x, y, (gray << 16) | (gray << 8) | gray);
}
}
// 二值化
int threshold = getThreshold(image);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int gray = image.getRGB(x, y) & 0xFF;
int newGray = gray < threshold ? 0 : 255;
image.setRGB(x, y, (newGray << 16) | (newGray << 8) | newGray);
}
}
// 保存图像
ImageIO.write(image, "jpg", new File("enhanced_image.jpg"));
}
private static int getMedian(int[][] pixels) {
int[] array = new int[9];
int index = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
array[index++] = pixels[i][j];
}
}
Arrays.sort(array);
return array[4];
}
private static int getThreshold(BufferedImage image) {
int[] hist = new int[256];
int width = image.getWidth();
int height = image.getHeight();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int gray = image.getRGB(x, y) & 0xFF;
hist[gray]++;
}
}
int sum = 0;
int count = 0;
for (int i = 0; i < 256; i++) {
sum += i * hist[i];
count += hist[i];
}
int mean = sum / count;
int threshold = mean;
int delta = 128;
while (delta > 1) {
int count1 = 0;
int sum1 = 0;
for (int i = 0; i < threshold; i++) {
count1 += hist[i];
sum1 += i * hist[i];
}
int mean1 = sum1 / count1;
int count2 = 0;
int sum2 = 0;
for (int i = threshold; i < 256; i++) {
count2 += hist[i];
sum2 += i * hist[i];
}
int mean2 = sum2 / count2;
threshold = (mean1 + mean2) / 2;
delta = Math.abs(threshold - mean);
mean = (mean + threshold) / 2;
}
return threshold;
}
}
```
以上代码实现了对灰度图像的图像增强操作,具体包括直方图均衡化、中值滤波、边缘增强和二值化等步骤。可以根据需要选择不同的算法进行边缘增强操作。