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

2024-10-02 10:21:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用“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的精确值,当我在上下限写相同的值,但我想做得更好的时候,我怎么能在没有范围的情况下做到呢?你知道吗

非常感谢。你知道吗


Tags: 图像l1img颜色nparraycv2opencv
1条回答
网友
1楼 · 发布于 2024-10-02 10:21:52

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

#!/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

相关问题 更多 >

    热门问题