2016-04-02 75 views
0

这里是我的代码:凸包返回java.lang.IndexOutOfBoundsException OpenCV的机器人

public void findHull(){ 
    int h = 0; 
    double area_Hull = 0; 
    hullArea = 0; 
    List<MatOfInt> hull = new ArrayList<MatOfInt>(); 
    hull.add(new MatOfInt()); 

    Imgproc.convexHull(contours2.get(largest_contour_index), hull.get(largest_contour_index)); 

    Point[] points = new Point[hull.get(largest_contour_index).rows()]; 

    for(h=0; h < hull.get(largest_contour_index).rows(); h++) { 
     int index = (int)hull.get(largest_contour_index).get(h, 0)[0]; 
     points[h] = new Point(contours2.get(largest_contour_index).get(index, 0)[0],contours2.get(largest_contour_index).get(index, 0)[1]); 
    } 
    List<Point[]> hullpoints = new ArrayList<Point[]>(); 
    hullpoints.add(points); 

    noOfDent = h; 

    List<MatOfPoint> hullmop = new ArrayList<MatOfPoint>(); 

    MatOfPoint mop = new MatOfPoint(); 
    mop.fromArray(hullpoints.get(0)); 
    hullmop.add(mop); 

    area_Hull = Imgproc.contourArea(hullmop.get(0)); 

    hullArea = area_Hull; 
    MatOfPoint2f Hullpt = new MatOfPoint2f(); 
    hullmop.get(0).convertTo(Hullpt, CvType.CV_32FC2); 
    hullPerimeter=Imgproc.arcLength(Hullpt, false); 
} 

contours2是这是以前检索使用findcontours.And largest_contour_index是最大的轮廓的索引图像中的所有轮廓。但我收到一个异常错误:

Imgproc.convexHull(contours2.get(largest_contour_index), hull.get(largest_contour_index)); 

你能告诉问题出在哪?

预先感谢您的帮助

+1

我建议你调试它以查看每个集合的大小:'contours2'和'hull'。因为其中一个或其他(甚至两个)没有'largest_contour_index'元素。 – aribeiro

+0

我发现错误,我只向船体添加一个值,并且当我使用largest_contour_index时,它不指向任何东西。我的坏,愚蠢的错误 – adams

回答

0

因为你hull收藏有1个元素上初始化索引0 (new MatOfInt()),和ArrayList的其余持有空引用。 Imgproc.convexHull方法试图转换您的轮廓(MatOfPoint)元素为一个船体(MatOfInt),但为了做到这一点,它需要进行初始化。具体来说,当你打电话给hull.get(largest_contour_index)时,它会得到一个空引用,除非你的largest_contour_index是0.

编辑:啊我看你回答了你自己的问题。 Nevermind then