https://github.com/adamrehn/maskutils
Python library for working with 2D image masks
https://github.com/adamrehn/maskutils
Last synced: 7 months ago
JSON representation
Python library for working with 2D image masks
- Host: GitHub
- URL: https://github.com/adamrehn/maskutils
- Owner: adamrehn
- License: mit
- Created: 2017-12-28T10:13:33.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-12-16T03:25:04.000Z (almost 7 years ago)
- Last Synced: 2025-02-08T15:48:04.268Z (8 months ago)
- Language: Python
- Size: 4.88 KB
- Stars: 0
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Mask Utilities
==============This is a simple little Python library for working with 2D image masks. Functionality is provided for manipulating pixel data and extracting subsets based on mask values.
Installation
------------To install, run:
```
pip install maskutils
```Usage Example
-------------```
import maskutils
import cv2# Load our input image
image = cv2.imread('image.png', cv2.IMREAD_UNCHANGED)# Split the mask (alpha channel) from the other image channels
channels, mask = maskutils.splitAlphaMask(image)# Extract the subset of the image with mask value zero, blanking all other pixels
zeroOnly = maskutils.isolatePixels(channels, mask, 0)
cv2.imwrite('zero_only.png', zeroOnly)# Use the OpenCV "connected components" algorithm to extract each segment of contiguous
# mask values, for each unique value in the mask, ignoring mask value zero
instancesForValues = maskutils.extractConnectedComponents(channels, mask, ignoreZero=True)# Save each segment as an individual image
for maskValue, instances in instancesForValues.items():
for instanceNum, instance in enumerate(instances):
subset = maskutils.extract(channels, instance)
cv2.imwrite('{}_{}.png'.format(maskValue, instanceNum), subset)```