我正在使用“ 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>
非常感谢您。
答案 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
看起来像这样-您的颜色介于绿色和蓝色之间:
result.png
看起来像这样: