我正在使用“ OpenCV”,我想在图像中显示一种颜色.现在我做了这个
img = cv2.imread('im02.jpg')
L1 = np.array([255,102])
U1 = np.array([255,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,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,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看起来像这样: