OpenCV图像处理技术(Python)——摄像头以及视频处理

OpenCV图像处理技术(Python)——目标追踪
© Fu Xianjun. All Rights Reserved.

文章目录

  • 前言
  • 学习目标
  • 学习内容
    • 1.打开摄像头
    • 2.摄像头图像处理
    • 3.摄像头美颜
    • 4.人脸识别API
    • 5.视频处理
  • 总结


前言

家人们没错又是我,我又来讲解东西了。


学习目标

能够熟练的应用电脑摄像头的各种使用方式!!!

学习内容

1.打开摄像头

# 打开摄像头并灰度化显示
import cv2
cap = cv2.VideoCapture(0)
while(cap.isOpened()):
    # 获取一帧
    ret, frame = cap.read()
    cv2.imshow('frame', frame)
    if cv2.waitKey(1) == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

2.摄像头图像处理

import cv2
cap = cv2.VideoCapture(0)
while(cap.isOpened()):
    # 获取一帧
    ret, frame = cap.read()
    ##图像处理
    frame1=cv2.flip(frame,1)
    gray=cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
    canny=cv2.Canny(gray,80,150)
    
    ##
    cv2.imshow('frame', canny)
    if cv2.waitKey(1) == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()
import cv2
cap = cv2.VideoCapture(0)

while(cap.isOpened()):
    # 获取一帧
    ret, frame = cap.read()
    ##图像处理
    frame1=cv2.flip(frame,1)
    gray=cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
    canny=cv2.Canny(gray,80,150)
    
    ##
    cv2.imshow('frame', canny)
    if cv2.waitKey(1) == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

3.摄像头美颜

def meiyan(img):
    rows, cols = img.shape[:2]
    dst = np.zeros((rows, cols, 3), dtype="uint8")

    #图像怀旧特效
    for i in range(rows):
        for j in range(cols):
            B = 0.272*img[i,j][2] + 0.534*img[i,j][1] + 0.131*img[i,j][0]
            G = 0.349*img[i,j][2] + 0.686*img[i,j][1] + 0.168*img[i,j][0]
            R = 0.393*img[i,j][2] + 0.769*img[i,j][1] + 0.189*img[i,j][0]
            if B>255:
                B = 255
            if G>255:
                G = 255
            if R>255:
                R = 255
            dst[i,j] = np.uint8((B, G, R))
    return dst
# 获得视频属性
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
width, height = cap.get(3), cap.get(4)
while(cap.isOpened()):
    ret, frame = cap.read()
    frame = meiyan(frame)
    cv2.imshow('frame', frame)
    if cv2.waitKey(1) == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()
print(width, height)

4.人脸识别API

切记要有这三个文件
在这里插入图片描述

import cv2
import numpy as np

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
smile_cascade = cv2.CascadeClassifier(cv2.data.haarcascades+'haarcascade_smile.xml')

cap = cv2.VideoCapture(0)
width=1280
height=960
cap.set(cv2.CAP_PROP_FRAME_WIDTH,width)#设置图像宽度
cap.set(cv2.CAP_PROP_FRAME_HEIGHT,height)#设置图像高度
fgbg = cv2.createBackgroundSubtractorMOG2(
    history=500, varThreshold=100, detectShadows=False)#基于自适应混合高斯背景建模的背景减除法,去除干扰

cnt=1
while(1):
    # get a frame
    ret, frame = cap.read()
    # show a frame
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5,0) #把灰度图片传给haar进行灰度处理,返回值是人脸左上角坐标,宽度和高度
    #1.3为缩放比例,默认为1.1即每次搜索窗口依次扩大10%
    #5为构成检测目标的相邻矩形的最小个数
    #0为flag,表示使用边缘抑制,会使用Canny边缘检测来排除边缘过多或过少的区域
    for(x, y, w, h) in faces:
        
        img = cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)

        roi_gray = gray[y:y+h, x:x+w]

        eyes = eye_cascade.detectMultiScale(roi_gray, 1.8, 5,0)

        roi_color = img[y:y + h, x:x + w]

        for(ex, ey, ew, eh) in eyes:
             cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), 2)
        smiles = smile_cascade.detectMultiScale(roi_gray, scaleFactor = 1.16,\
                                                minNeighbors= 65, minSize=(25,25), \
                                                flags = cv2.CASCADE_SCALE_IMAGE)
        
        for (ex,ey,ew,eh) in smiles:
            # 画出微笑框,红色(BGR色彩体系),画笔宽度为1
            cv2.rectangle(roi_gray, (ex,ey), (ex+ew,ey+eh), (0,0,255), 1)
            cv2.putText(img, "smile", (x,y-7), 3, 1.2, (0,0,225), 2, cv2.LINE_AA)
            #cv2.LINE_AA 为抗锯齿,这样看起来会非常平滑
            #cv2.imwrite(f"img{cnt}.png",img)
            cnt+=1
    cv2.imshow("camera", frame)
    if cv2.waitKey(5) == ord('q'):
         break
cap.release()
cv2.destroyAllWindows() 

5.视频处理

#获取视频
import cv2
cap = cv2.VideoCapture('caruav.mp4')
while(cap.isOpened()):
    # 获取一帧
    ret, frame = cap.read()
    frame=cv2.Canny(frame,100,200)
    cv2.imshow('frame', frame)    
    if cv2.waitKey(25) == ord('q'):
        break
        
cap.release()
cv2.destroyAllWindows()

总结

今天的学习内容就到这里了
谢谢家人们赏脸观看

你可能感兴趣的:(OpenCV图像处理技术(Python)——摄像头以及视频处理)