Mp4 to Img

# -*- 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()
相關文章
相關標籤/搜索