您的位置:首页 >宏观经济 >

使用 OPENCV Python 进行颜色检测和选择

时间:2022-08-16 14:16:22 来源:

在今天的文章中,我们将讨论如何使用 OpenCV-python 从图像或视频内容中检测和选择颜色。

在开始之前,想指出你需要与本文一起完成的先决条件。首先,考虑到你已经了解编程,你需要知道如何在 OpenCV-python 中读取和显示图像。

读取和显示图像

这里的第一步是使用 opencv 从图像文件中读取和显示图像。为此,我们将使用下面的代码片段:

import cv2

# read the image into memory

img = cv2.imread("./images/image1.jpg")

# resise the image

img = cv2.resize(img, (640, 480))

# create a window named image and display the image inside of it.

cv2.imshow("Image", img)

# wait untill a any key is pressed to quite the program

cv2.waitKey(0)

创建 Trackbar 滑动条

为了检测图像中的颜色,我们需要将图像转换为 HSV(Hue Saturation Value)图像。此图像比例将允许用于在给定的 UPPER 和 LOWER 范围内选择颜色。

在本节中,让我们继续编写代码来创建滑动条,并从滑块中提取值作为用户输入。

为此,我们的代码需要进行更多更改。它现在应该是这样的。

import cv2

import numpy as np

def do_nothing():

pass

# create slider here

cv2.namedWindow("Slider")

cv2.resizeWindow("Slider", 640, 480)

cv2.createTrackbar("Hue Min", "Slider", 0, 255, do_nothing)

cv2.createTrackbar("Hue Max", "Slider", 0, 255, do_nothing)

cv2.createTrackbar("Saturation Min", "Slider", 0, 255, do_nothing)

cv2.createTrackbar("Saturation Max", "Slider", 0, 255, do_nothing)

cv2.createTrackbar("Value Min", "Slider", 0, 255, do_nothing)

cv2.createTrackbar("Value Max", "Slider", 0, 255, do_nothing)

img = cv2.imread("./images/image1.jpg")

img = cv2.resize(img, (640, 480))

while True:

# extract the values from the trackbar

hue_min = cv2.getTrackbarPos("Hue Min", "Slider")

hue_max = cv2.getTrackbarPos("Hue Max", "Slider")

sat_min = cv2.getTrackbarPos("Saturation Min", "Slider")

sat_max = cv2.getTrackbarPos("Saturation Max", "Slider")

val_min = cv2.getTrackbarPos("Value Min", "Slider")

val_max = cv2.getTrackbarPos("Value Max", "Slider")

print(hue_min, hue_max, sat_min, sat_max, val_min, val_max)

if cv2.waitKey(1) & 0xFF == ord("q"):

break

cv2.destroyAllWindows()

将图像转换为 HSV 比例

现在我们能够让滑块和滑动条正常工作,让我们继续将我们的图像转换为 HSV 比例,然后能够在特定颜色范围内选择颜色。

我们可以用第35行的代码做到这一点

import cv2

import numpy as np

def do_nothing():

pass

# create slider here

cv2.namedWindow("Slider")

cv2.resizeWindow("Slider", 640, 480)

cv2.createTrackbar("Hue Min", "Slider", 0, 255, do_nothing)

cv2.createTrackbar("Hue Max", "Slider", 0, 255, do_nothing)

cv2.createTrackbar("Saturation Min", "Slider", 0, 255, do_nothing)

cv2.createTrackbar("Saturation Max", "Slider", 0, 255, do_nothing)

cv2.createTrackbar("Value Min", "Slider", 0, 255, do_nothing)

cv2.createTrackbar("Value Max", "Slider", 0, 255, do_nothing)

img = cv2.imread("./images/image1.jpg")

img = cv2.resize(img, (640, 480))

while True:

# extract the values from the trackbar

hue_min = cv2.getTrackbarPos("Hue Min", "Slider")

hue_max = cv2.getTrackbarPos("Hue Max", "Slider")

sat_min = cv2.getTrackbarPos("Saturation Min", "Slider")

sat_max = cv2.getTrackbarPos("Saturation Max", "Slider")

val_min = cv2.getTrackbarPos("Value Min", "Slider")

val_max = cv2.getTrackbarPos("Value Max", "Slider")

# print(hue_min, hue_max, sat_min, sat_max, val_min, val_max)

# convert to HSV image

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

if cv2.waitKey(1) & 0xFF == ord("q"):

break

cv2.destroyAllWindows()

得到我们想要的颜色的上下界

现在我们已经将图像转换为 HSV 比例,我们将更多地了解我们想要检测和选择的颜色的上限和下限。我们分别使用第35行和第36行的代码来执行此操作。

注意:确保此时已导入 numpy,否则会出现一些错误,numpy 预装了 OpenCV-Python。

import cv2

import numpy as np

def do_nothing():

pass

# create slider here

cv2.namedWindow("Slider")

cv2.resizeWindow("Slider", 640, 480)

cv2.createTrackbar("Hue Min", "Slider", 0, 255, do_nothing)

cv2.createTrackbar("Hue Max", "Slider", 0, 255, do_nothing)

cv2.createTrackbar("Saturation Min", "Slider", 0, 255, do_nothing)

cv2.createTrackbar("Saturation Max", "Slider", 0, 255, do_nothing)

cv2.createTrackbar("Value Min", "Slider", 0, 255, do_nothing)

cv2.createTrackbar("Value Max", "Slider", 0, 255, do_nothing)

img = cv2.imread("./images/image1.jpg")

img = cv2.resize(img, (640, 480))

