2013-08-06 53 views
1

我有一个问题,把我的凸面缺陷放在框架上。为了计算这些,我修改了C++源代码,这就是我存档:OpenCV Java凸面缺陷(计算机视觉)

mConvexityDefectsMatOfInt4 = new MatOfInt4(); 

    if(contours.size() > 0 && convexHullMatOfInt.rows() > 0) 
     Imgproc.convexityDefects(contours.get(0), convexHullMatOfInt, mConvexityDefectsMatOfInt4); 

然而,Imgproc.drawContours(...)方法需要传递给它的参数convexityDefects将ArrayList的。我不知道如何进行转换。我还与凸包类似的问题,但是我发现一个外观图释:

convexHullMatOfInt = new MatOfInt(); 
    convexHullPointArrayList = new ArrayList<Point>(); 
    convexHullMatOfPoint = new MatOfPoint(); 
    convexHullMatOfPointArrayList = new ArrayList<MatOfPoint>(); 

    //Calculate convex hulls 
    if(contours.size() > 0) 
    { 
    Imgproc.convexHull(contours.get(0), convexHullMatOfInt, false); 
    for(int j=0; j < convexHullMatOfInt.toList().size(); j++) 
     convexHullPointArrayList.add(contours.get(0).toList().get(convexHullMatOfInt.toList().get(j))); 
    convexHullMatOfPoint.fromList(convexHullPointArrayList); 
    convexHullMatOfPointArrayList.add(convexHullMatOfPoint); 
    } 

为凸缺陷类似的解决方案是行不通的。有没有人有一个想法,我该如何解决这个问题?

如何将MatOfInt4()转换为ArrayList()以绘制凸度缺陷?

回答

3

(我自己也挣扎了这么多与convexityDefect,我想杀谁就写的Java接口的OpenCV!)

现在的答案:

正如docs指出,MatOfInt4基本上是

start_index 
end_index 
farthest_pt_index 
fixpt_depth 

可以使用以下方法来mConvexityDefectsMatOfInt4转换为整数的列表:包含以下信息的4元件整数数组:

List<Integer> cdList = mConvexityDefectsMatOfInt4.toList(); 

现在,cdList每4个连续元素持有上述的信息:

cdList 0 : 23  
cdList 1 : 30  
cdList 2 : 26  
cdList 3 : 18101 
----------------- 
cdList 4 : 30 
cdList 5 : 44 
cdList 6 : 33 
cdList 7 : 43738 

因此,举例来说,如果你只想绘制各凹部的最远点,你可以简单地使用每4个元素的第三个索引。在这种情况下:26,33,...

希望它有帮助。

2

下面是一个例子:

for (int i = 0; i < contours.size(); i++) { 
    convDef.add(new MatOfInt4()); 
    Imgproc.convexityDefects(contours.get(i), hull.get(i), 
     convDef.get(i)); 
    cdList = convDef.get(i).toList(); 
    Point data[] = contours.get(i).toArray(); 

    for (int j = 0; j < cdList.size(); j = j+4) { 
     Point start = data[cdList.get(j)]; 
     Point end = data[cdList.get(j+1)]; 
     Point defect = data[cdList.get(j+2)]; 
     //Point depth = data[cdList.get(j+3)]; 

     Imgproc.circle(mat, start, 5, new Scalar(0, 255, 0), 2); 
     Imgproc.circle(mat, end, 5, new Scalar(0, 255, 0), 2); 
     Imgproc.circle(mat, defect, 5, new Scalar(0, 255, 0), 2); 
    } 
}