Android Opencv 获取轮廓坐标,轮廓结构关系

 

Canny要传黑白图片
findContours要传黑白图片,
Imgproc.RETR_CCOMP,Imgproc.CHAIN_APPROX_SIMPLE 根据需要自己设置。

 

代码有点乱,挑关键代码用

灰度图转黑白

    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;
    }

轮廓相关方法:

1.每个轮廓的坐标点

2.轮廓结构关系

 


        Mat hierarchy=new Mat(  );

        Mat edge=new Mat(  );
        Imgproc.Canny(src,edge,10,200,3,true);
        List contours=new ArrayList<>();
        //轮廓检测 CHAIN_APPROX_SIMPLE
        Imgproc.findContours(edge,contours,hierarchy,Imgproc.RETR_CCOMP,Imgproc.CHAIN_APPROX_SIMPLE);

        Mat mRgba=new Mat();
        mRgba.create(edge.rows(), edge.cols(), CvType.CV_8UC3);
        Bitmap b12= Bitmap.createBitmap(edge.cols(), edge.rows(),
                Bitmap.Config.ARGB_8888);

        if (contours.size()>0){
            int i=0;
            Log.i( "aaron","size "+contours.size() );
            MatOfPoint temp_contour=contours.get(0);//假设最大的轮廓在index=0处
            MatOfPoint2f approxCurve=new MatOfPoint2f();
           // Imgproc.drawContours(test, contours, -1, new Scalar(0,255,0), 1);

                //轮廓坐标
            List newContours = new ArrayList<>();
            for(MatOfPoint point : contours) {
                MatOfPoint2f newPoint = new MatOfPoint2f(point.toArray());
                Log.i( "aaron","MatOfPoint2f "+                newPoint.total() );
                double[] temp;
                Log.i( "aaron","Point----------------- ");
                for (int j=0;j0){
                    Log.i( "aaron","area  "+contourarea );
                 //   Imgproc.drawContours(test, contours, idx, new Scalar(0,255,0), 1);
//

                    i++;
                }

            }

 

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