|
- import cv2
- import numpy as np
-
- def main():
- img = cv2.imread('10076.jpg')
-
- img_shape = img.shape
- length = img_shape[0]
- height = img_shape[1]
-
- if length < height:
- img = cv2.resize(img, (800, int(800 * length / height)))
- else:
- img = cv2.resize(img, (int(800 * height / length), 800))
-
- # img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
- skinMask = HSVBin(img)
- contours = getContours(skinMask)
- cv2.drawContours(img,contours,-1,(0,255,0),2)
- cv2.imshow('capture',img)
- k = cv2.waitKey(0)
-
- 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)
- vaildContours = []
- for cont in contours:
- if cv2.contourArea(cont)>9000:
- #x,y,w,h = cv2.boundingRect(cont)
- #if h/w >0.75:
- #filter face failed
- vaildContours.append(cv2.convexHull(cont))
- #rect = cv2.minAreaRect(cont)
- #box = cv2.cv.BoxPoint(rect)
- #vaildContours.append(np.int0(box))
- return vaildContours
-
- def HSVBin(img):
- hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
- # cv2.imshow('hsv',hsv)
-
- '''
- lower_skin = np.array([11,43,46])
- upper_skin = np.array([34,255,255])
- '''
- lower_skin = np.array([14,43,46])
- upper_skin = np.array([30,255,255])
-
- mask = cv2.inRange(hsv,lower_skin,upper_skin)
- # cv2.imshow('mask',mask)
- res = cv2.bitwise_and(img,img,mask=mask)
- cv2.imshow('res',res)
- return mask
-
- if __name__ =='__main__':
- main()
|