Android OpenCv4 边缘检测+轮廓绘制出图像最大边缘

实现步骤:

  1. 图像灰度化
  2. 边缘检测
  3. 根据Canny检测得出来的Mat寻找轮廓
  4. 算出最大轮廓周长or面积
  5. 根据获取到的最大轮廓下标进行轮廓绘制
  6. 画出最大矩形,并返回Rect
Canny边缘检测

基于Canny算法的边缘检测主要有5个步骤,依次是高斯滤波、像素梯度计算、非极大值像素梯度抑制、滞后阈值处理和孤立弱边缘抑制。Canny在有噪声的情况下表现好不好,取决于前面的降噪过程,可以手动做高斯处理提高识别率。

/**image  输入图像,必须是CV_8U的单通道或者三通道图像。edges  输出图像,与输入图像具有相同尺寸的单通道图像,且数据类型为CV_8U。threshold1  第一个滞后阈值。threshold2  第二个滞后阈值。apertureSize  Sobel算子的直径。L2gradient  计算图像梯度幅值方法的标志。默认为false
**/
public static void Canny(Mat image, Mat edges, double threshold1, double threshold2, int apertureSize, boolean L2gradient)
使用
    /*** canny算法,边缘检测*/public static Mat canny(Bitmap bitmap) {Mat mSource = new Mat();Utils.bitmapToMat(bitmap, mSource);Mat grayMat = new Mat();Imgproc.cvtColor(mSource,grayMat,Imgproc.COLOR_BGR2GRAY);//转换成灰度图Mat mat = mSource.clone();Imgproc.Canny(mSource, mat, 75, 200);return mat;}

在这里插入图片描述

获取图像最大矩形
   /*** 返回边缘检测之后的最大矩形,并返回** @param cannyMat*            Canny之后的mat矩阵* @return*/public  Rect findMaxRect(Mat cannyMat) {Mat tmp = mSource.clone();List<MatOfPoint> contours = new ArrayList<MatOfPoint>();Mat hierarchy = new Mat();// 寻找轮廓Imgproc.findContours(cannyMat, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);int index = 0;double perimeter = 0;// 找出匹配到的最大轮廓for (int i = 0; i < contours.size(); i++) {// 最大面积
//            double area = Imgproc.contourArea(contours.get(i));//最大周长MatOfPoint2f source = new MatOfPoint2f();source.fromList(contours.get(i).toList());double length = Imgproc.arcLength(source,true);if(length>perimeter){perimeter =  length;index = i;}}/*** 参数一:image,待绘制轮廓的图像。** 参数二:contours,待绘制的轮廓集合。** 参数三:contourIdx,要绘制的轮廓在contours中的索引,若为负数,表示绘制全部轮廓。** 参数四:color,绘制轮廓的颜色。** 参数五:thickness,绘制轮廓的线条粗细。若为负数,那么绘制轮廓的内部。** 参数六:lineType,线条类型。FILLED   LINE_4   4连通   LINE_8   8连通  LINE_AA  抗锯齿*/Imgproc.drawContours(tmp,contours,index,new Scalar(0.0, 0.0, 255.0),9,Imgproc.LINE_AA);Rect rect = Imgproc.boundingRect(contours.get(index));
//        Imgproc.rectangle(tmp, rect, new Scalar(0.0, 0.0, 255.0), 4, Imgproc.LINE_8);showImg(tmp);return rect;}/*** 显示图像* @param mat*/private void showImg(Mat mat){Bitmap bitmap = Bitmap.createBitmap(mat.width(), mat.height(), Bitmap.Config.ARGB_8888);Utils.matToBitmap(mat, bitmap);mIvSrc.setImageBitmap(bitmap);mat.release();}

最终效果图

在这里插入图片描述

获得矩形坐标点以后,后期可以做裁剪,旋转之类操作,可以自行研究。