Skip to content

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 & Roth 2007 Adaptive Thresholding using the Integral Image
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
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
class BinarizerImage:
    # pylint: disable=line-too-long
    """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](https://docs.opencv.org/4.x/d7/d4d/tutorial_py_thresholding.html)                           |
    | Otsu           | 1979 | [A Threshold Selection Method from Gray-Level Histograms](https://ieeexplore.ieee.org/document/4310076)                                  |
    | 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](https://www.researchgate.net/publication/3710586_Adaptive_Document_Binarization)                        |
    | Wolf           | 2003 | [Extraction and Recognition of Artificial Text in Multimedia Documents](https://hal.science/hal-01504401v1)                              |
    | Feng           | 2004 | [Contrast adaptive binarization of low quality document images](https://www.jstage.jst.go.jp/article/elex/1/16/1_16_501/_pdf)            |
    | Gatos          | 2005 | [Adaptive degraded document image binarization](https://users.iit.demokritos.gr/~bgat/PatRec2006.pdf)                                    |
    | Bradley & Roth | 2007 | [Adaptive Thresholding using the Integral Image](https://www.researchgate.net/publication/220494200_Adaptive_Thresholding_using_the_Integral_Image) |
    | Nick           | 2009 | [Comparison of Niblack inspired Binarization Methods for Ancient Documents](https://www.researchgate.net/publication/221253803)           |
    | Su             | 2010 | [Binarization of historical document images using the local maximum and minimum](https://www.researchgate.net/publication/220933012)                                                             |
    | Phansalkar     | 2011 | [Adaptive Local Thresholding for Detection of Nuclei in Diversely Stained Cytology Images](https://www.researchgate.net/publication/224226466) |
    | Adotsu         | 2011 | [AdOtsu: An adaptive and parameterless generalization of Otsu’s method for document image binarization](https://www.researchgate.net/publication/220602345) |
    | Singh          | 2012 | [A New Local Adaptive Thresholding Technique in Binarization](https://www.researchgate.net/publication/220485031)                        |
    | FAIR           | 2013 | [FAIR: A Fast Algorithm for document Image Restoration](https://amu.hal.science/hal-01479805/document)                                   |
    | ISauvola       | 2016 | [ISauvola: Improved Sauvola’s Algorithm for Document Image Binarization](https://www.researchgate.net/publication/304621554_ISauvola_Improved_Sauvola) |
    | WAN            | 2018 | [Binarization of Document Image Using Optimum Threshold Modification](https://www.researchgate.net/publication/326026836)                 |
    """

    def __init__(self, base: BaseImage) -> None:
        self.base = base

    # ----------------------------- GLOBAL THRESHOLDING -------------------------------

    def threshold_simple(self, thresh: int) -> "Image":
        """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.

        Args:
            thresh (int): value to separate the black from the white pixels.
        """
        self.base.as_grayscale()
        self.base.asarray = np.array((self.base.asarray > thresh) * 255, dtype=np.uint8)
        return self.base.parent

    def threshold_otsu(self) -> "Image":
        """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](https://ieeexplore.ieee.org/document/4310076)

        Consider applying a gaussian blur before for better thresholding results.
        See why in the [OpenCV documentation](https://docs.opencv.org/4.x/d7/d4d/tutorial_py_thresholding.html).
        """
        self.base.as_grayscale()
        _, img_thresholded = cv2.threshold(
            self.base.asarray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU
        )
        self.base.asarray = img_thresholded
        return self.base.parent

    # ------------------------------ LOCAL THRESHOLDING -------------------------------

    def threshold_adaptive(
        self, block_size: int = 11, constant: float = 2.0
    ) -> "Image":
        """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](https://docs.opencv.org/4.x/d7/d4d/tutorial_py_thresholding.html).

        Args:
            block_size (int, optional): Size of a pixel neighborhood that is used to
                calculate a threshold value for the pixel: 3, 5, 7, and so on.
                Defaults to 11.
            constant (int, optional): Constant subtracted from the mean or weighted
                mean. Normally, it is positive but may be zero or negative as well.
                Defaults to 2.
        """
        self.base.as_grayscale()
        self.base.asarray = cv2.adaptiveThreshold(
            src=self.base.asarray,
            maxValue=255,
            adaptiveMethod=cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
            thresholdType=cv2.THRESH_BINARY,
            blockSize=block_size,
            C=constant,
        )
        return self.base.parent

    def threshold_bernsen(
        self,
        window_size: int = 75,
        contrast_limit: float = 25,
        threshold_global: int = 100,
    ) -> "Image":
        """Apply Bernsen local thresholding.

        Paper (1986):
        "Dynamic thresholding of grey-level images" by Bernsen.

        Args:
            window_size (int, optional): window size for local computations.
                Defaults to 75.
            contrast_limit (float, optional): contrast limit. If the
                contrast is higher than this value, the pixel is thresholded by the
                bernsen threshold otherwise the global threshold is used.
                Defaults to 25.
            threshold_global (int, optional): global threshold. Defaults to 100.
        """
        self.base.as_grayscale()
        self.base.asarray = threshold_bernsen(
            img=self.base.asarray,
            window_size=window_size,
            contrast_limit=contrast_limit,
            threshold_global=threshold_global,
        )
        return self.base.parent

    def threshold_niblack(self, window_size: int = 15, k: float = -0.2) -> "Image":
        """Apply Niblack local thresholding.

        Book (1986):
        "An Introduction to Digital Image Processing" by Wayne Niblack.

        Args:
            window_size (int, optional): apply on the
                image. Defaults to 15.
            k (float, optional): factor to apply to regulate the impact
                of the std. Defaults to -0.2.
        """
        self.base.as_grayscale()
        self.base.asarray = threshold_niblack_like(
            img=self.base.asarray, method="niblack", window_size=window_size, k=k
        )[1]
        return self.base.parent

    def threshold_sauvola(
        self, window_size: int = 15, k: float = 0.5, r: float = 128.0
    ) -> "Image":
        """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](https://www.researchgate.net/publication/3710586_Adaptive_Document_Binarization)

        Args:
            window_size (int, optional): sauvola window size to apply on the
                image. Defaults to 15.
            k (float, optional): sauvola k factor to apply to regulate the impact
                of the std. Defaults to 0.5.
            r (float, optional): sauvola r value. Defaults to 128.
        """
        self.base.as_grayscale()
        self.base.asarray = threshold_niblack_like(
            img=self.base.asarray, method="sauvola", window_size=window_size, k=k, r=r
        )[1]
        return self.base.parent

    def threshold_wolf(self, window_size: int = 15, k: float = 0.5) -> "Image":
        """Apply Wolf local thresholding.

        Paper (2003):
        [Extraction and Recognition of Artificial Text in Multimedia Documents](https://hal.science/hal-01504401v1)

        Args:
            window_size (int, optional): apply on the
                image. Defaults to 15.
            k (float, optional): factor to apply to regulate the impact
                of the std. Defaults to 0.5.
        """
        self.base.as_grayscale()
        self.base.asarray = threshold_niblack_like(
            img=self.base.asarray, method="wolf", window_size=window_size, k=k
        )[1]
        return self.base.parent

    def threshold_feng(
        self,
        w1: int = 19,
        w2: int = 33,
        alpha1: float = 0.12,
        k1: float = 0.25,
        k2: float = 0.04,
        gamma: float = 2.0,
    ) -> "Image":
        """Implementation of the Feng thresholding method.

        Paper (2004):
        [Contrast adaptive binarization of low quality document images](https://www.jstage.jst.go.jp/article/elex/1/16/1_16_501/_pdf)

        Args:
            w1 (int, optional): primary window size. Defaults to 19.
            w2 (int, optional): secondary window value. Defaults to 33.
            alpha1 (float, optional): alpha1 value. Defaults to 0.12.
            k1 (float, optional): k1 value. Defaults to 0.25.
            k2 (float, optional): k2 value. Defaults to 0.04.
            gamma (float, optional): gamma value. Defaults to 2.0.
        """
        # pylint: disable=too-many-arguments, too-many-positional-arguments
        self.base.as_grayscale()
        self.base.asarray = threshold_feng(
            img=self.base.asarray,
            w1=w1,
            w2=w2,
            alpha1=alpha1,
            k1=k1,
            k2=k2,
            gamma=gamma,
        )
        return self.base.parent

    def threshold_gatos(
        self,
        q: float = 0.6,
        p1: float = 0.5,
        p2: float = 0.8,
        lh: Optional[float] = None,
        upsampling: bool = False,
        upsampling_factor: int = 2,
    ) -> "Image":
        """Apply Gatos local thresholding.

        Paper (2005):
        [Adaptive degraded document image binarization](https://users.iit.demokritos.gr/~bgat/PatRec2006.pdf)

        Args:
            q (float, optional): q gatos factor. Defaults to 0.6.
            p1 (float, optional): p1 gatos factor. Defaults to 0.5.
            p2 (float, optional): p2 gatos factor. Defaults to 0.8.
            lh (Optional[float], optional): height of character.
                Defaults to None, meaning it is computed automatically to be
                a fraction of the image size.
            upsampling (bool, optional): whether to apply gatos upsampling definition.
                Defaults to False.
            upsampling_factor (int, optional): gatos upsampling factor. Defaults to 2.
        """
        # pylint: disable=too-many-arguments, too-many-positional-arguments
        # pylint: disable=duplicate-code
        self.base.as_grayscale()
        self.base.asarray = threshold_gatos(
            img=self.base.asarray,
            q=q,
            p1=p1,
            p2=p2,
            lh=lh,
            upsampling=upsampling,
            upsampling_factor=upsampling_factor,
        )
        return self.base.parent

    def threshold_bradley(self, window_size: int = 15, t: float = 0.15) -> "Image":
        """Implementation of the Bradley & Roth thresholding method.

        Paper (2007):
        [Adaptive Thresholding using the Integral Image](https://www.researchgate.net/publication/220494200_Adaptive_Thresholding_using_the_Integral_Image)

        Args:
            window_size (int, optional): window size for local computations.
                Defaults to 15.
            t (float, optional): t value in [0, 1]. Defaults to 0.15.

        Returns:
            NDArray[np.uint8]: output thresholded image
        """
        self.base.as_grayscale()
        self.base.asarray = threshold_bradley(
            img=self.base.asarray, window_size=window_size, t=t
        )
        return self.base.parent

    def threshold_nick(self, window_size: int = 19, k: float = -0.1) -> "Image":
        """Apply Nick local thresholding.

        Paper (2009):
        [Comparison of Niblack inspired Binarization Methods for Ancient Documents](https://www.researchgate.net/publication/221253803)

        The paper suggests to use a window size of 19 and a k factor in [-0.2, -0.1].

        Args:
            window_size (int, optional): apply on the
                image. Defaults to 15.
            k (float, optional): factor to apply to regulate the impact
                of the std. Defaults to -0.1.
        """
        self.base.as_grayscale()
        self.base.asarray = threshold_niblack_like(
            img=self.base.asarray, method="nick", window_size=window_size, k=k
        )[1]
        return self.base.parent

    def threshold_su(
        self,
        window_size: int = 3,
        n_min: int = -1,
    ) -> "Image":
        """Compute the Su local thresholding.

        Paper (2010):
        [Binarization of historical document images using the local maximum and minimum](https://www.researchgate.net/publication/220933012)

        Args:
            window_size (int, optional): window size for high contrast image
                computation. Defaults to 3.
            n_min (int, optional): minimum number of high contrast pixels within the
                neighborhood window. Defaults to -1 meaning that n_min = window_size.
        """
        self.base.as_grayscale()
        self.base.asarray = threshold_su(
            img=self.base.asarray, window_size=window_size, n_min=n_min
        )
        return self.base.parent

    def threshold_phansalkar(
        self, window_size: int = 40, k: float = 0.25, p: float = 3.0, q: float = 10.0
    ) -> "Image":
        """Apply Phansalkar et al. local thresholding.

        Paper (2011):
        [Adaptive Local Thresholding for Detection of Nuclei in Diversely Stained Cytology Images](https://www.researchgate.net/publication/224226466)

        Args:
            window_size (int, optional): apply on the
                image. Defaults to 40.
            k (float, optional): factor to apply to regulate the impact
                of the std. Defaults to 0.25.
            p (float, optional): Phansalkar parameter to regulate low contrast zones.
                Defaults to 3.0.
            q (float, optional): Phansalkar parameter to regulate low contrast zones.
                Defaults to 10.0.
        """
        self.base.as_grayscale()
        self.base.asarray = threshold_niblack_like(
            img=self.base.asarray,
            method="phansalkar",
            window_size=window_size,
            k=k,
            p=p,
            q=q,
        )[1]
        return self.base.parent

    def threshold_adotsu(
        self, grid_size: int = 50, k_sigma: float = 1.6, n_steps: int = 2
    ) -> "Image":
        """Apply Adotsu local thresholding.

        Paper (2011):
        [AdOtsu: An adaptive and parameterless generalization of Otsu’s method for document image binarization](https://www.researchgate.net/publication/220602345)

        Args:
            grid_size (int, optional): window size for local computations.
                Defaults to 15.
            k_sigma (float, optional): k_sigma value in [1, 2]. Defaults to 1.6.
            n_steps (int, optional): number of iterations to update the binarization by
                estimating a new background surface. Defaults to 2.
        """
        self.base.as_grayscale()
        self.base.asarray = threshold_adotsu(
            img=self.base.asarray, grid_size=grid_size, k_sigma=k_sigma, n_steps=n_steps
        )
        return self.base.parent

    def threshold_singh(self, window_size: int = 15, k: float = 0.06) -> "Image":
        """Apply Singh local thresholding.

        Paper (2012):
        [A New Local Adaptive Thresholding Technique in Binarization](https://www.researchgate.net/publication/220485031)

        Args:
            window_size (int, optional): apply on the
                image. Defaults to 15.
            k (float, optional): factor to apply to regulate the impact
                of the std. Defaults to 0.06.
        """
        self.base.as_grayscale()
        self.base.asarray = threshold_niblack_like(
            img=self.base.asarray, method="singh", window_size=window_size, k=k
        )[1]
        return self.base.parent

    def threshold_fair(
        self,
        sfair_window_size: int = 33,
        sfair_clustering_algo: str = "otsu",
        sfair_clustering_max_iter: int = 20,
        sfair_thining: float = 1.0,
        sfair_alpha: float = 0.38,
        post_stain_max_pixels: int = 25,
        post_misclass_txt: bool = True,
        post_clustering_algo: str = "otsu",
        post_clustering_max_iter: int = 10,
        post_max_iter: int = 15,
        post_window_size: int = 75,
        post_beta: float = 1.0,
    ) -> "Image":
        """Apply FAIR local thresholding.

        Paper (2013):
        [FAIR: A Fast Algorithm for document Image Restoration](https://amu.hal.science/hal-01479805/document)

        Args:
            sfair_window_size (int, optional): window size in preprocess
                to cluster background and foreground pixels around edge pixels.
                This parameter is important as a higher value will make the method
                more robust to noise but also more computationally expensive and slow.
                Defaults to 5.
            sfair_clustering_algo (str, optional): clustering algorithm for the S-FAIR
                step. Defaults to "otsu".
            sfair_clustering_max_iter (int, optional): maximum number of iterations for
                the clustering algorithm within the S-FAIR step. Defaults to 20.
            sfair_thining (float, optional): thining factor in [0, 1]. 0 means no
                thinning which means that all edge pixels are processed.
                1 means that only every
                sfair_window_size // 2 edge pixels are processed which signicantly
                speeds up the computation. Defaults to 1.0.
            sfair_alpha (float, optional): It defines the ratio to compute the lower
                threshold in the 1st step of the S-FAIR step.
                It is generally in [0.3, 0.5].
                Defaults to 0.38.
            post_stain_max_pixels (int, optional): maximum number of pixels for a stain
                to be considered as an unknown connected component. Defaults to 25.
            post_misclass_txt (bool, optional): whether to perform the
                post-processing correct_misclassified_text_pixels step.
                Defaults to True.
            post_clustering_algo (str, optional): clustering algorithm for the
                post-processing step. Defaults to "otsu".
            post_clustering_max_iter (int, optional): maximum number of iterations for
                the clustering algorithm within the post-processing step.
                Defaults to 10.
            post_max_iter (int, optional): maximum number of iterations for the
                correct_misclassified_text_pixels step within the post-processing step.
                Defaults to 15.
            post_window_size (int, optional): window size in postprocess
                to cluster background and foreground pixels around edge pixels.
                This parameter is important as a higher value will make the method
                more robust to noise but also more computationally expensive and slow.
                Defaults to 75.
            post_beta (float, optional): 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 pixels
                (N_t) is higher than the 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.
        """
        # pylint: disable=too-many-arguments, too-many-positional-arguments
        # pylint: disable=duplicate-code
        self.base.as_grayscale()
        self.base.asarray = threshold_fair(
            img=self.base.asarray,
            sfair_window_size=sfair_window_size,
            sfair_clustering_algo=sfair_clustering_algo,
            sfair_clustering_max_iter=sfair_clustering_max_iter,
            sfair_thining=sfair_thining,
            sfair_alpha=sfair_alpha,
            post_stain_max_pixels=post_stain_max_pixels,
            post_misclass_txt=post_misclass_txt,
            post_clustering_algo=post_clustering_algo,
            post_max_iter=post_max_iter,
            post_clustering_max_iter=post_clustering_max_iter,
            post_window_size=post_window_size,
            post_beta=post_beta,
        )
        return self.base.parent

    def threshold_isauvola(
        self,
        window_size: int = 15,
        k: float = 0.01,
        r: float = 128.0,
        connectivity: int = 8,
        contrast_window_size: int = 3,
        opening_n_min_pixels: int = 0,
        opening_connectivity: int = 8,
    ) -> "Image":
        """Apply ISauvola local thresholding.

        Paper (2016):
        [ISauvola: Improved Sauvola’s Algorithm for Document Image Binarization](https://www.researchgate.net/publication/304621554_ISauvola_Improved_Sauvola)

        Args:
            window_size (int, optional): apply on the
                image. Defaults to 15.
            k (float, optional): factor to apply to regulate the impact
                of the std. Defaults to 0.01.
            r (float, optional): factor to apply to regulate the impact
                of the std. Defaults to 128.
            connectivity (int, optional): connectivity to apply on the
                image. Defaults to 8.
            contrast_window_size (int, optional): contrast window size to apply on the
                image. Defaults to 3.
            opening_n_min_pixels (int, optional): opening n min pixels to apply on the
                image. Defaults to 0.
            opening_connectivity (int, optional): opening connectivity to apply on the
                image. Defaults to 8.
        """
        # pylint: disable=too-many-arguments, too-many-positional-arguments
        self.base.as_grayscale()
        self.base.asarray = threshold_isauvola(
            img=self.base.asarray,
            window_size=window_size,
            k=k,
            r=r,
            connectivity=connectivity,
            contrast_window_size=contrast_window_size,
            opening_n_min_pixels=opening_n_min_pixels,
            opening_connectivity=opening_connectivity,
        )
        return self.base.parent

    def threshold_wan(
        self, window_size: int = 15, k: float = 0.5, r: float = 128.0
    ) -> "Image":
        """Apply Wan local thresholding.

        Paper (2018):
        [Binarization of Document Image Using Optimum Threshold Modification](https://www.researchgate.net/publication/326026836)

        Args:
            window_size (int, optional): apply on the
                image. Defaults to 15.
            k (float, optional): factor to apply to regulate the impact
                of the std. Defaults to 0.5.
        """
        self.base.as_grayscale()
        self.base.asarray = threshold_niblack_like(
            img=self.base.asarray, method="wan", window_size=window_size, k=k, r=r
        )[1]
        return self.base.parent

    # ---------------------------- BINARY REPRESENTATION ------------------------------

    def binary(self, method: BinarizationMethods = "sauvola") -> NDArray:
        """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.

        Args:
            method (str, optional): the binarization method to apply.
                Look at the BinarizationMethods to see all the available methods.
                Defaults to "sauvola".

        Returns:
            NDArray: array where its inner values are 0 or 1
        """
        if method not in list(get_args(BinarizationMethods)):
            raise ValueError(
                f"Invalid binarization method {method}. "
                f"Must be in {BinarizationMethods}"
            )
        getattr(self, f"threshold_{method}")()
        return self.base.asarray_binary

    def binaryrev(self, method: BinarizationMethods = "sauvola") -> NDArray:
        """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".

        Args:
            method (str, optional): the binarization method to apply.
                Defaults to "adaptative".

        Returns:
            NDArray: array where its inner values are 0 or 1
        """
        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
