1 回答
TA贡献1799条经验 获得超8个赞
这是因为虽然您的零形状是正确的,但很可能是数据类型不匹配。Numpy Zeros文档的默认数据类型为numpy.float64. 只需dtype为您的案例传递参数。
color = cv2.imread("lohri.jpg")
b,g,r = cv2.split(color)
zeros = np.zeros(b.shape[0], b.shape[1]], dtype = b.dtype)
#Note that the dtype is most likely np.uint8, and you can use that instead too if you prefer
#zeros = np.zeros([b.shape[0], b.shape[1]], dtype = np.uint8)
#Also note that you can pass shape tuple directly.
#zeros = np.zeros(b.shape, dtype = np.uint8)
# Make other 2 channels as zeros
only_red = cv2.merge((zeros, zeros, r))
编辑:您还可以使用np.zeros_like正确的形状和数据类型创建数组,这也使代码更加简洁明了。谢谢马克!
color = cv2.imread("lohri.jpg")
b,g,r = cv2.split(color)
zeros = np.zeros_like(b)
only_red = cv2.merge((zeros, zeros, r))
添加回答
举报