Java图像处理:实现旋转、边缘检测、锐化、对比度增强和风格化

本文介绍如何使用Java实现对图像的常见操作,包括旋转、边缘检测、锐化、对比度增强和风格化。

代码实现javaimport javax.imageio.ImageIO; // 导入Java图像处理库import java.awt.image.BufferedImage; // 导入Java图像处理库import java.io.File; // 导入Java文件处理库import java.io.IOException; // 导入Java异常处理库

public class ImageProcessor { // 定义一个名为ImageProcessor的类 public static void main(String[] args) throws IOException { // 程序的入口函数 BufferedImage image = ImageIO.read(new File('image.jpg')); // 读入一张名为image.jpg的图片 // 旋转图像 BufferedImage rotatedImage = rotateImage(image, 90); // 对图片进行90度旋转 ImageIO.write(rotatedImage, 'jpg', new File('rotatedImage.jpg')); // 将旋转后的图片保存到名为rotatedImage.jpg的文件中 // 边缘检测 BufferedImage edgeDetectedImage = detectEdges(image); // 对图片进行边缘检测 ImageIO.write(edgeDetectedImage, 'jpg', new File('edgeDetectedImage.jpg')); // 将边缘检测后的图片保存到名为edgeDetectedImage.jpg的文件中 // 图像锐化 BufferedImage sharpenedImage = sharpenImage(image); // 对图片进行图像锐化 ImageIO.write(sharpenedImage, 'jpg', new File('sharpenedImage.jpg')); // 将锐化后的图片保存到名为sharpenedImage.jpg的文件中 // 对比度增强 BufferedImage contrastEnhancedImage = enhanceContrast(image); // 对图片进行对比度增强 ImageIO.write(contrastEnhancedImage, 'jpg', new File('contrastEnhancedImage.jpg')); // 将对比度增强后的图片保存到名为contrastEnhancedImage.jpg的文件中 // 图像风格化 BufferedImage stylizedImage = stylizeImage(image); // 对图片进行图像风格化 ImageIO.write(stylizedImage, 'jpg', new File('stylizedImage.jpg')); // 将风格化后的图片保存到名为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()); // 创建一个新的BufferedImage对象,用于存储旋转后的图片        for (int i = 0; i < width; i++) { // 遍历图片的每一个像素点            for (int j = 0; j < height; j++) {                rotatedImage.setRGB(height - 1 - j, i, image.getRGB(i, j)); // 将旋转后的像素点存储到新的BufferedImage对象中            }        }        return rotatedImage; // 返回旋转后的图片    }

public static BufferedImage detectEdges(BufferedImage image) { // 实现边缘检测的函数        int width = image.getWidth(); // 获取图片的宽度        int height = image.getHeight(); // 获取图片的高度        BufferedImage edgeDetectedImage = new BufferedImage(width, height, image.getType()); // 创建一个新的BufferedImage对象,用于存储边缘检测后的图片        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); // 将梯度值存储到新的BufferedImage对象中            }        }        return edgeDetectedImage; // 返回边缘检测后的图片    }

public static BufferedImage sharpenImage(BufferedImage image) { // 实现图像锐化的函数        int width = image.getWidth(); // 获取图片的宽度        int height = image.getHeight(); // 获取图片的高度        BufferedImage sharpenedImage = new BufferedImage(width, height, image.getType()); // 创建一个新的BufferedImage对象,用于存储锐化后的图片        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); // 将锐化值存储到新的BufferedImage对象中            }        }        return sharpenedImage; // 返回锐化后的图片    }

public static BufferedImage enhanceContrast(BufferedImage image) { // 实现对比度增强的函数        int width = image.getWidth(); // 获取图片的宽度        int height = image.getHeight(); // 获取图片的高度        BufferedImage contrastEnhancedImage = new BufferedImage(width, height, image.getType()); // 创建一个新的BufferedImage对象,用于存储对比度增强后的图片        int[] histogram = new int[256]; // 创建一个长度为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]; // 创建一个长度为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); // 将新的灰度值存储到新的BufferedImage对象中            }        }        return contrastEnhancedImage; // 返回对比度增强后的图片    }

public static BufferedImage stylizeImage(BufferedImage image) { // 实现图像风格化的函数        int width = image.getWidth(); // 获取图片的宽度        int height = image.getHeight(); // 获取图片的高度        BufferedImage stylizedImage = new BufferedImage(width, height, image.getType()); // 创建一个新的BufferedImage对象,用于存储风格化后的图片        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); // 将风格化值存储到新的BufferedImage对象中            }        }        return stylizedImage; // 返回风格化后的图片    }}

代码说明

该程序首先使用ImageIO.read()方法读取名为image.jpg的图像文件,并将其存储在BufferedImage对象中。然后,程序分别调用rotateImage()detectEdges()sharpenImage()enhanceContrast()stylizeImage()方法对图像进行旋转、边缘检测、锐化、对比度增强和风格化操作。最后,程序使用ImageIO.write()方法将处理后的图像保存到新的文件中。

总结

本文介绍了如何使用Java实现对图像的常见操作,并对代码进行了详细的注释说明。您可以根据自己的需要修改和扩展此代码,以实现更复杂的图像处理功能。

Java图像处理:实现旋转、边缘检测、锐化、对比度增强和风格化

原文地址: https://www.cveoy.top/t/topic/f1KU 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录