python代码
""" 从视频读取帧保存为图片"""
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
def A(img):
YCrCb = cv2.cvtColor(img, 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(img,img, mask = skin)
return res
def B(img):
h = cv2.findContours(img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
contour = h[0]
contour = sorted(contour, key = cv2.contourArea, reverse=True)
bg = np.ones(dst.shape, np.uint8) *255
ret = cv2.drawContours(bg,contour[0],-1,(0,0,0),3)
return ret
while(True):
ret, frame = cap.read()
src = cv2.resize(frame,(400,350), interpolation=cv2.INTER_CUBIC)
cv2.rectangle(src, (90, 60), (300, 300 ), (0, 255, 0))
roi = src[60:300 , 90:300]
res = A(roi)
cv2.imshow("0",roi)
gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
dst = cv2.Laplacian(gray, cv2.CV_16S, ksize = 3)
Laplacian = cv2.convertScaleAbs(dst)
contour = B(Laplacian)
cv2.imshow("2",contour)
key = cv2.waitKey(50) & 0xFF
if key == ord('q'):
break
cap.release()
cv2.destroyAllWindows()