![](/img/trans.png)
[英]How to stack colored images together with binary images using OpenCV, NumPy, and Python?
[英]What does opencv threshold THRESH_BINARY do on colored images?
关于THRESH_BINARY
的文档说:
dst(x,y) = maxval if src(x,y) > thresh else 0
在我看来,这并不意味着它不适用于彩色图像。 即使应用于彩色图像,我也希望获得两种颜色的输出,但是输出是多色的。 为什么? 当像素x,y
的可能值设置为maxval
和0
怎么可能?
例:
from sys import argv
import cv2
import numpy as np
img = cv2.imread(argv[1])
ret, threshold = cv2.threshold(img, 120, 255, cv2.THRESH_BINARY)
cv2.imshow('threshold', threshold)
cv2.imshow('ori', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
阈值分别应用于每个颜色通道。 如果tahn阈值较小,则颜色通道设置为0,否则不设置为maxval。 通道是独立处理的,这就是为什么结果是具有多种颜色的彩色图像的原因。 您可以获得的颜色是:(0,0,0),(255,0,0),(0,255,0),(255,255,0),(0,0,255),(255,0,255),(0,255,255)和(255,255,255)。
假设您具有3通道RGB图像的像素,其值为rgb(66, 134, 244)
。 现在,假设您给thresh
值135
。 您认为会发生什么?
r = 66
g = 134
b = 244
if(r > thresh) r = 255 else r = 0; // we have r = 0
if(g > thresh) g = 255 else g = 0; // we have g = 0
if(b > thresh) b = 255 else b = 0; // we have b = 255
新的像素值为rgb(0, 0, 255)
。 由于您的图像是RGB彩色图像,因此像素颜色现在是BLUE
而不是WHITE
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.