1 、布局文件
public class OpenCVUtils {
Bitmap bitmap;
public OpenCVUtils(Bitmap srcBitmap) {
bitmap = srcBitmap;
}
/**
* 灰度化处理
*/
public Bitmap previewGray() {
Mat rgbMat = new Mat();
Mat grayMat = new Mat();
Utils.bitmapToMat(bitmap, rgbMat);//convert original bitmap to Mat, R G B.
// 图像置灰
Imgproc.cvtColor(rgbMat, grayMat, Imgproc.COLOR_RGB2GRAY, 4);//rgbMat to gray grayMat
Bitmap processedImage = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.RGB_565);
Utils.matToBitmap(grayMat, processedImage); //convert mat to bitmap
return processedImage;
}
/**
* 边缘检测
* Imgproc.Canny(Mat image, Mat edges, double threshold1, double threshold2)
* 第一个参数表示图像输入
* 第二个参数表述图像输出
* 第三个参数表示低阈值
* 第四个参数表示高阈值
*/
public Bitmap canny() {
Mat grayMat = new Mat();
Mat cannyMat = new Mat();
// Bitmap转为Mat
Mat rgbMat = new Mat(bitmap.getHeight(), bitmap.getWidth(), CvType.CV_8UC4);
Utils.bitmapToMat(bitmap, rgbMat);
// 原图置灰
Imgproc.cvtColor(rgbMat, grayMat, Imgproc.COLOR_BGR2GRAY);
// Canny边缘检测器检测图像边缘
Imgproc.Canny(grayMat, cannyMat, 80, 100);
// Mat转Bitmap
Bitmap processedImage = Bitmap.createBitmap(cannyMat.cols(), cannyMat.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(cannyMat, processedImage);
return processedImage;
}
/**
* 图片虚化
* 均值模糊
*/
public Bitmap blur() {
// Bitmap转为Mat
Mat rgbMat = new Mat(bitmap.getHeight(), bitmap.getWidth(), CvType.CV_8UC4);
Utils.bitmapToMat(bitmap, rgbMat);
// 均值模糊方法
Imgproc.blur(rgbMat, rgbMat, new Size(80, 80));
// Mat转Bitmap
Bitmap processedImage = Bitmap.createBitmap(rgbMat.cols(), rgbMat.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(rgbMat, processedImage);
return processedImage;
}
/**
* 图片虚化
* 高斯模糊
*/
public Bitmap gaussianBlur() {
// Bitmap转为Mat
Mat rgbMat = new Mat(bitmap.getHeight(), bitmap.getWidth(), CvType.CV_8UC4);
Utils.bitmapToMat(bitmap, rgbMat);
// 高斯模糊方法,此处ksize.width > 0 && ksize.width % 2 == 1 && ksize.height > 0 && ksize.height % 2 == 1
Imgproc.GaussianBlur(rgbMat, rgbMat, new Size(91, 91), 0);
// Mat转Bitmap
Bitmap processedImage = Bitmap.createBitmap(rgbMat.cols(), rgbMat.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(rgbMat, processedImage);
return processedImage;
}
/**
* 图片虚化
* 中值模糊
*/
public Bitmap medianBlur() {
// Bitmap转为Mat
Mat rgbMat = new Mat(bitmap.getHeight(), bitmap.getWidth(), CvType.CV_8UC4);
Utils.bitmapToMat(bitmap, rgbMat);
// 中值模糊方法
Imgproc.medianBlur(rgbMat, rgbMat, 33);
// Mat转Bitmap
Bitmap processedImage = Bitmap.createBitmap(rgbMat.cols(), rgbMat.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(rgbMat, processedImage);
return processedImage;
}
/**
* 图片锐化
*/
public Bitmap filter2D() {
// Bitmap转为Mat
Mat rgbMat = new Mat(bitmap.getHeight(), bitmap.getWidth(), CvType.CV_8UC4);
Utils.bitmapToMat(bitmap, rgbMat);
/*
自定义核
0 -1 0
-1 5 -1
0 -1 0
*/
Mat kernel = new Mat(3, 3, CvType.CV_16SC1);
kernel.put(0, 0, 0, -1, 0, -1, 5, -1, 0, -1, 0);
// 对图像和自定义核做卷积
Imgproc.filter2D(rgbMat, rgbMat, rgbMat.depth(), kernel);
// Mat转Bitmap
Bitmap processedImage = Bitmap.createBitmap(rgbMat.cols(), rgbMat.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(rgbMat, processedImage);
return processedImage;
}
/**
* 扩大图片亮区
*/
public Bitmap dilate() {
// Bitmap转为Mat
Mat rgbMat = new Mat(bitmap.getHeight(), bitmap.getWidth(), CvType.CV_8UC4);
Utils.bitmapToMat(bitmap, rgbMat);
// 定义一个合适大小的核
Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10, 10));
// 扩大亮区
Imgproc.dilate(rgbMat, rgbMat, kernelDilate);
// Mat转Bitmap
Bitmap processedImage = Bitmap.createBitmap(rgbMat.cols(), rgbMat.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(rgbMat, processedImage);
return processedImage;
}
/**
* 扩大图片暗区(腐蚀图片)
*/
public Bitmap erode() {
// Bitmap转为Mat
Mat rgbMat = new Mat(bitmap.getHeight(), bitmap.getWidth(), CvType.CV_8UC4);
Utils.bitmapToMat(bitmap, rgbMat);
// 定义一个合适大小的核
Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(10, 10));
// 扩大暗区(腐蚀)
Imgproc.erode(rgbMat, rgbMat, kernelErode);
// Mat转Bitmap
Bitmap processedImage = Bitmap.createBitmap(rgbMat.cols(), rgbMat.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(rgbMat, processedImage);
return processedImage;
}
/**
* 自适应阈值
*/
public Bitmap adaptiveThreshold() {
// Bitmap转为Mat
Mat rgbMat = new Mat(bitmap.getHeight(), bitmap.getWidth(), CvType.CV_8UC4);
Utils.bitmapToMat(bitmap, rgbMat);
// 图像置灰
Imgproc.cvtColor(rgbMat, rgbMat, Imgproc.COLOR_BGR2GRAY);
// 自适应阈值化
// Imgproc.adaptiveThreshold(rgbMat, rgbMat, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 3, 0);
// 二值阈值化
// Imgproc.threshold(rgbMat,rgbMat,100,255,Imgproc.THRESH_BINARY);
// 阈值化到零
// Imgproc.threshold(rgbMat,rgbMat,100,255,Imgproc.THRESH_TOZERO);
// 截断阈值化
// Imgproc.threshold(rgbMat,rgbMat,100,255,Imgproc.THRESH_TRUNC);
// 反转二值阈值化
// Imgproc.threshold(rgbMat,rgbMat,100,255,Imgproc.THRESH_BINARY_INV);
// 反转阈值化到零
Imgproc.threshold(rgbMat,rgbMat,100,255,Imgproc.THRESH_TOZERO_INV);
// Mat转Bitmap
Bitmap processedImage = Bitmap.createBitmap(rgbMat.cols(), rgbMat.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(rgbMat, processedImage);
return processedImage;
}
/**
* 高斯差分算法边缘检测
*/
public Bitmap differenceOfGaussian() {
Mat grayMat = new Mat();
Mat blur1 = new Mat();
Mat blur2 = new Mat();
// Bitmap转为Mat
Mat rgbMat = new Mat(bitmap.getHeight(), bitmap.getWidth(), CvType.CV_8UC4);
Utils.bitmapToMat(bitmap, rgbMat);
// 原图置灰
Imgproc.cvtColor(rgbMat, grayMat, Imgproc.COLOR_BGR2GRAY);
// 以两个不同的模糊半径对图像做模糊处理
Imgproc.GaussianBlur(grayMat, blur1, new Size(15, 15), 5);
Imgproc.GaussianBlur(grayMat, blur2, new Size(21, 21), 5);
// 将两幅模糊后的图像相减
Mat diff = new Mat();
Core.absdiff(blur1, blur2, diff);
// // 反转二值阈值化
Core.multiply(diff, new Scalar(100), diff);
Imgproc.threshold(diff, diff, 50, 255, Imgproc.THRESH_BINARY_INV);
// Mat转Bitmap
Bitmap processedImage = Bitmap.createBitmap(rgbMat.cols(), rgbMat.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(diff, processedImage);
return processedImage;
}
}
网友评论