Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Three methods will be compared to segment the coins on the image observation.png: binary thresholding, Otsu’s method, and local thresholding. The methods will be evaluated through the Dice coefficient, by using the ground truth available in image groundtruth.png.

  • Apply binary thresholding to the image, by choosing manually the threshold value. Compute the Dice coefficient.

  • Use skimage.filters.threshold_otsu to get a threshold value by Otsu’s method and apply the thresholding. Compute the Dice coefficient. What differences do you observe between the two first segmentations? How can these differences be explained?

  • Use skimage.filters.threshold_local to perform local thresholding. How works this method? Compute the Dice coefficient.

  • Finally, criticize the three methods: identify the good results and the limitations. Suggest improvements.

Correction

The objectives of this exercise are:

  • to apply and compare several segmentation methods

  • to evaluate the results of these methods through the Dice coefficient

The image is:

<Figure size 640x480 with 1 Axes>

To evaluate the performance of a segmentation method, we can compare the segmentation with the ground truth, i.e. the optimal segmentation, shown below.

<Figure size 640x480 with 1 Axes>

Binary thresholding

Unfortunately, the image histogram does not show two clear modes. We choose here to set the threshold to 90.

<Figure size 1200x500 with 2 Axes>

With this threshold, the Dice coefficient equals 0.711, which is not very satisfying (recall that the Dice coefficient ranges between 0 and 1, 1 being the best value. This can be explained by the fact that the lighting of the image is not constant. Indeed, some coins are lighter than certain zones of the background, but other coins are darker, so it is impossible to choose a threshold to separate the coins from the background.

Otsu’s method

Manual thresholding has the disadvantage of being, precisely, manual. On the contrary, Otsu’s method automatically calculates a threshold value. However, for the considered image, the segmentation is still not very satisfying.

<Figure size 1200x500 with 2 Axes>

Otsu’s method finds a lower threshold than the one set manually above. As a result, there are more white pixels in the segmentation.

A method that optimizes the Dice coefficient (knowing the ground truth)

A small remark before continuing.

The simplest method for finding the best threshold in the sense of the Dice coefficient consists in testing all the threshold values (from 0 to 255) and calculating the Dice coefficient for each one. Of course, this method cannot be used in a real case since it requires knowing the ground truth! However, it has the interest of discussing the Otsu’s threshold.

The graph below represents the Dice coefficient as a function of the threshold value. The best threshold corresponds to the red line.

<Figure size 640x480 with 1 Axes>

The threshold that maximizes the Dice equals 60, which gives the result below.

<Figure size 1200x500 with 2 Axes>

Then, Otsu’s threshold is not the best, in the sense of Dice coefficient.

Local thresholding

The idea of local thresholding is to define a threshold Tm,nT_{m,n} for each pixel (m,n)(m,n) of the image. Besides, the thresholding is done as usual:

g(m,n)={1if f(m,n)⩾Tm,n,0if f(m,n)<Tm,n\begin{split} g(m,n) = \begin{cases} 1 & \text{if}\, f(m,n)\geqslant T_{m,n}, \\ 0 & \text{if}\, f(m,n)<T_{m,n} \end{cases} \end{split}

There are numerous ways to define the threshold Tm,nT_{m,n} for a particular pixel. The simplest way is to define Tm,nT_{m,n} as the mean of the intensities of a sub-image of size B×BB\times B centered on the pixel (m,n)(m,n). It is also possible to compute a weighted mean, such as the default option of skimage.filters.threshold_local.

<Figure size 500x500 with 1 Axes>

As you can see, the Dice coefficient is now very good! Of course, it depends on the size of the sub-images. In this example, how can you find the best size?

Comparison

The four previous results are presented below to ease a visual comparison.

<Figure size 2000x500 with 4 Axes>