@evilking
2019-01-17T08:45:01.000000Z
字数 2716
阅读 1144
Uri imageUri = imageIntent.getData();InputStream imageStream = getContentResolver().openInputStream(imageUri);Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);src = new Mat(selectedImage.getHeight(), selectedImage.getWidth(), CvType.CV_8UC4);// 将 Bitmap 转成 Mat 供 opencv 使用Utils.bitmapToMat(selectedImage, src);// 可操作 Mat 对象
Imgproc.blur(src, src, new Size(3,3))//处理完可预览操作效果Bitmap processedImage = Bitmap.createBitmap(src.cols(), src.rows(), Bitmap.Config.ARGB_8888);// Mat 对象转成 Bitmap 对象,供 android 使用Utils.matToBitmap(src, processedImage);// 原图ivImage.setImageBitmap(selectedImage);// 操作后的图ivImageProcessed.setImageBitmap(processedImage);
Imgproc.GaussianBlur(src, src, new Size(3,3), 0);
处理 淑盐噪声
Imgproc.medianBlur(src, src, 3);
使用的卷积核为 :
// 创建卷积核Mat kernel = new Mat(3,3, CvType.CV_16SC1);kernel.put(0,0, 0,-1,0,-1,5,-1,0,-1,0)// 锐化滤波Imgproc.filter2D(src, src, src.depth(), kernel);
膨胀是一种将图像中亮区域扩张的方法。
// 可以获得所需要的核Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3, 3));// 执行膨胀操作Imgproc.dilate(src, src, kernelDilate);
腐蚀是一种将图像中暗区域扩张的方法。腐蚀可以用来去除图像中的噪声。
// 得到椭圆形的核Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(5, 5));// 腐蚀Imgproc.erode(src, src, kernelErode);
膨胀和腐蚀并不是逆运算
// 100 是阈值,255 是最大值(纯白色的值)Imgproc.threshold(src, src, 100, 255, Imgproc.THRESH_CONSTANT);
二值阈值化:THRESH_BINARY
阈值化到零:THRESH_TOZERO
截断阈值化:THRESH_TRUNC
反转二值阈值化:THRESH_BINARY_INV
反转阈值化到零:THRESH_TOZERO_INV
块尺寸:即邻域的大小
常量 C:这是从对每个像素计算得到的均值或加权均值减去的常量
Imgproc.cvtColor(src, src, Imgproc.COLOR_BGR2GRAY);Imgproc.adaptiveThreshold(src, src, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY, 3, 0);
// 对 Bitmap 对象处理BitmapFactory.Options options = new BitmapFactory.Options();options.inSampleSize = 2;Bitmap temp = BitmapFactory.decodeFile(picturePath, options);// 获取方向信息int orientation = 0;try{ExifInterface imgParams = new ExifInterface(picturePath);orientation = imgParams.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);} catch (IOException e){e.printStackTrace();}// 旋转图像,得到正确的方向Matrix rotate90 = new Matrix();rotate90.postRotate(orientation);originalBitmap = rotateBitmap(temp, orientation);//将 Bitmap 转换为 Mat
Mat grayMat = new Mat();Mat blur1 = new Mat();Mat blur2 = new Mat();// 将图像转换为灰度Imgproc.cvtColor(originalMat, grayMat, Imgproc.COLOR_BGR2GRAY);// 以两个不同的模糊半径对图像做模糊处理Imgproc.GaussianBlur(grayMat, blur1, new Size(15, 15), 5);Imgproc.GaussianBlur(grayMat, blur2, new Size(21, 21), 5);// 将两幅模糊后的图像相减Mat DoG = new Mat();Core.absdiff(blur1, blur2, DoG);// 反转二值阈值化Core.multiply(DoG, new Scalar(100), DoG);Imgproc.threshold(DoG, DoG, 50, 255, Imgproc.THRESH_BINARY_INV);// 将 Mat 转换回位图Utils.matToBitmap(DoG, currentBitmap);