top of page

# Summary

### Thresholding

Python libraries like scikit-image offer some advanced image processing techniques used by scientists. We'll touch on a few below. If you're interested in learning more details, take a look at the code and tutorials in http://scikit-image.org/docs/dev/auto_examples

## ​

We briefly touched upon the idea of thresholding in the previous section when we picked out all the pixels that were above a certain value in order to identify which parts of the image corresponded to the white flowers. In general, thresholding is useful for picking out certain objects if these objects have a similar color (or intensity) to one another and are different in intensity from other objects in the picture. The threshold we set in the last section, 90%, was a good guess that let us identify the flowers, but how do we know whether there are better values we could have picked?

One popular technique is Otsu's method, which analyzes the values of all the pixels in a specific image to choose the best threshold value to separate different objects. The scikit-image library has a function called “threshold_otsu” that uses Otsu’s method to calculate a threshold. Because this is a very famous thresholding technique, Otsu’s method is included in a lot of image processing software.

We find that the threshold value of the flowers picture is 0.55 (recall that the maximum value is 1.0). We create a thresholded image with the command "thresholded_image = imagefile > threshold", which returns a 2D boolean array to determine which parts of the image are above or below the threshold. A boolean can be either True or False. If the value of a pixel is True, the pixel intensity is above the threshold (lighter than the threshold). If the value of the pixel is False, the pixel intensity is below the threshold (darker than the threshold).

In computer science, True and False are usually set equivalent to 1 and 0, respectively. Recall that in our image, 1 corresponded to white and 0 corresponded to black. So “thresholded_image” is a NumPy array that represents a binary image, where every pixel can only take on one of two possible values. Again, we can plot this array/image using matplotlib. Note that this method enables us to pick out the regions of the image with the flower petals, but you can’t really see the leaves anymore. This is useful if you want to simplify the image so that you can focus on the flowers. 1  import matplotlib.pyplot as plt

2  from skimage import io

3  from skimage.filters import threshold_otsu

5  imagefile = io.imread("Flowers.jpg", as_gray = True)

5  threshold = threshold_otsu(imagefile)
6  print("The threshold value returned by Otsu's method is %.2f" % threshold)
thresholded_image = imagefile > threshold

7

8  plt.figure()
9  plt.imshow(thresholded_image, cmap = 'gray')

10 plt.savefig("thresholded_image.jpg")

bottom of page