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<MatOfPoint> 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<MatOfPoint2f> 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;j<newPoint.total();j++){
                    temp=newPoint.get( j,0 );
                    Point point1=new Point( temp[0],temp[1] );
                    Log.i( "aaron","Point "+ point1);
                }
                Log.i( "aaron","----------------- ");

                newContours.add(newPoint);
            }

            //轮廓结构关系
            Log.i( "aaron","轮廓数量:  "+contours.size() );
            Log.i( "aaron","hierarchy类型:  "+hierarchy);
            for(int k=0;k<hierarchy.cols();k++) {
                double[] ds = hierarchy.get(0, k);
                Log.i( "aaron","------------------  ");
                Log.i( "aaron","轮廓下标:  "+k );
                for (int l=0;l<ds.length;l++) {
                    switch (l) {
                        case 0:
                            Log.i( "aaron","后一个轮廓下标:  "+ds[l] );
                            break;
                        case 1:
                            Log.i( "aaron","前一个轮廓下标:  "+ds[l] );
                            break;
                        case 2:
                            Log.i( "aaron","父轮廓下标:  "+ds[l] );
                            break;
                        case 3:
                            Log.i( "aaron","内嵌轮廓下标:  "+ds[l] );
                            break;

                        default:
                            break;
                    }
                }
            }



            for (int idx=0;idx<contours.size();idx++) {


            //    Rect rect= Imgproc.boundingRect( contours.get( idx ) );
            //    Log.i( "aaron","rect  "+rect.x+" "+rect.y+"  "+rect.width+"  "+rect.height );
            //    Imgproc.rectangle( test,new Point( rect.x,rect.y ),new Point( rect.x+rect.width,rect.y+rect.height ),new Scalar( 0,255,0 ) );
                temp_contour = contours.get( idx );
                Imgproc.drawContours(test, contours, idx, new Scalar(0,255,0), 2);
                double contourarea = Imgproc.contourArea( temp_contour );
                if (contourarea>0){
                    Log.i( "aaron","area  "+contourarea );
                 //   Imgproc.drawContours(test, contours, idx, new Scalar(0,255,0), 1);
//

                    i++;
                }

            }

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值