method str

the binarization method to apply. Look at the BinarizationMethods to see all the available methods. Defaults to "sauvola".

'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
def binary(self, method: BinarizationMethods = "sauvola") -> NDArray:
    """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.

    Args:
        method (str, optional): the binarization method to apply.
            Look at the BinarizationMethods to see all the available methods.
            Defaults to "sauvola".

    Returns:
        NDArray: array where its inner values are 0 or 1
    """
    if method not in list(get_args(BinarizationMethods)):
        raise ValueError(
            f"Invalid binarization method {method}. "
            f"Must be in {BinarizationMethods}"
        )
    getattr(self, f"threshold_{method}")()
    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 "adaptative".

'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
def binaryrev(self, method: BinarizationMethods = "sauvola") -> NDArray:
    """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".

    Args:
        method (str, optional): the binarization method to apply.
            Defaults to "adaptative".

    Returns:
        NDArray: array where its inner values are 0 or 1
    """
    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
block_size int

Size of a pixel neighborhood that is used to calculate a threshold value for the pixel: 3, 5, 7, and so on. Defaults to 11.

11
constant int

Constant subtracted from the mean or weighted mean. Normally, it is positive but may be zero or negative as well. Defaults to 2.

2.0
Source code in otary/image/components/transformer/components/binarizer/binarizer.py
def threshold_adaptive(
    self, block_size: int = 11, constant: float = 2.0
) -> "Image":
    """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](https://docs.opencv.org/4.x/d7/d4d/tutorial_py_thresholding.html).

    Args:
        block_size (int, optional): Size of a pixel neighborhood that is used to
            calculate a threshold value for the pixel: 3, 5, 7, and so on.
            Defaults to 11.
        constant (int, optional): Constant subtracted from the mean or weighted
            mean. Normally, it is positive but may be zero or negative as well.
            Defaults to 2.
    """
    self.base.as_grayscale()
    self.base.asarray = cv2.adaptiveThreshold(
        src=self.base.asarray,
        maxValue=255,
        adaptiveMethod=cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
        thresholdType=cv2.THRESH_BINARY,
        blockSize=block_size,
        C=constant,
    )
    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
grid_size int

window size for local computations. Defaults to 15.

50
k_sigma float

k_sigma value in [1, 2]. Defaults to 1.6.

1.6
n_steps int

number of iterations to update the binarization by estimating a new background surface. Defaults to 2.

2
Source code in otary/image/components/transformer/components/binarizer/binarizer.py
def threshold_adotsu(
    self, grid_size: int = 50, k_sigma: float = 1.6, n_steps: int = 2
) -> "Image":
    """Apply Adotsu local thresholding.

    Paper (2011):
    [AdOtsu: An adaptive and parameterless generalization of Otsu’s method for document image binarization](https://www.researchgate.net/publication/220602345)

    Args:
        grid_size (int, optional): window size for local computations.
            Defaults to 15.
        k_sigma (float, optional): k_sigma value in [1, 2]. Defaults to 1.6.
        n_steps (int, optional): number of iterations to update the binarization by
            estimating a new background surface. Defaults to 2.
    """
    self.base.as_grayscale()
    self.base.asarray = threshold_adotsu(
        img=self.base.asarray, grid_size=grid_size, k_sigma=k_sigma, n_steps=n_steps
    )
    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 int

window size for local computations. Defaults to 75.

75
contrast_limit float

contrast limit. If the contrast is higher than this value, the pixel is thresholded by the bernsen threshold otherwise the global threshold is used. Defaults to 25.

