基于opencv的姿态识别探索

2018-04-21  本文已影响0人  瑶瑶_2930

给一个觉得比较好的数据集(http://www.nada.kth.se/cvap/actions/)

手势识别之手势检测

代码参考于(https://github.com/rainyear/lolita/issues/8)

import cv2
import numpy as np

def main():
   cap = cv2.VideoCapture(0)
   while(cap.isOpened()):
       ret, img       = cap.read()

       skinMask = HSVBin(img)
       contours = getContours(skinMask)
       cv2.drawContours(img, contours, -1, (0, 255, 0), 2)
       cv2.imshow('capture', img)
       k = cv2.waitKey(10)
       if k == 27:
           break
def getContours(img):
   kernel = np.ones((5,5),np.uint8)
   closed = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
   closed = cv2.morphologyEx(closed, cv2.MORPH_CLOSE, kernel)
   _,contours, h  = cv2.findContours(closed, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
   validContours = [];
   for cont in contours:
       if cv2.contourArea(cont) > 9000:
           # x,y,w,h = cv2.boundingRect(cont)
           # if h/w > 0.75:
           validContours.append(cv2.convexHull(cont))
           # rect = cv2.minAreaRect(cont)
           # box = cv2.cv.BoxPoints(rect)
           # validContours.append(np.int0(box))
   return validContours
# 皮肤检测,皮肤颜色在HSV颜色空间下与周围环境区分度更高
# 所以把RGB转换到HSV颜色空间下针对皮肤颜色进行二值化,得到mask
def HSVBin(img):
   hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)

   lower_skin = np.array([100, 50, 0])
   upper_skin = np.array([125, 255, 255])

   mask = cv2.inRange(hsv, lower_skin, upper_skin)
   # res = cv2.bitwise_and(img, img, mask=mask)
   return mask
if __name__ == '__main__':
   main()
import cv2  
  
img = cv2.imread('D:\\test\\contour.jpg')  
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  
ret, binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)  
  
contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)  
cv2.drawContours(img,contours,-1,(0,0,255),3)  
  
cv2.imshow("img", img)  

函数原型:

contours, hierarchy = cv2.findContours(image, mode, method[, contours[, hierarchy[, offset]]])

参数说明:

  1. mode:轮廓检索模式。RETR_TREE: 建立一个等级树结构的轮廓
  2. method:轮廓的近似办法。cv2.CHAIN_APPROX_SIMP:压缩水平方向、垂直方向、对角线方向的元素,只保留该方向的终点坐标。例如一个矩形轮廓只需要 4 个点来保存轮廓信息
  3. contours-返回的轮廓,返回一个list,list中每个元素是图像中一个轮廓
    4.hierarchy-每条轮廓对应的属性
    5.cv2.drawContours()绘制轮廓
cv2.drawContours(image, contours, contourIdx, color[, thickness[, lineType[, hierarchy[, maxLevel[, offset ]]]]])
 
  1. coutourIdx:指定绘制哪条轮廓,如果为-1则所有,
    7.(0,0,225)表颜色
上一篇 下一篇

猜你喜欢

热点阅读