二值图像分析笔记(2)—— 基于均值的图像二值化

1 基于均值的图像二值化

  • 抽象类 AbstractImageOptionFilter
package binimage.utils;

import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;

public abstract class AbstractImageOptionFilter {

    public abstract BufferedImage process(BufferedImage image);

    public static BufferedImage createCompatibleDestImage(BufferedImage src, ColorModel dstCM) {
        if (dstCM == null)
            dstCM = src.getColorModel();
        return new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster(src.getWidth(), src.getHeight()),
                dstCM.isAlphaPremultiplied(), null);
    }


    public static int clamp(int value) {
        return value < 0  ? 0 : ((value > 255) ?  255 :  value);
    }

    /**
     * A convenience method for getting ARGB pixels from an image. This tries to
     * avoid the performance penalty of BufferedImage.getRGB unmanaging the
     * image.
     */
    public static int[] getRGB(BufferedImage image, int x, int y, int width, int height, int[] pixels) {
        int type = image.getType();
        if (type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB)
            return (int[]) image.getRaster().getDataElements(x, y, width, height, pixels);
        return image.getRGB(x, y, width, height, pixels, 0, width);
    }

    /**
     * A convenience method for setting ARGB pixels in an image. This tries to
     * avoid the performance penalty of BufferedImage.setRGB unmanaging the
     * image.
     */
    public static void setRGB(BufferedImage image, int x, int y, int width, int height, int[] pixels) {
        int type = image.getType();
        if (type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB)
            image.getRaster().setDataElements(x, y, width, height, pixels);
        else
            image.setRGB(x, y, width, height, pixels, 0, width);
    }

}

  • BinaryFilter
package binimage.binary;

import binimage.utils.AbstractImageOptionFilter;

import java.awt.image.BufferedImage;

public class BinaryFilter extends AbstractImageOptionFilter {

    private static final double WIGHT = 1.0 / 3.0;

    @Override
    public BufferedImage process(BufferedImage image) {
        int width = image.getWidth();
        int height = image.getHeight();
        int[] pixels = new int[width * height];
        getRGB(image, 0, 0, width, height, pixels);

        int r = 0, g = 0, b = 0;
        int offset;
        for (int row = 0; row < height; row++) {
            offset = row * width;
            for (int col = 0; col < width; col++) {
                r = (pixels[offset + col] >> 16) & 0xff;
                g = (pixels[offset + col] >> 8) & 0xff;
                b = (pixels[offset + col]) & 0xff;

                int gray = (int) ((r + g + b) * WIGHT);
                pixels[offset + col] = gray;

            }

        }

        int sum = 0;
        int count = pixels.length;
        for (int i = 0; i < pixels.length; i++) {
            sum += pixels[i];
        }

        int means = sum / count;

        // 二值化
        for (int i = 0; i < pixels.length; i++) {
            if (pixels[i] > means) {
                pixels[i] = (0xff << 24) | ((255 & 0xff) << 16) | ((255 & 0xff) << 8) | (255 & 0xff);
            } else {
                // 黑色
                pixels[i] = (0xff << 24) | ((0 & 0xff) << 16) | ((0 & 0xff) << 8) | (0 & 0xff);
            }
        }

        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

        setRGB(bi, 0, 0, width, height, pixels);

        return bi;

    }
}

  • ImagePanel
package binimage.binary;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ImagePanel extends JComponent implements ActionListener {

    private BufferedImage image;
   private BufferedImage resultImage;

    private JButton processBtn;

    public ImagePanel(BufferedImage image) {
        this.image = image;
    }

    public JButton getButton() {
        processBtn = new JButton("按钮");
        processBtn.addActionListener(this);

        return processBtn;
    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        if (null != image) {
            g2d.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
        }

        if(resultImage != null) {
            g2d.drawImage(resultImage, image.getWidth() + 10, 0, resultImage.getWidth(), resultImage.getHeight(), null);
        }

    }

    public void process() {
        BinaryFilter filter = new BinaryFilter();
        resultImage = filter.process(this.image);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == processBtn) {
            this.process();
            this.repaint();
        }
    }


    public static void main(String[] args) {
        File file = new File("resource/color.png");

        try {
            BufferedImage image = ImageIO.read(file);

            ImagePanel imp = new ImagePanel(image);
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(imp, BorderLayout.CENTER);
            frame.getContentPane().add(imp.getButton(), BorderLayout.SOUTH);
            frame.setSize(1000, 600);
            frame.setTitle("图像显示测试");
            frame.setVisible(true);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值