现在使用python写一下视频逐帧将手部语义分割出来
- User
现在使用python写一下视频逐帧将手部语义分割出来
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
现在使用python写一下视频逐帧将手部语义分割出来
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
要实现视频逐帧手部语义分割,我们可以采用以下两种主流方法:
下面我将分别给出两种方法的完整实现代码,并推荐最适合课程设计的方案。
import cv2
import numpy as np
def skin_detection(frame):
"""基于HSV/YCrCb色彩空间的肤色检测"""
# 转换为YCrCb色彩空间(对光照变化更鲁棒)
ycrcb = cv2.cvtColor(frame, cv2.COLOR_BGR2YCrCb)
# 定义肤色范围(需要根据实际调整)
lower_skin = np.array([0, 135, 85], dtype=np.uint8)
upper_skin = np.array([255, 180, 135], dtype=np.uint8)
# 获取肤色掩膜
mask = cv2.inRange(ycrcb, lower_skin, upper_skin)
# 形态学处理
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel, iterations=2)
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel, iterations=2)
return mask
def find_hand_contour(mask):
"""从掩膜中提取最大轮廓(手部区域)"""
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
if not contours:
return None
# 获取面积最大的轮廓
max_contour = max(contours, key=cv2.contourArea)
# 过滤小面积噪声
if cv2.contourArea(max_contour) < 1000:
return None
return max_contour
# 主处理流程
cap = cv2.VideoCapture(0) # 打开摄像头
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# 镜像翻转(更符合直觉)
frame = cv2.flip(frame, 1)
# 肤色检测
skin_mask = skin_detection(frame)
# 寻找手部轮廓
hand_contour = find_hand_contour(skin_mask)
# 可视化结果
if hand_contour is not None:
# 绘制轮廓
cv2.drawContours(frame, [hand_contour], -1, (0, 255, 0), 2)
# 获取轮廓外接矩形
x, y, w, h = cv2.boundingRect(hand_contour)
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
# 显示结果
cv2.imshow('Hand Segmentation', frame)
cv2.imshow('Skin Mask', skin_mask)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
import cv2
import mediapipe as mp
# 初始化MediaPipe手部模型
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(
static_image_mode=False,
max_num_hands=1,
min_detection_confidence=0.5,
min_tracking_confidence=0.5)
mp_drawing = mp.solutions.drawing_utils
cap = cv2.VideoCapture(0)
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
frame = cv2.flip(frame, 1)
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# 手部检测
results = hands.process(rgb_frame)
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
# 绘制手部关键点和连接线
mp_drawing.draw_landmarks(
frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
# 获取手部外接矩形
h, w, _ = frame.shape
landmarks = []
for landmark in hand_landmarks.landmark:
x, y = int(landmark.x * w), int(landmark.y * h)
landmarks.append((x, y))
if landmarks:
x_coords = [p[0] for p in landmarks]
y_coords = [p[1] for p in landmarks]
x1, y1 = min(x_coords), min(y_coords)
x2, y2 = max(x_coords), max(y_coords)
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.imshow('MediaPipe Hands', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
建议采用传统方法作为课程设计主体,因为:
在报告中可以:
如果需要更精确的分割,可以在传统方法基础上加入:
# 背景减除(需先拍摄几帧纯背景)
fgbg = cv2.createBackgroundSubtractorMOG2()
fgmask = fgbg.apply(frame)
skin_mask = cv2.bitwise_and(skin_mask, fgmask)