[英]openCV imread to make grayscale image (height, width, 1)
我正在使用opencv讀取灰度圖像,但是當我將其讀取為灰度圖像時:
data_dir = "/.../data/"
images = []
files = glob.glob (data_dir + "*.jpg")
for file in files:
image = cv2.imread(file, 0)
images.append(image)
我的圖像形狀實際上是:
images[0].shape
(2993,670)
如何使用cv2制作(2993、670、1)?
您還可以從numpy使用np.reshape()函數。 做這個:
>>> import numpy as np
>>> image = np.zeros((2993, 670), dtype=np.uint8)
>>> resized_image = np.reshape(image,(2993,670,1))
>>> resized_image.shape
(2993, 670, 1)
使用numpy中的np.expand_dims()
,因為OpenCV映像只是numpy數組:
>>> import numpy as np
>>> img = np.zeros((3, 3), dtype=np.uint8)
>>> img.shape
(3, 3)
>>> img = np.expand_dims(img, axis=2)
>>> img.shape
(3, 3, 1)
請注意, axis=2
指示要擴展到的尺寸; 在您的情況下,您希望它是第三個軸(軸從0開始,所以2)。 如以上文檔所述,您還可以執行以下操作:
>>> img = img[:, :, np.newaxis]
>>> img.shape
(3, 3, 1)
甚至
>>> img = img[:, :, None]
>>> img.shape
(3, 3, 1)
所有這些都是等效的,盡管第一個是更多自我記錄。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.