📜  Python – 使用 OpenCV 处理视频图像

📅  最后修改于: 2022-05-13 01:55:07.136000             🧑  作者: Mango

Python – 使用 OpenCV 处理视频图像

处理视频是指逐帧对视频进行操作。帧只是视频在单个时间点的特定实例。即使在一秒钟内,我们也可能有多个帧。帧可以被视为类似于图像。
因此,我们可以对图像执行的任何操作也可以在帧上执行。让我们通过示例来看看一些操作。

自适应阈值 –

通过使用这种技术,我们可以在帧的小区域上应用阈值。因此,整个框架的集体价值会有所不同。

Python3
# importing the necessary libraries
import cv2
import numpy as np
 
# Creating a VideoCapture object to read the video
cap = cv2.VideoCapture('sample.mp4')
 
 
# Loop until the end of the video
while (cap.isOpened()):
 
    # Capture frame-by-frame
    ret, frame = cap.read()
    frame = cv2.resize(frame, (540, 380), fx = 0, fy = 0,
                         interpolation = cv2.INTER_CUBIC)
 
    # Display the resulting frame
    cv2.imshow('Frame', frame)
 
    # conversion of BGR to grayscale is necessary to apply this operation
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
 
    # adaptive thresholding to use different threshold
    # values on different regions of the frame.
    Thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
                                           cv2.THRESH_BINARY_INV, 11, 2)
 
    cv2.imshow('Thresh', Thresh)
    # define q as the exit button
    if cv2.waitKey(25) & 0xFF == ord('q'):
        break
 
# release the video capture object
cap.release()
# Closes all the windows currently opened.
cv2.destroyAllWindows()


Python3
# importing the necessary libraries
import cv2
import numpy as np
 
# Creating a VideoCapture object to read the video
cap = cv2.VideoCapture('sample.mp4')
 
 
# Loop until the end of the video
while (cap.isOpened()):
    # Capture frame-by-frame
    ret, frame = cap.read()
    frame = cv2.resize(frame, (540, 380), fx = 0, fy = 0,
                         interpolation = cv2.INTER_CUBIC)
 
    # Display the resulting frame
    cv2.imshow('Frame', frame)
 
    # using cv2.Gaussianblur() method to blur the video
 
    # (5, 5) is the kernel size for blurring.
    gaussianblur = cv2.GaussianBlur(frame, (5, 5), 0)
    cv2.imshow('gblur', gaussianblur)
 
    # define q as the exit button
    if cv2.waitKey(25) & 0xFF == ord('q'):
        break
 
# release the video capture object
cap.release()
 
# Closes all the windows currently opened.
cv2.destroyAllWindows()


Python3
# importing the necessary libraries
import cv2
import numpy as np
 
# Creating a VideoCapture object to read the video
cap = cv2.VideoCapture('sample.mp4')
 
 
# Loop until the end of the video
while (cap.isOpened()):
    # Capture frame-by-frame
    ret, frame = cap.read()
 
    frame = cv2.resize(frame, (540, 380), fx = 0, fy = 0,
                         interpolation = cv2.INTER_CUBIC)
 
    # Display the resulting frame
    cv2.imshow('Frame', frame)
 
    # using cv2.Canny() for edge detection.
    edge_detect = cv2.Canny(frame, 100, 200)
    cv2.imshow('Edge detect', edge_detect)
 
    # define q as the exit button
    if cv2.waitKey(25) & 0xFF == ord('q'):
        break
 
# release the video capture object
cap.release()
# Closes all the windows currently opened.
cv2.destroyAllWindows()


Python3
# importing the necessary libraries
import cv2
import numpy as np
 
# Creating a VideoCapture object to read the video
cap = cv2.VideoCapture('sample.mp4')
 
 
# Loop until the end of the video
while (cap.isOpened()):
    # Capture frame-by-frame
    ret, frame = cap.read()
    frame = cv2.resize(frame, (540, 380), fx = 0, fy = 0,
                         interpolation = cv2.INTER_CUBIC)
 
    # Display the resulting frame
    cv2.imshow('Frame', frame)
     
    # conversion of BGR to grayscale is necessary to apply this operation
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
 
    _, mask = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
 
    # apply NOT operation on image and mask generated by thresholding
    BIT = cv2.bitwise_not(frame, frame, mask = mask)
    cv2.imshow('BIT', BIT)
 
    # define q as the exit button
    if cv2.waitKey(25) & 0xFF == ord('q'):
        break
 
# release the video capture object
cap.release()
 
# Closes all the windows currently opened.
cv2.destroyAllWindows()


输出:

平滑 –

平滑视频意味着消除视频的清晰度并为视频提供模糊性。有多种平滑方法,例如 cv2.Gaussianblur()、cv2.medianBlur()、cv2.bilateralFilter()。出于我们的目的,我们将使用 cv2.Gaussianblur()。

Python3

# importing the necessary libraries
import cv2
import numpy as np
 
# Creating a VideoCapture object to read the video
cap = cv2.VideoCapture('sample.mp4')
 
 
# Loop until the end of the video
while (cap.isOpened()):
    # Capture frame-by-frame
    ret, frame = cap.read()
    frame = cv2.resize(frame, (540, 380), fx = 0, fy = 0,
                         interpolation = cv2.INTER_CUBIC)
 
    # Display the resulting frame
    cv2.imshow('Frame', frame)
 
    # using cv2.Gaussianblur() method to blur the video
 
    # (5, 5) is the kernel size for blurring.
    gaussianblur = cv2.GaussianBlur(frame, (5, 5), 0)
    cv2.imshow('gblur', gaussianblur)
 
    # define q as the exit button
    if cv2.waitKey(25) & 0xFF == ord('q'):
        break
 
# release the video capture object
cap.release()
 
# Closes all the windows currently opened.
cv2.destroyAllWindows()

输出:

边缘检测 –

边缘检测是一种有用的技术来检测视频中表面和物体的边缘。边缘检测涉及以下步骤:

  • 降噪
  • 梯度计算
  • 非最大抑制
  • 双阈值
  • 通过滞后进行边缘跟踪

Python3

# importing the necessary libraries
import cv2
import numpy as np
 
# Creating a VideoCapture object to read the video
cap = cv2.VideoCapture('sample.mp4')
 
 
# Loop until the end of the video
while (cap.isOpened()):
    # Capture frame-by-frame
    ret, frame = cap.read()
 
    frame = cv2.resize(frame, (540, 380), fx = 0, fy = 0,
                         interpolation = cv2.INTER_CUBIC)
 
    # Display the resulting frame
    cv2.imshow('Frame', frame)
 
    # using cv2.Canny() for edge detection.
    edge_detect = cv2.Canny(frame, 100, 200)
    cv2.imshow('Edge detect', edge_detect)
 
    # define q as the exit button
    if cv2.waitKey(25) & 0xFF == ord('q'):
        break
 
# release the video capture object
cap.release()
# Closes all the windows currently opened.
cv2.destroyAllWindows()

输出:

位运算 –

按位运算可用于将视频的不同帧屏蔽在一起。位运算就像我们在课堂上学过的AND、OR、NOT、XOR。

Python3

# importing the necessary libraries
import cv2
import numpy as np
 
# Creating a VideoCapture object to read the video
cap = cv2.VideoCapture('sample.mp4')
 
 
# Loop until the end of the video
while (cap.isOpened()):
    # Capture frame-by-frame
    ret, frame = cap.read()
    frame = cv2.resize(frame, (540, 380), fx = 0, fy = 0,
                         interpolation = cv2.INTER_CUBIC)
 
    # Display the resulting frame
    cv2.imshow('Frame', frame)
     
    # conversion of BGR to grayscale is necessary to apply this operation
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
 
    _, mask = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
 
    # apply NOT operation on image and mask generated by thresholding
    BIT = cv2.bitwise_not(frame, frame, mask = mask)
    cv2.imshow('BIT', BIT)
 
    # define q as the exit button
    if cv2.waitKey(25) & 0xFF == ord('q'):
        break
 
# release the video capture object
cap.release()
 
# Closes all the windows currently opened.
cv2.destroyAllWindows()

输出:

我们可以根据需要执行任何其他操作。这些只是最常用的几个基本操作。