tesseract 二值化 去验证码干扰线

公司要求爬一个系统的数据,最近这个系统验证码图片增加了干扰线,遂找资料研究了下。
原图片:
在这里插入图片描述
去杂线效果:
在这里插入图片描述

参考博客在这里,非常感谢:参考链接←


    /**
     * 利用tesseract识别验证码
     */
    public String tens(InputStream image) {
        String result = "";
        try {
            BufferedImage imagetmp = ImageIO.read(image);
            
            //写入本地生成图片,测试效果用
            //ImageIO.write(imagetmp, "png", new File("D:\\last.png"));
            
            int width=imagetmp.getWidth();
            int height = imagetmp.getHeight();
            //二值化
            BufferedImage grayImage = new BufferedImage(imagetmp.getWidth(), imagetmp.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
            for (int i = 0; i <width; i++) {
                for (int j = 0; j < height; j++) {
                    int rgb = imagetmp.getRGB(i, j);
                    grayImage.setRGB(i, j, rgb);
                }
            }

            //去除干扰线条
        for(int y = 1; y < height-1; y++){
            for(int x = 1; x < width-1; x++){
                boolean flag = false ;
                if(isBlack(grayImage.getRGB(x, y))){
                    //左右均为空时,去掉此点
                    if(isWhite(grayImage.getRGB(x-1, y)) && isWhite(grayImage.getRGB(x+1, y))){
                        flag = true;
                    }
                    //上下均为空时,去掉此点
                    if(isWhite(grayImage.getRGB(x, y+1)) && isWhite(grayImage.getRGB(x, y-1))){
                        flag = true;
                    }
                    //斜上下为空时,去掉此点
                    if(isWhite(grayImage.getRGB(x-1, y+1)) && isWhite(grayImage.getRGB(x+1, y-1))){
                        flag = true;
                    }
                    if(isWhite(grayImage.getRGB(x+1, y+1)) && isWhite(grayImage.getRGB(x-1, y-1))){
                        flag = true;
                    }
                    if(flag){
                        grayImage.setRGB(x,y,-1);
                    }
                }
            }
        }

            //ImageIO.write(grayImage, "png", new File("D:\\after.png"));

			//识别验证码
            ITesseract instance = new Tesseract();
            instance.setDatapath(billDetailsPro.getTesseractPath());
            result = instance.doOCR(grayImage);
        } catch (Exception e) {
            log.error("验证码识别异常..",e);
        }
        return result.replace(" ", "").replace("\n", "");
    }

    public  boolean isBlack(int colorInt) {
        Color color = new Color(colorInt);
        if (color.getRed() + color.getGreen() + color.getBlue() <= 300) {
            return true;
        }
        return false;
    }

    public boolean isWhite(int colorInt) {
        Color color = new Color(colorInt);
        if (color.getRed() + color.getGreen() + color.getBlue() > 300) {
            return true;
        }
        return false;
    }

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值