OpenCV Java convexity defects (Computer Vision)

2019-08-11 16:51发布

问题:

I have a problem with putting my convexity defects on the frame. To calculate them, I modified c++ source and this is what i archived:

    mConvexityDefectsMatOfInt4 = new MatOfInt4();

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

However, the Imgproc.drawContours(...) method requires that convexityDefects passed to it as a parameters will be ArrayList. I don't know how can I make the conversion. I had also similar problem with convex hulls, but I found out a walkaround:

  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);   
  }

Similar solution for convexity defects is not working. Does anyone have an idea on how can I solve the problem?

How to convert from MatOfInt4() to ArrayList() to be able to draw convexity defects?

回答1:

(I myself was struggling so much with convexityDefect that I wanted to kill whoever has written the Java interface for OpenCV!)

Now the answer:

As stated in docs, MatOfInt4 is basically a 4-element integer array containing the following information:

start_index
end_index
farthest_pt_index
fixpt_depth

You can use the following to convert mConvexityDefectsMatOfInt4 to a list of integers:

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

Now, each 4 consecutive elements in cdList holds the info stated above:

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

So, for example if you want to draw only the farthest point of each concavity, you can simply use the third index of each 4 elements. In this case: 26, 33, ...

Hope it helps.



回答2:

Here is an example:

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);
    }
}