天天看點

java-BufferedImage 圖檔驗證碼去除幹擾線的方法( 用于OCR tesseract圖像智能字元識别)

最近工作需要做了一下圖檔驗證碼自動識别的功能。但是網上對于初始圖檔的處理方法有去噪點、灰階化等,唯獨難搜到去除幹擾線的方法。于是根據網上搜來的代碼,自己嘗試寫了一段,親測有效,可以比較幹淨地去除幹擾線,提高OCR識别的準确率。

  • 以下代碼除“去除幹擾線條“”一小段為原創,其他均為網上搜尋所得,但是很抱歉我忘記了來源網址,以後如果能找到再補上。在此先謝過慷慨分享原始代碼的前輩!

demo如下:

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;


import javax.imageio.ImageIO;


public class CopyOfCleanLines {

      public static void main(String[] args) throws IOException  
        {    
            File testDataDir = new File("imgWithLines");  
            final String destDir = testDataDir.getAbsolutePath()+"/tmp";  
            for (File file : testDataDir.listFiles())  
            {  
                cleanLinesInImage(file, destDir);  
                cleanLinesInImage(file, destDir); 
                cleanLinesInImage(file, destDir);
            }  
        }  

      /** 
         *  
         * @param sfile 
         *            需要去噪的圖像 
         * @param destDir 
         *            去噪後的圖像儲存位址 
         * @throws IOException 
         */  
        public static void cleanLinesInImage(File sfile, String destDir)  throws IOException{  
            File destF = new File(destDir);  
            if (!destF.exists())  
            {  
                destF.mkdirs();  
            }  

            BufferedImage bufferedImage = ImageIO.read(sfile);  
            int h = bufferedImage.getHeight();  
            int w = bufferedImage.getWidth();  

            // 灰階化  
            int[][] gray = new int[w][h];  
            for (int x = ; x < w; x++)  
            {  
                for (int y = ; y < h; y++)  
                {  
                    int argb = bufferedImage.getRGB(x, y);  
                    // 圖像加亮(調整亮度識别率非常高)  
                    int r = (int) (((argb >> ) & ) *  + );  
                    int g = (int) (((argb >> ) & ) *  + );  
                    int b = (int) (((argb >> ) & ) *  + );  
                    if (r >= )  
                    {  
                        r = ;  
                    }  
                    if (g >= )  
                    {  
                        g = ;  
                    }  
                    if (b >= )  
                    {  
                        b = ;  
                    }  
                    gray[x][y] = (int) Math  
                            .pow((Math.pow(r, ) *  + Math.pow(g, )  
                                    *  + Math.pow(b, ) * ),  / );  
                }  
            }  

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

            //去除幹擾線條
            for(int y = ; y < h-; y++){
                for(int x = ; x < w-; x++){                   
                    boolean flag = false ;
                    if(isBlack(binaryBufferedImage.getRGB(x, y))){
                        //左右均為空時,去掉此點
                        if(isWhite(binaryBufferedImage.getRGB(x-, y)) && isWhite(binaryBufferedImage.getRGB(x+, y))){
                            flag = true;
                        }
                        //上下均為空時,去掉此點
                        if(isWhite(binaryBufferedImage.getRGB(x, y+)) && isWhite(binaryBufferedImage.getRGB(x, y-))){
                            flag = true;
                        }
                        //斜上下為空時,去掉此點
                        if(isWhite(binaryBufferedImage.getRGB(x-, y+)) && isWhite(binaryBufferedImage.getRGB(x+, y-))){
                            flag = true;
                        }
                        if(isWhite(binaryBufferedImage.getRGB(x+, y+)) && isWhite(binaryBufferedImage.getRGB(x-, y-))){
                            flag = true;
                        } 
                        if(flag){
                            binaryBufferedImage.setRGB(x,y,-);                     
                        }
                    }
                }
            }


            // 矩陣列印  
            for (int y = ; y < h; y++)  
            {  
                for (int x = ; x < w; x++)  
                {  
                    if (isBlack(binaryBufferedImage.getRGB(x, y)))  
                    {  
                        System.out.print("*");  
                    } else  
                    {  
                        System.out.print(" ");  
                    }  
                }  
                System.out.println();  
            }  

            ImageIO.write(binaryBufferedImage, "jpg", new File(destDir, sfile  
                    .getName()));  
        }  

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

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

        public static int isBlackOrWhite(int colorInt)  
        {  
            if (getColorBright(colorInt) <  || getColorBright(colorInt) > )  
            {  
                return ;  
            }  
            return ;  
        }  

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

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

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

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

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

            float varMax = ;  
            int threshold = ;  

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

                wF = total - wB; // Weight Foreground  
                if (wF == )  
                    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;  
        }  
}