【发布时间】: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