基於OpenCV的手掌檢測和手指計數(hand_detection_and_finger_counting)

基於OpenCV的手掌檢測和手指計數(hand_detection_and_finger_counting)

基於OpenCV的手掌檢測和手指計數(hand_detection_and_finger_counting)


資料來源: https://mp.weixin.qq.com/s/YBhl1yBQ7SDVczna2CgxHQ

https://github.com/madhav727/hand-detection-and-finger-counting


文字摘要:

    ★偵測皮膚Mask
    ★輪廓線繪製
    ★凸包檢測
    ★凸缺陷檢測
    ★餘弦定理(角度小於90度或pi / 2 ,則將其視為手指)
    ★手指個數計算


Code

import cv2 as cv
import numpy as np

img_path = "data/palm.jpg"
img = cv.imread(img_path)
cv.imshow('palm image',img)


hsvim = cv.cvtColor(img, cv.COLOR_BGR2HSV)
lower = np.array([0, 48, 80], dtype = "uint8")
upper = np.array([20, 255, 255], dtype = "uint8")
skinRegionHSV = cv.inRange(hsvim, lower, upper)
blurred = cv.blur(skinRegionHSV, (2,2))
ret,thresh = cv.threshold(blurred,0,255,cv.THRESH_BINARY)
cv.imshow("thresh", thresh)


contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
contours = max(contours, key=lambda x: cv.contourArea(x))
cv.drawContours(img, [contours], -1, (255,255,0), 2)
cv.imshow("contours", img)

hull = cv.convexHull(contours)
cv.drawContours(img, [hull], -1, (0, 255, 255), 2)
cv.imshow("hull", img)


hull = cv.convexHull(contours, returnPoints=False)
defects = cv.convexityDefects(contours, hull)


if defects is not None:
cnt = 0
for i in range(defects.shape[0]): # calculate the angle
s, e, f, d = defects[i][0]
start = tuple(contours[s][0])
end = tuple(contours[e][0])
far = tuple(contours[f][0])
a = np.sqrt((end[0] - start[0]) ** 2 + (end[1] - start[1]) ** 2)
b = np.sqrt((far[0] - start[0]) ** 2 + (far[1] - start[1]) ** 2)
c = np.sqrt((end[0] - far[0]) ** 2 + (end[1] - far[1]) ** 2)
angle = np.arccos((b ** 2 + c ** 2 - a ** 2) / (2 * b * c)) # cosine theorem
if angle <= np.pi / 2: # angle less than 90 degree, treat as fingers
cnt += 1
cv.circle(img, far, 4, [0, 0, 255], -1)
if cnt > 0:
cnt = cnt+1
cv.putText(img, str(cnt), (0, 50), cv.FONT_HERSHEY_SIMPLEX,1, (255, 0, 0) , 2, cv.LINE_AA)

cv.imshow('final_result',img)


完整圖文:

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *