Code Monkey home page Code Monkey logo

pythresh's Introduction

pythresh

A collection of thresholding methods implemented for numpy and pytorch arrays

Description

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.

Installation

Install img_threshold using pip:

pip install git+https://https://github.com/hallurr/pythresh.git

Examples

For illustrative purposes a normal optical coherence tomography image has been chosen to demonstrate from the publicly available kaggle databank.

Normal OCT

Phansalkar’s thresholding method

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:

$$ \mu \cdot \left(1.0 + p \cdot \exp(-q \cdot \mu) + k \cdot \left(\frac{\sigma}{r} - 1\right)\right) $$

where $\mu$ and $\sigma$ are the mean and the standard deviation of neighbourhood, respectively. $k$, $r$, $p$, and $q$ are changeable parameters defaulted to:
$k=0.25$
$r=0.5$
$p=2.0$
$q=10.0$

Described in N. Phansalskar, S. More, and A. Sabale, et al., Adaptive local thresholding for detection of nuclei in diversity stained cytology images, International Conference on Communications and Signal Processing (ICCSP), 2011.

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 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))
Phansalkar Thresholding

Otsu thresholding method

Using a "Faster Approach" the threshold with the maximum between class variance also has the minimum within class variance labbookpages.

Described in N. Otsu, "A Threshold Selection Method from Gray-Level Histograms," in IEEE Transactions on Systems, Man, and Cybernetics, vol. 9, no. 1, pp. 62-66, Jan. 1979, doi: 10.1109/TSMC.1979.4310076..

Example

# 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))
Otsu Thresholding

pythresh's People

Contributors

hallurr avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.