while True:

# extract the values from the trackbar

hue_min = cv2.getTrackbarPos("Hue Min", "Slider")

hue_max = cv2.getTrackbarPos("Hue Max", "Slider")

sat_min = cv2.getTrackbarPos("Saturation Min", "Slider")

sat_max = cv2.getTrackbarPos("Saturation Max", "Slider")

val_min = cv2.getTrackbarPos("Value Min", "Slider")

val_max = cv2.getTrackbarPos("Value Max", "Slider")

# print(hue_min, hue_max, sat_min, sat_max, val_min, val_max)

# set bounds

lower_bound = np.array([hue_min, sat_min, val_min])

upper_bound = np.array([hue_max, sat_max, val_max])

# convert to HSV image

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

if cv2.waitKey(1) & 0xFF == ord("q"):

break

cv2.destroyAllWindows()

创建一个掩码并将其用于 Bitwise_and 操作

现在,我们需要创建一个图像掩码,并使用它来执行按位操作,使用用户使用滑动条设置的 UPPER 边界和 LOWER 边界。

这将使我们能够选择该颜色范围内的颜色。我们的代码现在应该如下所示,分别在第43行和第46行进行了更改。

在第50行,我们简单地显示图像。现在你可以调整滑块并选择你选择的不同颜色。

import cv2

import numpy as np

def do_nothing():

pass

# create slider here

cv2.namedWindow("Slider")

cv2.resizeWindow("Slider", 640, 480)

cv2.createTrackbar("Hue Min", "Slider", 0, 255, do_nothing)

cv2.createTrackbar("Hue Max", "Slider", 0, 255, do_nothing)

cv2.createTrackbar("Saturation Min", "Slider", 0, 255, do_nothing)

cv2.createTrackbar("Saturation Max", "Slider", 0, 255, do_nothing)

cv2.createTrackbar("Value Min", "Slider", 0, 255, do_nothing)

cv2.createTrackbar("Value Max", "Slider", 0, 255, do_nothing)

img = cv2.imread("./images/image1.jpg")

img = cv2.resize(img, (640, 480))

while True:

# extract the values from the trackbar

hue_min = cv2.getTrackbarPos("Hue Min", "Slider")

hue_max = cv2.getTrackbarPos("Hue Max", "Slider")

sat_min = cv2.getTrackbarPos("Saturation Min", "Slider")

sat_max = cv2.getTrackbarPos("Saturation Max", "Slider")

val_min = cv2.getTrackbarPos("Value Min", "Slider")

val_max = cv2.getTrackbarPos("Value Max", "Slider")

# print(hue_min, hue_max, sat_min, sat_max, val_min, val_max)

# set bounds

lower_bound = np.array([hue_min, sat_min, val_min])

upper_bound = np.array([hue_max, sat_max, val_max])

# convert to HSV image

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

# create mask

mask = cv2.inRange(hsv_img, lower_bound, upper_bound)

# we perform bitwise and operation here

resulting_img = cv2.bitwise_and(img, img, mask=mask)

# display the image here

cv2.imshow("Image", resulting_img)

if cv2.waitKey(1) & 0xFF == ord("q"):

break

cv2.destroyAllWindows()

最后的润色

现在我们已经做到了这一点,让我们堆叠图像以获得更好的演示。可以在此代码段中找到此代码。

在第46行,我们创建了一个堆叠图像并将其显示在第49行。

import cv2

import numpy as np

def do_nothing():

pass

# create slider here

cv2.namedWindow("Slider")

cv2.resizeWindow("Slider", 640, 480)

cv2.createTrackbar("Hue Min", "Slider", 0, 255, do_nothing)

cv2.createTrackbar("Hue Max", "Slider", 0, 255, do_nothing)

cv2.createTrackbar("Saturation Min", "Slider", 0, 255, do_nothing)

cv2.createTrackbar("Saturation Max", "Slider", 0, 255, do_nothing)

cv2.createTrackbar("Value Min", "Slider", 0, 255, do_nothing)

cv2.createTrackbar("Value Max", "Slider", 0, 255, do_nothing)

img = cv2.imread("./images/image1.jpg")

img = cv2.resize(img, (640, 480))

while True:

hue_min = cv2.getTrackbarPos("Hue Min", "Slider")

hue_max = cv2.getTrackbarPos("Hue Max", "Slider")

sat_min = cv2.getTrackbarPos("Saturation Min", "Slider")

sat_max = cv2.getTrackbarPos("Saturation Max", "Slider")

val_min = cv2.getTrackbarPos("Value Min", "Slider")

val_max = cv2.getTrackbarPos("Value Max", "Slider")

# set bounds

lower_bound = np.array([hue_min, sat_min, val_min])

upper_bound = np.array([hue_max, sat_max, val_max])

# convert to HSV image

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

# create mask

mask = cv2.inRange(hsv_img, lower_bound, upper_bound)

resulting_img = cv2.bitwise_and(img, img, mask=mask)

stacked_imgs = np.hstack([img, resulting_img])

# create a stacked image of the original and the HSV one.

cv2.imshow("Image", stacked_imgs)

if cv2.waitKey(1) & 0xFF == ord("q"):

break

cv2.destroyAllWindows()

结论

有关该项目的视频说明,请查看:

使用 OPENCV Python 进行颜色检测和选择

原文标题:使用 OPENCV Python 进行颜色检测和选择


郑重声明:文章仅代表原作者观点,不代表本站立场;如有侵权、违规,可直接反馈本站,我们将会作修改或删除处理。