-
Notifications
You must be signed in to change notification settings - Fork 3
/
adaptive.py
45 lines (34 loc) · 1.45 KB
/
adaptive.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
44
45
import cv2
import numpy as np
# Load the image
img = cv2.imread('dataset_sample/Women/3/3_woman (3).jpg')
# Convert the image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Apply adaptive thresholding to the grayscale image
thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
# Apply a median blur to the thresholded image to remove noise
blur = cv2.medianBlur(thresh, 7)
# Convert the image to the YCrCb color space
ycrcb = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)
# Apply a skin color range filter to the YCrCb image
lower_skin = np.array([0, 135, 85])
upper_skin = np.array([255, 180, 135])
mask = cv2.inRange(ycrcb, lower_skin, upper_skin)
# # Combine the thresholded image and the skin color mask
hand = cv2.bitwise_and(blur, blur, mask=mask)
sift = cv2.SIFT_create()
keypoints, descriptors = sift.detectAndCompute(mask, None)
sift_img = cv2.drawKeypoints(mask, keypoints, None, (255, 0, 0), 4)
sift_img2 = cv2.drawKeypoints(hand, keypoints, None, (255, 0, 0), 4)
print(descriptors.shape)
# Display the result
cv2.namedWindow('Hand detection', cv2.WINDOW_NORMAL)
cv2.resizeWindow('Hand detection', 800, 600)
cv2.imshow('Hand detection', sift_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.namedWindow('Hand detection', cv2.WINDOW_NORMAL)
cv2.resizeWindow('Hand detection', 800, 600)
cv2.imshow('Hand detection', sift_img2)
cv2.waitKey(0)
cv2.destroyAllWindows()