我先分离出RGB图片,然后在每个通道上进行阈值处理,然后合并3个通道时,就会出现如下图所示的假象。
b, g, r = cv2.split(image)
r = cv2.threshold(r, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
g = cv2.threshold(g, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
b = cv2.threshold(b, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
rgb_image = cv2.merge([b, g, r])
k = cv2.getStructuringElement(cv2.MORPH_RECT, (15, 15))
blackhat = cv2.morphologyEx(rgb_image, cv2.MORPH_BLACKHAT, k)
mask = cv2.inRange(rgb_image, (225, 225, 225), (255, 255, 255))
# dst = np.dstack((frame, 255-mask))
dst = np.dstack((rgb_image, mask))
cv2.imshow('lo', dst)
cv2.waitKey()