• Stars
    star
    160
  • Rank 234,703 (Top 5 %)
  • Language
    Python
  • License
    MIT License
  • Created almost 7 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A library that helps you split image into small, overlappable patches, and merge patches into original image.

patchify

patchfy can split images into small overlappable patches by given patch cell size, and merge patches into original image.

This library provides two functions: patchify, unpatchify.

Installation

pip install patchify

Usage

Split image to patches

patchify(image_to_patch, patch_shape, step=1)

2D image:

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

3D image:

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

Merge patches into original image

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.

Help! unpatchify yields distorted images

In order for unpatchify to work, patchies should be created with equal step size. e.g. if the original image has width 3 and the patch has width 2, you cannot really create equal step size patches with step size 2. (first patch [elem0, elem1] and second patch [elem2, elem3], in which elem3 is out of bound).

The required condition to successfully recover the image using unpatchify is to have (width - patch_width) mod step_size = 0 when calling patchify.

Full running examples

2D image patchify and merge

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 image patchify and merge

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()

More Repositories

1

deribit-rs

Deribit API V2 WS client for Rust.
Rust
45
star
2

bitmex-rs

Bitmex (non-official) client for rust WIP
Rust
45
star
3

xtp-rs

A Rust binding for XTP SDK
Rust
37
star
4

sidekiq-rs

sidekiq compatible server in rust
Rust
34
star
5

gowebQQ

webqq API for Golang
Go
31
star
6

factor-expr

Factor Expression + Historical Data = Factor Values
Rust
23
star
7

tldextract-rs

extract tld info from url
Rust
8
star
8

okex-rs

Rust
7
star
9

treerite

treelite runtime binding in Rust
Rust
7
star
10

nextaction-rs

add omnifocus like nextaction capability to todoist using an api server.
Rust
7
star
11

rust-apex

Rust
6
star
12

solana-client-async

Rust
6
star
13

anchor-skeleton

Battery-included Solana/Anchor project skeleton.
Rust
6
star
14

kraken-rs

Rust
5
star
15

ds100

Jupyter Notebook
5
star
16

beegoHelper

创建beego控制器时候的一个辅助控制器,方便开发
Go
4
star
17

peace-lock

A Mutex/RwLock that helps you sanity check your concurrent algorithm and becomes zero-cost with the check mode disabled.
Rust
4
star
18

keenio-rs

keenio api v3 binding for rust
Rust
3
star
19

ucontext-rs

ucontext binding for rust
Rust
3
star
20

matrixrs

matrix manipulation for rust languange
Rust
2
star
21

google-geo-rs

a library help lookup the geo infomation by latitude and longitude
Rust
2
star
22

lightgbm-rs

A Rust binding for LightGBM
Rust
2
star
23

dovahcrow.github.io

My Blog
HTML
2
star
24

docker-k2pdfopt

Docker application for k2pdfopt application
2
star
25

voipbits

Voipbits lets you send and receive SMS using voipms with acrobits softphone
Rust
2
star
26

distributions

some statistic distributions for rust-lang
Rust
1
star
27

easylog

a easylog module use beego's log module and beego's conf module
Go
1
star
28

mixpanel-rs

mixpanel export api library for rust
Rust
1
star
29

xtp-sdk

A mirror of the SDK from http://xtp.zts.com.cn on Github
C++
1
star
30

CPPStream

CPPStream wrapper for rust
Rust
1
star
31

rust_svg

a svg library for rust
Rust
1
star
32

pixiu

Rust
1
star
33

ml.rs

my machine learning lib for rust
Rust
1
star
34

.emacs.d

my emacs configuration
Emacs Lisp
1
star
35

solana-base58-json-converter

A small CLI tool to convert Solana keys from/to Base58 format to/from JSON format.
Rust
1
star