Java-bufferedimage Image Verification code for removing interference lines (for OCR tesseract Image Smart character recognition)

Source: Internet
Author: User
Tags pow

Recently the work needs to do a picture verification code automatic recognition function. But the internet for the original image processing methods have to noise, gray, and so on, but difficult to find the way to remove the interference line. So according to the code found on the Internet, I tried to write a paragraph, the pro-test effective, can be more clean to remove interference lines, improve the accuracy of OCR recognition. The following code in addition to "remove interference lines" "a small paragraph for the original , the others are online search results, but I am sorry I forgot the source URL, if you can find a replacement." First of all, thank the predecessors who generously shared the original code.

Demo as follows:

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 * need to be noisy image * @param destdir * Denoising Image Save Address * @throws IOException */public static void Cleanlinesini  
            Mage (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 ();  
            Gray 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);  
                    Image highlight (adjust brightness recognition rate is very high) int r = (int) ((ARGB >>) & 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);  
            }///two value 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]); }///Remove interference line for (int y = 1; y < h-1; y++) {for (int x = 1; x &l T W-1;
                    X + +) {Boolean flag = false; if (Isblack (Binarybufferedimage.getrgb (x, y)) {//left or right is empty, remove this point if (iswhite) (bi Narybufferedimage.getrgb (x-1, y)) && iswhite (Binarybufferedimage.getrgb (x+1, y))) {FLA
                        G = true; Remove this point if Iswhite (Binarybufferedimage.getrgb (x, y+1)) &&
                        Iswhite (Binarybufferedimage.getrgb (x, y-1))) {flag = true; Remove this point if (Iswhite (x-1, y+1) &&amp) when the//ramp is empty (BINARYBUFFEREDIMAGE.GETRGB) ;
             Iswhite (Binarybufferedimage.getrgb (x+1, y-1))) {flag = true;           if (Iswhite Binarybufferedimage.getrgb (x+1, y+1)) && Iswhite (Binarybufferedima
                        Ge.getrgb (X-1, y-1))) {flag = true;                     
                        } if (flag) {Binarybufferedimage.setrgb (x,y,-1); ()}}//Matrix print for ( int y = 0; Y < H; y++) {for (int x = 0; x < W x + +) {if (Isblac  
                    K (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 Tru  
            E  
        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) < 30 ||  
            Getcolorbright (Colorint) > 730) {return 1;  
        return 0; 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 = 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) {varm  
                    Ax = Varbetween;  
                threshold = t;  
        } return threshold; }  
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.