繁体   English   中英

在 OpenCV 4.5.0 Java 什么方法返回轮廓中的点数?

[英]In OpenCV 4.5.0 Java what method returns the number of points in a contour?

在 C++ 中,我正在这样做:一切都很好,现在我需要为 Android 切换到 Java。

    vector<vector<Point> > contours;
    findContours(image, contours, 
        CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE);

    printf( "\n%d contours\n\n",   contours.size() ) ;

    /* our ASSUMPTION is that the contour with the most points is 
        the one we want */
    int mx = 0;
    int nm = -1;
    for( int i = 0; i < contours.size(); i++ ){
        if( contours[i].size() > mx ) { 
            mx = contours[i].size() ; nm = i ; 
        }
    }

    printf( "largest contour (number %d) has %d points.\n\n",
         nm,  mx );

    Point ul, ur, lr, ll;
    ul = contours[nm][0];
    for( int i = 1; i < contours[nm].size(); i++ ) {
        /* TODO -- handle equal case */
        if( ( contours[nm][i].x +  contours[nm][i].y ) < 
            ( ul.x + ul.y ) ){ ul = contours[nm][i]; }
        if( ( contours[nm][i].x +  contours[nm][i].y ) > 
            ( lr.x + lr.y ) ){ lr = contours[nm][i]; }
        if( ( contours[nm][i].x -  contours[nm][i].y ) > 
            ( ur.x - ur.y ) ){ ur = contours[nm][i]; }
        if( ( contours[nm][i].x -  contours[nm][i].y ) < 
            ( ll.x - ll.y ) ){ ll = contours[nm][i]; }
    }

    printf( "The upper left point is at ( %d, %d )\n\n",
        ul.x , ul.y );
    printf( "The upper right point is at ( %d, %d )\n\n", 
        ur.x , ur.y );
    printf( "The lower right point is at ( %d, %d )\n\n", 
        lr.x , lr.y );
    printf( "The lower left point is at ( %d, %d )\n\n",
        ll.x , ll.y );

    /* got the corners */
 

Java 使用“MatOfPoint”列表,因此我无法获得与每个轮廓相关的点数。

我认为这不应该那么困难。

List<MatOfPoint > contours =  null;
Mat hierarchy = new Mat();
findContours( grayimage, contours, hierarchy,  Imgproc.RETR_LIST, 
    Imgproc.CHAIN_APPROX_SIMPLE);

for ( int  i = 0 ;  i < contours.size() ; i++ ) {
    int  sss  = contours.get( i ).size()  ;
}

但是 contours.get( i ).size() 返回一个 Size object 这根本不是我想要的。

任何帮助深表感谢。

谢谢,

吉姆

int sss = contours.get(i).toList().size();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM