参考:https://blog.csdn.net/jkjj2015/article/details/87160291
现在市面上有很多小型打印机,采用用手机拍摄文档图像后,通过蓝牙连接打印机,打印出来。但是手机拍摄出来的图像往往带有灰或者黄底色,影响打印效果。如下图所示。
经过处理后效果如下:
具体代码如下:
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