把图像划分为patch以及用图像块重建图像

github上有了现成的代码: 

GitHub - dovahcrow/patchify.py: A library that helps you split image into small, overlappable patches, and merge patches into original image.A library that helps you split image into small, overlappable patches, and merge patches into original image. - GitHub - dovahcrow/patchify.py: A library that helps you split image into small, overlappable patches, and merge patches into original image.https://github.com/dovahcrow/patchify.py该package可以直接通过pip安装。

安装:

pip install patchify

使用:

(1)把图像划分为图像块(patch)

patchify(image_to_patch, patch_shape, step=1)

示例1:2D image

#This will split the image into small images of shape [3,3]
patches = patchify(image, (3, 3), step=1)

示例2:3D image:

#This will split the image into small images of shape [3,3,3]
patches = patchify(image, (3, 3, 3), step=1)

(2)把patch拼接回原始图像

unpatchify(patches_to_merge, merged_image_size)
示例:
reconstructed_image = unpatchify(patches, image.shape)

This will reconstruct the original image that was patchified in previous code.

完整的示例

2D 图像分块与拼接

import numpy as np
from patchify import patchify, unpatchify

image = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])

patches = patchify(image, (2,2), step=1) # split image into 2*3 small 2*2 patches.

assert patches.shape == (2, 3, 2, 2)
reconstructed_image = unpatchify(patches, image.shape)

assert (reconstructed_image == image).all()

3D 图像分块与拼接

import numpy as np
from patchify import patchify, unpatchify

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

patches = patchify(image, (2,2,3), step=1) # patch shape [2,2,3]
print(patches.shape) # (511, 511, 1, 2, 2, 3). Total patches created: 511x511x1

assert patches.shape == (511, 511, 1, 2, 2, 3)
reconstructed_image = unpatchify(patches, image.shape)
print(reconstructed_image.shape) # (512, 512, 3)

assert (reconstructed_image == image).all()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值