图像处理学习笔记(8)—— 图像灰度化与二值化

1 灰度化

  1. 平均值法 : (R + G + B)/ 3
  2. 最大最小值法 : (Max(R,G,B) + Min(R,G,B)) / 2
  3. 权重法:0.299 * R + 0.587 * G + 0.144 * B

2 二值化

  • 基于平均值
  • 经验值 127

3 灰度化测试

  • GrayFilter
package demo2.gray;

import demo2.utils.AbstractImageOptionFilter;

import java.awt.image.BufferedImage;

public class GrayFilter extends AbstractImageOptionFilter {

    public final static int MEANS_RGB = 1;
    public final static int MIN_MAX_RGB = 2;
    public final static int WEIGHT_RGB = 3;
    public final static double MEANS_WEIGHT = 1.0 / 3.0;

    private int method;

    public GrayFilter() {
        method = MEANS_RGB;
    }

    public int getMethod() {
        return method;
    }

    public void setMethod(int method) {
        this.method = method;
    }

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

        BufferedImage dest = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        int[] output = new int[width * height];

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

                if (method == MEANS_RGB) {
                    gray = (int) ((r + g + b) * MEANS_WEIGHT);
                } else if (method == MIN_MAX_RGB) {
                    int max = Math.max(Math.max(r, g), b);
                    int min = Math.min(Math.min(r, g), b);
                    gray = (int) ((max + min) / 2.0);
                } else if (method == MIN_MAX_RGB) {
                    gray = (int) (0.299 * r + 0.587 * g + 0.114 * b);
                }
                output[index] = (0xff << 24) | ((gray & 0xff) << 16) | ((gray & 0xff) << 8) | (gray & 0xff);
            }
        }

        setRGB(dest, 0, 0, width, height, output);
        return dest;
    }

}

  • ImagePanel
package demo2.gray;

import demo2.contrast.ContrastAdjust;

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() {
        GrayFilter filter = new GrayFilter();
        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(600, 600);
            frame.setTitle("图像显示测试");
            frame.setVisible(true);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


}

图像处理学习笔记(8)—— 图像灰度化与二值化_第1张图片

4 二值化测试

  • BinaryFilter
package demo2.gray;

import java.awt.*;
import java.awt.image.BufferedImage;

public class BinaryFilter extends GrayFilter {

    @Override
    public BufferedImage process(BufferedImage src) {
        BufferedImage grayImage = super.process(src);

        int width = grayImage.getWidth();
        int height = grayImage.getHeight();
        int[] pixels = new int[width * height];
        getRGB(grayImage, 0, 0, width, height, pixels);
        BufferedImage dest = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

        int[] out = new int[width * height];

        int means = 0;
        long sum = 0;
        int index = 0;
        int r = 0;

        for (int row = 0; row < height; row++) {
            for (int col = 0; col < width; col++) {
                index = row * width + col;
                r = (pixels[index] >> 16) & 0xff;
                sum += r;
            }
        }

        means = (int) (sum / pixels.length);
        for (int row = 0; row < height; row++) {
            for (int col = 0; col < width; col++) {
                index = row * width + col;
                r = (pixels[index] >> 16) & 0xff;
                if (r >= means) {
                    out[index] = Color.WHITE.getRGB();
                } else {
                    out[index] = Color.BLACK.getRGB();
                }
            }
        }

        setRGB(dest, 0, 0, width, height, out);
        return dest;
    }
}

  • ImagePanel
package demo2.gray;

import demo2.contrast.ContrastAdjust;

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();
        }

    }


}

图像处理学习笔记(8)—— 图像灰度化与二值化_第2张图片

你可能感兴趣的:(数字图像处理Java学习笔记)