Android opencv轮廓检测

Posted danfengw

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android opencv轮廓检测相关的知识,希望对你有一定的参考价值。

轮廓检测函数:
Imgproc. findContours(image, contours, hierarchy, mode, method)
通过轮廓检测找到面积最大的轮廓并绘制轮廓:

        List<MatOfPoint> contours=new ArrayList<>();
        Imgproc.findContours(blurredImage,contours,new Mat(),Imgproc.RETR_LIST,Imgproc.CHAIN_APPROX_SIMPLE);
        double maxVal = 0;
        int maxValIdx = 0;
        for (int contourIdx = 0; contourIdx < contours.size(); contourIdx++)
        
            double contourArea = Imgproc.contourArea(contours.get(contourIdx));
            if (maxVal < contourArea)
            
                maxVal = contourArea;
                maxValIdx = contourIdx;
            
        
 Mat mRgba=new Mat();
        mRgba.create(blurredImage.rows(), blurredImage.cols(), CvType.CV_8UC3);
        //绘制检测到的轮廓
         Imgproc.drawContours(mRgba, contours, maxAreaIdx, new Scalar(0,255,0), 5);

//通过轮廓检测获取4个交点


    public static List<Point> getCornersByContour(Mat imgsource)
        List<MatOfPoint> contours=new ArrayList<>();
        //轮廓检测
        Imgproc.findContours(imgsource,contours,new Mat(),Imgproc.RETR_LIST,Imgproc.CHAIN_APPROX_SIMPLE);
        double maxArea=-1;
        int maxAreaIdx=-1;
        MatOfPoint temp_contour=contours.get(0);//假设最大的轮廓在index=0处
        MatOfPoint2f approxCurve=new MatOfPoint2f();
        for (int idx=0;idx<contours.size();idx++)
            temp_contour=contours.get(idx);
            double contourarea=Imgproc.contourArea(temp_contour);
            //当前轮廓面积比最大的区域面积大就检测是否为四边形
            if (contourarea>maxArea)
                //检测contour是否是四边形
                MatOfPoint2f new_mat=new MatOfPoint2f(temp_contour.toArray());
                int contourSize= (int) temp_contour.total();
                MatOfPoint2f approxCurve_temp=new MatOfPoint2f();
                //对图像轮廓点进行多边形拟合
                Imgproc.approxPolyDP(new_mat,approxCurve_temp,contourSize*0.05,true);
                if (approxCurve_temp.total()==4)
                    maxArea=contourarea;
                    maxAreaIdx=idx;
                    approxCurve=approxCurve_temp;
                
            
        
        double[] temp_double=approxCurve.get(0,0);
        Point point1=new Point(temp_double[0],temp_double[1]);

        temp_double=approxCurve.get(1,0);
        Point point2=new Point(temp_double[0],temp_double[1]);

        temp_double=approxCurve.get(2,0);
        Point point3=new Point(temp_double[0],temp_double[1]);
        temp_double=approxCurve.get(3,0);

        Point point4=new Point(temp_double[0],temp_double[1]);

        List<Point> source=new ArrayList<>();
        source.add(point1);
        source.add(point2);
        source.add(point3);
        source.add(point4);
        //对4个点进行排序
        Point centerPoint=new Point(0,0);//质心
        for (Point corner:source)
            centerPoint.x+=corner.x;
            centerPoint.y+=corner.y;
        
        centerPoint.x=centerPoint.x/source.size();
        centerPoint.y=centerPoint.y/source.size();
        Point lefttop=new Point();
        Point righttop=new Point();
        Point leftbottom=new Point();
        Point rightbottom=new Point();
        for (int i=0;i<source.size();i++)
            if (source.get(i).x<centerPoint.x&&source.get(i).y<centerPoint.y)
                lefttop=source.get(i);
            else if (source.get(i).x>centerPoint.x&&source.get(i).y<centerPoint.y)
                righttop=source.get(i);
            else if (source.get(i).x<centerPoint.x&& source.get(i).y>centerPoint.y)
                leftbottom=source.get(i);
            else if (source.get(i).x>centerPoint.x&&source.get(i).y>centerPoint.y)
                rightbottom=source.get(i);
            
        
        source.clear();
        source.add(lefttop);
        source.add(righttop);
        source.add(leftbottom);
        source.add(rightbottom);
        return source;
    

ps:轮廓检测的输入图像必须是二值化的图片否则只能检测一个轮廓。

以上是关于Android opencv轮廓检测的主要内容,如果未能解决你的问题,请参考以下文章

youcans 的 OpenCV 例程200篇197.轮廓的基本特征

Android opencv轮廓检测

opencv+python实现视频实时质心读取

什么叫图像或轮廓的空间矩中心矩归一化中心矩?并利用OpenCV的类Moments计算轮廓的这几个矩和质心位置

使用OpenCV检测损坏的形状

android如何在 java opencv 中查找最大轮廓