【问题标题】:Threshold an image using RBG使用 RGB 对图像进行阈值处理
【发布时间】:2021-06-03 21:10:56
【问题描述】:

我想对图像进行阈值处理,但我希望它不是黑白输出,而是白色和其他颜色。我可以使用嵌套的 for 循环来实现这一点,但这很慢,我想知道是否有人知道使用 CV2 功能有效地做到这一点的任何方法。

img = cv2.imread("Naas.png", 1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
threshold, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)

# Changing black to green and converting from Grayscale to RGB

lis = []
for i in thresh:
    for j in i:
        if j == 0:
            lis.append((0, 255, 0))
        else:
            lis.append((255, 255, 255))
        
img = np.array(lis, dtype = "uint8")
img = img.reshape(thresh.shape[0], thresh_inv.shape[], 3) 

此循环将阈值图像中的所有黑色像素更改为绿色。

【问题讨论】:

  • 你为什么要把所有东西都放在一个列表中?
  • 只是为了存储,正如我所说,这是一种非常低效的方法,它更像是展示我的最终目标的一种方式

标签: python computer-vision cv2 grayscale image-thresholding


【解决方案1】:

所以绿色通道始终是 255 而红色和蓝色通道只是阈值?

所以你正在看这样的东西

import numpy as np 
img = cv2.imread("Naas.png", 1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
threshold, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)
img_result=np.ones(img.shape)*255 #set all to 255
img_result[:,:,0]=thresh[:,:] #set red channel to threshold
img_result[:,:,2]=thresh[:,:] #set blue channel to threshold

【讨论】:

    猜你喜欢
    • 2015-10-19
    • 2014-11-30
    • 1970-01-01
    • 1970-01-01
    • 2017-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-10
    相关资源
    最近更新 更多