在opencv 4.5.0 java中,什么方法返回轮廓中的点数?

tp5buhyn  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(376)

在c++中,我正在这样做:一切都很好,现在我需要切换到java for android。

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对象不是我想要的。
非常感谢您的帮助。
谢谢,
吉姆

uqdfh47h

uqdfh47h1#

int sss=等高线.get(i).tolist().size();

相关问题