我想在 python 中切出一個影像并將其作為視窗再次粘貼在一起。
圖塊大小為 8 像素乘 9 像素,每行需要跳過 1 像素
然后我需要將瓷磚再次合并在一起,每個瓷磚周圍都有 1 個像素的填充,以提供視窗效果。
影像是黑白的,但在示例中我使用了顏色來表明視窗效果需要具有白色背景
res.jpg
remove_tiles.jpg
print(image.shape, new_img.shape)
給 (952, 1429, 3) (1332, 1899, 3)
uj5u.com熱心網友回復:
您可以skimage.utils.view_as_windows
從scikit-image包中嘗試使用:
from skimage.util import view_as_windows
import matplotlib.pyplot as plt
import numpy as np
img = np.random.rand(90, 90, 1) # gray-scale image, you can change the channels accordingly
img[8::9,] = 0
tiles = view_as_windows(img, (9, 9, 1), (9, 9, 1)).squeeze(2) # squeeze out unneded dim
tiles = tiles[:, :, :-1, :, :] # Remove last row of each tile
# plot the original image
plt.axis("off")
plt.imshow(img.squeeze(2))
plt.show()
# plot the tiles
fig, axes = plt.subplots(10, 10)
for i in range(10):
for j in range(10):
axes[i, j].axis("off")
axes[i, j].imshow(tiles[i, j, ...].squeeze(-1))
plt.show()
PyTorch的torch.Tensor.unfold
操作員也可以是一個選擇。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/368218.html
標籤:Python 图像处理 细胞 numpy切片 细胞间距