Binarize Image in Android

后端 未结 7 977
耶瑟儿~
耶瑟儿~ 2020-12-17 03:11

I am developing android app that uses tesseract OCR to scan a text from image,
I heard that binarizing image before performing OCR on it will give better result,
So

相关标签:
7条回答
  • 2020-12-17 04:17

    I had a similar project involving colors, although on another platform.

    Although they might be other better algorithms, I used a function (GetColorDistance) to calculate the distances between two colors, in the 3D RGB space, by using the Pythagorean theorem. GetNewColor calculates whether the a color is closer to white or black, and then returns a black or white accordingly. Finally, the GetBitmapBinary function processes the pixels on the bitmap and converts them into black & white.

    private Bitmap GetBinaryBitmap(Bitmap bitmap_src)
        {
            Bitmap bitmap_new=bitmap_src.copy(bitmap_src.getConfig(), true);
    
    
    
        for(int x=0; x<bitmap_new.getWidth(); x++)
        {
            for(int y=0; y<bitmap_new.getHeight(); y++)
            {
                int color=bitmap_new.getPixel(x, y);
                color=GetNewColor(color);
                bitmap_new.setPixel(x, y, color);
            }
        }
    
        return bitmap_new;
    }
    
    
    private double GetColorDistance(int c1, int c2)
    {
        int db=Color.blue(c1)-Color.blue(c2);
        int dg=Color.green(c1)-Color.green(c2);
        int dr=Color.red(c1)-Color.red(c2);
    
    
        double d=Math.sqrt(  Math.pow(db, 2) + Math.pow(dg, 2) +Math.pow(dr, 2)  );
        return d;
    }
    
    private int GetNewColor(int c)
    {
        double dwhite=GetColorDistance(c,Color.WHITE);
        double dblack=GetColorDistance(c,Color.BLACK);
    
        if(dwhite<=dblack)
        {
            return Color.WHITE;
    
        }
        else
        {
            return Color.BLACK;
        }
    
    
    }
    

    You can modify the GetNewColor function for better results in different light densities. For example, you may multiply dblack by 1.5 so that darker pixels can become white, in a dark environment.

    0 讨论(0)
提交回复
热议问题