2013-08-06 57 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); 
    } 
}