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:
Advanced methods are accessible via the transformer.binarizer attribute:
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:
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 |
|
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
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
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
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
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
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
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
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 |
|
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
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
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
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
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
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
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
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
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
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
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
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
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
|