Android Opencv 轮廓提取

1.把图片转成灰度图

2.再转成黑白
3.用canny检查边缘

4.查找轮廓

这里查找的是图片中最大的矩形

        Mat src=new Mat();
        Mat grayMat=new Mat();
        Mat edge=new Mat();


        Utils.bitmapToMat(bitmap_src, src);
        Imgproc.cvtColor(src,grayMat,Imgproc.COLOR_RGBA2GRAY);

        grayMat=processImage(grayMat);

        Imgproc.Canny(grayMat,edge,10,200,3,true);

        List pointList= getCornersByContour(edge);

 

 

 

    public List getCornersByContour(Mat imgsource){
        List 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;idxmaxArea){
                //检测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;
                }
            }
        }

        //把轮廓画出来
        /*
        Mat mRgba=new Mat();
        mRgba.create(imgsource.rows(), imgsource.cols(), CvType.CV_8UC3);

        Imgproc.drawContours(mRgba, contours, maxAreaIdx, new Scalar(0,255,0), 5);
        Bitmap b12=bitmap_src.copy(Bitmap.Config.ARGB_8888, true);
        Utils.matToBitmap(mRgba,b12);
        imageView4.setImageBitmap(b12);*/


        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 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;icenterPoint.x&&source.get(i).ycenterPoint.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;
    }

    private Mat processImage(Mat gray){
        Mat b=new Mat();
        Imgproc.medianBlur(gray,b,7);
        Mat t=new Mat();
        Imgproc.threshold(b,t,80,255,Imgproc.THRESH_BINARY);

        return t;
    }

 

你可能感兴趣的:(Android,OpenCV)