zoukankan      html  css  js  c++  java
  • 指纹识别-图像二值化

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /**
     *
     * @author charles
     */
    
    import java.awt.Color;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    
    public class ImageTest {
    
        public static void main(String[] args) throws IOException {
            String filename = "C:\Users\USER\Pictures\1.png";
            File file = new File(filename);
            BufferedImage bi = ImageIO.read(file);
            // 获取当前图片的高,宽,ARGB
            int h = bi.getHeight();
            int w = bi.getWidth();
            int rgb = bi.getRGB(0, 0);
            int arr[][] = new int[w][h];
    
            // 获取图片每一像素点的灰度值
            for (int i = 0; i < w; i++) {
                for (int j = 0; j < h; j++) {
                    // getRGB()返回默认的RGB颜色模型(十进制)
                    arr[i][j] = getImageRgb(bi.getRGB(i, j));//该点的灰度值 BufferedImage里的getRGB得到的是一个ARGB,A代表透明,R代表红色,G代表绿色,B代表蓝色。
                }
    
            }
    
            BufferedImage bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_BINARY);//  构造一个类型为预定义图像类型之一的 BufferedImage,TYPE_BYTE_BINARY(表示一个不透明的以字节打包的 1、2 或 4 位图像。)
            int FZ = 100;//2、定义一个阀值0-255。
            for (int i = 0; i < w; i++) {
                for (int j = 0; j < h; j++) {
                    if (getGray(arr, i, j, w, h) > FZ) {
                        int black = new Color(255, 255, 255).getRGB();
                        bufferedImage.setRGB(i, j, black);
                    } else {
                        int white = new Color(0, 0, 0).getRGB();
                        bufferedImage.setRGB(i, j, white);
                    }
                }
    
            }
            ImageIO.write(bufferedImage, "png", new File("D:" + File.separator + "new123.png"));
        }
    
        private static int getImageRgb(int i) {
            String argb = Integer.toHexString(i);// 将十进制的颜色值转为十六进制
            // argb分别代表透明,红,绿,蓝 分别占16进制2位
            int r = Integer.parseInt(argb.substring(2, 4), 16);//后面参数为使用进制
            int g = Integer.parseInt(argb.substring(4, 6), 16);
            int b = Integer.parseInt(argb.substring(6, 8), 16);
            int result = (int) ((r + g + b) / 3);
            return result;
        }
    
        //自己加周围8个灰度值再除以9,算出其相对灰度值 
        public static int getGray(int gray[][], int x, int y, int w, int h) {
            int rs = gray[x][y]
                    + (x == 0 ? 255 : gray[x - 1][y])//左边
                    + (x == 0 || y == 0 ? 255 : gray[x - 1][y - 1])//upper left
                    + (x == 0 || y == h - 1 ? 255 : gray[x - 1][y + 1])//low left
                    + (y == 0 ? 255 : gray[x][y - 1])//middle upper
                    + (y == h - 1 ? 255 : gray[x][y + 1])//middle lower
                    + (x == w - 1 ? 255 : gray[x + 1][y])//righte
                    + (x == w - 1 || y == 0 ? 255 : gray[x + 1][y - 1])
                    + (x == w - 1 || y == h - 1 ? 255 : gray[x + 1][y + 1]);
            return rs / 9;
        }
    }
    

  • 相关阅读:
    表连接 索引
    数据库类型
    Java重试机制
    Result Maps collection already contains value for xxxMapper.BaseResultMap错误解决办法
    Lombok插件
    REST以及RESTful
    Dubbo
    【日志】MySQL中有多少种日志
    【缓存池】转-Mysql参数innodb_buffer_pool_size
    【MRR】转-MySQL 的 MRR 优化
  • 原文地址:https://www.cnblogs.com/mrcharles/p/11879822.html
Copyright © 2011-2022 走看看