[英]Why np.ones creates a black image?
I want to understand what is the difference between np.zeros
and np.ones
.我想了解np.zeros
和np.ones
之间有什么区别。 In my bellow code both create a black images.在我的波纹管代码中,都创建了黑色图像。 But what is the use of np.zeros
and np.ones
.但是np.zeros
和np.ones
什么用。 When should i use them.我应该什么时候使用它们。 What is the purpose.什么目的。 I know np.ones
fill matrix with 1 but what is the purpose.我知道np.ones
用 1 填充矩阵,但目的是什么。 What i want to create a white image
我想创建一个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()
Initialize your array as float
data type.将数组初始化为float
数据类型。 Only then 0 will mean black and 1 will mean white:只有这样 0 表示黑色,1 表示白色:
img = np.ones((512,512,3), np.float)
Explanation : When imshow
receives an array of ints, it will assume, that 0 is the minimum and 255 the maximum value, representing black and white in a greyscale image.说明:当imshow
接收到一个整数数组时,它会假设 0 是最小值,255 是最大值,代表灰度图像中的黑色和白色。 However if it receives an array of floats it sets these values to 0.0 and 1.0, because it assumes a scaled input.然而,如果它接收一个浮点数组,它会将这些值设置为 0.0 和 1.0,因为它假设一个缩放输入。
np.zeros
"Return a new array of given shape and type, filled with zeros." np.zeros
“返回一个给定形状和类型的新数组,用零填充。”
np.ones
"Return a new array of given shape and type, filled with ones." 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.])
You need to scale by the number of bits you have in a pixel.您需要按像素中的位数进行缩放。 For example,例如,
img = 255*np.ones((512, 512, 3), uint8)
will produce a white image for 8-bit pixels.将为 8 位像素生成白色图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.