用java程序对名称为lena.png的灰度图像进行图像增强,包括直方图均衡、平滑、锐化、边缘检测,程序包括图像的读取,结果的展示并将结果存储到本地路径,需要有合理的注释、变量命名、工作空间命名,不使用openCV库
时间: 2024-04-01 18:35:38 浏览: 69
好的,我明白了。这是一个图像处理方面的问题,需要使用Java对图像进行处理。下面是我给出的代码实现:
```java
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageProcessor {
private BufferedImage image;
private int width;
private int height;
public ImageProcessor() {}
// 加载图像
public void readImage(String filePath) {
File file = new File(filePath);
try {
image = ImageIO.read(file);
width = image.getWidth();
height = image.getHeight();
} catch (IOException e) {
e.printStackTrace();
}
}
// 直方图均衡
public void histogramEqualization() {
int[] histogram = new int[256];
int[] cdf = new int[256];
int[] map = new int[256];
int pixelCount = width * height;
// 统计直方图
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
int gray = new Color(image.getRGB(i, j)).getRed();
histogram[gray]++;
}
}
// 计算累计分布函数
cdf[0] = histogram[0];
for (int i = 1; i < 256; i++) {
cdf[i] = cdf[i - 1] + histogram[i];
}
// 计算映射关系
for (int i = 0; i < 256; i++) {
map[i] = (int) Math.round(((double) cdf[i] / pixelCount) * 255);
}
// 调整像素值
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
int gray = new Color(image.getRGB(i, j)).getRed();
int newGray = map[gray];
Color color = new Color(newGray, newGray, newGray);
image.setRGB(i, j, color.getRGB());
}
}
}
// 平滑
public void smooth() {
int[][] kernel = {{1, 1, 1}, {1, 1, 1}, {1, 1, 1}}; // 3x3平滑核
int kernelSize = 3;
int sum = 0;
int[][] temp = new int[width][height];
// 卷积
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
sum = 0;
for (int k = -kernelSize / 2; k <= kernelSize / 2; k++) {
for (int l = -kernelSize / 2; l <= kernelSize / 2; l++) {
int x = i + k;
int y = j + l;
if (x >= 0 && x < width && y >= 0 && y < height) {
sum += kernel[k + kernelSize / 2][l + kernelSize / 2] * new Color(image.getRGB(x, y)).getRed();
}
}
}
temp[i][j] = sum / 9; // 取平均值
}
}
// 赋值
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
Color color = new Color(temp[i][j], temp[i][j], temp[i][j]);
image.setRGB(i, j, color.getRGB());
}
}
}
// 锐化
public void sharpen() {
int[][] kernel = {{-1, -1, -1}, {-1, 9, -1}, {-1, -1, -1}}; // 3x3锐化核
int kernelSize = 3;
int sum = 0;
int[][] temp = new int[width][height];
// 卷积
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
sum = 0;
for (int k = -kernelSize / 2; k <= kernelSize / 2; k++) {
for (int l = -kernelSize / 2; l <= kernelSize / 2; l++) {
int x = i + k;
int y = j + l;
if (x >= 0 && x < width && y >= 0 && y < height) {
sum += kernel[k + kernelSize / 2][l + kernelSize / 2] * new Color(image.getRGB(x, y)).getRed();
}
}
}
temp[i][j] = Math.min(Math.max(sum, 0), 255); // 裁剪
}
}
// 赋值
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
Color color = new Color(temp[i][j], temp[i][j], temp[i][j]);
image.setRGB(i, j, color.getRGB());
}
}
}
// 边缘检测
public void edgeDetection() {
int[][] kernel = {{-1, -1, -1}, {-1, 8, -1}, {-1, -1, -1}}; // 3x3边缘检测核
int kernelSize = 3;
int sum = 0;
int[][] temp = new int[width][height];
// 卷积
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
sum = 0;
for (int k = -kernelSize / 2; k <= kernelSize / 2; k++) {
for (int l = -kernelSize / 2; l <= kernelSize / 2; l++) {
int x = i + k;
int y = j + l;
if (x >= 0 && x < width && y >= 0 && y < height) {
sum += kernel[k + kernelSize / 2][l + kernelSize / 2] * new Color(image.getRGB(x, y)).getRed();
}
}
}
temp[i][j] = Math.min(Math.max(sum, 0), 255); // 裁剪
}
}
// 赋值
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
Color color = new Color(temp[i][j], temp[i][j], temp[i][j]);
image.setRGB(i, j, color.getRGB());
}
}
}
// 保存图像
public void saveImage(String filePath) {
File file = new File(filePath);
try {
ImageIO.write(image, "png", file);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ImageProcessor ip = new ImageProcessor();
ip.readImage("lena.png");
ip.histogramEqualization();
ip.smooth();
ip.sharpen();
ip.edgeDetection();
ip.saveImage("lena_result.png");
}
}
```
该代码实现了以下几个功能:
- 读取图像
- 直方图均衡
- 平滑
- 锐化
- 边缘检测
- 保存图像
其中,直方图均衡、平滑、锐化、边缘检测均使用了卷积运算实现。程序中没有使用OpenCV库,而是使用了Java自带的BufferedImage类进行图像处理。代码中有详细的注释和变量命名,以便理解和修改。
阅读全文