0

So I have been trying to get the defects of a contour and its hull. After looking into a few tutorials I have come across similar code, but no matter how I implement it, the line cv2.convexityDefects seems to kick me out of the loop, not displaying the video. The program works without the defects part and I'm not getting any errors with the defects part in but it just seems to end the code.

    contours, H = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    contours = sorted(contours, key=cv2.contourArea, reverse=True)

    max_area = 0
    for i in range(len(contours)):  # finding largest contour by area [3]
        contour = contours[i]
        area = cv2.contourArea(contour)
        if area > max_area:
            max_area = area
            ci = i
    if len(contours) > 0:
        (x, y, w, h) = cv2.boundingRect(contours[ci])
        # cv2.rectangle(resized, (x, y), (x + w, y + h), (0, 255, 0), 2)
        moments = cv2.moments(contours[ci])
        if moments['m00'] != 0:  # this gives the centre of the moments [3]
            cx = int(moments['m10'] / moments['m00'])  # cx = M10/M00
            cy = int(moments['m01'] / moments['m00'])  # cy = M01/M00
        center = (cx, cy)
        cv2.circle(resized, center, 5, [0, 0, 255], 2)  # draws small circle at the center moment
        hull = cv2.convexHull(contours[ci])
        defects = cv2.convexityDefects(contours[ci], hull)

        if len(defects) > 0:
            for i in range(defects.shape[0]):
                s, e, f, d = defects[i, 0]
                start = tuple(contours[ci][s][0])
                end = tuple(contours[ci][e][0])
                far = tuple(contours[ci][f][0])
                cv2.line(resized, start, end, [0, 255, 0], 2)
                cv2.circle(resized, far, 5, [0, 0, 255], -1)
        else:
            cv2.drawContours(resized, [contours[ci]], 0, (0, 255, 0), 2)
            cv2.drawContours(resized, [hull], 0, (0, 0, 255), 2)

If anyone has come across a similar issue or knows where I am going wrong it would be a big help.

ilke444
  • 2,641
  • 1
  • 17
  • 31
  • 2
    Can you add an example image? – ilke444 Mar 11 '20 at 17:17
  • Also, can you explain exactly how you find the issue to be with `cv2.convexityDefects`? – Warpstar22 Mar 11 '20 at 22:35
  • @ilke444 I have the full code up on my github with a sample video there as well under the gesture_recognition branch: github.com/Pallesnik/hand_tracker – Peter Kinsella Mar 12 '20 at 15:19
  • @Warpstar22 after having gone through and doing testing I have stepped through the full code and found that that particular line just ends the program without an error. I am doing this for a project and my supervisor has also tested it and found the same issue – Peter Kinsella Mar 12 '20 at 15:20

1 Answers1

0

So the reason you are not seeing any errors is because of your try, catch block. Typically with a catch block you need to "catch" the errors. You have two options to see your error:

  1. Remove the try, catch statements. You'll see the full error here

  2. Write

catch Exception as e:
     print(e)
     break

Then you'll see that you have ValueError: too many values to unpack (expected 2). From what I've seen online, it seems like you might be basing your code off of an example that is using a different version of OpenCV from yours. I'm not sure what you are using but keep in mind that there is OpenCV2, OpenCV3, and OpenCV4 and then they have their minor versions.

Edit:
Actually, I just realized that you might have a different version of OpenCV. I am using Opencv 3.4.2 with Python3 that I think I installed with pip3. You might have a different error. I actually found the problem to be in cv2.findContours and not cv2.convexityDefects so you might not actually have a ValueError. Catch the exception or remove the try, catch statements and you should be able to find your problem and google the solution.

Warpstar22
  • 567
  • 4
  • 19