Image Segmentation (Part 1). Thresholding, Otsu’s and HSV... | by Ralph Caubalejo | Towards Data Science

One of the most important contributions of image processing to data science is the ability to use the processing technique to create different segmentation over the image. By segmentation, we mean segmenting different objects from their background. Normally if we have a raw image, and we want to create a dataset of the objects in the image, we would want to first isolate these objects. But how can we do that?
We use different image segmentation techniques to isolate these distinct objects.
There are a lot of segmentation techniques that are frequently used but for part 1 of this article, we will highlight and discuss the following:
Trial and Error ThresholdingOtsu’s MethodHSV Space Segmentation
Trial and Error Thresholding
Otsu’s Method
HSV Space Segmentation
Let us load a sample image:
Our sample image is a collection of small flowers on a plain brown background. Our challenge for this article is to be able to segment each of these flowers from the background. We will try to use the mention image segmentation and see if we are victorious at the end.
Trial and Error in image processing is always been the norm, especially when dealing with new images. This goes the same way as the thresholding method. We try to determine the best value where we can threshold the image and attenuate our desired objects.
Normally in thresholding, we try different thresholding values and compare and contrast which is the better results. An example below shows how we can do that:
#experimented threshold valuessample_t = sample_g>0.70sample_t1 = sample_g>0.50fig, ax = plt.subplots(1,3,figsize=(15,5))im = ax[0].imshow(sample_g,cmap='gray')fig.colorbar(im,ax=ax[0])ax[1].imshow(sample_t,cmap='gray')ax[0].set_title('Grayscale Image',fontsize=15)ax[1].set_title('Threshold at 0.70',fontsize=15)ax[2].imshow(sample_t1,cmap='gray')ax[2].set_title('Threshold at 0.50',fontsize=15)plt.show()
We can see in Figure, two different threshold values at 0.7 and 0.6. Notice that these threshold values are very near to each other but the results by using each one of them are evident. In the 0.70 value, we can clearly segment the white flower, while in the 0.50 value, we did segment the white flower but were joined by other pixel values between that range. If we would want only to segment the white flower, then the best thresholding value is around 0.7.
One thing to do to find the best thresholding value is by looking at the color bar beside the grayscale plot, from here we can choose what is the range that we can set to extract our needed objects.
The next technique is called Otsu’s Method and is actually almost the same with the trial and error but this time it is automated. This method is developed by Nobuyuki Otsu. The idea behind otsu’s method is that the method checks the pixel values and finds the best sweet spot where the two classes can be divided into two by minimizing the variance over the histogram of it.
There is already a predefined scikit-function that can be called upon for easier usage.
from skimage.filters import threshold_otsuthresh = threshold_otsu(sample_g)sample_ot  = sample_g > thresh
From Figure 3, we can see the difference between using Otsu’s Method and using a trial and error thresholding. Notice that the result of otsu’s has a more defined blob object compared to the 0.7 threshold graph, this means that it was able to segment and see the entirety of the white flower.
Notice that from the two segmentation techniques above, it is easy to distinguish the bright and dark pixel value because we are dealing with the grayscale image dimension. In terms of doing in the RGB Color Channel dimension, we use HSV or Hue, Saturation, and Value Space to properly segment the flowers.
Let us first show how the sample image looks like in the HSV Values.
from skimage.color import rgb2hsv#convert to hsv scalesample_h= rgb2hsv(sample)#graph per HSV Channelfig, ax = plt.subplots(1, 3, figsize=(15,5))ax[0].imshow(sample_h[:,:,0], cmap='hsv')ax[0].set_title('Hue',fontsize=15)ax[1].imshow(sample_h[:,:,1], cmap='hsv')ax[1].set_title('Saturation',fontsize=15)ax[2].imshow(sample_h[:,:,2], cmap='hsv')ax[2].set_title('Value',fontsize=15);plt.show()
The figure shows the different channels of the HSV Color space, and notice that from this different channel we can identify the needed segmentation objects. From the Value Graph, we can see the white flowers to be having a different intensity from the background.
Using HSV Color Space, we can actually segment the flowers more appropriately compared to the first two techniques. Sample code as follows:
fig, ax = plt.subplots(1,3,figsize=(15,5))im = ax[0].imshow(sample_h[:,:,0],cmap='hsv')fig.colorbar(im,ax=ax[0])ax[0].set_title('Hue Graph',fontsize=15)#set the lower and upper mask based on hue colorbar value of the desired fruitlower_mask = sample_h[:,:,0] > 0.11upper_mask = sample_h[:,:,0] < 0.3mask = upper_mask*lower_mask# get the desired mask and show in original imagered = sample[:,:,0]*maskgreen = sample[:,:,1]*maskblue = sample[:,:,2]*maskmask2 = np.dstack((red,green,blue))ax[1].imshow(mask)ax[2].imshow(mask2)ax[1].set_title('Mask',fontsize=15)ax[2].set_title('Final Image',fontsize=15)plt.tight_layout()plt.show()
From our code, notice that we first defined a lower and upper mask for the intended object. The value of the mask is derived from the color bar value at the side of the Hue Graph. Notice that the flower has a hue that is really different from the background. Using the Value we can already segment the small flower bouquets as a whole.
We also showed the resulting final image when we multiplied the mask to the original image. Notice how defined the segment flowers are from the background. We were able to segment each one of them!
From the results, we can see that we were victorious in segmenting the white flowers from the image by using a trial and error thresholding and also by using otsu’s method. By using also the HSV Color Channel, we were able to segment the flower bouquets much more precisely compared to the other two techniques. To conclude, it is important to note that these technologies have different advantages and disadvantages and can be used simultaneously to suit your needs.
Stay Tuned for Part 2!