https://alexandrepoupeau.com/otary/api/image/transformers/thresholding/ [ ] [ ] Skip to content logo Otary Binarization & Thresholding ( ) ( ) [ ] Initializing search poupeaua/otary * Home * API * Learn * About logo Otary poupeaua/otary * [ ] Home Home + Why Otary? + Quickstart + Installation + PyPI Releases * [*] API API + [*] Image Image o [ ] I/O I/O # Reader # Writer o Properties o Conversion o [ ] Analysis Analysis # Scoring o [*] Transformers Transformers # Cropping # Geometry # Morphology # [ ] Binarization & Thresholding Binarization & Thresholding Table of contents @ binarizer @ BinarizerImage - binary - binaryrev - threshold_adaptive - threshold_adotsu - threshold_bernsen - threshold_bradley - threshold_fair - threshold_feng - threshold_gatos - threshold_isauvola - threshold_niblack - threshold_nick - threshold_otsu - threshold_phansalkar - threshold_sauvola - threshold_simple - threshold_singh - threshold_su - threshold_wan - threshold_wolf o [ ] Drawer Drawer # Drawing # Renderers + [ ] Geometry Geometry o [ ] Discrete Discrete # Point # [ ] Shape Shape @ Polygon @ Rectangle @ Triangle # [ ] Linear Linear @ Segment @ Linear Spline @ [ ] Directed Directed - Vector - Vectorized Linear Spline o [ ] Continuous Continuous # Circle # Ellipse * [ ] Learn Learn * [ ] About About + History + A Fair Advice + Contributing + License Table of contents * binarizer * BinarizerImage + binary + binaryrev + threshold_adaptive + threshold_adotsu + threshold_bernsen + threshold_bradley + threshold_fair + threshold_feng + threshold_gatos + threshold_isauvola + threshold_niblack + threshold_nick + threshold_otsu + threshold_phansalkar + threshold_sauvola + threshold_simple + threshold_singh + threshold_su + threshold_wan + threshold_wolf Binarization & Thresholding Methods BinarizerImage component is a subpart of the Image Transformer component. Binarization converts an image to black and white, making every pixel either 0 or 255. Any color or grayscale image can be binarized. Methods fall into two types: * Global: applies a single threshold to all pixels. * Local: applies varying thresholds per pixel, usually performing better on images with uneven lighting. Otary offers 5 basic and 12 advanced binarization methods. Basic methods: simple, otsu, adaptive, bradley, and sauvola are available directly from the Image object: import otary as ot im = ot.Image.from_file(filepath="path/to/file/image") im.threshold_sauvola() Advanced methods are accessible via the transformer.binarizer attribute: im.transformer.binarizer.threshold_isauvola() BinarizerImage BinarizerImage class contains all the binarization methods. It includes only two global thresholding methods: threshold_simple and threshold_otsu. The other methods are local thresholding methods. It includes the following binarization methods, sorted by year of publication: Name Year Reference Adaptive - OpenCV Adaptive Thresholding Documentation Otsu 1979 A Threshold Selection Method from Gray-Level Histograms Bernsen 1986 "Dynamic thresholding of grey-level images" by Bernsen Niblack 1986 "An Introduction to Digital Image Processing" by Wayne Niblack Sauvola 1997 Adaptive Document Binarization Wolf 2003 Extraction and Recognition of Artificial Text in Multimedia Documents Feng 2004 Contrast adaptive binarization of low quality document images Gatos 2005 Adaptive degraded document image binarization Bradley & 2007 Adaptive Thresholding using the Integral Image Roth Nick 2009 Comparison of Niblack inspired Binarization Methods for Ancient Documents Su 2010 Binarization of historical document images using the local maximum and minimum Phansalkar 2011 Adaptive Local Thresholding for Detection of Nuclei in Diversely Stained Cytology Images Adotsu 2011 AdOtsu: An adaptive and parameterless generalization of Otsu's method for document image binarization Singh 2012 A New Local Adaptive Thresholding Technique in Binarization FAIR 2013 FAIR: A Fast Algorithm for document Image Restoration ISauvola 2016 ISauvola: Improved Sauvola's Algorithm for Document Image Binarization WAN 2018 Binarization of Document Image Using Optimum Threshold Modification Source code in otary/image/components/transformer/components/ binarizer/binarizer.py 74 class BinarizerImage: 75 # pylint: disable=line-too-long 76 """BinarizerImage class contains all the binarization methods. 77 78 It includes only two global thresholding methods: `threshold_simple` and `threshold_otsu`. The other methods are local thresholding methods. 79 80 It includes the following binarization methods, sorted by year of publication: 81 82 | Name | Year | Reference | 83 |----------------|------|------------------------------------------------------------------------------------------------------------------------------------------| 84 | Adaptive | - | [OpenCV Adaptive Thresholding Documentation](https://docs.opencv.org/4.x/d7/d4d/tutorial_py_thresholding.html) | 85 | Otsu | 1979 | [A Threshold Selection Method from Gray-Level Histograms](https://ieeexplore.ieee.org/document/4310076) | 86 | Bernsen | 1986 | "Dynamic thresholding of grey-level images" by Bernsen | 87 | Niblack | 1986 | "An Introduction to Digital Image Processing" by Wayne Niblack | 88 | Sauvola | 1997 | [Adaptive Document Binarization](https://www.researchgate.net/publication/3710586_Adaptive_Document_Binarization) | 89 | Wolf | 2003 | [Extraction and Recognition of Artificial Text in Multimedia Documents](https://hal.science/hal-01504401v1) | 90 | Feng | 2004 | [Contrast adaptive binarization of low quality document images](https://www.jstage.jst.go.jp/article/elex/1/16/1_16_501/_pdf) | 91 | Gatos | 2005 | [Adaptive degraded document image binarization](https://users.iit.demokritos.gr/~bgat/PatRec2006.pdf) | 92 | Bradley & Roth | 2007 | [Adaptive Thresholding using the Integral Image](https://www.researchgate.net/publication/220494200_Adaptive_Thresholding_using_the_Integral_Image) | 93 | Nick | 2009 | [Comparison of Niblack inspired Binarization Methods for Ancient Documents](https://www.researchgate.net/publication/221253803) | 94 | Su | 2010 | [Binarization of historical document images using the local maximum and minimum](https://www.researchgate.net/publication/220933012) | 95 | Phansalkar | 2011 | [Adaptive Local Thresholding for Detection of Nuclei in Diversely Stained Cytology Images](https://www.researchgate.net/publication/224226466) | 96 | Adotsu | 2011 | [AdOtsu: An adaptive and parameterless generalization of Otsu's method for document image binarization](https://www.researchgate.net/publication/220602345) | 97 | Singh | 2012 | [A New Local Adaptive Thresholding Technique in Binarization](https://www.researchgate.net/publication/220485031) | 98 | FAIR | 2013 | [FAIR: A Fast Algorithm for document Image Restoration](https://amu.hal.science/hal-01479805/document) | 99 | ISauvola | 2016 | [ISauvola: Improved Sauvola's Algorithm for Document Image Binarization](https://www.researchgate.net/publication/304621554_ISauvola_Improved_Sauvola) | 100 | WAN | 2018 | [Binarization of Document Image Using Optimum Threshold Modification](https://www.researchgate.net/publication/326026836) | 101 """ 102 103 def __init__(self, base: BaseImage) -> None: 104 self.base = base 105 106 # ----------------------------- GLOBAL THRESHOLDING ------------------------------- 107 108 def threshold_simple(self, thresh: int) -> "Image": 109 """Compute the image thresholded by a single value T. 110 All pixels with value v <= T are turned black and those with value v > T are 111 turned white. This is a global thresholding method. 112 113 Args: 114 thresh (int): value to separate the black from the white pixels. 115 """ 116 self.base.as_grayscale() 117 self.base.asarray = np.array((self.base.asarray > thresh) * 255, dtype=np.uint8) 118 return self.base.parent 119 120 def threshold_otsu(self) -> "Image": 121 """Apply Otsu global thresholding. 122 This is a global thresholding method that automatically determines 123 an optimal threshold value from the image histogram. 124 125 Paper (1979): 126 [A Threshold Selection Method from Gray-Level Histograms](https://ieeexplore.ieee.org/document/4310076) 127 128 Consider applying a gaussian blur before for better thresholding results. 129 See why in the [OpenCV documentation](https://docs.opencv.org/4.x/d7/d4d/tutorial_py_thresholding.html). 130 """ 131 self.base.as_grayscale() 132 _, img_thresholded = cv2.threshold( 133 self.base.asarray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU 134 ) 135 self.base.asarray = img_thresholded 136 return self.base.parent 137 138 # ------------------------------ LOCAL THRESHOLDING ------------------------------- 139 140 def threshold_adaptive( 141 self, block_size: int = 11, constant: float = 2.0 142 ) -> "Image": 143 """Apply adaptive local thresholding. 144 This is a local thresholding method that computes the threshold for a pixel 145 based on a small region around it. 146 147 Consider applying a gaussian blur before for better thresholding results. 148 See why in the [OpenCV documentation](https://docs.opencv.org/4.x/d7/d4d/tutorial_py_thresholding.html). 149 150 Args: 151 block_size (int, optional): Size of a pixel neighborhood that is used to 152 calculate a threshold value for the pixel: 3, 5, 7, and so on. 153 Defaults to 11. 154 constant (int, optional): Constant subtracted from the mean or weighted 155 mean. Normally, it is positive but may be zero or negative as well. 156 Defaults to 2. 157 """ 158 self.base.as_grayscale() 159 self.base.asarray = cv2.adaptiveThreshold( 160 src=self.base.asarray, 161 maxValue=255, 162 adaptiveMethod=cv2.ADAPTIVE_THRESH_GAUSSIAN_C, 163 thresholdType=cv2.THRESH_BINARY, 164 blockSize=block_size, 165 C=constant, 166 ) 167 return self.base.parent 168 169 def threshold_bernsen( 170 self, 171 window_size: int = 75, 172 contrast_limit: float = 25, 173 threshold_global: int = 100, 174 ) -> "Image": 175 """Apply Bernsen local thresholding. 176 177 Paper (1986): 178 "Dynamic thresholding of grey-level images" by Bernsen. 179 180 Args: 181 window_size (int, optional): window size for local computations. 182 Defaults to 75. 183 contrast_limit (float, optional): contrast limit. If the 184 contrast is higher than this value, the pixel is thresholded by the 185 bernsen threshold otherwise the global threshold is used. 186 Defaults to 25. 187 threshold_global (int, optional): global threshold. Defaults to 100. 188 """ 189 self.base.as_grayscale() 190 self.base.asarray = threshold_bernsen( 191 img=self.base.asarray, 192 window_size=window_size, 193 contrast_limit=contrast_limit, 194 threshold_global=threshold_global, 195 ) 196 return self.base.parent 197 198 def threshold_niblack(self, window_size: int = 15, k: float = -0.2) -> "Image": 199 """Apply Niblack local thresholding. 200 201 Book (1986): 202 "An Introduction to Digital Image Processing" by Wayne Niblack. 203 204 Args: 205 window_size (int, optional): apply on the 206 image. Defaults to 15. 207 k (float, optional): factor to apply to regulate the impact 208 of the std. Defaults to -0.2. 209 """ 210 self.base.as_grayscale() 211 self.base.asarray = threshold_niblack_like( 212 img=self.base.asarray, method="niblack", window_size=window_size, k=k 213 )[1] 214 return self.base.parent 215 216 def threshold_sauvola( 217 self, window_size: int = 15, k: float = 0.5, r: float = 128.0 218 ) -> "Image": 219 """Apply Sauvola local thresholding. 220 This is a local thresholding method that computes the threshold for a pixel 221 based on a small region around it. 222 223 Paper (1997): 224 [Adaptive Document Binarization](https://www.researchgate.net/publication/3710586_Adaptive_Document_Binarization) 225 226 Args: 227 window_size (int, optional): sauvola window size to apply on the 228 image. Defaults to 15. 229 k (float, optional): sauvola k factor to apply to regulate the impact 230 of the std. Defaults to 0.5. 231 r (float, optional): sauvola r value. Defaults to 128. 232 """ 233 self.base.as_grayscale() 234 self.base.asarray = threshold_niblack_like( 235 img=self.base.asarray, method="sauvola", window_size=window_size, k=k, r=r 236 )[1] 237 return self.base.parent 238 239 def threshold_wolf(self, window_size: int = 15, k: float = 0.5) -> "Image": 240 """Apply Wolf local thresholding. 241 242 Paper (2003): 243 [Extraction and Recognition of Artificial Text in Multimedia Documents](https://hal.science/hal-01504401v1) 244 245 Args: 246 window_size (int, optional): apply on the 247 image. Defaults to 15. 248 k (float, optional): factor to apply to regulate the impact 249 of the std. Defaults to 0.5. 250 """ 251 self.base.as_grayscale() 252 self.base.asarray = threshold_niblack_like( 253 img=self.base.asarray, method="wolf", window_size=window_size, k=k 254 )[1] 255 return self.base.parent 256 257 def threshold_feng( 258 self, 259 w1: int = 19, 260 w2: int = 33, 261 alpha1: float = 0.12, 262 k1: float = 0.25, 263 k2: float = 0.04, 264 gamma: float = 2.0, 265 ) -> "Image": 266 """Implementation of the Feng thresholding method. 267 268 Paper (2004): 269 [Contrast adaptive binarization of low quality document images](https://www.jstage.jst.go.jp/article/elex/1/16/1_16_501/_pdf) 270 271 Args: 272 w1 (int, optional): primary window size. Defaults to 19. 273 w2 (int, optional): secondary window value. Defaults to 33. 274 alpha1 (float, optional): alpha1 value. Defaults to 0.12. 275 k1 (float, optional): k1 value. Defaults to 0.25. 276 k2 (float, optional): k2 value. Defaults to 0.04. 277 gamma (float, optional): gamma value. Defaults to 2.0. 278 """ 279 # pylint: disable=too-many-arguments, too-many-positional-arguments 280 self.base.as_grayscale() 281 self.base.asarray = threshold_feng( 282 img=self.base.asarray, 283 w1=w1, 284 w2=w2, 285 alpha1=alpha1, 286 k1=k1, 287 k2=k2, 288 gamma=gamma, 289 ) 290 return self.base.parent 291 292 def threshold_gatos( 293 self, 294 q: float = 0.6, 295 p1: float = 0.5, 296 p2: float = 0.8, 297 lh: Optional[float] = None, 298 upsampling: bool = False, 299 upsampling_factor: int = 2, 300 ) -> "Image": 301 """Apply Gatos local thresholding. 302 303 Paper (2005): 304 [Adaptive degraded document image binarization](https://users.iit.demokritos.gr/~bgat/PatRec2006.pdf) 305 306 Args: 307 q (float, optional): q gatos factor. Defaults to 0.6. 308 p1 (float, optional): p1 gatos factor. Defaults to 0.5. 309 p2 (float, optional): p2 gatos factor. Defaults to 0.8. 310 lh (Optional[float], optional): height of character. 311 Defaults to None, meaning it is computed automatically to be 312 a fraction of the image size. 313 upsampling (bool, optional): whether to apply gatos upsampling definition. 314 Defaults to False. 315 upsampling_factor (int, optional): gatos upsampling factor. Defaults to 2. 316 """ 317 # pylint: disable=too-many-arguments, too-many-positional-arguments 318 # pylint: disable=duplicate-code 319 self.base.as_grayscale() 320 self.base.asarray = threshold_gatos( 321 img=self.base.asarray, 322 q=q, 323 p1=p1, 324 p2=p2, 325 lh=lh, 326 upsampling=upsampling, 327 upsampling_factor=upsampling_factor, 328 ) 329 return self.base.parent 330 331 def threshold_bradley(self, window_size: int = 15, t: float = 0.15) -> "Image": 332 """Implementation of the Bradley & Roth thresholding method. 333 334 Paper (2007): 335 [Adaptive Thresholding using the Integral Image](https://www.researchgate.net/publication/220494200_Adaptive_Thresholding_using_the_Integral_Image) 336 337 Args: 338 window_size (int, optional): window size for local computations. 339 Defaults to 15. 340 t (float, optional): t value in [0, 1]. Defaults to 0.15. 341 342 Returns: 343 NDArray[np.uint8]: output thresholded image 344 """ 345 self.base.as_grayscale() 346 self.base.asarray = threshold_bradley( 347 img=self.base.asarray, window_size=window_size, t=t 348 ) 349 return self.base.parent 350 351 def threshold_nick(self, window_size: int = 19, k: float = -0.1) -> "Image": 352 """Apply Nick local thresholding. 353 354 Paper (2009): 355 [Comparison of Niblack inspired Binarization Methods for Ancient Documents](https://www.researchgate.net/publication/221253803) 356 357 The paper suggests to use a window size of 19 and a k factor in [-0.2, -0.1]. 358 359 Args: 360 window_size (int, optional): apply on the 361 image. Defaults to 15. 362 k (float, optional): factor to apply to regulate the impact 363 of the std. Defaults to -0.1. 364 """ 365 self.base.as_grayscale() 366 self.base.asarray = threshold_niblack_like( 367 img=self.base.asarray, method="nick", window_size=window_size, k=k 368 )[1] 369 return self.base.parent 370 371 def threshold_su( 372 self, 373 window_size: int = 3, 374 n_min: int = -1, 375 ) -> "Image": 376 """Compute the Su local thresholding. 377 378 Paper (2010): 379 [Binarization of historical document images using the local maximum and minimum](https://www.researchgate.net/publication/220933012) 380 381 Args: 382 window_size (int, optional): window size for high contrast image 383 computation. Defaults to 3. 384 n_min (int, optional): minimum number of high contrast pixels within the 385 neighborhood window. Defaults to -1 meaning that n_min = window_size. 386 """ 387 self.base.as_grayscale() 388 self.base.asarray = threshold_su( 389 img=self.base.asarray, window_size=window_size, n_min=n_min 390 ) 391 return self.base.parent 392 393 def threshold_phansalkar( 394 self, window_size: int = 40, k: float = 0.25, p: float = 3.0, q: float = 10.0 395 ) -> "Image": 396 """Apply Phansalkar et al. local thresholding. 397 398 Paper (2011): 399 [Adaptive Local Thresholding for Detection of Nuclei in Diversely Stained Cytology Images](https://www.researchgate.net/publication/224226466) 400 401 Args: 402 window_size (int, optional): apply on the 403 image. Defaults to 40. 404 k (float, optional): factor to apply to regulate the impact 405 of the std. Defaults to 0.25. 406 p (float, optional): Phansalkar parameter to regulate low contrast zones. 407 Defaults to 3.0. 408 q (float, optional): Phansalkar parameter to regulate low contrast zones. 409 Defaults to 10.0. 410 """ 411 self.base.as_grayscale() 412 self.base.asarray = threshold_niblack_like( 413 img=self.base.asarray, 414 method="phansalkar", 415 window_size=window_size, 416 k=k, 417 p=p, 418 q=q, 419 )[1] 420 return self.base.parent 421 422 def threshold_adotsu( 423 self, grid_size: int = 50, k_sigma: float = 1.6, n_steps: int = 2 424 ) -> "Image": 425 """Apply Adotsu local thresholding. 426 427 Paper (2011): 428 [AdOtsu: An adaptive and parameterless generalization of Otsu's method for document image binarization](https://www.researchgate.net/publication/220602345) 429 430 Args: 431 grid_size (int, optional): window size for local computations. 432 Defaults to 15. 433 k_sigma (float, optional): k_sigma value in [1, 2]. Defaults to 1.6. 434 n_steps (int, optional): number of iterations to update the binarization by 435 estimating a new background surface. Defaults to 2. 436 """ 437 self.base.as_grayscale() 438 self.base.asarray = threshold_adotsu( 439 img=self.base.asarray, grid_size=grid_size, k_sigma=k_sigma, n_steps=n_steps 440 ) 441 return self.base.parent 442 443 def threshold_singh(self, window_size: int = 15, k: float = 0.06) -> "Image": 444 """Apply Singh local thresholding. 445 446 Paper (2012): 447 [A New Local Adaptive Thresholding Technique in Binarization](https://www.researchgate.net/publication/220485031) 448 449 Args: 450 window_size (int, optional): apply on the 451 image. Defaults to 15. 452 k (float, optional): factor to apply to regulate the impact 453 of the std. Defaults to 0.06. 454 """ 455 self.base.as_grayscale() 456 self.base.asarray = threshold_niblack_like( 457 img=self.base.asarray, method="singh", window_size=window_size, k=k 458 )[1] 459 return self.base.parent 460 461 def threshold_fair( 462 self, 463 sfair_window_size: int = 33, 464 sfair_clustering_algo: str = "otsu", 465 sfair_clustering_max_iter: int = 20, 466 sfair_thining: float = 1.0, 467 sfair_alpha: float = 0.38, 468 post_stain_max_pixels: int = 25, 469 post_misclass_txt: bool = True, 470 post_clustering_algo: str = "otsu", 471 post_clustering_max_iter: int = 10, 472 post_max_iter: int = 15, 473 post_window_size: int = 75, 474 post_beta: float = 1.0, 475 ) -> "Image": 476 """Apply FAIR local thresholding. 477 478 Paper (2013): 479 [FAIR: A Fast Algorithm for document Image Restoration](https://amu.hal.science/hal-01479805/document) 480 481 Args: 482 sfair_window_size (int, optional): window size in preprocess 483 to cluster background and foreground pixels around edge pixels. 484 This parameter is important as a higher value will make the method 485 more robust to noise but also more computationally expensive and slow. 486 Defaults to 5. 487 sfair_clustering_algo (str, optional): clustering algorithm for the S-FAIR 488 step. Defaults to "otsu". 489 sfair_clustering_max_iter (int, optional): maximum number of iterations for 490 the clustering algorithm within the S-FAIR step. Defaults to 20. 491 sfair_thining (float, optional): thining factor in [0, 1]. 0 means no 492 thinning which means that all edge pixels are processed. 493 1 means that only every 494 sfair_window_size // 2 edge pixels are processed which signicantly 495 speeds up the computation. Defaults to 1.0. 496 sfair_alpha (float, optional): It defines the ratio to compute the lower 497 threshold in the 1st step of the S-FAIR step. 498 It is generally in [0.3, 0.5]. 499 Defaults to 0.38. 500 post_stain_max_pixels (int, optional): maximum number of pixels for a stain 501 to be considered as an unknown connected component. Defaults to 25. 502 post_misclass_txt (bool, optional): whether to perform the 503 post-processing correct_misclassified_text_pixels step. 504 Defaults to True. 505 post_clustering_algo (str, optional): clustering algorithm for the 506 post-processing step. Defaults to "otsu". 507 post_clustering_max_iter (int, optional): maximum number of iterations for 508 the clustering algorithm within the post-processing step. 509 Defaults to 10. 510 post_max_iter (int, optional): maximum number of iterations for the 511 correct_misclassified_text_pixels step within the post-processing step. 512 Defaults to 15. 513 post_window_size (int, optional): window size in postprocess 514 to cluster background and foreground pixels around edge pixels. 515 This parameter is important as a higher value will make the method 516 more robust to noise but also more computationally expensive and slow. 517 Defaults to 75. 518 post_beta (float, optional): factor to define if the unkown pixels 519 should be set as text or background. If beta is 1 then 520 unknown pixels are set to text if the number of surrounding text pixels 521 (N_t) is higher than the number of surrounding background pixels (N_b). 522 Simply N_t > N_b. Beta is the value to put more flexibility on the rule 523 and thus set unknown pixels to text if N_t > beta * N_b 524 Defaults to 1.0. 525 """ 526 # pylint: disable=too-many-arguments, too-many-positional-arguments 527 # pylint: disable=duplicate-code 528 self.base.as_grayscale() 529 self.base.asarray = threshold_fair( 530 img=self.base.asarray, 531 sfair_window_size=sfair_window_size, 532 sfair_clustering_algo=sfair_clustering_algo, 533 sfair_clustering_max_iter=sfair_clustering_max_iter, 534 sfair_thining=sfair_thining, 535 sfair_alpha=sfair_alpha, 536 post_stain_max_pixels=post_stain_max_pixels, 537 post_misclass_txt=post_misclass_txt, 538 post_clustering_algo=post_clustering_algo, 539 post_max_iter=post_max_iter, 540 post_clustering_max_iter=post_clustering_max_iter, 541 post_window_size=post_window_size, 542 post_beta=post_beta, 543 ) 544 return self.base.parent 545 546 def threshold_isauvola( 547 self, 548 window_size: int = 15, 549 k: float = 0.01, 550 r: float = 128.0, 551 connectivity: int = 8, 552 contrast_window_size: int = 3, 553 opening_n_min_pixels: int = 0, 554 opening_connectivity: int = 8, 555 ) -> "Image": 556 """Apply ISauvola local thresholding. 557 558 Paper (2016): 559 [ISauvola: Improved Sauvola's Algorithm for Document Image Binarization](https://www.researchgate.net/publication/304621554_ISauvola_Improved_Sauvola) 560 561 Args: 562 window_size (int, optional): apply on the 563 image. Defaults to 15. 564 k (float, optional): factor to apply to regulate the impact 565 of the std. Defaults to 0.01. 566 r (float, optional): factor to apply to regulate the impact 567 of the std. Defaults to 128. 568 connectivity (int, optional): connectivity to apply on the 569 image. Defaults to 8. 570 contrast_window_size (int, optional): contrast window size to apply on the 571 image. Defaults to 3. 572 opening_n_min_pixels (int, optional): opening n min pixels to apply on the 573 image. Defaults to 0. 574 opening_connectivity (int, optional): opening connectivity to apply on the 575 image. Defaults to 8. 576 """ 577 # pylint: disable=too-many-arguments, too-many-positional-arguments 578 self.base.as_grayscale() 579 self.base.asarray = threshold_isauvola( 580 img=self.base.asarray, 581 window_size=window_size, 582 k=k, 583 r=r, 584 connectivity=connectivity, 585 contrast_window_size=contrast_window_size, 586 opening_n_min_pixels=opening_n_min_pixels, 587 opening_connectivity=opening_connectivity, 588 ) 589 return self.base.parent 590 591 def threshold_wan( 592 self, window_size: int = 15, k: float = 0.5, r: float = 128.0 593 ) -> "Image": 594 """Apply Wan local thresholding. 595 596 Paper (2018): 597 [Binarization of Document Image Using Optimum Threshold Modification](https://www.researchgate.net/publication/326026836) 598 599 Args: 600 window_size (int, optional): apply on the 601 image. Defaults to 15. 602 k (float, optional): factor to apply to regulate the impact 603 of the std. Defaults to 0.5. 604 """ 605 self.base.as_grayscale() 606 self.base.asarray = threshold_niblack_like( 607 img=self.base.asarray, method="wan", window_size=window_size, k=k, r=r 608 )[1] 609 return self.base.parent 610 611 # ---------------------------- BINARY REPRESENTATION ------------------------------ 612 613 def binary(self, method: BinarizationMethods = "sauvola") -> NDArray: 614 """Binary representation of the image with values that can be only 0 or 1. 615 The value 0 is now 0 and value of 255 are now 1. Black is 0 and white is 1. 616 We can also talk about the mask of the image to refer to the binary 617 representation of it. 618 619 The sauvola is generally the best binarization method however it is 620 way slower than the others methods. The adaptative or otsu method are the best 621 method in terms of speed and quality. 622 623 Args: 624 method (str, optional): the binarization method to apply. 625 Look at the BinarizationMethods to see all the available methods. 626 Defaults to "sauvola". 627 628 Returns: 629 NDArray: array where its inner values are 0 or 1 630 """ 631 if method not in list(get_args(BinarizationMethods)): 632 raise ValueError( 633 f"Invalid binarization method {method}. " 634 f"Must be in {BinarizationMethods}" 635 ) 636 getattr(self, f"threshold_{method}")() 637 return self.base.asarray_binary 638 639 def binaryrev(self, method: BinarizationMethods = "sauvola") -> NDArray: 640 """Reversed binary representation of the image. 641 The value 0 is now 1 and value of 255 are now 0. Black is 1 and white is 0. 642 This is why it is called the "binary rev" or "binary reversed". 643 644 Args: 645 method (str, optional): the binarization method to apply. 646 Defaults to "adaptative". 647 648 Returns: 649 NDArray: array where its inner values are 0 or 1 650 """ 651 return 1 - self.binary(method=method) binary(method='sauvola') Binary representation of the image with values that can be only 0 or 1. The value 0 is now 0 and value of 255 are now 1. Black is 0 and white is 1. We can also talk about the mask of the image to refer to the binary representation of it. The sauvola is generally the best binarization method however it is way slower than the others methods. The adaptative or otsu method are the best method in terms of speed and quality. Parameters: Name Type Description Default the binarization method to apply. Look at the method str BinarizationMethods to see all the available 'sauvola' methods. Defaults to "sauvola". Returns: Name Type Description NDArray NDArray array where its inner values are 0 or 1 Source code in otary/image/components/transformer/components/ binarizer/binarizer.py 613 def binary(self, method: BinarizationMethods = "sauvola") -> NDArray: 614 """Binary representation of the image with values that can be only 0 or 1. 615 The value 0 is now 0 and value of 255 are now 1. Black is 0 and white is 1. 616 We can also talk about the mask of the image to refer to the binary 617 representation of it. 618 619 The sauvola is generally the best binarization method however it is 620 way slower than the others methods. The adaptative or otsu method are the best 621 method in terms of speed and quality. 622 623 Args: 624 method (str, optional): the binarization method to apply. 625 Look at the BinarizationMethods to see all the available methods. 626 Defaults to "sauvola". 627 628 Returns: 629 NDArray: array where its inner values are 0 or 1 630 """ 631 if method not in list(get_args(BinarizationMethods)): 632 raise ValueError( 633 f"Invalid binarization method {method}. " 634 f"Must be in {BinarizationMethods}" 635 ) 636 getattr(self, f"threshold_{method}")() 637 return self.base.asarray_binary binaryrev(method='sauvola') Reversed binary representation of the image. The value 0 is now 1 and value of 255 are now 0. Black is 1 and white is 0. This is why it is called the "binary rev" or "binary reversed". Parameters: Name Type Description Default method str the binarization method to apply. Defaults to 'sauvola' "adaptative". Returns: Name Type Description NDArray NDArray array where its inner values are 0 or 1 Source code in otary/image/components/transformer/components/ binarizer/binarizer.py 639 def binaryrev(self, method: BinarizationMethods = "sauvola") -> NDArray: 640 """Reversed binary representation of the image. 641 The value 0 is now 1 and value of 255 are now 0. Black is 1 and white is 0. 642 This is why it is called the "binary rev" or "binary reversed". 643 644 Args: 645 method (str, optional): the binarization method to apply. 646 Defaults to "adaptative". 647 648 Returns: 649 NDArray: array where its inner values are 0 or 1 650 """ 651 return 1 - self.binary(method=method) threshold_adaptive(block_size=11, constant=2.0) Apply adaptive local thresholding. This is a local thresholding method that computes the threshold for a pixel based on a small region around it. Consider applying a gaussian blur before for better thresholding results. See why in the OpenCV documentation. Parameters: Name Type Description Default Size of a pixel neighborhood that is used to block_size int calculate a threshold value for the pixel: 3, 11 5, 7, and so on. Defaults to 11. Constant subtracted from the mean or weighted constant int mean. Normally, it is positive but may be 2.0 zero or negative as well. Defaults to 2. Source code in otary/image/components/transformer/components/ binarizer/binarizer.py 140 def threshold_adaptive( 141 self, block_size: int = 11, constant: float = 2.0 142 ) -> "Image": 143 """Apply adaptive local thresholding. 144 This is a local thresholding method that computes the threshold for a pixel 145 based on a small region around it. 146 147 Consider applying a gaussian blur before for better thresholding results. 148 See why in the [OpenCV documentation](https://docs.opencv.org/4.x/d7/d4d/tutorial_py_thresholding.html). 149 150 Args: 151 block_size (int, optional): Size of a pixel neighborhood that is used to 152 calculate a threshold value for the pixel: 3, 5, 7, and so on. 153 Defaults to 11. 154 constant (int, optional): Constant subtracted from the mean or weighted 155 mean. Normally, it is positive but may be zero or negative as well. 156 Defaults to 2. 157 """ 158 self.base.as_grayscale() 159 self.base.asarray = cv2.adaptiveThreshold( 160 src=self.base.asarray, 161 maxValue=255, 162 adaptiveMethod=cv2.ADAPTIVE_THRESH_GAUSSIAN_C, 163 thresholdType=cv2.THRESH_BINARY, 164 blockSize=block_size, 165 C=constant, 166 ) 167 return self.base.parent threshold_adotsu(grid_size=50, k_sigma=1.6, n_steps=2) Apply Adotsu local thresholding. Paper (2011): AdOtsu: An adaptive and parameterless generalization of Otsu's method for document image binarization Parameters: Name Type Description Default window size for local computations. Defaults grid_size int to 15. 50 k_sigma float k_sigma value in [1, 2]. Defaults to 1.6. 1.6 number of iterations to update the n_steps int binarization by estimating a new background 2 surface. Defaults to 2. Source code in otary/image/components/transformer/components/ binarizer/binarizer.py 422 def threshold_adotsu( 423 self, grid_size: int = 50, k_sigma: float = 1.6, n_steps: int = 2 424 ) -> "Image": 425 """Apply Adotsu local thresholding. 426 427 Paper (2011): 428 [AdOtsu: An adaptive and parameterless generalization of Otsu's method for document image binarization](https://www.researchgate.net/publication/220602345) 429 430 Args: 431 grid_size (int, optional): window size for local computations. 432 Defaults to 15. 433 k_sigma (float, optional): k_sigma value in [1, 2]. Defaults to 1.6. 434 n_steps (int, optional): number of iterations to update the binarization by 435 estimating a new background surface. Defaults to 2. 436 """ 437 self.base.as_grayscale() 438 self.base.asarray = threshold_adotsu( 439 img=self.base.asarray, grid_size=grid_size, k_sigma=k_sigma, n_steps=n_steps 440 ) 441 return self.base.parent threshold_bernsen(window_size=75, contrast_limit=25, threshold_global =100) Apply Bernsen local thresholding. Paper (1986): "Dynamic thresholding of grey-level images" by Bernsen. Parameters: Name Type Description Default window size for local computations. window_size int Defaults to 75. 75 contrast limit. If the contrast is higher than this value, the pixel is contrast_limit float thresholded by the bernsen threshold 25 otherwise the global threshold is used. Defaults to 25. threshold_global int global threshold. Defaults to 100. 100 Source code in otary/image/components/transformer/components/ binarizer/binarizer.py 169 def threshold_bernsen( 170 self, 171 window_size: int = 75, 172 contrast_limit: float = 25, 173 threshold_global: int = 100, 174 ) -> "Image": 175 """Apply Bernsen local thresholding. 176 177 Paper (1986): 178 "Dynamic thresholding of grey-level images" by Bernsen. 179 180 Args: 181 window_size (int, optional): window size for local computations. 182 Defaults to 75. 183 contrast_limit (float, optional): contrast limit. If the 184 contrast is higher than this value, the pixel is thresholded by the 185 bernsen threshold otherwise the global threshold is used. 186 Defaults to 25. 187 threshold_global (int, optional): global threshold. Defaults to 100. 188 """ 189 self.base.as_grayscale() 190 self.base.asarray = threshold_bernsen( 191 img=self.base.asarray, 192 window_size=window_size, 193 contrast_limit=contrast_limit, 194 threshold_global=threshold_global, 195 ) 196 return self.base.parent threshold_bradley(window_size=15, t=0.15) Implementation of the Bradley & Roth thresholding method. Paper (2007): Adaptive Thresholding using the Integral Image Parameters: Name Type Description Default window size for local computations. window_size int Defaults to 15. 15 t float t value in [0, 1]. Defaults to 0.15. 0.15 Returns: Type Description Image NDArray[np.uint8]: output thresholded image Source code in otary/image/components/transformer/components/ binarizer/binarizer.py 331 def threshold_bradley(self, window_size: int = 15, t: float = 0.15) -> "Image": 332 """Implementation of the Bradley & Roth thresholding method. 333 334 Paper (2007): 335 [Adaptive Thresholding using the Integral Image](https://www.researchgate.net/publication/220494200_Adaptive_Thresholding_using_the_Integral_Image) 336 337 Args: 338 window_size (int, optional): window size for local computations. 339 Defaults to 15. 340 t (float, optional): t value in [0, 1]. Defaults to 0.15. 341 342 Returns: 343 NDArray[np.uint8]: output thresholded image 344 """ 345 self.base.as_grayscale() 346 self.base.asarray = threshold_bradley( 347 img=self.base.asarray, window_size=window_size, t=t 348 ) 349 return self.base.parent threshold_fair(sfair_window_size=33, sfair_clustering_algo='otsu', sfair_clustering_max_iter=20, sfair_thining=1.0, sfair_alpha=0.38, post_stain_max_pixels=25, post_misclass_txt=True, post_clustering_algo='otsu', post_clustering_max_iter=10, post_max_iter=15, post_window_size=75, post_beta=1.0) Apply FAIR local thresholding. Paper (2013): FAIR: A Fast Algorithm for document Image Restoration Parameters: Name Type Description Default window size in preprocess to cluster background and foreground pixels around edge pixels. This parameter is important as a sfair_window_size int higher value will make the method 33 more robust to noise but also more computationally expensive and slow. Defaults to 5. clustering algorithm for the sfair_clustering_algo str S-FAIR step. Defaults to "otsu". 'otsu' maximum number of iterations for sfair_clustering_max_iter int the clustering algorithm within 20 the S-FAIR step. Defaults to 20. thining factor in [0, 1]. 0 means no thinning which means that all edge pixels are processed. 1 means that only every sfair_thining float sfair_window_size // 2 edge 1.0 pixels are processed which signicantly speeds up the computation. Defaults to 1.0. It defines the ratio to compute the lower threshold in the 1st sfair_alpha float step of the S-FAIR step. It is 0.38 generally in [0.3, 0.5]. Defaults to 0.38. maximum number of pixels for a stain to be considered as an post_stain_max_pixels int unknown connected component. 25 Defaults to 25. whether to perform the post-processing post_misclass_txt bool correct_misclassified_text_pixels True step. Defaults to True. clustering algorithm for the post_clustering_algo str post-processing step. Defaults to 'otsu' "otsu". maximum number of iterations for the clustering algorithm within post_clustering_max_iter int the post-processing step. 10 Defaults to 10. maximum number of iterations for the post_max_iter int correct_misclassified_text_pixels 15 step within the post-processing step. Defaults to 15. window size in postprocess to cluster background and foreground pixels around edge pixels. This parameter is important as a post_window_size int higher value will make the method 75 more robust to noise but also more computationally expensive and slow. Defaults to 75. factor to define if the unkown pixels should be set as text or background. If beta is 1 then unknown pixels are set to text if the number of surrounding text post_beta float pixels (N_t) is higher than the 1.0 number of surrounding background pixels (N_b). Simply N_t > N_b. Beta is the value to put more flexibility on the rule and thus set unknown pixels to text if N_t > beta * N_b Defaults to 1.0. Source code in otary/image/components/transformer/components/ binarizer/binarizer.py 461 def threshold_fair( 462 self, 463 sfair_window_size: int = 33, 464 sfair_clustering_algo: str = "otsu", 465 sfair_clustering_max_iter: int = 20, 466 sfair_thining: float = 1.0, 467 sfair_alpha: float = 0.38, 468 post_stain_max_pixels: int = 25, 469 post_misclass_txt: bool = True, 470 post_clustering_algo: str = "otsu", 471 post_clustering_max_iter: int = 10, 472 post_max_iter: int = 15, 473 post_window_size: int = 75, 474 post_beta: float = 1.0, 475 ) -> "Image": 476 """Apply FAIR local thresholding. 477 478 Paper (2013): 479 [FAIR: A Fast Algorithm for document Image Restoration](https://amu.hal.science/hal-01479805/document) 480 481 Args: 482 sfair_window_size (int, optional): window size in preprocess 483 to cluster background and foreground pixels around edge pixels. 484 This parameter is important as a higher value will make the method 485 more robust to noise but also more computationally expensive and slow. 486 Defaults to 5. 487 sfair_clustering_algo (str, optional): clustering algorithm for the S-FAIR 488 step. Defaults to "otsu". 489 sfair_clustering_max_iter (int, optional): maximum number of iterations for 490 the clustering algorithm within the S-FAIR step. Defaults to 20. 491 sfair_thining (float, optional): thining factor in [0, 1]. 0 means no 492 thinning which means that all edge pixels are processed. 493 1 means that only every 494 sfair_window_size // 2 edge pixels are processed which signicantly 495 speeds up the computation. Defaults to 1.0. 496 sfair_alpha (float, optional): It defines the ratio to compute the lower 497 threshold in the 1st step of the S-FAIR step. 498 It is generally in [0.3, 0.5]. 499 Defaults to 0.38. 500 post_stain_max_pixels (int, optional): maximum number of pixels for a stain 501 to be considered as an unknown connected component. Defaults to 25. 502 post_misclass_txt (bool, optional): whether to perform the 503 post-processing correct_misclassified_text_pixels step. 504 Defaults to True. 505 post_clustering_algo (str, optional): clustering algorithm for the 506 post-processing step. Defaults to "otsu". 507 post_clustering_max_iter (int, optional): maximum number of iterations for 508 the clustering algorithm within the post-processing step. 509 Defaults to 10. 510 post_max_iter (int, optional): maximum number of iterations for the 511 correct_misclassified_text_pixels step within the post-processing step. 512 Defaults to 15. 513 post_window_size (int, optional): window size in postprocess 514 to cluster background and foreground pixels around edge pixels. 515 This parameter is important as a higher value will make the method 516 more robust to noise but also more computationally expensive and slow. 517 Defaults to 75. 518 post_beta (float, optional): factor to define if the unkown pixels 519 should be set as text or background. If beta is 1 then 520 unknown pixels are set to text if the number of surrounding text pixels 521 (N_t) is higher than the number of surrounding background pixels (N_b). 522 Simply N_t > N_b. Beta is the value to put more flexibility on the rule 523 and thus set unknown pixels to text if N_t > beta * N_b 524 Defaults to 1.0. 525 """ 526 # pylint: disable=too-many-arguments, too-many-positional-arguments 527 # pylint: disable=duplicate-code 528 self.base.as_grayscale() 529 self.base.asarray = threshold_fair( 530 img=self.base.asarray, 531 sfair_window_size=sfair_window_size, 532 sfair_clustering_algo=sfair_clustering_algo, 533 sfair_clustering_max_iter=sfair_clustering_max_iter, 534 sfair_thining=sfair_thining, 535 sfair_alpha=sfair_alpha, 536 post_stain_max_pixels=post_stain_max_pixels, 537 post_misclass_txt=post_misclass_txt, 538 post_clustering_algo=post_clustering_algo, 539 post_max_iter=post_max_iter, 540 post_clustering_max_iter=post_clustering_max_iter, 541 post_window_size=post_window_size, 542 post_beta=post_beta, 543 ) 544 return self.base.parent threshold_feng(w1=19, w2=33, alpha1=0.12, k1=0.25, k2=0.04, gamma=2.0 ) Implementation of the Feng thresholding method. Paper (2004): Contrast adaptive binarization of low quality document images Parameters: Name Type Description Default w1 int primary window size. Defaults to 19. 19 w2 int secondary window value. Defaults to 33. 33 alpha1 float alpha1 value. Defaults to 0.12. 0.12 k1 float k1 value. Defaults to 0.25. 0.25 k2 float k2 value. Defaults to 0.04. 0.04 gamma float gamma value. Defaults to 2.0. 2.0 Source code in otary/image/components/transformer/components/ binarizer/binarizer.py 257 def threshold_feng( 258 self, 259 w1: int = 19, 260 w2: int = 33, 261 alpha1: float = 0.12, 262 k1: float = 0.25, 263 k2: float = 0.04, 264 gamma: float = 2.0, 265 ) -> "Image": 266 """Implementation of the Feng thresholding method. 267 268 Paper (2004): 269 [Contrast adaptive binarization of low quality document images](https://www.jstage.jst.go.jp/article/elex/1/16/1_16_501/_pdf) 270 271 Args: 272 w1 (int, optional): primary window size. Defaults to 19. 273 w2 (int, optional): secondary window value. Defaults to 33. 274 alpha1 (float, optional): alpha1 value. Defaults to 0.12. 275 k1 (float, optional): k1 value. Defaults to 0.25. 276 k2 (float, optional): k2 value. Defaults to 0.04. 277 gamma (float, optional): gamma value. Defaults to 2.0. 278 """ 279 # pylint: disable=too-many-arguments, too-many-positional-arguments 280 self.base.as_grayscale() 281 self.base.asarray = threshold_feng( 282 img=self.base.asarray, 283 w1=w1, 284 w2=w2, 285 alpha1=alpha1, 286 k1=k1, 287 k2=k2, 288 gamma=gamma, 289 ) 290 return self.base.parent threshold_gatos(q=0.6, p1=0.5, p2=0.8, lh=None, upsampling=False, upsampling_factor=2) Apply Gatos local thresholding. Paper (2005): Adaptive degraded document image binarization Parameters: Name Type Description Default q float q gatos factor. Defaults to 0.6. 0.6 p1 float p1 gatos factor. Defaults to 0.5. 0.5 p2 float p2 gatos factor. Defaults to 0.8. 0.8 height of character. Defaults to Optional None, meaning it is computed lh [float] automatically to be a fraction of None the image size. whether to apply gatos upsampling upsampling bool definition. Defaults to False. False upsampling_factor int gatos upsampling factor. Defaults 2 to 2. Source code in otary/image/components/transformer/components/ binarizer/binarizer.py 292 def threshold_gatos( 293 self, 294 q: float = 0.6, 295 p1: float = 0.5, 296 p2: float = 0.8, 297 lh: Optional[float] = None, 298 upsampling: bool = False, 299 upsampling_factor: int = 2, 300 ) -> "Image": 301 """Apply Gatos local thresholding. 302 303 Paper (2005): 304 [Adaptive degraded document image binarization](https://users.iit.demokritos.gr/~bgat/PatRec2006.pdf) 305 306 Args: 307 q (float, optional): q gatos factor. Defaults to 0.6. 308 p1 (float, optional): p1 gatos factor. Defaults to 0.5. 309 p2 (float, optional): p2 gatos factor. Defaults to 0.8. 310 lh (Optional[float], optional): height of character. 311 Defaults to None, meaning it is computed automatically to be 312 a fraction of the image size. 313 upsampling (bool, optional): whether to apply gatos upsampling definition. 314 Defaults to False. 315 upsampling_factor (int, optional): gatos upsampling factor. Defaults to 2. 316 """ 317 # pylint: disable=too-many-arguments, too-many-positional-arguments 318 # pylint: disable=duplicate-code 319 self.base.as_grayscale() 320 self.base.asarray = threshold_gatos( 321 img=self.base.asarray, 322 q=q, 323 p1=p1, 324 p2=p2, 325 lh=lh, 326 upsampling=upsampling, 327 upsampling_factor=upsampling_factor, 328 ) 329 return self.base.parent threshold_isauvola(window_size=15, k=0.01, r=128.0, connectivity=8, contrast_window_size=3, opening_n_min_pixels=0, opening_connectivity= 8) Apply ISauvola local thresholding. Paper (2016): ISauvola: Improved Sauvola's Algorithm for Document Image Binarization Parameters: Name Type Description Default apply on the image. Defaults to window_size int 15. 15 factor to apply to regulate the k float impact of the std. Defaults to 0.01 0.01. factor to apply to regulate the r float impact of the std. Defaults to 128.0 128. connectivity to apply on the connectivity int image. Defaults to 8. 8 contrast window size to apply on contrast_window_size int the image. Defaults to 3. 3 opening n min pixels to apply on opening_n_min_pixels int the image. Defaults to 0. 0 opening_connectivity int opening connectivity to apply on 8 the image. Defaults to 8. Source code in otary/image/components/transformer/components/ binarizer/binarizer.py 546 def threshold_isauvola( 547 self, 548 window_size: int = 15, 549 k: float = 0.01, 550 r: float = 128.0, 551 connectivity: int = 8, 552 contrast_window_size: int = 3, 553 opening_n_min_pixels: int = 0, 554 opening_connectivity: int = 8, 555 ) -> "Image": 556 """Apply ISauvola local thresholding. 557 558 Paper (2016): 559 [ISauvola: Improved Sauvola's Algorithm for Document Image Binarization](https://www.researchgate.net/publication/304621554_ISauvola_Improved_Sauvola) 560 561 Args: 562 window_size (int, optional): apply on the 563 image. Defaults to 15. 564 k (float, optional): factor to apply to regulate the impact 565 of the std. Defaults to 0.01. 566 r (float, optional): factor to apply to regulate the impact 567 of the std. Defaults to 128. 568 connectivity (int, optional): connectivity to apply on the 569 image. Defaults to 8. 570 contrast_window_size (int, optional): contrast window size to apply on the 571 image. Defaults to 3. 572 opening_n_min_pixels (int, optional): opening n min pixels to apply on the 573 image. Defaults to 0. 574 opening_connectivity (int, optional): opening connectivity to apply on the 575 image. Defaults to 8. 576 """ 577 # pylint: disable=too-many-arguments, too-many-positional-arguments 578 self.base.as_grayscale() 579 self.base.asarray = threshold_isauvola( 580 img=self.base.asarray, 581 window_size=window_size, 582 k=k, 583 r=r, 584 connectivity=connectivity, 585 contrast_window_size=contrast_window_size, 586 opening_n_min_pixels=opening_n_min_pixels, 587 opening_connectivity=opening_connectivity, 588 ) 589 return self.base.parent threshold_niblack(window_size=15, k=-0.2) Apply Niblack local thresholding. Book (1986): "An Introduction to Digital Image Processing" by Wayne Niblack. Parameters: Name Type Description Default window_size int apply on the image. Defaults to 15. 15 k float factor to apply to regulate the impact of -0.2 the std. Defaults to -0.2. Source code in otary/image/components/transformer/components/ binarizer/binarizer.py 198 def threshold_niblack(self, window_size: int = 15, k: float = -0.2) -> "Image": 199 """Apply Niblack local thresholding. 200 201 Book (1986): 202 "An Introduction to Digital Image Processing" by Wayne Niblack. 203 204 Args: 205 window_size (int, optional): apply on the 206 image. Defaults to 15. 207 k (float, optional): factor to apply to regulate the impact 208 of the std. Defaults to -0.2. 209 """ 210 self.base.as_grayscale() 211 self.base.asarray = threshold_niblack_like( 212 img=self.base.asarray, method="niblack", window_size=window_size, k=k 213 )[1] 214 return self.base.parent threshold_nick(window_size=19, k=-0.1) Apply Nick local thresholding. Paper (2009): Comparison of Niblack inspired Binarization Methods for Ancient Documents The paper suggests to use a window size of 19 and a k factor in [-0.2, -0.1]. Parameters: Name Type Description Default window_size int apply on the image. Defaults to 15. 19 k float factor to apply to regulate the impact of -0.1 the std. Defaults to -0.1. Source code in otary/image/components/transformer/components/ binarizer/binarizer.py 351 def threshold_nick(self, window_size: int = 19, k: float = -0.1) -> "Image": 352 """Apply Nick local thresholding. 353 354 Paper (2009): 355 [Comparison of Niblack inspired Binarization Methods for Ancient Documents](https://www.researchgate.net/publication/221253803) 356 357 The paper suggests to use a window size of 19 and a k factor in [-0.2, -0.1]. 358 359 Args: 360 window_size (int, optional): apply on the 361 image. Defaults to 15. 362 k (float, optional): factor to apply to regulate the impact 363 of the std. Defaults to -0.1. 364 """ 365 self.base.as_grayscale() 366 self.base.asarray = threshold_niblack_like( 367 img=self.base.asarray, method="nick", window_size=window_size, k=k 368 )[1] 369 return self.base.parent threshold_otsu() Apply Otsu global thresholding. This is a global thresholding method that automatically determines an optimal threshold value from the image histogram. Paper (1979): A Threshold Selection Method from Gray-Level Histograms Consider applying a gaussian blur before for better thresholding results. See why in the OpenCV documentation. Source code in otary/image/components/transformer/components/ binarizer/binarizer.py 120 def threshold_otsu(self) -> "Image": 121 """Apply Otsu global thresholding. 122 This is a global thresholding method that automatically determines 123 an optimal threshold value from the image histogram. 124 125 Paper (1979): 126 [A Threshold Selection Method from Gray-Level Histograms](https://ieeexplore.ieee.org/document/4310076) 127 128 Consider applying a gaussian blur before for better thresholding results. 129 See why in the [OpenCV documentation](https://docs.opencv.org/4.x/d7/d4d/tutorial_py_thresholding.html). 130 """ 131 self.base.as_grayscale() 132 _, img_thresholded = cv2.threshold( 133 self.base.asarray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU 134 ) 135 self.base.asarray = img_thresholded 136 return self.base.parent threshold_phansalkar(window_size=40, k=0.25, p=3.0, q=10.0) Apply Phansalkar et al. local thresholding. Paper (2011): Adaptive Local Thresholding for Detection of Nuclei in Diversely Stained Cytology Images Parameters: Name Type Description Default window_size int apply on the image. Defaults to 40. 40 factor to apply to regulate the impact of k float the std. Defaults to 0.25. 0.25 Phansalkar parameter to regulate low p float contrast zones. Defaults to 3.0. 3.0 q float Phansalkar parameter to regulate low 10.0 contrast zones. Defaults to 10.0. Source code in otary/image/components/transformer/components/ binarizer/binarizer.py 393 def threshold_phansalkar( 394 self, window_size: int = 40, k: float = 0.25, p: float = 3.0, q: float = 10.0 395 ) -> "Image": 396 """Apply Phansalkar et al. local thresholding. 397 398 Paper (2011): 399 [Adaptive Local Thresholding for Detection of Nuclei in Diversely Stained Cytology Images](https://www.researchgate.net/publication/224226466) 400 401 Args: 402 window_size (int, optional): apply on the 403 image. Defaults to 40. 404 k (float, optional): factor to apply to regulate the impact 405 of the std. Defaults to 0.25. 406 p (float, optional): Phansalkar parameter to regulate low contrast zones. 407 Defaults to 3.0. 408 q (float, optional): Phansalkar parameter to regulate low contrast zones. 409 Defaults to 10.0. 410 """ 411 self.base.as_grayscale() 412 self.base.asarray = threshold_niblack_like( 413 img=self.base.asarray, 414 method="phansalkar", 415 window_size=window_size, 416 k=k, 417 p=p, 418 q=q, 419 )[1] 420 return self.base.parent threshold_sauvola(window_size=15, k=0.5, r=128.0) Apply Sauvola local thresholding. This is a local thresholding method that computes the threshold for a pixel based on a small region around it. Paper (1997): Adaptive Document Binarization Parameters: Name Type Description Default sauvola window size to apply on the image. window_size int Defaults to 15. 15 sauvola k factor to apply to regulate the k float impact of the std. Defaults to 0.5. 0.5 r float sauvola r value. Defaults to 128. 128.0 Source code in otary/image/components/transformer/components/ binarizer/binarizer.py 216 def threshold_sauvola( 217 self, window_size: int = 15, k: float = 0.5, r: float = 128.0 218 ) -> "Image": 219 """Apply Sauvola local thresholding. 220 This is a local thresholding method that computes the threshold for a pixel 221 based on a small region around it. 222 223 Paper (1997): 224 [Adaptive Document Binarization](https://www.researchgate.net/publication/3710586_Adaptive_Document_Binarization) 225 226 Args: 227 window_size (int, optional): sauvola window size to apply on the 228 image. Defaults to 15. 229 k (float, optional): sauvola k factor to apply to regulate the impact 230 of the std. Defaults to 0.5. 231 r (float, optional): sauvola r value. Defaults to 128. 232 """ 233 self.base.as_grayscale() 234 self.base.asarray = threshold_niblack_like( 235 img=self.base.asarray, method="sauvola", window_size=window_size, k=k, r=r 236 )[1] 237 return self.base.parent threshold_simple(thresh) Compute the image thresholded by a single value T. All pixels with value v <= T are turned black and those with value v > T are turned white. This is a global thresholding method. Parameters: Name Type Description Default thresh int value to separate the black from the white required pixels. Source code in otary/image/components/transformer/components/ binarizer/binarizer.py 108 def threshold_simple(self, thresh: int) -> "Image": 109 """Compute the image thresholded by a single value T. 110 All pixels with value v <= T are turned black and those with value v > T are 111 turned white. This is a global thresholding method. 112 113 Args: 114 thresh (int): value to separate the black from the white pixels. 115 """ 116 self.base.as_grayscale() 117 self.base.asarray = np.array((self.base.asarray > thresh) * 255, dtype=np.uint8) 118 return self.base.parent threshold_singh(window_size=15, k=0.06) Apply Singh local thresholding. Paper (2012): A New Local Adaptive Thresholding Technique in Binarization Parameters: Name Type Description Default window_size int apply on the image. Defaults to 15. 15 k float factor to apply to regulate the impact of 0.06 the std. Defaults to 0.06. Source code in otary/image/components/transformer/components/ binarizer/binarizer.py 443 def threshold_singh(self, window_size: int = 15, k: float = 0.06) -> "Image": 444 """Apply Singh local thresholding. 445 446 Paper (2012): 447 [A New Local Adaptive Thresholding Technique in Binarization](https://www.researchgate.net/publication/220485031) 448 449 Args: 450 window_size (int, optional): apply on the 451 image. Defaults to 15. 452 k (float, optional): factor to apply to regulate the impact 453 of the std. Defaults to 0.06. 454 """ 455 self.base.as_grayscale() 456 self.base.asarray = threshold_niblack_like( 457 img=self.base.asarray, method="singh", window_size=window_size, k=k 458 )[1] 459 return self.base.parent threshold_su(window_size=3, n_min=-1) Compute the Su local thresholding. Paper (2010): Binarization of historical document images using the local maximum and minimum Parameters: Name Type Description Default window size for high contrast image window_size int computation. Defaults to 3. 3 minimum number of high contrast pixels n_min int within the neighborhood window. Defaults to -1 -1 meaning that n_min = window_size. Source code in otary/image/components/transformer/components/ binarizer/binarizer.py 371 def threshold_su( 372 self, 373 window_size: int = 3, 374 n_min: int = -1, 375 ) -> "Image": 376 """Compute the Su local thresholding. 377 378 Paper (2010): 379 [Binarization of historical document images using the local maximum and minimum](https://www.researchgate.net/publication/220933012) 380 381 Args: 382 window_size (int, optional): window size for high contrast image 383 computation. Defaults to 3. 384 n_min (int, optional): minimum number of high contrast pixels within the 385 neighborhood window. Defaults to -1 meaning that n_min = window_size. 386 """ 387 self.base.as_grayscale() 388 self.base.asarray = threshold_su( 389 img=self.base.asarray, window_size=window_size, n_min=n_min 390 ) 391 return self.base.parent threshold_wan(window_size=15, k=0.5, r=128.0) Apply Wan local thresholding. Paper (2018): Binarization of Document Image Using Optimum Threshold Modification Parameters: Name Type Description Default window_size int apply on the image. Defaults to 15. 15 k float factor to apply to regulate the impact of 0.5 the std. Defaults to 0.5. Source code in otary/image/components/transformer/components/ binarizer/binarizer.py 591 def threshold_wan( 592 self, window_size: int = 15, k: float = 0.5, r: float = 128.0 593 ) -> "Image": 594 """Apply Wan local thresholding. 595 596 Paper (2018): 597 [Binarization of Document Image Using Optimum Threshold Modification](https://www.researchgate.net/publication/326026836) 598 599 Args: 600 window_size (int, optional): apply on the 601 image. Defaults to 15. 602 k (float, optional): factor to apply to regulate the impact 603 of the std. Defaults to 0.5. 604 """ 605 self.base.as_grayscale() 606 self.base.asarray = threshold_niblack_like( 607 img=self.base.asarray, method="wan", window_size=window_size, k=k, r=r 608 )[1] 609 return self.base.parent threshold_wolf(window_size=15, k=0.5) Apply Wolf local thresholding. Paper (2003): Extraction and Recognition of Artificial Text in Multimedia Documents Parameters: Name Type Description Default window_size int apply on the image. Defaults to 15. 15 k float factor to apply to regulate the impact of 0.5 the std. Defaults to 0.5. Source code in otary/image/components/transformer/components/ binarizer/binarizer.py 239 def threshold_wolf(self, window_size: int = 15, k: float = 0.5) -> "Image": 240 """Apply Wolf local thresholding. 241 242 Paper (2003): 243 [Extraction and Recognition of Artificial Text in Multimedia Documents](https://hal.science/hal-01504401v1) 244 245 Args: 246 window_size (int, optional): apply on the 247 image. Defaults to 15. 248 k (float, optional): factor to apply to regulate the impact 249 of the std. Defaults to 0.5. 250 """ 251 self.base.as_grayscale() 252 self.base.asarray = threshold_niblack_like( 253 img=self.base.asarray, method="wolf", window_size=window_size, k=k 254 )[1] 255 return self.base.parent Back to top Previous Morphology Next Drawer Made with Material for MkDocs