如何使用OpenCV Python找到一种颜色的HSV值?
要找到一个颜色的HSV值,我们可以使用从BGR到HSV的颜色空间转换。首先,我们将颜色值以BGR格式定义为numpy.ndarray,然后将其转换为HSV空间。
We can also find the lower and upper limits of HSV value as [H-10, 100, 100] and [H+10, 255, 255] respectively. These lower and upper limits can be used to track an object of particular color.
要找到一个颜色的HSV值,请按照以下步骤进行:
步骤
Import the required libraries. In all the following Python examples, the required Python libraries are OpenCV and NumPy. Make sure you have already installed them.
import cv2 import numpy as np
为颜色定义一个numpy.ndarray,dtype=np.uint8。
green = np.uint8([[[0, 255, 0]]])
Convert the above defined color to HSV.
hsvGreen = cv2.cvtColor(green, cv2.COLOR_BGR2HSV)
Print the color values.
print("HSV of Green:", hsvGreen)
Let's look at some program examples to understand it clearly.
Example 1
In this example, we find the HSV value for green color. The BGR value of green is [0,255,0].
# import required libraries import numpy as np import cv2 # define a numpy.ndarray for the color # here insert the bgr values which you want to convert to hsv green = np.uint8([[[0, 255, 0]]]) # convert the color to HSV hsvGreen = cv2.cvtColor(green, cv2.COLOR_BGR2HSV) # display the color values print("BGR of Green:", green) print("HSV of Green:", hsvGreen) # Compute the lower and upper limits lowerLimit = hsvGreen[0][0][0] - 10, 100, 100 upperLimit = hsvGreen[0][0][0] + 10, 255, 255 # display the lower and upper limits print("Lower Limit:",lowerLimit) print("Upper Limit", upperLimit)
输出
When you run the above Python program, it will produce the following output −
BGR of Green: [[[ 0 255 0]]] HSV of Green: [[[ 60 255 255]]] Lower Limit: (50, 100, 100) Upper Limit (70, 255, 255)
Example 2
的中文翻译为:示例 2
In this example, we find the HSV value for a color whose BGR value is [106,76,89].
# import required libraries import numpy as np import cv2 green = np.uint8([[[0, 255, 0]]]) # convert the color to HSV hsvGreen = cv2.cvtColor(green, cv2.COLOR_BGR2HSV) # here insert the bgr values which you want to convert to hsv bgr = np.uint8([[[106,76,89]]]) hsv = cv2.cvtColor(green, cv2.COLOR_BGR2HSV) print("BGR Value:", bgr) print("HSV Value:", hsv) # compute the lower and upper limits lowerLimit = hsvGreen[0][0][0] - 10, 100, 100 upperLimit = hsvGreen[0][0][0] + 10, 255, 255 # display the lower and upper limits print("Lower Limit:",lowerLimit) print("Upper Limit", upperLimit)
输出
When you run the above python program, it will produce the following output −
BGR Value: [[[76 76 89]]] HSV Value: [[[ 60 255 255]]] Lower Limit: (50, 100, 100) Upper Limit (70, 255, 255)
以上是如何使用OpenCV Python找到一种颜色的HSV值?的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

Linux终端中查看Python版本时遇到权限问题的解决方法当你在Linux终端中尝试查看Python的版本时,输入python...

如何在10小时内教计算机小白编程基础?如果你只有10个小时来教计算机小白一些编程知识,你会选择教些什么�...

在使用Python的pandas库时,如何在两个结构不同的DataFrame之间进行整列复制是一个常见的问题。假设我们有两个Dat...

使用FiddlerEverywhere进行中间人读取时如何避免被检测到当你使用FiddlerEverywhere...

Uvicorn是如何持续监听HTTP请求的?Uvicorn是一个基于ASGI的轻量级Web服务器,其核心功能之一便是监听HTTP请求并进�...

本文讨论了诸如Numpy,Pandas,Matplotlib,Scikit-Learn,Tensorflow,Tensorflow,Django,Blask和请求等流行的Python库,并详细介绍了它们在科学计算,数据分析,可视化,机器学习,网络开发和H中的用途

在Python中,如何通过字符串动态创建对象并调用其方法?这是一个常见的编程需求,尤其在需要根据配置或运行...