25
threshold_global int

global threshold. Defaults to 100.

100
Source code in otary/image/components/transformer/components/binarizer/binarizer.py
def threshold_bernsen(
    self,
    window_size: int = 75,
    contrast_limit: float = 25,
    threshold_global: int = 100,
) -> "Image":
    """Apply Bernsen local thresholding.

    Paper (1986):
    "Dynamic thresholding of grey-level images" by Bernsen.

    Args:
        window_size (int, optional): window size for local computations.
            Defaults to 75.
        contrast_limit (float, optional): contrast limit. If the
            contrast is higher than this value, the pixel is thresholded by the
            bernsen threshold otherwise the global threshold is used.
            Defaults to 25.
        threshold_global (int, optional): global threshold. Defaults to 100.
    """
    self.base.as_grayscale()
    self.base.asarray = threshold_bernsen(
        img=self.base.asarray,
        window_size=window_size,
        contrast_limit=contrast_limit,
        threshold_global=threshold_global,
    )
    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 int

window size for local computations. 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
def threshold_bradley(self, window_size: int = 15, t: float = 0.15) -> "Image":
    """Implementation of the Bradley & Roth thresholding method.

    Paper (2007):
    [Adaptive Thresholding using the Integral Image](https://www.researchgate.net/publication/220494200_Adaptive_Thresholding_using_the_Integral_Image)

    Args:
        window_size (int, optional): window size for local computations.
            Defaults to 15.
        t (float, optional): t value in [0, 1]. Defaults to 0.15.

    Returns:
        NDArray[np.uint8]: output thresholded image
    """
    self.base.as_grayscale()
    self.base.asarray = threshold_bradley(
        img=self.base.asarray, window_size=window_size, t=t
    )
    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
