![](/img/trans.png)
[英]Why both image created from np.ones and np.zeros shows black blank image while combining both gives black and white mixed image as expected?
[英]Why np.ones creates a black image?
我想了解np.zeros
和np.ones
之间有什么区别。 在我的波纹管代码中,都创建了黑色图像。 但是np.zeros
和np.ones
什么用。 我应该什么时候使用它们。 什么目的。 我知道np.ones
用 1 填充矩阵,但目的是什么。 我想创建一个white image
img = np.ones((512,512,3), np.uint8)
cv2.imshow('IMG', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
img = np.zeros((512,512,3), np.uint8)
cv2.imshow('IMG', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
将数组初始化为float
数据类型。 只有这样 0 表示黑色,1 表示白色:
img = np.ones((512,512,3), np.float)
说明:当imshow
接收到一个整数数组时,它会假设 0 是最小值,255 是最大值,代表灰度图像中的黑色和白色。 然而,如果它接收一个浮点数组,它会将这些值设置为 0.0 和 1.0,因为它假设一个缩放输入。
np.zeros
“返回一个给定形状和类型的新数组,用零填充。”
np.ones
“返回一个给定形状和类型的新数组,用一个填充。”
np.zeros(10)
Out: array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
np.ones(10)
Out:array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
您需要按像素中的位数进行缩放。 例如,
img = 255*np.ones((512, 512, 3), uint8)
将为 8 位像素生成白色图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.