亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在圖像上使用 skimage.transform.rescale 兩次創建額外的通道

在圖像上使用 skimage.transform.rescale 兩次創建額外的通道

米琪卡哇伊 2023-03-16 16:15:02
在我正在做的 coursera 指導項目中,講師使用from skimage.transform import rescaleimage_rescaled = rescale(rescale(image,0.5),2.0)扭曲圖像。在我自己的設備上發生的錯誤(并且在項目的 jupyter notebook 上沒有出現,可能是由于模塊和 python 的版本不同)是通道的數量增加image_rescaled了1.例如=>images_normal.shape = (256,256,256,3)和images_with_twice_reshape.shape=(256,256,256,4)如果我使用,則不會出現此問題rescaled(rescale(image,2.0),0.5)。這是在較新版本的 python/skimage 中使用還是我做錯了什么?對于其他參考(沒有從源代碼中刪除任何內容,但用#s 突出顯示了重要部分):import osimport refrom scipy import ndimage, miscfrom skimage.transform import resize, rescalefrom matplotlib import pyplotimport numpy as npdef train_batches(just_load_dataset=False):    batches = 256 # Number of images to have at the same time in a batch    batch = 0 # Number if images in the current batch (grows over time and then resets for each batch)    batch_nb = 0 # Batch current index        ep = 4 # Number of epochs    images = []    x_train_n = []    x_train_down = []        x_train_n2 = [] # Resulting high res dataset    x_train_down2 = [] # Resulting low res dataset        for root, dirnames, filenames in os.walk("data/cars_train.nosync"):        for filename in filenames:            if re.search("\.(jpg|jpeg|JPEG|png|bmp|tiff)$", filename):                filepath = os.path.join(root, filename)                image = pyplot.imread(filepath)                if len(image.shape) > 2:                                            image_resized = resize(image, (256, 256)) # Resize the image so that every image is the same size#########################通過上面的代碼,我得到了x_train_n2.shape = (256,256,256,3)和x_train_down2.shape=(256,256,256,4)。
查看完整描述

1 回答

?
湖上湖

TA貢獻2003條經驗 獲得超2個贊

我能夠按如下方式重現您的問題:


import numpy as np

from skimage.transform import resize, rescale


image = np.random.random((512, 512, 3))

resized = resize(image, (256, 256))

rescaled2x = rescale(

        rescale(resized, 0.5),

        2,

)

print(rescaled2x.shape)

# prints (256, 256, 4)

問題是resize可以推斷你的最終維度是通道/RGB,因為你給它一個 2D 形狀。rescale,另一方面,將您的數組視為形狀為 (256, 256, 3) 的 3D 圖像,它下降到 (128, 128, 2),也沿著顏色進行插值,就好像它們是另一個空間維度一樣,然后上采樣到 (256, 256, 4)。


如果您查看rescale文檔,您會發現“多通道”參數,描述為:


圖像的最后一個軸是被解釋為多通道還是另一個空間維度。


所以,更新我的代碼:


rescaled2x = rescale(

        rescale(resized, 0.5, multichannel=True),

        2,

        multichannel=True,

)

print(rescaled2x.shape)

# prints (256, 256, 3)


查看完整回答
反對 回復 2023-03-16
  • 1 回答
  • 0 關注
  • 176 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號