Shashank
Shashank

Reputation: 199

Dectection of finger tip

I am trying to calculate the pose of the hand in a image. So, I have extracted hand from the image. To extract finger tip from the hand I have used convexHull(). I am getting this error "points is not a numpy array, neither a scalar".

Here is the code:

import numpy as np
import cv2

img = cv2.imread("hand.jpg")
hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV);
lower_hand = np.array([0,30,60])
upper_hand = np.array([20,150,255])

mask = cv2.inRange(hsv, lower_hand, upper_hand)

res = cv2.bitwise_and(img, img, mask=mask)

derp,contours,hierarchy = cv2.findContours(mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
hull = cv2.convexHull(contours)
print hull
cv2.drawContours(img, contours, -1, (0,255,0), 3)

Upvotes: 0

Views: 518

Answers (1)

Maarten Peels
Maarten Peels

Reputation: 1022

findContours returns: "detected contours. Each contour is stored as a vector of points." And convexHull() needs a 2D point set. So i think you will need a for loop for this, like so:

    for cnt in contours:
        hull = cv2.convexHull(cnt)
        cv2.drawContours(img,[cnt],0,(0,255,0),2)
        cv2.drawContours(img,[hull],0,(0,0,255),2)

Full working code:

import numpy as np
import cv2

img = cv2.imread("hand.jpg")
hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV);
lower_hand = np.array([0,30,60])
upper_hand = np.array([20,150,255])

mask = cv2.inRange(hsv, lower_hand, upper_hand)

res = cv2.bitwise_and(img, img, mask=mask)

#"derp" wasn't needed in my code tho..
derp,contours,hierarchy = cv2.findContours(mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
    hull = cv2.convexHull(cnt)
    cv2.drawContours(img,[cnt],0,(0,255,0),2)
    cv2.drawContours(img,[hull],0,(0,0,255),2)

cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Result:

enter image description here

Upvotes: 1

Related Questions