sfair_window_size int

window size in preprocess to cluster background and foreground pixels around edge pixels. This parameter is important as a higher value will make the method more robust to noise but also more computationally expensive and slow. Defaults to 5.

33
sfair_clustering_algo str

clustering algorithm for the S-FAIR step. Defaults to "otsu".

'otsu'
sfair_clustering_max_iter int

maximum number of iterations for the clustering algorithm within the S-FAIR step. Defaults to 20.

20
sfair_thining float

thining factor in [0, 1]. 0 means no thinning which means that all edge pixels are processed. 1 means that only every sfair_window_size // 2 edge pixels are processed which signicantly speeds up the computation. Defaults to 1.0.

1.0
sfair_alpha float

It defines the ratio to compute the lower threshold in the 1st step of the S-FAIR step. It is generally in [0.3, 0.5]. Defaults to 0.38.

0.38
post_stain_max_pixels int

maximum number of pixels for a stain to be considered as an unknown connected component. Defaults to 25.

25
post_misclass_txt bool

whether to perform the post-processing correct_misclassified_text_pixels step. Defaults to True.

True
post_clustering_algo str

clustering algorithm for the post-processing step. Defaults to "otsu".

'otsu'
post_clustering_max_iter int

maximum number of iterations for the clustering algorithm within the post-processing step. Defaults to 10.

10
post_max_iter int

