📜  使用 OpenCV 在Python中检测相似颜色的对象

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

使用 OpenCV 在Python中检测相似颜色的对象

OpenCV是一个主要针对实时计算机视觉的编程函数库。

在本文中,我们将了解如何在图像中获取相同颜色的对象。我们可以通过 cv2 命令 cv2.createTrackbar 创建的滑动条选择颜色。

需要的库:

OpenCV
Numpy

方法:

首先我们需要使用 cv2.imread() 读取本地文件夹中的图像。为了过滤特定颜色,我们需要将图像转换 HSV格式,它是色调、饱和度和值,并使用 cv2.inRange() 通过提供我们想要过滤的 RGB 值的下限和上限来掩盖图像,这给我们一个黑白图像,其中图像具有我们感兴趣的颜色为白色,其余为黑色。我们可以通过执行 cv2 bitwise_and 操作来取回我们通过 trackbar 给它的指定颜色的图像

代码:

Python3
# import required library
import cv2
import numpy as np
import matplotlib.pyplot as plt
  
# create a video object
# for capture the frames.
# for Webcamera we pass 0 
# as an argument 
cap = cv2.VideoCapture(0)
  
# define a empty function 
def nothing(x):
    pass
  
# set windown name
cv2.namedWindow('Tracking')
  
# Creates a trackbar and attaches 
# it to the specified window 
# with nothing function
cv2.createTrackbar("LH", "Tracking",
                   0, 255, nothing)
cv2.createTrackbar("LS", "Tracking", 
                   0, 255, nothing)
cv2.createTrackbar("LV", "Tracking", 
                   0, 255, nothing)
cv2.createTrackbar("HH", "Tracking", 
                   0, 255, nothing)
cv2.createTrackbar("HS", "Tracking", 
                   0, 255, nothing)
cv2.createTrackbar("HV", "Tracking",
                   0, 255, nothing)
  
# This drives the program 
# into an infinite loop. 
while True:
    
    # Captures the live stream frame-by-frame
    _, frame = cap.read()
      
    # Converts images from BGR to HSV 
    hsv = cv2.cvtColor(frame, 
                       cv2.COLOR_BGR2HSV)
      
    # find LH trackbar position
    l_h = cv2.getTrackbarPos("LH",
                             "Tracking")
    # find LS trackbar position
    l_s = cv2.getTrackbarPos("LS",
                             "Tracking")
    # find LV trackbar position
    l_v = cv2.getTrackbarPos("LV", 
                             "Tracking")
    # find HH trackbar position
    h_h = cv2.getTrackbarPos("HH", 
                             "Tracking")
    # find HS trackbar position
    h_s = cv2.getTrackbarPos("HS",
                             "Tracking")
    # find HV trackbar position
    h_v = cv2.getTrackbarPos("HV",
                             "Tracking")
    # create a given numpy array
    l_b = np.array([l_h, l_s,
                    l_v])
    # create a given numpy array
    u_b = np.array([h_h, h_s,
                    h_v])
    # create a mask
    mask = cv2.inRange(hsv, l_b,
                       u_b)
    # applying bitwise_and operation
    res = cv2.bitwise_and(frame, 
                          frame, mask = mask)
      
    # display frame, mask
    # and res window
    cv2.imshow('frame', frame)
    cv2.imshow('mask', mask)
    cv2.imshow('res', res)
      
    # wait for 1 sec
    k =  cv2.waitKey(1)
      
    # break out of while loop
    # if k value is 27
    if k == 27:
        break
          
# release the captured frames 
cap.release()
  
# Destroys all windows. 
cv2.destroyAllWindows()


输出:

检测相同颜色的物体