Skip to content Skip to sidebar Skip to footer

Opencv Java Convexity Defects (computer Vision)

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 MatOfInt

Solution 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 :23cdList 1 :30cdList 2 :26cdList 3 :18101-----------------cdList 4 :30cdList 5 :44cdList 6 :33cdList 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.

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

Post a Comment for "Opencv Java Convexity Defects (computer Vision)"