我有一個包含 13 個不同影像(.tiff 格式)的路徑檔案,我想將它們全部連接成一個陣列(13、224、224)。我得到了所有 13 個值的形狀,但我仍在努力將它們放入所需的陣列中。有沒有辦法從 for 回圈中連接它們?
imgs = os.listdir(path) #images list .tif
imgs.remove('metadata.json')
for i in imgs:
image = cv2.imread(os.path.join(path, i))
#print(image.shape) # (h, w, 3)
image = image[:, :, 0]
#print(image.shape) # (h, w)
width = 224
height = 224
dimensions = (width, height)
image = cv2.resize(image, dimensions,
interpolation=cv2.INTER_AREA) # specifying the scaling factor, using INTER_AREA for shrinking
min_value = np.min(image) # min value: 720
max_value = np.max(image) # max value: 2564
#image = ((image - min_value) / (max_value - min_value)) * 255
image = np.uint8(image)
image = np.expand_dims(image, axis=0)
#image = np.concatenate(image, axis=0)
print(image.shape) #(1, 224, 224) for 13 images
uj5u.com熱心網友回復:
您的問題是您正在覆寫image回圈內部的內容。解決此問題的一種方法是構建影像串列,然后在回圈后將其轉換為陣列:
images = []
for i in imgs:
image = cv2.imread(os.path.join(path, i))
#
# ...
#
image = np.uint8(image)
# note, don't use `expand_dims` here
images.append(image)
images = np.array(images)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/520180.html
上一篇:如何裁剪影像3:4?迅速
