简体   繁体   English

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

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

I'm working with ´OpenCV´ and I want to show a single colour in an image. 我正在使用'OpenCV',我想在图像中显示单一颜色。 Now I made this, 现在我做了这个,

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]))

This works ok but it works when you need a range of colour tonalities. 这可以正常工作,但是当你需要一系列色彩色调时它会起作用。 But in my case, I want to know the exact value of RGB, by the moment I'm writing the same value in the low and upper range but I'm trying to do it better, how can I do this without range? 但在我的情况下,我想知道RGB的确切值,当我在低和高范围内写入相同的值但是我想要做得更好,我怎么能在没有范围的情况下做到这一点?

Thank you very much. 非常感谢你。

I think I understand your question. 我想我理解你的问题。 Try this: 尝试这个:

#!/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 looks like this - your colour is between green and blue: start.png看起来像这样 - 你的颜色介于绿色和蓝色之间:

在此输入图像描述

result.png looks like this: result.png看起来像这样:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM