{"id":1338,"date":"2020-03-13T02:05:41","date_gmt":"2020-03-13T02:05:41","guid":{"rendered":"https:\/\/muthu.co\/?p=1338"},"modified":"2023-12-21T00:36:48","modified_gmt":"2023-12-21T00:36:48","slug":"otsus-method-for-image-thresholding-explained-and-implemented","status":"publish","type":"post","link":"http:\/\/write.muthu.co\/otsus-method-for-image-thresholding-explained-and-implemented\/","title":{"rendered":"Otsu’s method for image thresholding explained and implemented"},"content":{"rendered":"\n
The process of separating the foreground pixels from the background is called thresholding. There are many ways of achieving optimal thresholding and one of the ways is called the Otsu’s method, proposed by Nobuyuki Otsu.<\/a> Otsu’s method[1] is a variance-based technique to find the threshold value where the weighted variance between the foreground and background pixels is the least. The key idea here is to iterate through all the possible values of threshold and measure the spread of background and foreground pixels. Then find the threshold where the spread is least.<\/p>\n\n\n\n The algorithm iteratively searches for the threshold that minimizes the within-class variance, defined as a weighted sum of variances of the two classes (background and foreground). The colors in grayscale are usually between 0-255 (0-1 in case of float). So, If we choose a threshold of 100, then all the pixels with values less than 100 becomes the background and all pixels with values greater than or equal to 100 becomes the foreground of the image.<\/p>\n\n\n\n The formula for finding the within-class variance at any threshold t<\/em> is given by:<\/p>\n\n\n\n \\(\\sigma ^2 (t) = \\omega_{bg}(t) \\sigma_{bg} ^2 (t) + \\omega_{fg}(t) \\sigma_{fg} ^2 (t) \\) \\((1) \\)<\/p>\n\n\n\n where \\(\\omega_{bg}(t) \\) and \\(\\omega_{fg}(t) \\) represents the probability of number of pixels for each class at threshold t and \\(\\sigma ^2 \\) represents the variance of color values.<\/p>\n\n\n\n To understand what this probaility means, Let,<\/p>\n\n\n\n \\(P_{all} \\) be the total count of pixels in an image, So the weights are given by,<\/p>\n\n\n\n \\(\\omega_{bg}(t) = \\frac{P_{BG}(t)}{ P_{all}} \\)<\/p>\n\n\n\n \\(\\omega_{fg}(t) = \\frac{P_{FG}(t)}{ P_{all}} \\)<\/p>\n\n\n\n \\(\\omega_{fg}(t) = \\)<\/p>\n\n\n\n The variance can be calculated using the below formula:<\/p>\n\n\n\n \\(\\sigma ^2 (t) = \\frac{ \\sum (x_{i}-\\overline{x} )^{2} }{N-1} \\)<\/p>\n\n\n\n where,<\/p>\n\n\n\n \\(x_{i} \\) is the value of pixel at i in the group (bg or fg) Now, Let’s understand the formula by finding the within-class variance at one threshold, T=100<\/p>\n\n\n For the above image, at T=100, we will get the background and foreground as shown below:<\/p>\n\n\n Here, Our weights will be,<\/p>\n\n\n\n \\(\\omega_{bg}(t) = \\frac{P_{BG}(t)}{ P_{all}} = 7\/16 = 0.44\\) Now to find the variance, we find the mean first.<\/p>\n\n\n\n \\(\\overline{x_{bg}} = \\frac{21+22+25+26+27+23+24}{7} = 24\\) The variances are given by,<\/p>\n\n\n\n \\(\\sigma_{bg} ^2 (t=100)= \\frac{(21-24)^2+(22-24)^2…….(24-24)^2}{7} = 4.0\\) Substituting everything in equation (1) we get,<\/p>\n\n\n\n \\(\\sigma ^2 (t=100) = 0.44 * 4.0 + 0.56 * 657.43 = 369.9208 \\)<\/p>\n\n\n\n Similarly, we can find for other values of t also.<\/p>\n\n\n\nAlgorithm<\/h2>\n\n\n\n
\\(P_{BG}(t) \\) be the count of background pixels at threshold t,
<\/em>\\(P_{FG}(t) \\) be the count of foreground pixels at threshold t<\/em><\/p>\n\n\n\n
\\(\\overline{x} \\) is the means of pixel values in the group (bg or fg)
\\(N \\) is the number of pixels.<\/p>\n\n\n\n
\\(P_{all} = 16 \\)
\\(P_{BG} = 7 \\)
\\(P_{FG} = 9 \\)<\/p>\n\n\n\n
\\(\\omega_{fg}(t) = \\frac{P_{FG}(t)}{ P_{all}} = 9\/16 = 0.56\\)<\/p>\n\n\n\n
\\(\\overline{x_{fg}} = \\frac{120+120+160+180+190+123+145+165+175}{9} = 153.1\\)<\/p>\n\n\n\n
\\(\\sigma_{fg} ^2 (t=100)= \\frac{(120-153.1)^2+(120-153.1)^2…….(175-153.1)^2}{9} = 657.43\\)<\/p>\n\n\n\n