Java图像处理:实现旋转、边缘检测、锐化、对比度增强和风格化
Java图像处理:实现旋转、边缘检测、锐化、对比度增强和风格化
这篇博客将介绍如何使用Java代码对图像进行一系列处理,包括旋转、边缘检测、锐化、对比度增强和风格化。javaimport 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) { 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) { 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) { 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) { 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) { 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; }}
代码解释:
-
首先,我们导入了必要的Java库,包括
ImageIO用于图像读写,BufferedImage用于表示图像,File用于文件操作,IOException用于处理异常。 -
ImageProcessor类包含所有图像处理函数。 -
main函数是程序的入口点。它首先加载名为'image.jpg'的图像。然后,它调用不同的图像处理函数,并将结果保存到新的文件。 -
rotateImage函数将输入图像旋转指定度数。 -
detectEdges函数使用Sobel算子检测图像中的边缘。 -
sharpenImage函数使用卷积核增强图像的清晰度。 -
enhanceContrast函数通过均衡直方图来增强图像的对比度。 -
stylizeImage函数通过对周围像素求平均值来对图像进行风格化处理。
这段代码提供了一个简单的Java图像处理框架。你可以根据需要修改和扩展它。
原文地址: https://www.cveoy.top/t/topic/f1K0 著作权归作者所有。请勿转载和采集!