读取电脑自带摄像头

读取摄像头:

import numpy as np
import cv2 as cv

cap = cv.VideoCapture(0)
while True:
    ret, frame = cap.read()
    # 参数ret 为True 或者False,代表有没有读取到图片
    # 参数frame 表示截取到一帧的图片
    cv.imshow('frame', frame)  # 显示帧图片

    if cv.waitKey(1) == 27: # 判断按键,按下即退出
        break

cap.release()  # 释放图像资源
cv.destroyAllWindows()  # 关闭窗口

说明:waitKey()一定要有的,如果没有,就直接白屏了,图像读取一直运行,人的视觉跟不上,如果是0,那就只能播放一张就卡一张,因为0的话他等待按键时间是永远

播放电脑中的视频文件就直接将路径放在VideoCapture()中就可以了其他均不变

cap = cv.VideoCapture(file_address)
while True:
    ret, frame = cap.read()
    # 参数ret 为True 或者False,代表有没有读取到图片
    # 参数frame 表示截取到一帧的图片
    cv.imshow('frame', frame)  # 显示帧图片

    if cv.waitKey(1) & 0xff == ord('a'):  # 与上0xff还是原始值,这里是来取waiKey返回值的低八位,防止出现bug
        break
cap.release()  # 释放图像资源
cv.destroyAllWindows()  # 关闭窗口

 分出来GBR(灰度):

import numpy as np
import cv2 as cv
import matplotlib as plt
cap = cv.VideoCapture(0)
while True:
    ret, frame = cap.read()
    b, g, r = cv.split(frame)
    bgr = cv.merge((b, g, r))
    cv.imshow('frame1', b)
    cv.imshow('frame2', g)
    cv.imshow('frame3', r)
    cv.imshow('bgr', bgr)
    if cv.waitKey(1) == 27:
        break

其他通道设0:

import numpy as np
import cv2 as cv
import matplotlib as plt
cap = cv.VideoCapture(0)
while True:
    ret, frame = cap.read()
    b = frame
    # g = frame
    # r = frame

    b[:, :, 1] = 0
    b[:, :, 2] = 0

    # g[:, :, 0] = 0
    # g[:, :, 2] = 0

    # r[:, :, 0] = 0
    # r[:, :, 1] = 0

    cv.imshow('b', b)
    # cv.imshow('g', g)
    # cv.imshow('r', r)

    if cv.waitKey(1) == 27:
        break

边界填充:

import numpy as np
import cv2 as cv
import matplotlib as plt
cap = cv.VideoCapture(0)
top_size, bottom_size, left_size, right_size = (50, 50, 50, 50)
while True:
    ret, frame = cap.read()
    frame1 = cv.copyMakeBorder(frame, top_size, bottom_size, left_size, right_size, cv.BORDER_WRAP)
    cv.imshow('frame1', frame1)
    if cv.waitKey(1) == 27:
        break

阈值:

import numpy as np
import cv2 as cv
import matplotlib as plt
cap = cv.VideoCapture(0)
top_size, bottom_size, left_size, right_size = (50, 50, 50, 50)
while True:
    ret, frame = cap.read()
    ret, dst = cv.threshold(frame, 127, 255, cv.THRESH_BINARY_INV)
    cv.imshow('dst', dst)
    if cv.waitKey(1) == 27:
        break

均值滤波(卷积和):

import numpy as np
import cv2 as cv
import matplotlib as plt
cap = cv.VideoCapture(0)
top_size, bottom_size, left_size, right_size = (50, 50, 50, 50)
while True:
    ret, frame = cap.read()
    blur = cv.blur(frame, (10, 10))
    # 参数越大越模糊
    cv.imshow('blur', blur)
    cv.imshow('frame', frame)
    if cv.waitKey(1) == 27:
        break

高斯滤波(高斯函数分配周围权重):

import numpy as np
import cv2 as cv
import matplotlib as plt
cap = cv.VideoCapture(0)
while True:
    ret, frame = cap.read()
    gauss = cv.GaussianBlur(frame, (5, 5), 1)
   
    cv.imshow('gauss', gauss)
    cv.imshow('frame', frame)
    if cv.waitKey(1) == 27:
        break

中值滤波:

import numpy as np
import cv2 as cv
import matplotlib as plt
cap = cv.VideoCapture(0)
while True:
    ret, frame = cap.read()
    median = cv.medianBlur(frame, 3)

    cv.imshow('median', median)
    cv.imshow('frame', frame)
    if cv.waitKey(1) == 27:
        break

水平拼接:

import numpy as np
import cv2 as cv
import matplotlib as plt
cap = cv.VideoCapture(0)
while True:
    ret, frame = cap.read()
    blur = cv.blur(frame, (3, 3))
    gauss = cv.GaussianBlur(frame, (3, 3), 1)
    median = cv.medianBlur(frame, 3)

    # 拼接一起显示
    image_add = np.hstack((frame, blur, gauss, median))
    cv.imshow('image_add', image_add)
    if cv.waitKey(1) == 27:
        break

腐蚀:

import numpy as np
import cv2 as cv
import matplotlib as plt
cap = cv.VideoCapture(0)
kerne = np.ones((10, 10), np.uint8)
while True:
    ret, frame = cap.read()
    ersion = cv.erode(frame, kerne, 100)
    add = np.hstack((frame, ersion))
    cv.imshow('add', add)
    if cv.waitKey(1) == 27:
        break

膨胀(一般操作先腐蚀再膨胀,去除杂点):

import numpy as np
import cv2 as cv
import matplotlib as plt
cap = cv.VideoCapture(0)
kerne = np.ones((10, 10), np.uint8)
while True:
    ret, frame = cap.read()
    dilate = cv.dilate(frame, kerne, 50)
    
    add = np.hstack((frame, dilate))
    cv.imshow('add', add)
    if cv.waitKey(1) == 27:
        break

开运算(先腐蚀,再膨胀):

import numpy as np
import cv2 as cv
import matplotlib as plt
cap = cv.VideoCapture(0)
kerne = np.ones((10, 10), np.uint8)
while True:
    ret, frame = cap.read()
    opening = cv.morphologyEx(frame, cv.MORPH_OPEN, kerne)
    add = np.hstack((frame, opening))
    cv.imshow('add', add)
    if cv.waitKey(1) == 27:
        break

闭运算(先膨胀,再腐蚀):

import numpy as np
import cv2 as cv
import matplotlib as plt
cap = cv.VideoCapture(0)
kerne = np.ones((10, 10), np.uint8)
while True:
    ret, frame = cap.read()
    opening = cv.morphologyEx(frame, cv.MORPH_CLOSE, kerne)
    add = np.hstack((frame, opening))
    cv.imshow('add', add)
    if cv.waitKey(1) == 27:
        break

梯度操作(膨胀-腐蚀,求解边缘):

import numpy as np
import cv2 as cv
import matplotlib as plt
cap = cv.VideoCapture(0)
kerne = np.ones((10, 10), np.uint8)
while True:
    ret, frame = cap.read()
    gradident = cv.morphologyEx(frame, cv.MORPH_GRADIENT, kerne)
    add = np.hstack((frame, gradident))
    cv.imshow('add', add)
    if cv.waitKey(1) == 27:
        break
原文地址:https://www.cnblogs.com/CaiFengYuanXing/p/13775303.html