图像灰度、二值化、膨胀和腐蚀(纯Java篇)

在c、c++或者python环境中可以使用opencv非常容易实现图像灰度、二值化、膨胀和腐蚀等功能;但在java环境中,使用opencv来处理相对比较麻烦,简单的操作用opencv处理也相对过重,这里记录下java版图片相关处理。

原图:

图像灰度、二值化、膨胀和腐蚀(纯Java篇)_第1张图片

1、灰度图

public static void main(String[] args) throws Exception {

        File file = new File("test.png");
        BufferedImage image = ImageIO.read(file);
        BufferedImage bi = grayImage(image);
        ImageIO.write(bi, "jpg", new File("gray.jpg"));
    }

public static BufferedImage grayImage(BufferedImage bufferedImage) {

        int width = bufferedImage.getWidth();
        int height = bufferedImage.getHeight();

        BufferedImage grayBufferedImage = new BufferedImage(width, height, bufferedImage.getType());
        for (int i = 0; i < bufferedImage.getWidth(); i++) {
            for (int j = 0; j < bufferedImage.getHeight(); j++) {
                final int color = bufferedImage.getRGB(i, j);
                final int r = (color >> 16) & 0xff;
                final int g = (color >> 8) & 0xff;
                final int b = color & 0xff;
                int gray = (int) (0.3 * r + 0.59 * g + 0.11 * b);
                int newPixel = colorToRGB(255, gray, gray, gray);
                grayBufferedImage.setRGB(i, j, newPixel);
            }
        }
        return grayBufferedImage;
    }

private static int colorToRGB(int alpha, int red, int green, int blue) {
        int newPixel = 0;
        newPixel += alpha;
        newPixel = newPixel << 8;
        newPixel += red;
        newPixel = newPixel << 8;
        newPixel += green;
        newPixel = newPixel << 8;
        newPixel += blue;

        return newPixel;

    }

图像灰度、二值化、膨胀和腐蚀(纯Java篇)_第2张图片

 2、二值化

    bi = binaryImage(bi, 130);
    ImageIO.write(bi, "jpg", new File("binary.jpg"));
    
    public static BufferedImage binaryImage(BufferedImage image, int threshold) {
        int w = image.getWidth();
        int h = image.getHeight();

        int black = new Color(0, 0, 0).getRGB();
        int white = new Color(255, 255, 255).getRGB();

        BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_BINARY);
        for (int x = 0; x < w; x++) {
            for (int y = 0; y < h; y++) {
                int rgb = image.getRGB(x, y);
                int gray = rgb & 0xff;
                if (gray < threshold) {
                    bi.setRGB(x, y, black);
                } else {
                    bi.setRGB(x, y, white);
                }
            }
        }
        return bi;
    }

图像灰度、二值化、膨胀和腐蚀(纯Java篇)_第3张图片

 3、膨胀腐蚀

    int[] kernel = new int[]{2, 2};
    bi = erode(bi, kernel);
    ImageIO.write(bi, "jpg", new File("erode.jpg"));


    public static BufferedImage erode(BufferedImage image, int[] kernel) {
        int black = new Color(0, 0, 0).getRGB();
        int white = new Color(255, 255, 255).getRGB();
        int w = image.getWidth();
        int h = image.getHeight();
        for (int x = 0; x < w; x++) {
            for (int y = 0; y < h; y++) {
                int min = 255;
                for (int i = x; i < x + kernel[0]; i++) {
                    for (int j = y; j < y + kernel[1]; j++) {
                        if (i >= 0 && i < w && j >= 0 && j < h) {
                            int value = image.getRGB(i, j) & 0xff;
                            if (value < min) {
                                min = value;
                            }
                        }
                    }
                }
                if (min == 255) {
                    image.setRGB(x, y, white);
                } else {
                    image.setRGB(x, y, black);
                }
            }
        }
        return image;
    }

图像灰度、二值化、膨胀和腐蚀(纯Java篇)_第4张图片

 

你可能感兴趣的:(java,计算机视觉)