maximum number of iterations for the correct_misclassified_text_pixels step within the post-processing step. Defaults to 15.

15
post_window_size int

window size in postprocess to cluster background and foreground pixels around edge pixels. This parameter is important as a higher value will make the method more robust to noise but also more computationally expensive and slow. Defaults to 75.

75
post_beta float

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 pixels (N_t) is higher than the 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.

1.0
Source code in otary/image/components/transformer/components/binarizer/binarizer.py
def threshold_fair(
    self,
    sfair_window_size: int = 33,
    sfair_clustering_algo: str = "otsu",
    sfair_clustering_max_iter: int = 20,
    sfair_thining: float = 1.0,
    sfair_alpha: float = 0.38,
    post_stain_max_pixels: int = 25,
    post_misclass_txt: bool = True,
    post_clustering_algo: str = "otsu",
    post_clustering_max_iter: int = 10,
    post_max_iter: int = 15,
    post_window_size: int = 75,
    post_beta: float = 1.0,
) -> "Image":
    """Apply FAIR local thresholding.

    Paper (2013):
    [FAIR: A Fast Algorithm for document Image Restoration](https://amu.hal.science/hal-01479805/document)

    Args:
        sfair_window_size (int, optional): window size in preprocess
            to cluster background and foreground pixels around edge pixels.
            This parameter is important as a higher value will make the method
            more robust to noise but also more computationally expensive and slow.
            Defaults to 5.
        sfair_clustering_algo (str, optional): clustering algorithm for the S-FAIR
            step. Defaults to "otsu".
        sfair_clustering_max_iter (int, optional): maximum number of iterations for
            the clustering algorithm within the S-FAIR step. Defaults to 20.
        sfair_thining (float, optional): thining factor in [0, 1]. 0 means no
            thinning which means that all edge pixels are processed.
            1 means that only every
            sfair_window_size // 2 edge pixels are processed which signicantly
            speeds up the computation. Defaults to 1.0.
        sfair_alpha (float, optional): It defines the ratio to compute the lower
            threshold in the 1st step of the S-FAIR step.
            It is generally in [0.3, 0.5].
            Defaults to 0.38.
        post_stain_max_pixels (int, optional): maximum number of pixels for a stain
            to be considered as an unknown connected component. Defaults to 25.
        post_misclass_txt (bool, optional): whether to perform the
            post-processing correct_misclassified_text_pixels step.
            Defaults to True.
        post_clustering_algo (str, optional): clustering algorithm for the
            post-processing step. Defaults to "otsu".
        post_clustering_max_iter (int, optional): maximum number of iterations for
            the clustering algorithm within the post-processing step.
            Defaults to 10.
        post_max_iter (int, optional): maximum number of iterations for the
            correct_misclassified_text_pixels step within the post-processing step.
            Defaults to 15.
        post_window_size (int, optional): window size in postprocess
            to cluster background and foreground pixels around edge pixels.
            This parameter is important as a higher value will make the method
            more robust to noise but also more computationally expensive and slow.
            Defaults to 75.
        post_beta (float, optional): 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 pixels
            (N_t) is higher than the 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.
    """
    # pylint: disable=too-many-arguments, too-many-positional-arguments
    # pylint: disable=duplicate-code
    self.base.as_grayscale()
    self.base.asarray = threshold_fair(
        img=self.base.asarray,
        sfair_window_size=sfair_window_size,
        sfair_clustering_algo=sfair_clustering_algo,
        sfair_clustering_max_iter=sfair_clustering_max_iter,
        sfair_thining=sfair_thining,
        sfair_alpha=sfair_alpha,
        post_stain_max_pixels=post_stain_max_pixels,
        post_misclass_txt=post_misclass_txt,
        post_clustering_algo=post_clustering_algo,
        post_max_iter=post_max_iter,
        post_clustering_max_iter=post_clustering_max_iter,
        post_window_size=post_window_size,
        post_beta=post_beta,
    )
    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
def threshold_feng(
    self,
    w1: int = 19,
    w2: int = 33,
    alpha1: float = 0.12,
    k1: float = 0.25,
    k2: float = 0.04,
    gamma: float = 2.0,
) -> "Image":
    """Implementation of the Feng thresholding method.

    Paper (2004):
    [Contrast adaptive binarization of low quality document images](https://www.jstage.jst.go.jp/article/elex/1/16/1_16_501/_pdf)

    Args:
        w1 (int, optional): primary window size. Defaults to 19.
        w2 (int, optional): secondary window value. Defaults to 33.
        alpha1 (float, optional): alpha1 value. Defaults to 0.12.
        k1 (float, optional): k1 value. Defaults to 0.25.
        k2 (float, optional): k2 value. Defaults to 0.04.
        gamma (float, optional): gamma value. Defaults to 2.0.
    """
    # pylint: disable=too-many-arguments, too-many-positional-arguments
    self.base.as_grayscale()
    self.base.asarray = threshold_feng(
        img=self.base.asarray,
        w1=w1,
        w2=w2,
        alpha1=alpha1,
        k1=k1,
        k2=k2,
        gamma=gamma,
    )
    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
lh Optional[float]

height of character. Defaults to None, meaning it is computed automatically to be a fraction of the image size.

None
upsampling bool

whether to apply gatos upsampling definition. Defaults to False.

False
upsampling_factor int

gatos upsampling factor. Defaults to 2.

