簡體   English   中英

知道圖像中的單個RGB顏色,而不是OpenCV的范圍

[英]Know a single RGB color in an image, not a range with OpenCV

我正在使用'OpenCV',我想在圖像中顯示單一顏色。 現在我做了這個,

img = cv2.imread('im02.jpg')

L1 = np.array([255,0,102])
U1 = np.array([255,0,102])

m1 = cv2.inRange(img, L1, U1)

r1 = cv2.bitwise_and(img, img, mask=m1)

#print(r1.any()) #know if all the image is black

cv2.imshow("WM", np.hstack([img, r1]))

這可以正常工作,但是當你需要一系列色彩色調時它會起作用。 但在我的情況下,我想知道RGB的確切值,當我在低和高范圍內寫入相同的值但是我想要做得更好,我怎么能在沒有范圍的情況下做到這一點?

非常感謝你。

我想我理解你的問題。 嘗試這個:

#!/usr/local/bin/python3
import numpy as np
import cv2

# Open image into numpy array
im=cv2.imread('start.png')

# Sought colour
sought = [255,0,102]

# Find all pixels where the 3 RGB values match the sought colour
matches = np.all(im==sought, axis=2)

# Make empty (black) output array same size as input image
result = np.zeros_like(im)

# Make anything matching our sought colour into magenta
result[matches] = [255,0,255]

# Or maybe you want to color the non-matching pixels yellow
result[~matches] = [0,255,255]

# Save result
cv2.imwrite("result.png",result) 

start.png看起來像這樣 - 你的顏色介於綠色和藍色之間:

在此輸入圖像描述

result.png看起來像這樣:

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM