我想对一个图像进行阈值化,但是我不希望输出是黑白的,我希望它是白色和其他一些颜色。我能够使用嵌套的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) 此循环将阈值图像中的任何黑色像素更改为绿色。
发布于 2021-06-03 13:10:56
那么绿色通道总是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 thresholdhttps://stackoverflow.com/questions/67821916
复制相似问题