加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-17 17:35:17 所属栏目:Python 来源:网络整理
导读:我正在使用“ 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 ima

我正在使用“ 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看起来像这样-您的颜色介于绿色和蓝色之间:

enter image description here

result.png看起来像这样:

enter image description here

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读