去验证码干扰线-java

首先看看去干扰线的结果(java)

原始图片
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

去掉干扰线以后的效果
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

这里说下开发过程中遇到的问题
1.在网上使用了各种java类型的算法,直接对BufferedImage进行操作,但是都不理想
2.在使用Tesseract工具进行ocr识别的时候识别率也不高

解决第一个问题,我结合了网上的去干扰线算法,以及使用了opencv算法。使用的opencv也是借鉴一篇网上的博客。
解决第二个问题,是实用Tesseract工具针对我要识别的验证码进行独立的训练,而不是使用原始的训练数据进行识别,这样子可以明显的提升识别率。

源码

// 这里是调用的核心方法
public class ImageCleanPlanOpencv implements ImageClean{

    Logger logger = LoggerFactory.getLogger(ImageCleanPlanOpencv.class);

    public BufferedImage clean(BufferedImage oriBufferedImage) {
        try {
            BufferedImage cleanedBufferedImage = null;
            //这里可以看到去燥的方法反复调用了几次,是为了得更好的去干扰线结果,这里可以根据自己的验证码情况来编写调用的次数,必须是偶数次,因为opencv的api会进行图像反色
            cleanedBufferedImage = cleanLinesInImage(oriBufferedImage);
            cleanedBufferedImage=cleanLinesInImage(cleanedBufferedImage);
            cleanedBufferedImage=cleanLinesInImage(cleanedBufferedImage);
            cleanedBufferedImage=cleanLinesInImage(cleanedBufferedImage);
//            try {
//                ImageUtil.generateImage(cleanedBufferedImage, ImageConstant.url,"new_","");
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
            return cleanedBufferedImage;
        } catch (IOException e) {
            logger.error("去噪过程异常",e);
            e.printStackTrace();
        }
        return null;
    }

    /**
     *
     * @param oriBufferedImage 需要去噪的图像
     * @throws IOException
     */
    public BufferedImage cleanLinesInImage(BufferedImage oriBufferedImage)  throws IOException{

        BufferedImage bufferedImage = oriBufferedImage;
        int h = bufferedImage.getHeight();
        int w = bufferedImage.getWidth();

        // 灰度化
        int[][] gray = new int[w][h];
        for (int x = 0; x < w; x++)
        {
            for (int y = 0; y < h; y++)
            {
                int argb = bufferedImage.getRGB(x, y);
                // 图像加亮(调整亮度识别率非常高)
                int r = (int) (((argb >> 16) & 0xFF) * 1.1 + 30);
                int g = (int) (((argb >> 8) & 0xFF) * 1.1 + 30);
                int b = (int) (((argb >> 0) & 0xFF) * 1.1 + 30);
                if (r >= 255)
                {
                    r = 255;
                }
                if (g >= 255)
                {
                    g = 255;
                }
                if (b >= 255)
                {
                    b = 255;
                }
                gray[x][y] = (int) Math
                        .pow((Math.pow(r, 2.2) * 0.2973 + Math.pow(g, 2.2)
                                * 0.6274 + Math.pow(b, 2.2) * 0.0753), 1 / 2.2);
            }
        }

        // 二值化
        int threshold = ostu(gray, w, h);
        BufferedImage binaryBufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_BINARY);
        for (int x = 0; x < w; x++)
        {
            for (int y = 0; y < h; y++)
            {
                if (gray[x][y] > threshold)
                {
                    gray[x][y] |= 0x00FFFF;
                } else
                {
                    gray[x][y] &= 0xFF0000;
                }
                binaryBufferedImage.setRGB(x, y, gray[x][y]);
            }
        }


