# -*- coding: utf-8 -*-
"""
Created on Thu May  3 16:51:50 2018

"""


# 录像转换为图片
from time import gmtime, strftime
import cv2
import numpy as np

def rotate(image, angle, center=None, scale=1.0):
    # 获取图像尺寸
    (h, w) = image.shape[:2]

    # 若未指定旋转中心,则将图像中心设为旋转中心
    if center is None:
        center = (w / 2, h / 2)

    # 执行旋转
    M = cv2.getRotationMatrix2D(center, angle, scale)
    rotated = cv2.warpAffine(image, M, (w, h))

    # 返回旋转后的图像
    return rotated

videoFile = 'py5.mp4'
cap = cv2.VideoCapture(videoFile)
numF=cap.get(cv2.CAP_PROP_FRAME_COUNT)
fps=cap.get(cv2.CAP_PROP_FPS)
#cap.set(cv2.CAP_PROP_FRAME_WIDTH,640)
#cap.set(cv2.CAP_PROP_FRAME_HEIGHT,480)

while(True):
    ret, frame = cap.read()
    if ret ==True:
        img = frame
        #img=rotate(frame,-90)     
        #img=np.rot90(frame)
        #img=np.rot90(img) 
        #img=np.rot90(img) 
        cv2.imshow('my', img)
        
        # 肤色检测之一: YCrCb之Cr分量 + OTSU二值化
#        img= cv2.imread('YCbCr OR.jpg', cv2.IMREAD_COLOR)
        ycrcb = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb) # 把图像转换到YUV色域
        (y, cr, cb) = cv2.split(ycrcb) # 图像分割, 分别获取y, cr, br通道图像
        # 高斯滤波, cr 是待滤波的源图像数据, (5,5)是值窗口大小, 0 是指根据窗口大小来计算高斯函数标准差
        cr1 = cv2.GaussianBlur(cr, (5, 5), 0) # 对cr通道分量进行高斯滤波
        # 根据OTSU算法求图像阈值, 对图像进行二值化
        #_, skin1 = cv2.threshold(cr1, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) 
        _, skin1 = cv2.threshold(cr1, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
        #cv2.imshow("image CR", cr1)
#        cv2.imshow("Skin Cr+OSTU", skin1 )
        
#        img0 = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)#将图片转换为灰度图片
        f = strftime("%Y%m%d%H%M%S.jpg", gmtime())   
        cv2.imwrite('2/'+ f, skin1)
        #if img.size == 0:
         #   break
        
    if cv2.waitKey(200) & 0xFF == ord('q'):
        break

cap.release
cv2.destroyAllWindows()

 

posted on 2019-02-25 11:13  Manuel  阅读(149)  评论(0编辑  收藏  举报