Java 图片处理:旋转、边界检测、锐化、对比度增强、风格化
这是一个 Java 类,名为 ImageProcessor,主要对图片进行处理,包含以下方法:
-
main方法:程序入口,读取图片,调用其他方法对图片进行处理,输出处理后的图片。
-
rotateImage方法:对图片进行旋转,参数为旋转角度,返回旋转后的图片。
-
detectEdges方法:对图片进行边界检测,返回检测后的图片。
-
sharpenImage方法:对图片进行锐化处理,返回处理后的图片。
-
enhanceContrast方法:对图片进行对比度增强处理,返回处理后的图片。
-
stylizeImage方法:对图片进行风格化处理,返回处理后的图片。
在每个方法中都有'TODO'注释,说明需要实现的功能,这些方法都是通过遍历图片的像素点,对每个像素点进行处理,最终得到处理后的图片。其中,detectEdges方法和enhanceContrast方法使用了直方图均衡化的方法来增强图像对比度。
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ImageProcessor {
public static void main(String[] args) throws IOException {
BufferedImage image = ImageIO.read(new File('image.jpg'));
// 旋转图像
BufferedImage rotatedImage = rotateImage(image, 90);
ImageIO.write(rotatedImage, 'jpg', new File('rotatedImage.jpg'));
// 边界检测
BufferedImage edgeDetectedImage = detectEdges(image);
ImageIO.write(edgeDetectedImage, 'jpg', new File('edgeDetectedImage.jpg'));
// 图像锐化
BufferedImage sharpenedImage = sharpenImage(image);
ImageIO.write(sharpenedImage, 'jpg', new File('sharpenedImage.jpg'));
// 对比度增强
BufferedImage contrastEnhancedImage = enhanceContrast(image);
ImageIO.write(contrastEnhancedImage, 'jpg', new File('contrastEnhancedImage.jpg'));
// 图像风格化
BufferedImage stylizedImage = stylizeImage(image);
ImageIO.write(stylizedImage, 'jpg', new File('stylizedImage.jpg'));
}
public static BufferedImage rotateImage(BufferedImage image, int degree) {
// TODO: 实现旋转图像的代码
int width = image.getWidth();
int height = image.getHeight();
BufferedImage rotatedImage = new BufferedImage(height, width, image.getType());
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
rotatedImage.setRGB(height - 1 - j, i, image.getRGB(i, j));
}
}
return rotatedImage;
}
public static BufferedImage detectEdges(BufferedImage image) {
// TODO: 实现边界检测的代码
int width = image.getWidth();
int height = image.getHeight();
BufferedImage edgeDetectedImage = new BufferedImage(width, height, image.getType());
for (int i = 1; i < width - 1; i++) {
for (int j = 1; j < height - 1; j++) {
int gx = (image.getRGB(i + 1, j - 1) & 0xff) + 2 * (image.getRGB(i + 1, j) & 0xff) + (image.getRGB(i + 1, j + 1) & 0xff)
- (image.getRGB(i - 1, j - 1) & 0xff) - 2 * (image.getRGB(i - 1, j) & 0xff) - (image.getRGB(i - 1, j + 1) & 0xff);
int gy = (image.getRGB(i - 1, j + 1) & 0xff) + 2 * (image.getRGB(i, j + 1) & 0xff) + (image.getRGB(i + 1, j + 1) & 0xff)
- (image.getRGB(i - 1, j - 1) & 0xff) - 2 * (image.getRGB(i, j - 1) & 0xff) - (image.getRGB(i + 1, j - 1) & 0xff);
int g = (int) Math.sqrt(gx * gx + gy * gy);
edgeDetectedImage.setRGB(i, j, (g << 16) | (g << 8) | g);
}
}
return edgeDetectedImage;
}
public static BufferedImage sharpenImage(BufferedImage image) {
// TODO: 实现图像锐化的代码
int width = image.getWidth();
int height = image.getHeight();
BufferedImage sharpenedImage = new BufferedImage(width, height, image.getType());
for (int i = 1; i < width - 1; i++) {
for (int j = 1; j < height - 1; j++) {
int c = 5 * image.getRGB(i, j) - image.getRGB(i - 1, j) - image.getRGB(i + 1, j)
- image.getRGB(i, j - 1) - image.getRGB(i, j + 1);
sharpenedImage.setRGB(i, j, c);
}
}
return sharpenedImage;
}
public static BufferedImage enhanceContrast(BufferedImage image) {
// TODO: 实现对比度增强的代码
int width = image.getWidth();
int height = image.getHeight();
BufferedImage contrastEnhancedImage = new BufferedImage(width, height, image.getType());
int[] histogram = new int[256];
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
int c = image.getRGB(i, j) & 0xff;
histogram[c]++;
}
}
int totalPixels = width * height;
float[] cumulativeHistogram = new float[256];
cumulativeHistogram[0] = histogram[0] / (float) totalPixels;
for (int i = 1; i < 256; i++) {
cumulativeHistogram[i] = cumulativeHistogram[i - 1] + histogram[i] / (float) totalPixels;
}
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
int c = image.getRGB(i, j) & 0xff;
int newC = (int) (255 * cumulativeHistogram[c]);
contrastEnhancedImage.setRGB(i, j, (newC << 16) | (newC << 8) | newC);
}
}
return contrastEnhancedImage;
}
public static BufferedImage stylizeImage(BufferedImage image) {
// TODO: 实现图像风格化的代码
int width = image.getWidth();
int height = image.getHeight();
BufferedImage stylizedImage = new BufferedImage(width, height, image.getType());
for (int i = 1; i < width - 1; i++) {
for (int j = 1; j < height - 1; j++) {
int c = (image.getRGB(i - 1, j - 1) + image.getRGB(i - 1, j) + image.getRGB(i - 1, j + 1)
+ image.getRGB(i, j - 1) + 4 * image.getRGB(i, j) + image.getRGB(i, j + 1)
+ image.getRGB(i + 1, j - 1) + image.getRGB(i + 1, j) + image.getRGB(i + 1, j + 1)) / 12;
stylizedImage.setRGB(i, j, c);
}
}
return stylizedImage;
}
}
原文地址: https://www.cveoy.top/t/topic/f1KT 著作权归作者所有。请勿转载和采集!