        //这里开始是利用opencv的api进行处理
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        Mat mat = bufferedImageToMat(binaryBufferedImage);
        //第一次用opencv,这里不太明白这个size对象如何使用,针对我要识别的验证码图片,调整为4,4的效果比较好,如果是不同的验证码图片,可以尝试先不用这段opencv的代码,或者微调这里的参数
        Mat kelner = Imgproc.getStructuringElement(MORPH_RECT, new Size(4, 4), new Point(-1, -1));
        //腐蚀
        Imgproc.erode(mat,mat,kelner);
        //膨胀
        Imgproc.dilate(mat,mat,kelner);
        //图像反色
        Core.bitwise_not(mat,mat);
        //去噪点
//        Imgproc.morphologyEx(mat,mat, MORPH_OPEN, kelner,new Point(-1,-1),1);
        binaryBufferedImage = mat2BufImg(mat,".png");

        cleanImage(binaryBufferedImage,h,w );


        return binaryBufferedImage;
    }

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

    public Mat bufferedImageToMat(BufferedImage bi) {
        Mat mat = new Mat(bi.getHeight(), bi.getWidth(), CvType.CV_8UC1);

        byte[] white = new byte[] { (byte) 255 };
        byte[] black = new byte[] { (byte) 0 };

        for (int x=0; xfor (int y=0; yif (bi.getRGB(x, y) == Color.BLACK.getRGB()) {
                    mat.put(y, x, black);
                } else {
                    mat.put(y, x, white);
                }
            }
        }
        return mat;
    }

    /**
     * Mat转换成BufferedImage
     *
     * @param matrix
     *            要转换的Mat
     * @param fileExtension
     *            格式为 ".jpg", ".png", etc
     * @return
     */
    public BufferedImage mat2BufImg (Mat matrix, String fileExtension) {
        // convert the matrix into a matrix of bytes appropriate for
        // this file extension
        MatOfByte mob = new MatOfByte();
        Imgcodecs.imencode(fileExtension, matrix, mob);
        // convert the "matrix of bytes" into a byte array
        byte[] byteArray = mob.toArray();
        BufferedImage bufImage = null;
        try {
            InputStream in = new ByteArrayInputStream(byteArray);
            bufImage = ImageIO.read(in);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bufImage;
    }

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

    public int isBlackOrWhite(int colorInt)
    {
        if (getColorBright(colorInt) < 30 || getColorBright(colorInt) > 730)
        {
            return 1;
        }
        return 0;
    }

    public int getColorBright(int colorInt)
    {
        Color color = new Color(colorInt);
        return color.getRed() + color.getGreen() + color.getBlue();
    }

    public int ostu(int[][] gray, int w, int h)
    {
        int[] histData = new int[w * h];
        // Calculate histogram
        for (int x = 0; x < w; x++)
        {
            for (int y = 0; y < h; y++)
            {
                int red = 0xFF & gray[x][y];
                histData[red]++;
            }
        }

        // Total number of pixels
        int total = w * h;

        float sum = 0;
        for (int t = 0; t < 256; t++)
            sum += t * histData[t];

        float sumB = 0;
        int wB = 0;
        int wF = 0;

        float varMax = 0;
        int threshold = 0;

        for (int t = 0; t < 256; t++)
        {
            wB += histData[t]; // Weight Background
            if (wB == 0)
                continue;

            wF = total - wB; // Weight Foreground
            if (wF == 0)
                break;

            sumB += (float) (t * histData[t]);

            float mB = sumB / wB; // Mean Background
            float mF = (sum - sumB) / wF; // Mean Foreground

            // Calculate Between Class Variance
            float varBetween = (float) wB * (float) wF * (mB - mF) * (mB - mF);

            // Check if new maximum found
            if (varBetween > varMax)
            {
                varMax = varBetween;
                threshold = t;
            }
        }

        return threshold;
    }



}

这段代码除开opencv那段是我个人编写的,其他的也是借鉴网上的源代码。如果有编写不对,或者可以修改的更好的建议请指出。

参考博客
https://blog.csdn.net/firehood_/article/details/8433077
https://blog.csdn.net/shengfn/article/details/53582694
http://pinnau.blogspot.com/2016/06/java-create-opencv-mat-from.html
https://blog.csdn.net/qianmang/article/details/79158366

你可能感兴趣的:(疑难杂症)