public Bitmap removePoint(Bitmap srcBitmap) {
Bitmap bitmap = null;
try {
Mat scrMat = new Mat();
// 将Bitmap转换为Mat
Utils.bitmapToMat(srcBitmap, scrMat);
Imgproc.cvtColor(scrMat, scrMat, Imgproc.COLOR_BGR2GRAY);
// 应用阈值分割得到二值图像(假设白色像素为前景)
Mat binaryImage = new Mat();
Imgproc.threshold(scrMat, binaryImage, 100, 255, Imgproc.THRESH_BINARY);//不同的值 有去色 反转颜色等
// 初始化与输入图像同样大小的标签矩阵和统计信息矩阵
int numLabels = 0;//连通域数量
Mat labels = new Mat(binaryImage.size(), CV_32S);// labels是个1,2等数组,相同连通域数字一样,每一个像素的标签1、2、3.。。,同一个连通域的标签是一致的
Mat stats = new Mat();//连通域的信息:对应各个轮廓的x、y、width、height和面积
Mat centroids = new Mat();//连通域的中心点
// 计算连通组件及其统计信息
numLabels = Imgproc.connectedComponentsWithStats(binaryImage, labels, stats, centroids,8,CV_32S);
Log.i(TAG, "removePoint: numLabels=" + numLabels+" "+stats.size());
// 遍历统计信息数组,找到面积小于100的区域并将对应区域在原图上设置为背景色(通常是黑色或白色)
for (int i = 1; i < numLabels; i++) {
double val[]= stats.get(i, Imgproc.CC_STAT_AREA) ;
int area =(int) val[0] ;
if (area < 250) { // 如果面积小于100
Log.i(TAG, "removePoint:100 area=" + area );
Mat dst = new Mat();
// 将原图像中属于该区域的所有像素标记为背景色
Core.compare(labels, new Scalar(i), dst, Core.CMP_EQ);
//类似与threshold()函数,但是threshold()函数是对某个区间内的像素值进行操作,compare()函数则可以只是对某一个单独的像素值进行操作。比如我们要从图像中找出像素值为50的像素点,可以下面这样做:
// 将标记出来的区域置为0(假设是黑色背景)
binaryImage.setTo(new Scalar(0), dst);
}
}
// 将Mat对象转换为Bitmap
bitmap = Bitmap.createBitmap(binaryImage.cols(), binaryImage.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(binaryImage, bitmap);
} catch (CvException e) {
e.printStackTrace();
}
return bitmap;
}
public Bitmap medianBlur(Bitmap srcBitmap, int size) {
Bitmap bitmap = null;
try {
Mat scrMat = new Mat();
// 将Bitmap转换为Mat
Utils.bitmapToMat(srcBitmap, scrMat);
Mat dstMat = new Mat();
Imgproc.medianBlur(scrMat, dstMat, size);
Imgproc.cvtColor(dstMat, dstMat, Imgproc.COLOR_BGR2GRAY);
// 应用阈值分割得到二值图像(假设白色像素为前景)
Mat binaryImage = new Mat();
Imgproc.threshold(dstMat, dstMat, 80, 255, Imgproc.THRESH_BINARY);//不同的值 有去色 反转颜色等
// 将Mat对象转换为Bitmap
bitmap = Bitmap.createBitmap(dstMat.cols(), dstMat.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(dstMat, bitmap);
} catch (CvException e) {
e.printStackTrace();
}
Log.i(TAG, "medianBlur: ");
return bitmap;
}
public Bitmap noiseRemoval(Bitmap srcBitmap, int size) {
Mat image = new Mat();
// 将Bitmap转换为Mat
Utils.bitmapToMat(srcBitmap, image);
Mat out = new Mat();
Mat tmp = new Mat();
Mat kernel = new Mat(new Size(size, size), CvType.CV_8UC1, new Scalar(255));
// Mat kernel = new Mat(image.size(), CvType.CV_8UC1, new Scalar(255));
Imgproc.morphologyEx(image, tmp, Imgproc.MORPH_OPEN, kernel);
Imgproc.morphologyEx(tmp, out, Imgproc.MORPH_CLOSE, kernel);
// 将Mat对象转换为Bitmap
bitmap = Bitmap.createBitmap(out.cols(), out.rows(), Bitmap.Config.ARGB_8888);
// bitmap = Bitmap.createBitmap(openedImage.cols(), openedImage.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(out, bitmap);
Log.i(TAG, "denoise: ");
return bitmap;
}
public Bitmap erode(Bitmap bitmap) {
// Bitmap转为Mat
Mat src = new Mat(bitmap.getHeight(), bitmap.getWidth(), CvType.CV_8UC4);
Utils.bitmapToMat(bitmap, src);
// 定义一个合适大小的核 ELLIPSE椭圆形
Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(5, 5));
// 扩大暗区(腐蚀)
Imgproc.erode(src, src, kernelErode);
// Mat转Bitmap
Bitmap processedImage = Bitmap.createBitmap(src.cols(), src.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(src, processedImage);
return processedImage;
}