2
Source code in otary/image/components/transformer/components/binarizer/binarizer.py
def threshold_gatos(
    self,
    q: float = 0.6,
    p1: float = 0.5,
    p2: float = 0.8,
    lh: Optional[float] = None,
    upsampling: bool = False,
    upsampling_factor: int = 2,
) -> "Image":
    """Apply Gatos local thresholding.

    Paper (2005):
    [Adaptive degraded document image binarization](https://users.iit.demokritos.gr/~bgat/PatRec2006.pdf)

    Args:
        q (float, optional): q gatos factor. Defaults to 0.6.
        p1 (float, optional): p1 gatos factor. Defaults to 0.5.
        p2 (float, optional): p2 gatos factor. Defaults to 0.8.
        lh (Optional[float], optional): height of character.
            Defaults to None, meaning it is computed automatically to be
            a fraction of the image size.
        upsampling (bool, optional): whether to apply gatos upsampling definition.
            Defaults to False.
        upsampling_factor (int, optional): gatos upsampling factor. Defaults to 2.
    """
    # pylint: disable=too-many-arguments, too-many-positional-arguments
    # pylint: disable=duplicate-code
    self.base.as_grayscale()
    self.base.asarray = threshold_gatos(
        img=self.base.asarray,
        q=q,
        p1=p1,
        p2=p2,
        lh=lh,
        upsampling=upsampling,
        upsampling_factor=upsampling_factor,
    )
    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
window_size int

apply on the image. Defaults to 15.

15
k float

factor to apply to regulate the impact of the std. Defaults to 0.01.

0.01
r float

factor to apply to regulate the impact of the std. Defaults to 128.

128.0
connectivity int

connectivity to apply on the image. Defaults to 8.

8
contrast_window_size int

contrast window size to apply on the image. Defaults to 3.

3
opening_n_min_pixels int

opening n min pixels to apply on the image. Defaults to 0.

0
opening_connectivity int

opening connectivity to apply on the image. Defaults to 8.

8
Source code in otary/image/components/transformer/components/binarizer/binarizer.py
def threshold_isauvola(
    self,
    window_size: int = 15,
    k: float = 0.01,
    r: float = 128.0,
    connectivity: int = 8,
    contrast_window_size: int = 3,
    opening_n_min_pixels: int = 0,
    opening_connectivity: int = 8,
) -> "Image":
    """Apply ISauvola local thresholding.

    Paper (2016):
    [ISauvola: Improved Sauvola’s Algorithm for Document Image Binarization](https://www.researchgate.net/publication/304621554_ISauvola_Improved_Sauvola)

    Args:
        window_size (int, optional): apply on the
            image. Defaults to 15.
        k (float, optional): factor to apply to regulate the impact
            of the std. Defaults to 0.01.
        r (float, optional): factor to apply to regulate the impact
            of the std. Defaults to 128.
        connectivity (int, optional): connectivity to apply on the
            image. Defaults to 8.
        contrast_window_size (int, optional): contrast window size to apply on the
            image. Defaults to 3.
        opening_n_min_pixels (int, optional): opening n min pixels to apply on the
            image. Defaults to 0.
        opening_connectivity (int, optional): opening connectivity to apply on the
            image. Defaults to 8.
    """
    # pylint: disable=too-many-arguments, too-many-positional-arguments
    self.base.as_grayscale()
    self.base.asarray = threshold_isauvola(
        img=self.base.asarray,
        window_size=window_size,
        k=k,
        r=r,
        connectivity=connectivity,
        contrast_window_size=contrast_window_size,
        opening_n_min_pixels=opening_n_min_pixels,
        opening_connectivity=opening_connectivity,
    )
    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 the std. Defaults to -0.2.

-0.2
Source code in otary/image/components/transformer/components/binarizer/binarizer.py
def threshold_niblack(self, window_size: int = 15, k: float = -0.2) -> "Image":
    """Apply Niblack local thresholding.

    Book (1986):
    "An Introduction to Digital Image Processing" by Wayne Niblack.

    Args:
        window_size (int, optional): apply on the
            image. Defaults to 15.
        k (float, optional): factor to apply to regulate the impact
            of the std. Defaults to -0.2.
    """
    self.base.as_grayscale()
    self.base.asarray = threshold_niblack_like(
        img=self.base.asarray, method="niblack", window_size=window_size, k=k
    )[1]
    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 the std. Defaults to -0.1.

-0.1
Source code in otary/image/components/transformer/components/binarizer/binarizer.py
def threshold_nick(self, window_size: int = 19, k: float = -0.1) -> "Image":
    """Apply Nick local thresholding.

    Paper (2009):
    [Comparison of Niblack inspired Binarization Methods for Ancient Documents](https://www.researchgate.net/publication/221253803)

    The paper suggests to use a window size of 19 and a k factor in [-0.2, -0.1].

    Args:
        window_size (int, optional): apply on the
            image. Defaults to 15.
        k (float, optional): factor to apply to regulate the impact
            of the std. Defaults to -0.1.
    """
    self.base.as_grayscale()
    self.base.asarray = threshold_niblack_like(
        img=self.base.asarray, method="nick", window_size=window_size, k=k
    )[1]
    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
def threshold_otsu(self) -> "Image":
    """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](https://ieeexplore.ieee.org/document/4310076)

    Consider applying a gaussian blur before for better thresholding results.
    See why in the [OpenCV documentation](https://docs.opencv.org/4.x/d7/d4d/tutorial_py_thresholding.html).
    """
    self.base.as_grayscale()
    _, img_thresholded = cv2.threshold(
        self.base.asarray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU
    )
    self.base.asarray = img_thresholded
    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
k float

factor to apply to regulate the impact of the std. Defaults to 0.25.

0.25
p float

Phansalkar parameter to regulate low contrast zones. Defaults to 3.0.

