Task1:python(opencv)手势轮廓提取

效果图:
在这里插入图片描述

在这里插入图片描述

获取视频(摄像头)

def main():
    cap = cv2.VideoCapture(0)  # Read from camera
    while cap.isOpened():
        try:
            ret, frame = cap.read()
            if ret:
                # Display the video feed
                cv2.imshow("Video Feed", frame)
                 # Perform gesture recognition
                gesture = gesture_recognition(frame)
                cv2.imshow("Gesture", gesture)
             # Wait for user input to exit
            key = cv2.waitKey(50) & 0xFF
            if key == ord('q'):
                break
        except:
            break
     # Release resources and close windows
    cap.release()
    cv2.destroyAllWindows()

肤色检测和轮廓处理

def gesture_recognition(frame):
    # Crop the video frame to the region of interest
    roi = frame[:,:]
     # Skin detection
    YCrCb = cv2.cvtColor(roi, cv2.COLOR_BGR2YCR_CB)
    (y,cr,cb) = cv2.split(YCrCb)
    cr1 = cv2.GaussianBlur(cr, (5,5), 0)
    _, skin = cv2.threshold(cr1, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    res = cv2.bitwise_and(roi, roi, mask = skin)
     # Edge detection
    gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
    dst = cv2.Laplacian(gray, cv2.CV_16S, ksize = 3)
    laplacian = cv2.convertScaleAbs(dst)
     # Find contours
    h = cv2.findContours(laplacian, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
    contour = h[0]
    contour = sorted(contour, key = cv2.contourArea, reverse=True)
    bg = np.ones(laplacian.shape, np.uint8) * 255
    ret = cv2.drawContours(bg, contour[0], -1, (0,0,0), 3)
    return ret

全部代码

import cv2
import numpy as np
 # Function for gesture recognition
def gesture_recognition(frame):
    # Crop the video frame to the region of interest
    roi = frame[:,:]
     # Skin detection
    YCrCb = cv2.cvtColor(roi, cv2.COLOR_BGR2YCR_CB)
    (y,cr,cb) = cv2.split(YCrCb)
    cr1 = cv2.GaussianBlur(cr, (5,5), 0)
    _, skin = cv2.threshold(cr1, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    res = cv2.bitwise_and(roi, roi, mask = skin)
     # Edge detection
    gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
    dst = cv2.Laplacian(gray, cv2.CV_16S, ksize = 3)
    laplacian = cv2.convertScaleAbs(dst)
     # Find contours
    h = cv2.findContours(laplacian, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
    contour = h[0]
    contour = sorted(contour, key = cv2.contourArea, reverse=True)
    bg = np.ones(laplacian.shape, np.uint8) * 255
    ret = cv2.drawContours(bg, contour[0], -1, (0,0,0), 3)
    return ret
 # Main function
def main():
    # cap = cv2.VideoCapture(r"E:\szg.mp4")  # Read video file
    cap = cv2.VideoCapture(0)  # Read from camera
    while cap.isOpened():
        try:
            ret, frame = cap.read()
            if ret:
                # Display the video feed
                cv2.imshow("Video Feed", frame)
                 # Perform gesture recognition
                gesture = gesture_recognition(frame)
                cv2.imshow("Gesture", gesture)
             # Wait for user input to exit
            key = cv2.waitKey(50) & 0xFF
            if key == ord('q'):
                break
        except:
            break
     # Release resources and close windows
    cap.release()
    cv2.destroyAllWindows()
if __name__ == '__main__':
    main()
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值