首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用RBG对图像进行阈值化

用RBG对图像进行阈值化
EN

Stack Overflow用户
提问于 2021-06-03 12:57:10
回答 1查看 533关注 0票数 1

我想对一个图像进行阈值化,但是我不希望输出是黑白的,我希望它是白色和其他一些颜色。我能够使用嵌套的for-循环实现这一点,但是这是缓慢的,我想知道是否有人知道使用CV2功能有效地完成此操作的方法。

代码语言:javascript
复制
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) 

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-03 13:10:56

那么绿色通道总是255,而红色和蓝色通道只是阈值?

所以你会看到这样的事情

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67821916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档