我想知道如何在3通道图像中查找黑色像素图像。我使用cv2.countNonZero()查找非黑色像素,以便可以从像素总数中减去,但它仅适用于单通道图像。应该使用Numpy或Opencv或keras中的哪一个?如何使用?
答案 0 :(得分:0)
使用OpenCV,您只需执行以下操作即可:
import cv2
# Load input image
input = cv2.imread('images/colors.png', cv2.IMREAD_COLOR)
# Mask black pixels
mask = 255 * ((input[:, :, 0] == 0) & (input[:, :, 1] == 0) & (input[:, :, 2] == 0))
# Count black pixel
print("Total number of pixels in image: " + str(input.shape[0] * input.shape[1]))
print("Number of black pixels in image: " + str(cv2.countNonZero(mask)))
这是我的输入图片:
这是生成的黑色像素蒙版:
这是输出:
Total number of pixels in image: 1474560
Number of black pixels in image: 258745