java opencv实现扫描图像去底色增强文字

       现在市面上有很多小型打印机,采用用手机拍摄文档图像后,通过蓝牙连接打印机,打印出来。但是手机拍摄出来的图像往往带有灰或者黄底色,影响打印效果。如下图所示。

经过处理后效果如下:

 

 

 

具体代码如下:

 private void ReduceBackGround() {
        Mat src = new Mat();
        Utils.bitmapToMat(selectbp, src);
        Imgproc.cvtColor(src, src, Imgproc.COLOR_BGR2GRAY);
        src.convertTo(src, CvType.CV_32FC1, 1.0 / 255);
        Mat dst3=ReduceBackGroundAlgorithm(src,0);
        Imgproc.GaussianBlur(dst3, dst3, new Size(1, 1), 0, 0, 4); //Size(width, heigth),width,heigth参数可调,两个参数相同,默认值1,调节范围1-9(需为奇数);
            //dst3 = ColorGradation(dst3);
        Imgproc.adaptiveThreshold(dst3,dst3,255,0,0,31,30);//去除背景色后再进一步二值化,C参数可调,默认值30,调节范围1-50;   
        Utils.matToBitmap(dst3, selectbptmp);
        myImageView.setImageBitmap(selectbptmp);
    }
    private Mat ImageSharp(Mat src,int nAmount)
    {
        Mat dst= new Mat();
        double sigma = 3;
       // int threshold = 1;
        float amount = nAmount / 100.0f;
        Mat imgBlurred=new Mat();
        Imgproc.GaussianBlur(src, imgBlurred, new Size(7,7), sigma, sigma,4);
        Mat temp_sub= new Mat();
        //Mat temp_abs= new Mat();
        Core.subtract(src,imgBlurred,temp_sub);
       // Core.convertScaleAbs(temp_sub,temp_abs);
       // Mat lowContrastMask = new Mat();
        //Imgproc.threshold(temp_abs,lowContrastMask,threshold,255,1);
        //Mat temp_gen= new Mat();
        Core.addWeighted(src,1,temp_sub,amount,0,dst);
       // dst = src+temp_sub*amount;
        //src.copyTo(dst, lowContrastMask);
        return dst;
    }
    private Mat ReduceBackGroundAlgorithm(Mat src,int flag) {
        Mat gauss = new Mat();
        Mat dst2 = new Mat();
        Mat dst3 = new Mat();
        if (flag==1) {
            Imgproc.GaussianBlur(src, gauss, new Size(31, 31), 0, 0, 4);
        }
        else
            {
            Imgproc.blur(src, gauss, new Size(101,101));
        }
        Core.divide(src,gauss,dst2);
        dst2=ImageSharp(dst2, 101);
        //Imgproc.GaussianBlur(dst2, dst2, new Size(3,3), 0,0,4);//
        dst2.convertTo(dst3, CvType.CV_8UC1,255);
        return dst3;
    }

github链接:https://github.com/zhouqun92/TextEnhanceForAndroid

评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值