如何使用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...

使用FiddlerEverywhere進行中間人讀取時如何避免被檢測到當你使用FiddlerEverywhere...

在使用Python的pandas庫時,如何在兩個結構不同的DataFrame之間進行整列複製是一個常見的問題。假設我們有兩個Dat...

如何在10小時內教計算機小白編程基礎?如果你只有10個小時來教計算機小白一些編程知識,你會選擇教些什麼�...

Uvicorn是如何持續監聽HTTP請求的? Uvicorn是一個基於ASGI的輕量級Web服務器,其核心功能之一便是監聽HTTP請求並進�...

攻克Investing.com的反爬蟲策略許多人嘗試爬取Investing.com(https://cn.investing.com/news/latest-news)的新聞數據時,常常�...
