File tree Expand file tree Collapse file tree 1 file changed +7
-1
lines changed Expand file tree Collapse file tree 1 file changed +7
-1
lines changed Original file line number Diff line number Diff line change 6
6
https://en.wikipedia.org/wiki/Otsu%27s_method
7
7
"""
8
8
9
+
9
10
def otsu_threshold (image : Image ) -> Image :
10
11
"""
11
12
image: is a grayscale PIL image object
@@ -40,7 +41,11 @@ def otsu_threshold(image: Image) -> Image:
40
41
mean_background = sum_foreground / weight_background
41
42
mean_foreground = (sum_total - sum_foreground ) / weight_foreground
42
43
43
- between_class_variance = weight_background * weight_foreground * (mean_background - mean_foreground ) ** 2
44
+ between_class_variance = (
45
+ weight_background
46
+ * weight_foreground
47
+ * (mean_background - mean_foreground ) ** 2
48
+ )
44
49
45
50
if between_class_variance > current_max :
46
51
current_max = between_class_variance
@@ -53,6 +58,7 @@ def otsu_threshold(image: Image) -> Image:
53
58
# Convert numpy array back to PIL image
54
59
return Image .fromarray (binary_image )
55
60
61
+
56
62
if __name__ == "__main__" :
57
63
image = otsu_threshold (Image .open ("path_to_image" ).convert ("L" ))
58
64
image .save ("output_image_path" )
You can’t perform that action at this time.
0 commit comments