0

I am working with the following image:

original

And I would like to make it binary, however, as can be seen in the image, there are different brightness' of grey spots. I would like for all the grey spots to be captured in the binary at the exact size.

If I apply 1 (one) threshold to the image, I get one of the following outcomes:'

  1. Large threshold (t=0.8): More of the lighter spots shown in the binary, BUT the darker spots are then made larger than in the original.
  2. Smaller threshold (t=0.7): Less of the lighter spots shown in binary, BUT the darker spots appear as their exact size.

Is there a way to apply multiple thresholds to one image according to the colour of the pixel? So to apply a larger threshold to the "lighter" pixels and a smaller threshold to the "darker" pixels?

This is my code with the threshold: t = 0.8

import skimage.io
import skimage.viewer
import skimage                 
import skimage.io    
         
# Read image.TIF:
image = skimage.io.imread(fname="<source-directory>")
image[2,1]= 1.0

# Process the file
gray_image = skimage.color.rgb2gray(image)

# Blur the image to denoise
blurred_image = skimage.filters.gaussian(gray_image, sigma=5)

# Adding threshold, t:
t = 0.8
binary_mask = blurred_image < t

# Save the file to another location:
skimage.io.imsave(fname="<target-directory>", arr = binary_mask)

Any help is much sprreciated!

Fred
  • 103
  • 7

1 Answers1

1

There are so many methods to do thresholding in your case.

I think that you can use a local thresholding techniques that are useful for images where the image is not uniform.

Look to Niblack Or Sauvola. As below an example to explain difference between global threshold that you are used and local threshold methods :

import matplotlib
import matplotlib.pyplot as plt

from skimage.data import page
from skimage.filters import (threshold_otsu, threshold_niblack,
                             threshold_sauvola)


matplotlib.rcParams['font.size'] = 9


image = page()
binary_global = image > threshold_otsu(image)

window_size = 25
thresh_niblack = threshold_niblack(image, window_size=window_size, k=0.8)
thresh_sauvola = threshold_sauvola(image, window_size=window_size)

binary_niblack = image > thresh_niblack
binary_sauvola = image > thresh_sauvola

plt.figure(figsize=(8, 7))
plt.subplot(2, 2, 1)
plt.imshow(image, cmap=plt.cm.gray)
plt.title('Original')
plt.axis('off')

plt.subplot(2, 2, 2)
plt.title('Global Threshold')
plt.imshow(binary_global, cmap=plt.cm.gray)
plt.axis('off')

plt.subplot(2, 2, 3)
plt.imshow(binary_niblack, cmap=plt.cm.gray)
plt.title('Niblack Threshold')
plt.axis('off')

plt.subplot(2, 2, 4)
plt.imshow(binary_sauvola, cmap=plt.cm.gray)
plt.title('Sauvola Threshold')
plt.axis('off')

plt.show()
  • Thank you - I will have a look at it. However, I don't think it does exactly what I want. – Fred Mar 15 '22 at 09:04