Android+OpenCv4怎么实现边缘检测及轮廓绘制出图像最大边缘

猿友 2021-08-04 10:27:48 浏览数 (2123)
反馈

Android边缘检测我们一般采用canny边缘检测来实现。通过边缘检测我们可以得到图像的最大边缘,接下来这篇文章我们就来介绍如何基于Android边缘检测实现绘制图像最大边缘的功能。

实现步骤:

  • 图像灰度化
  • 边缘检测
  • 根据Canny检测得出来的Mat寻找轮廓
  • 算出最大轮廓周长or面积
  • 根据获取到的最大轮廓下标进行轮廓绘制
  • 画出最大矩形,并返回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();
    }

最终效果图

最终效果

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

到此这篇如何基于Android边缘检测实现绘制图像最大边缘的功能的文章就介绍到这了,更多相关Android学习内容请搜索W3Cschool以前的文章或继续浏览下面的相关文章。


0 人点赞