Skip to main content

To create/merge 2D or 3D patches

Project description

Patch and Merge

  1. Can create patches from both 2D or 3D images
  2. Can merge 2D or 3D patches

As for 3D, the primary focus is 3D CT data. The data should be converted to an array before making 3D patches.

So, the 2D image or 3D volume is needed to convert to an array first.

How to install

pip install jenti==0.0.1
pip install jenti

How to use

Sample demonstration on how to use this code is given in test.py and test.ipynb.

How to import

from jenti import patch

Create patches from a 2D image

# Read image
im = Image.open('c.jpg') # color image
im.show()
image = np.array(im) # convert to array

# Create patches
patch_shape = [100, 100]
overlap = [10,10] # overlap between two adjacent patches along both axes
patch = Patch(patch_shape, overlap, patch_name='patch2d', csv_output=True)
patches, info, org_shape = patch.patch2d(image)

# Save patches       
patch.save2d(patches, save_dir='./save2d', ext = '.png')

If the csv_output is set to True, then it will save the locations of each patch in the original image in a csv file.
Patch names will be like: xxxx0000, xxxx0001, xxxx0002, and so on.

Create patches from a 3D volume

# Read volume
data = sio.loadmat('volume.mat')
data = data['data'] # shape: 128 x 128 x 50  x 1

# Create patches
patch_shape = [32, 32, 16, 1] # H x W x D x Ch
overlap = [8, 8, 8, 0]
patch = Patch(patch_shape, overlap, patch_name='patch3d', csv_output=True)
patches, info, org_shape = patch.patch3d(data)

# Save patches
patch.save3d(patches, save_dir='./save3d', ext = '.mat')

Merge 2D patches
Merging can be done in two ways.
Method 1: Read all the patch files first, then merge them together.

# Merge patches
names = os.listdir('./save2d')
patches = []
info = pd.read_csv('patch2d.csv')
info = np.array(info)
org_shape = (3024, 4032, 3)

for name in names:
    p = Image.open(os.path.join('./save2d', name))
    p = np.array(p)
    patches.append(p)
    
merge = Merge(info, org_shape, dtype='uint8')
merged = merge.merge2d(patches)
merged_im = Image.fromarray(merged)
merged_im.show()
merged_im.save('merged2d.png')

Method 2: Provide only the patch directory.

merge = Merge(info, org_shape, dtype='uint8')
merged = merge.merge_from_dir2d('./save2d') 
merged_im = Image.fromarray(merged)
merged_im.show()
merged_im.save('merged2d.png')

Merge 3D patches
Method 1: Read all the patch files first, then merge them together.

# Merge patches
names = os.listdir('./save3d')
patches = []
info = pd.read_csv('patch3d.csv')
info = np.array(info)
org_shape = (128, 128, 50, 1)

for name in names:
    p = sio.loadmat(os.path.join('./save3d', name))
    p = p['p']
    patches.append(p)
    
merge = Merge(info, org_shape, dtype='float32')
merged = merge.merge3d(patches)
sio.savemat('merged3d.mat', {'m': merged}, do_compression=True)

Method 2: Provide only the patch directory.

merge = Merge(info, org_shape, dtype='float32')
merged = merge.merge_from_dir3d('./save3d') 
sio.savemat('merged3d.mat', {'m': merged}, do_compression=True)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jenti-0.0.1.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jenti-0.0.1-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file jenti-0.0.1.tar.gz.

File metadata

  • Download URL: jenti-0.0.1.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.5

File hashes

Hashes for jenti-0.0.1.tar.gz
Algorithm Hash digest
SHA256 691854ce55c6768441ac10463ac0af95c4ce781f5df2debe07939d09ab16ee07
MD5 d53153657f0a85b3e444579e8c67e7bc
BLAKE2b-256 ee0b7d90d4053c1640c47d83f08ea15c53fd5940b5c802642d203f18259ba368

See more details on using hashes here.

File details

Details for the file jenti-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: jenti-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.5

File hashes

Hashes for jenti-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d62c2b8837cccd811792f2d5fe6186c09fd6a7b71f900e4e55b9b0e483e08a0c
MD5 69bee1068697785a9d5c6e5b88a4f90b
BLAKE2b-256 3e5ae9894990e44865f054816798350bf8a88320c38840be80255f3510103549

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page