Skip to content

Commit 17541b1

Browse files
authored
Adding binarization support
1 parent 3e771a2 commit 17541b1

File tree

18 files changed

+338
-0
lines changed

18 files changed

+338
-0
lines changed

contrib/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Each project should live in its own subdirectory ```/contrib/<project>``` and co
1010
|---|---|---|
1111
| [Crowd counting](crowd_counting) | Counting the number of people in low-crowd-density (e.g. less than 10 people) and high-crowd-density (e.g. thousands of people) scenarios. | [![Build Status](https://dev.azure.com/team-sharat/crowd-counting/_apis/build/status/lixzhang.cnt?branchName=lixzhang%2Fsubmodule-rev3)](https://dev.azure.com/team-sharat/crowd-counting/_build/latest?definitionId=49&branchName=lixzhang%2Fsubmodule-rev3)|
1212
| [Action Recognition with I3D](action_recognition) | Action recognition to identify video/webcam footage from what actions are performed (e.g. "running", "opening a bottle") and at what respective start/end times. Please note, that we also have a R(2+1)D implementation of action recognition that you can find under [scenarios](../sceanrios).| |
13+
| [Document Image Binarization](binarization) | Binarization is a technique to segment foreground from the background pixels. A simple technique for binarization is thresholding of gray-level or color document scanned images.| |
1314

1415
## Tools
1516
| Directory | Project description | Build status (optional) |

contrib/binarization/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Binarization
2+
Binarization is a technique to segment foreground from the background pixels. A simple technique for binarization is thresholding of gray-level or color document scanned images.
3+
## At a glance
4+
5+
This binarization technique is an improvement over Sauvola's binarization technique. In this work, we improve the existing Sauvola's binarization technique by preserving more foreground information in the binarized document-images. In order to achieve this, we introduce a confidence score for the background pixels.
6+
7+
### Input images
8+
9+
<img src="./confidence_based_Sauvola_binarization/test_images/2.jpeg" width="33%"> </img>
10+
<img src="./confidence_based_Sauvola_binarization/test_images/10.jpeg" width="33%"> </img>
11+
<img src="./confidence_based_Sauvola_binarization/test_images/new1.jpg" width="33%"> </img>
12+
13+
### Binary outputs
14+
15+
<img src="./confidence_based_Sauvola_binarization/results/2_bin_new.png" width="33%"> </img>
16+
<img src="./confidence_based_Sauvola_binarization/results/10_bin_new.png" width="33%"> </img>
17+
<img src="./confidence_based_Sauvola_binarization/results/new1_bin_new.png" width="33%"> </img>

contrib/binarization/confidence_based_Sauvola_binarization/Modified-Sauvola_Binarization.ipynb

Lines changed: 213 additions & 0 deletions
Large diffs are not rendered by default.
Binary file not shown.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
'''
2+
@author: Soumyadeep Dey
3+
email:Soumyadeep.Dey@microsoft.com
4+
Date: October 2020
5+
cite: https://drive.google.com/file/d/1D3CyI5vtodPJeZaD2UV5wdcaIMtkBbdZ/view?usp=sharing
6+
'''
7+
8+
# importing required libraries
9+
import numpy as np
10+
import cv2
11+
from skimage.filters import threshold_sauvola
12+
13+
14+
def SauvolaModBinarization(image,n1=51,n2=51,k1=0.3,k2=0.3,default=True):
15+
'''
16+
Binarization using Sauvola's algorithm
17+
@name : SauvolaModBinarization
18+
parameters
19+
@param image (numpy array of shape (3/1) of type np.uint8): color or gray scale image
20+
optional parameters
21+
@param n1 (int) : window size for running sauvola during the first pass
22+
@param n2 (int): window size for running sauvola during the second pass
23+
@param k1 (float): k value corresponding to sauvola during the first pass
24+
@param k2 (float): k value corresponding to sauvola during the second pass
25+
@param default (bool) : bollean variable to set the above parameter as default.
26+
27+
@param default is set to True : thus default values of the above optional parameters (n1,n2,k1,k2) are set to
28+
n1 = 5 % of min(image height, image width)
29+
n2 = 10 % of min(image height, image width)
30+
k1 = 0.5
31+
k2 = 0.5
32+
Returns
33+
@return A binary image of same size as @param image
34+
35+
@cite https://drive.google.com/file/d/1D3CyI5vtodPJeZaD2UV5wdcaIMtkBbdZ/view?usp=sharing
36+
'''
37+
38+
if(default):
39+
n1 = int(0.05*min(image.shape[0],image.shape[1]))
40+
if (n1%2==0):
41+
n1 = n1+1
42+
n2 = int(0.1*min(image.shape[0],image.shape[1]))
43+
if (n2%2==0):
44+
n2 = n2+1
45+
k1 = 0.5
46+
k2 = 0.5
47+
if(image.ndim==3):
48+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
49+
else:
50+
gray = np.copy(image)
51+
T1 = threshold_sauvola(gray, window_size=n1,k=k1)
52+
max_val = np.amax(gray)
53+
min_val = np.amin(gray)
54+
C = np.copy(T1)
55+
C = C.astype(np.float32)
56+
C[gray > T1] = (gray[gray > T1] - T1[gray > T1])/(max_val - T1[gray > T1])
57+
C[gray <= T1] = 0
58+
C = C * 255.0
59+
new_in = np.copy(C.astype(np.uint8))
60+
T2 = threshold_sauvola(new_in, window_size=n2,k=k2)
61+
binary = np.copy(gray)
62+
binary[new_in <= T2] = 0
63+
binary[new_in > T2] = 255
64+
return binary
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Binarization
2+
Binarization is a technique to segment foreground from the background pixels. A simple technique for binarization is thresholding of gray-level or color document scanned images.
3+
## At a glance
4+
5+
This binarization technique is an improvement over Sauvola's binarization technique. In this work, we improve the existing Sauvola's binarization technique by preserving more foreground information in the binarized document-images. In order to achieve this, we introduce a confidence score for the background pixels.
6+
7+
### Input images
8+
9+
<img src="./test_images/2.jpeg" width="33%"> </img>
10+
<img src="./test_images/10.jpeg" width="33%"> </img>
11+
<img src="./test_images/new1.jpg" width="33%"> </img>
12+
13+
### Sauvola outputs
14+
15+
<img src="./results/2_bin_old.png" width="33%"> </img>
16+
<img src="./results/10_bin_old.png" width="33%"> </img>
17+
<img src="./results/new1_bin_old.png" width="33%"> </img>
18+
19+
### Confidence based Sauvola outputs
20+
21+
<img src="./results/2_bin_new.png" width="33%"> </img>
22+
<img src="./results/10_bin_new.png" width="33%"> </img>
23+
<img src="./results/new1_bin_new.png" width="33%"> </img>
24+
25+
## Reference
26+
27+
For details refer to this [paper](./ModifiedSauvola.pdf)
28+
29+
30+
31+
32+
## Setup
33+
34+
### Dependencies
35+
- python 3.7
36+
- numpy 1.16
37+
- opencv 4.2
38+
- skimage 0.17
39+
40+
41+
### Example
42+
43+
Sample example of the usage of the said binarization technique can be found in this [notebook](./Modified-Sauvola_Binarization.ipynb).
218 KB
Loading
158 KB
Loading
82 KB
Loading
67.9 KB
Loading

0 commit comments

Comments
 (0)