【问题标题】:In OpenCV 4.5.0 Java what method returns the number of points in a contour?在 OpenCV 4.5.0 Java 中,什么方法返回轮廓中的点数?
【发布时间】:2021-03-27 14:21:50
【问题描述】:

在 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 对象,这根本不是我想要的。

非常感谢任何帮助。

谢谢,

吉姆

【问题讨论】:

  • 非常感谢。现在我知道要访问第 I 轮廓的第 J 元素,我需要使用:```Point P = contours.get(I).toList() .get(J) ; ```

标签: java image android-studio opencv contour


【解决方案1】:

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

【讨论】:

  • 这正是我需要的答案。
    非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-12
  • 1970-01-01
  • 2023-03-21
  • 2011-12-25
相关资源
最近更新 更多