cagri
cagri

Reputation: 828

OpenCV Splitting Contours

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!

Upvotes: 2

Views: 4541

Answers (1)

Jeru Luke
Jeru Luke

Reputation: 21233

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.

Upvotes: 2

Related Questions