A collection of thresholding methods implemented for numpy and pytorch arrays
img_threshold
is a Python package providing a variety of image thresholding techniques with a focus on support for PyTorch gradient functionality. This package fills the gap for advanced thresholding methods that are differentiable and can be used in deep learning pipelines.
Install img_threshold
using pip:
pip install git+https://https://github.com/hallurr/pythresh.git
For illustrative purposes a normal optical coherence tomography image has been chosen to demonstrate from the publicly available kaggle databank.
Phansalkar’s thresholding is suited for local thresholding.
The normalized image is assessed for each pixel whether its value is greater than the following threshold:
where
import sys
import os
# Add the parent directory to the sys.path list
sys.path.append(os.path.abspath('../'))
# Phansalkar method example
from img_threshold.threshold_methods import phansalkar
from img_threshold.utils import *
# load the Normal OCT image
img = load_image('NORMAL-OCT.jpeg')[:, :, 0]
# Show the input and outputs
images = [img,
phansalkar(img, radius=10),
phansalkar(img, radius=25),
phansalkar(img, radius=50)]
titles = ['Original Image',
'Phansalkar Threshold\n radius = 5',
'Phansalkar Threshold\n radius = 10',
'Phansalkar Threshold\n radius = 50']
show_images(images, titles=titles, cmap='gray', figsize=(15, 10))
Using a "Faster Approach" the threshold with the maximum between class variance also has the minimum within class variance labbookpages.
# Otsu method example
import sys
import os
# Add the parent directory to the sys.path list
sys.path.append(os.path.abspath('../'))
# Phansalkar method example
from img_threshold.threshold_methods import otsu
from img_threshold.utils import *
# load the Normal OCT image
img = load_image('NORMAL-OCT.jpeg')[:, :, 0]
otsu_thresholded = otsu(img)
images = [load_image('NORMAL-OCT.jpeg')[:, :, 0],
otsu(img)]
titles = ['Original Image',
'Otsu Threshold']
show_images(images, titles=titles, cmap='gray', figsize=(15, 10))