我正在嘗試根據分割網路的類掩碼 (16x16) 的值標記要忽略的影像陣列 (224x224) 的區域。先前的處理意味著不需要的區域將被標記為 -1。我希望能夠將類掩碼報告-1的影像的所有區域的值設定為一些無意義的值(例如,999),但保持陣列的形狀(224x224)。下面是我的意思的一個簡潔示例,使用 4x4 影像和 2x2 蒙版。
# prefilter
image = 1 2 3 4
5 6 7 8
9 1 2 3
4 5 6 7
mask = -1 4
-1 5
# postfilter
image_filtered = 999 999 3 4
999 999 7 8
999 999 2 3
999 999 6 7
Is there an efficient way to do this modification?
uj5u.com熱心網友回復:
這是我必須作業的一些代碼。它確實要求蒙版和影像具有相同的縱橫比,并且是彼此大小的整數倍。
import numpy as np
image = np.array([[1,2,3,4],
[5,6,7,8],
[9,1,2,3],
[4,5,6,7]])
mask = np.array([[-1,4],
[-1,5]])
def mask_image(image, mask, masked_value):
scale_factor = image.shape[0]//mask.shape[0] # how much the mask needs to be scaled up by to match the image's size
resized_mask = np.kron(mask, np.ones((scale_factor,scale_factor))) # resize the mask (magic)
return(np.where((resized_mask==-1),masked_value,image)) # where the mask==-1, return the masked value, else return the value of the original array.
print(mask_image(image, mask, 999))
看到這個答案后,我曾經np.kron調整陣列的大小。
這個功能非常快,用 1920x1080 蒙版遮蓋 1920x1080 影像大約需要 2 秒。
編輯:使用@Jér?me Richard 在他們的評論中所說的話。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/411571.html
標籤:
上一篇:如何更改地圖中的對?
下一篇:將多類分類轉換為二值影像分類
