![](/img/trans.png)
[英]Subtract 2D array from each pixel of a 3D image and get a 4D array
[英]Create centered patches in each image pixel for 2D image
大家好,我正在嘗試從 2D 圖像創建補丁。 我需要這些補丁必須在每個像素圖像中居中。 我正在使用此代碼:
#patches for each point in 2D slice
patch_size = 27
pacth_m_size = patch_size//2
for x in range(0, sld_arr_norm.shape[0]):
for y in range(0, sld_arr_norm.shape[1]):
if x-pacth_m_size>0: # if all is ok the get the patch
if y-pacth_m_size>0:
if x+pacth_m_size<sld_arr_norm.shape[1]:
if y+pacth_m_size<sld_arr_norm.shape[1]:
x_i = x-pacth_m_size
x_s = x+pacth_m_size+1
y_i = y-pacth_m_size
y_s = y+pacth_m_size+1
curr_patch= sld_arr_norm[x_i:x_s, y_i:y_s]
assert curr_patch.shape == (patch_size, patch_size)
print(curr_patch.shape)
else:
x_i = x-pacth_m_size
x_s = x+pacth_m_size+1
y_i = y-pacth_m_size
y_s = y+pacth_m_size+1
if x-pacth_m_size<0:
issue_patch = sld_arr_norm[0:x_s, y_i:y_s]
curr_patch = np.zeros((patch_size, patch_size))
star_index = abs(x-pacth_m_size)
curr_pacth[star_index:,:]=issue_patch.copy()
if y-pacth_m_size<0:
issue_patch = sld_arr_norm[x_i:x_s, 0:y_s]
curr_patch = np.zeros((patch_size, patch_size))
star_index = abs(y_i)
curr_pacth[:, star_index:]=issue_patch.copy()
if y+pacth_m_size>sld_arr_norm.shape[1]:
issue_patch = sld_arr_norm[x_i:x_s, y_i:y_s]
curr_patch = np.zeros((patch_size, patch_size))
end_index = abs(y_s-issue_patch.shape[1])
curr_patch[0:, 0:curr_patch.shape[1]-end_index]=issue_patch.copy() #issue_patch[x_i:x_s, y_i:issue_patch.shape[1]]
if x+pacth_m_size>sld_arr_norm.shape[0]:
issue_patch = sld_arr_norm[x_i:x_s, y_i:y_s]
curr_patch = np.zeros((patch_size, patch_size))
end_index = abs(x_s-issue_patch.shape[0])
curr_patch[0:arr_zeros.shape[0]-end_index, :]=issue_patch.copy()
assert curr_patch.shape == (patch_size, patch_size)
print(curr_patch.shape)
問題出在圖像的邊界上,我面臨一些問題,例如補丁不依賴於定義的補丁大小。 您知道任何允許以這種方式創建補丁的庫嗎?
最好的方法是首先填充整個圖像。 然后,我們可以繼續提取補丁而不必擔心邊緣。 代碼如下所示:
#Define patch size
patch_size = 13
pacth_m_size = patch_size//2
#Patch the whole image
sld_arr_norm_pad = np.pad(sld_arr_norm, pacth_m_size, 'wrap')
for x in range(0, sld_arr_norm.shape[0]):
for y in range(0, sld_arr_norm.shape[1]):
x_real = x + pacth_m_size
y_real = y + pacth_m_size
x_i = x_real - pacth_m_size
x_s = x_real + pacth_m_size + 1
y_i = y_real - pacth_m_size
y_s = y_real + pacth_m_size + 1
curr_patch = sld_arr_norm_pad[x_i:x_s, y_i:y_s]
print(curr_patch.shape)
print((x_i, x_s, y_i, y_s))
assert curr_patch.shape == (patch_size, patch_size)
希望這對其他人有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.