知道图像中的单一RGB颜色,而不是OpenCV的范围

时间:2019-01-06 11:08:03

标签: python python-3.x 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的确切值,目前我正在上下范围写入相同的值,但我试图做得更好,如何在没有范围的情况下做到这一点? / p>

非常感谢您。

1 个答案:

答案 0 :(得分:2)

我想我理解你的问题。试试这个:

#!/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看起来像这样-您的颜色介于绿色和蓝色之间:

enter image description here

result.png看起来像这样:

enter image description here