OpenCV image processing----video processing and HSV color model

read video

cv2.VideoCapture(0)方法参数:

  1. If the parameter is a path, it is to open the video of the path
  2. If the parameter is a number, the number of the camera device is generally 0
  3. return video object

video对象:

  1. get()Method cv2.CAP_PROP_FPSget fps
    cv2.CAP_PROP_FRAME_WIDTHget width
    cv2.CAP_PROP_FRAME_HEIGHTget height

  2. read()The method reads each frame of the video
    and returns flag(whether it is successfully read)
    frame(picture of each frame)

import cv2

# 读取视频或开启摄像头  
video = cv2.VideoCapture(0)
# 视频是否开启成功
is_opened = video.isOpened()
# 获取fps  和视频每一帧的高度和宽度
fps = video.get(cv2.CAP_PROP_FPS)
width = video.get(cv2.CAP_PROP_FRAME_WIDTH)
height = video.get(cv2.CAP_PROP_FRAME_HEIGHT)
print(f'视频帧率:{
      
      fps},高度:{
      
      height},宽度:{
      
      width}')

# 读取每一帧
flag, frame = video.read()
# flag为True就一直读取
while flag:

    flag, frame = video.read()
    # 展示每一帧
    if flag:
        cv2.imshow('img', frame)
        cv2.waitKey(80)


HSV color model

HSV (Hue, Saturation, Value) is a color space created by AR Smith in 1978 based on the intuitive characteristics of color, also known as the Hexcone Model.

  • H:色调

    Measured by angle, the value range is 0°~360°, starting from red and counting counterclockwise, red is 0°, green is 120°, and blue is 240°. Their complementary colors are: yellow is 60°, cyan is 180°, magenta is 300°;

  • S:饱和度

    Saturation S indicates how close a color is to spectral colors. A color that can be seen as the result of mixing a certain spectral color with white. Among them, the larger the proportion of spectral color, the higher the degree of color close to spectral color, and the higher the saturation of color. The saturation is high, and the color is deep and vivid. The white light component of the spectral color is 0, and the saturation reaches the highest. Usually the value ranges from 0% to 100%, the larger the value, the more saturated the color.

  • V:亮度

    Brightness indicates the brightness of the color. For the light source color, the lightness value is related to the brightness of the illuminant; for the object color, this value is related to the transmittance or reflectance of the object. Usually values ​​range from 0% (black) to 100% (white).

  • When S=1 V=1, any color represented by H is called a pure color;

  • When S=0, that is, the saturation is 0, the color is the lightest, and the lightest is described as gray (gray also has brightness, black and white also belong to gray), the brightness of gray is determined by V, and H is meaningless at this time;

  • When V=0, the color is the darkest, and the darkest is described as black, so at this time H (the darkest color is black no matter what color) and S (the darkest color is black no matter what shade) are meaningless.

注意: In opencv, the value ranges of H, S, and V are [0,180], [0,255], [0,255], instead of [0,360], [0,1], [0,1];

Determine whether the picture is daytime or night demo:

import cv2
import numpy as np


def avgV(img):
    """
    计算图片亮度平均值函数
    :param img: 图片矩阵
    :return: 图片亮度平均值
    """
    # 转为HSV模型图片
    hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    # cv2的split方法可以分割出HSV模型 按顺序第三个是V通道
    v_info = cv2.split(hsv_img)[2]
    # 计算V通道平均值
    result = np.mean(v_info)
    # 返回
    return result


# 读取图片并丢进函数计算
day_img = cv2.imread('img/day.jpg')
day_avg_v = avgV(day_img)
night_img = cv2.imread('img/night.jpg')
night_avg_v = avgV(night_img)
print(f'白天的亮度平均值:{
      
      day_avg_v}')
print(f'晚上亮度平均值:{
      
      night_avg_v}')
cv2.imshow('day', day_img)
cv2.imshow('night', night_img)
cv2.waitKey()

insert image description here


color filter

inRange()方法

  • read a color image
  • Convert RGB to HSV image
  • Define the range of colors, the lower limit is (30,120,130), and the upper limit is (60,255,255)
  • inRange()Create a mask based on the range of colors using the method
import cv2

img = cv2.imread('./img/tenis1.jpg')

hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

# 定义颜色范围
lower_color = (30, 120, 130)
upper_color = (60, 255, 255)
# 颜色范围之内的为白色  颜色范围之外的为黑色
mask_img = cv2.inRange(hsv_img, lower_color, upper_color)
cv2.imshow('img', img)
cv2.imshow('mask_img', mask_img)
cv2.waitKey()

insert image description here

Guess you like

Origin blog.csdn.net/bjsyc123456/article/details/124736525