2

I'm trying to detect hand with OpenCV on Python.
I am working on this thresholded image:
Thresholded image

And that's contour drawed state:
Contour drawed image

I am trying to detect hand, but contour is too big, it captures my whole body.
I need it like this:
I need it like this


My code:

import cv2
orImage = cv2.imread("f.png")
image = cv2.cvtColor(orImage,cv2.COLOR_BGR2GRAY)
image = cv2.blur(image,(15,15))
(_,img_th) = cv2.threshold(image,96,255,1)
        (contours,_) = cv2.findContours(img_th, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
       if cv2.contourArea(c) > 15:
             x,y,w,h = cv2.boundingRect(c)
             cv2.rectangle(image,(x-20,y-20),(x+w+20,y+h+20),(0,255,0),2)
cv2.drawContours(image,contours,-1,(255,0,0),2)
cv2.imwrite("hi.jpg",image)




Thanks!

cagri
  • 807
  • 8
  • 17
  • To seperate hand contour from the rest of the body, you would need to work on threshold algorithm. Do you have actual input image? – v.coder Dec 30 '16 at 20:15
  • Or maybe you could detect edges in the actual image and then overlap it with the contour image. – v.coder Dec 30 '16 at 20:16
  • Yes I do; http://i.imgur.com/YOFXf9u.jpg but I think I need something like "max area" – cagri Dec 30 '16 at 20:21

1 Answers1

2

I have a solution (I got some help from HERE), it has many other wonderful tutorials on image processing exclusively for OpenCV users.)

I first converted the image you have uploaded to HSV color space:

HSV = cv2.cvtColor(orimage, cv2.COLOR_BGR2HSV)

I then set an approximate range for skin detection once the image is converted to HSV color space:

l = np.array([0, 48, 80], dtype = "uint8")
u = np.array([20, 255, 255], dtype = "uint8")

I then applied this range to the HSV image:

skinDetect = cv2.inRange(HSV, l, u)

This is what I obtained (I also resized the image to make it smaller):

enter image description here

Now you can find the biggest contour in this image followed by morphological operations to obtain the hand perfectly.

Hope this helps.

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
  • Just a reminder, the blog post that I mentioned has many other awesome tutorials too!! – Jeru Luke Dec 31 '16 at 11:52
  • I just came across another solution involving YCrCb Color space available on [THIS LINK](http://stackoverflow.com/questions/14752006/computer-vision-masking-a-human-hand/14756351#14756351) – Jeru Luke Dec 31 '16 at 12:16