-
Notifications
You must be signed in to change notification settings - Fork 32
/
jpg_contour_hand.py
43 lines (35 loc) · 1.43 KB
/
jpg_contour_hand.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Hand contour detection
import imutils
import cv2
filename = "hand1.jpg" # background is dark
#filename = "hand2.jpg" # background is light
image = cv2.imread(filename)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5,5), 0)
cv2.imshow("blur", blur)
if filename=="hand1.jpg":
thresh = cv2.threshold(blur, 45, 255, cv2.THRESH_BINARY)[1]
if filename=="hand2.jpg":
thresh = cv2.threshold(blur, 230, 255, cv2.THRESH_BINARY_INV)[1]
cv2.imshow("thresh", thresh)
#thresh = cv2.erode(thresh, None, iterations=2)
#thresh = cv2.dilate(thresh, None, iterations=2)
# find contours in thresholded image, and grab the largest one
cnts = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
c = max(cnts, key=cv2.contourArea)
# determine the most extreme points along the contour
extLeft = tuple(c[c[:, :, 0].argmin()][0])
extRight = tuple(c[c[:, :, 0].argmax()][0])
extTop = tuple(c[c[:, :, 1].argmin()][0])
extBot = tuple(c[c[:, :, 1].argmax()][0])
# draw the outline of the object and its extreme points
cv2.drawContours(image, [c], -1, (0, 255, 255), 2)
cv2.circle(image, extLeft, 8, (0, 0, 255), -1)
cv2.circle(image, extRight, 8, (0, 255, 0), -1)
cv2.circle(image, extTop, 8, (255, 0, 0), -1)
cv2.circle(image, extBot, 8, (255, 255, 0), -1)
# show the output image
cv2.imshow("Image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()