![](/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.