3.0
q float

Phansalkar parameter to regulate low contrast zones. Defaults to 10.0.

10.0
Source code in otary/image/components/transformer/components/binarizer/binarizer.py
def threshold_phansalkar(
    self, window_size: int = 40, k: float = 0.25, p: float = 3.0, q: float = 10.0
) -> "Image":
    """Apply Phansalkar et al. local thresholding.

    Paper (2011):
    [Adaptive Local Thresholding for Detection of Nuclei in Diversely Stained Cytology Images](https://www.researchgate.net/publication/224226466)

    Args:
        window_size (int, optional): apply on the
            image. Defaults to 40.
        k (float, optional): factor to apply to regulate the impact
            of the std. Defaults to 0.25.
        p (float, optional): Phansalkar parameter to regulate low contrast zones.
            Defaults to 3.0.
        q (float, optional): Phansalkar parameter to regulate low contrast zones.
            Defaults to 10.0.
    """
    self.base.as_grayscale()
    self.base.asarray = threshold_niblack_like(
        img=self.base.asarray,
        method="phansalkar",
        window_size=window_size,
        k=k,
        p=p,
        q=q,
    )[1]
    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
window_size int

sauvola window size to apply on the image. Defaults to 15.

15
k float

sauvola k factor to apply to regulate the 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
def threshold_sauvola(
    self, window_size: int = 15, k: float = 0.5, r: float = 128.0
) -> "Image":
    """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](https://www.researchgate.net/publication/3710586_Adaptive_Document_Binarization)

    Args:
        window_size (int, optional): sauvola window size to apply on the
            image. Defaults to 15.
        k (float, optional): sauvola k factor to apply to regulate the impact
            of the std. Defaults to 0.5.
        r (float, optional): sauvola r value. Defaults to 128.
    """
    self.base.as_grayscale()
    self.base.asarray = threshold_niblack_like(
        img=self.base.asarray, method="sauvola", window_size=window_size, k=k, r=r
    )[1]
    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 pixels.

required
Source code in otary/image/components/transformer/components/binarizer/binarizer.py
def threshold_simple(self, thresh: int) -> "Image":
    """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.

    Args:
        thresh (int): value to separate the black from the white pixels.
    """
    self.base.as_grayscale()
    self.base.asarray = np.array((self.base.asarray > thresh) * 255, dtype=np.uint8)
    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 the std. Defaults to 0.06.

0.06
Source code in otary/image/components/transformer/components/binarizer/binarizer.py
def threshold_singh(self, window_size: int = 15, k: float = 0.06) -> "Image":
    """Apply Singh local thresholding.

    Paper (2012):
    [A New Local Adaptive Thresholding Technique in Binarization](https://www.researchgate.net/publication/220485031)

    Args:
        window_size (int, optional): apply on the
            image. Defaults to 15.
        k (float, optional): factor to apply to regulate the impact
            of the std. Defaults to 0.06.
    """
    self.base.as_grayscale()
    self.base.asarray = threshold_niblack_like(
        img=self.base.asarray, method="singh", window_size=window_size, k=k
    )[1]
    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 int

window size for high contrast image computation. Defaults to 3.

3
n_min int

minimum number of high contrast pixels within the neighborhood window. Defaults to -1 meaning that n_min = window_size.

-1
Source code in otary/image/components/transformer/components/binarizer/binarizer.py
def threshold_su(
    self,
    window_size: int = 3,
    n_min: int = -1,
) -> "Image":
    """Compute the Su local thresholding.

    Paper (2010):
    [Binarization of historical document images using the local maximum and minimum](https://www.researchgate.net/publication/220933012)

    Args:
        window_size (int, optional): window size for high contrast image
            computation. Defaults to 3.
        n_min (int, optional): minimum number of high contrast pixels within the
            neighborhood window. Defaults to -1 meaning that n_min = window_size.
    """
    self.base.as_grayscale()
    self.base.asarray = threshold_su(
        img=self.base.asarray, window_size=window_size, n_min=n_min
    )
    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 the std. Defaults to 0.5.

0.5
Source code in otary/image/components/transformer/components/binarizer/binarizer.py
def threshold_wan(
    self, window_size: int = 15, k: float = 0.5, r: float = 128.0
) -> "Image":
    """Apply Wan local thresholding.

    Paper (2018):
    [Binarization of Document Image Using Optimum Threshold Modification](https://www.researchgate.net/publication/326026836)

    Args:
        window_size (int, optional): apply on the
            image. Defaults to 15.
        k (float, optional): factor to apply to regulate the impact
            of the std. Defaults to 0.5.
    """
    self.base.as_grayscale()
    self.base.asarray = threshold_niblack_like(
        img=self.base.asarray, method="wan", window_size=window_size, k=k, r=r
    )[1]
    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 the std. Defaults to 0.5.

0.5
Source code in otary/image/components/transformer/components/binarizer/binarizer.py
def threshold_wolf(self, window_size: int = 15, k: float = 0.5) -> "Image":
    """Apply Wolf local thresholding.

    Paper (2003):
    [Extraction and Recognition of Artificial Text in Multimedia Documents](https://hal.science/hal-01504401v1)

    Args:
        window_size (int, optional): apply on the
            image. Defaults to 15.
        k (float, optional): factor to apply to regulate the impact
            of the std. Defaults to 0.5.
    """
    self.base.as_grayscale()
    self.base.asarray = threshold_niblack_like(
        img=self.base.asarray, method="wolf", window_size=window_size, k=k
    )[1]
    return self.base.parent