Segmentation#

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:

../_images/97bb7b2e27ce5e2a7572a2ecb15228639d0522193d73dc791d98d2b7aa4a77c8.png

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

../_images/8d2c7f4d187208b0326e8a3a98821059428ebea86eb64677b07d1b1feded260f.png

Binary thresholding#

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

../_images/78f79efc8f67ecc8f2003ee81870d09ac5b771640095403246a75db5f5b44794.png

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.

../_images/a66472594d09d2da7b6ee85ffce713843d468ee60bf1fab3417628cf32295a46.png

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.

../_images/92b0409fb1dbca4ba12e04c04f22c8b7acbb365828040f59a1d7fab2bfa3c2b7.png

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

../_images/db629130c906e90ded2730195143dcfc0bc7fd6fa201c0c4a15947184b590856.png

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 \(T_{m,n}\) for each pixel \((m,n)\) of the image. Besides, the thresholding is done as usual:

\[\begin{split} \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} \end{split}\]

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

../_images/e306ed69d74850cf600ea3caf72203794405c06485cab9792bd019cbf05fdee3.png

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.

../_images/08a7882f58907bab20942ad74155d8b6b60cbaee9a18d18e004531f2069ba897.png