https://github.com/asadiahmad/digit-binarization
Binarization Digits of numbers and prepare digits for OCR.
https://github.com/asadiahmad/digit-binarization
binarization digit-classification image-processing opencv remove-noise shadow-removal
Last synced: about 2 months ago
JSON representation
Binarization Digits of numbers and prepare digits for OCR.
- Host: GitHub
- URL: https://github.com/asadiahmad/digit-binarization
- Owner: AsadiAhmad
- License: mit
- Created: 2025-04-26T20:07:21.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-05-07T13:33:55.000Z (12 months ago)
- Last Synced: 2025-05-07T14:43:58.867Z (12 months ago)
- Topics: binarization, digit-classification, image-processing, opencv, remove-noise, shadow-removal
- Language: Jupyter Notebook
- Homepage:
- Size: 693 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Digit-Binarization
Binarization Digits of numbers and prepare digits for OCR or number detection and remove the noise and shadow (background) from the picture completely.
## Tech :hammer_and_wrench: Languages and Tools :
- Python : Popular language for implementing Neural Network
- Jupyter Notebook : Best tool for running python cell by cell
- Google Colab : Best Space for running Jupyter Notebook with hosted server
- OpenCV : Best Library for working with images
- Numpy : Best Library for working with arrays in python
## Run the Notebook on Google Colab
You can easily run this code on google colab by just clicking this badge [](https://colab.research.google.com/github/AsadiAhmad/Digit-Binarization/blob/main/Code/Image_Binarization.ipynb)
## Tutorial
### Step 1: Import Libraries
we need to import these libraries :
`cv2`, `numpy`, `cv2_imshow`
```python
import cv2 as cv
from google.colab.patches import cv2_imshow
import numpy as np
```
### Step 2: Download Image
We need to Download the images from my `Github` repository or you can download others that have digits by your own.
```sh
!wget https://raw.githubusercontent.com/AsadiAhmad/Digit-Binarization/main/Pictures/number0.jpg -O number0.jpg
!wget https://raw.githubusercontent.com/AsadiAhmad/Digit-Binarization/main/Pictures/number1.jpg -O number1.jpg
!wget https://raw.githubusercontent.com/AsadiAhmad/Digit-Binarization/main/Pictures/number2.jpg -O number2.jpg
```
### Step 3: Load Images
we need to load images into `python` variables we ues `OpenCV` library to read the images also the format of the images are `nd.array`
```python
image = cv.imread('number0.jpg', cv.IMREAD_GRAYSCALE)
```
### Step 4: Image Binarization
this is our primary state that we should remove noise with median filter and then use the adaptive thresholding for removing the background in each section of the image so we do not have any dark section in the image.
```python
noise_removed = cv.medianBlur(image, 5)
binary_image = cv.adaptiveThreshold(noise_removed, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY,11,2)
```
### Step 5: Invert the Binary Image
For using the morpholgy in image processing we need to invert the images
```python
inverted_image = 255 - binary_image
```
### Step 6: Opening Image for Completely remove Noise
Actually Opening have two section :
1- Erosion for removing noise that are not eleminated by midan filter and created after biniarization the image.
```python
kernel = np.ones((2, 2), np.uint8)
erosion = cv.erode(inverted_image, kernel, iterations = 1)
```
2- Dilation for bolding the text because after the erosion we lose some part of the text so we need to refill the text.
```python
kernel2 = np.ones((5, 5), np.uint8)
dilation = cv.dilate(erosion, kernel2, iterations = 1)
```
### Step 7: Invert Image again
we have an Image with white text and black background and we don't want this so we invert that again.
```python
inverted_image2 = 255 - dilation
```
### Step 8: All together for other images
so in this step we put all of things together and test that for other images.
```python
def binarization_image(image, blur_value=5, kernel_erosion=(2, 2), kernel_dilation=(5, 5)):
noise_removed = cv.medianBlur(image, blur_value)
binary_image = cv.adaptiveThreshold(noise_removed, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY,11,2)
inverted_image = 255 - binary_image
kernel = np.ones(kernel_erosion, np.uint8)
erosion = cv.erode(inverted_image, kernel, iterations = 1)
kernel2 = np.ones(kernel_dilation, np.uint8)
dilation = cv.dilate(erosion, kernel2, iterations = 1)
inverted_image2 = 255 - dilation
return inverted_image2
```
## Parameters
these are parameters of the digit binarization.
- blur_value : the format is like a number `5` higher value higher remove noise and sometime lose the parts of the main digit
- kernel_erosion : the format is like a tuple `(2, 2)` this is a kernel for erosion higher value of this kernal can remove more noise but maybe we lose some part of the main digit
- kernel_dilation : the format is like a tuple `(5, 5)` this is a kernel for dilation higher value of this kernel bolded the digit more and lower kernel value can make digit unreadable after the erosion!
So for each image can vary depending on the size of the digit or size or shape of the noise.
## License
This project is licensed under the MIT License.