Binarization Digits of numbers and prepare digits for OCR or number detection and remove the noise and shadow (background) from the picture completely.
- 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
You can easily run this code on google colab by just clicking this badge
we need to import these libraries :
cv2, numpy, cv2_imshow
import cv2 as cv
from google.colab.patches import cv2_imshow
import numpy as npWe need to Download the images from my Github repository or you can download others that have digits by your own.
!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.jpgwe need to load images into python variables we ues OpenCV library to read the images also the format of the images are nd.array
image = cv.imread('number0.jpg', cv.IMREAD_GRAYSCALE)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.
noise_removed = cv.medianBlur(image, 5)
binary_image = cv.adaptiveThreshold(noise_removed, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY,11,2)For using the morpholgy in image processing we need to invert the images
inverted_image = 255 - binary_imageActually Opening have two section :
1- Erosion for removing noise that are not eleminated by midan filter and created after biniarization the image.
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.
kernel2 = np.ones((5, 5), np.uint8)
dilation = cv.dilate(erosion, kernel2, iterations = 1)we have an Image with white text and black background and we don't want this so we invert that again.
inverted_image2 = 255 - dilationso in this step we put all of things together and test that for other images.
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_image2these are parameters of the digit binarization.
- blur_value : the format is like a number
5higher 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.
This project is licensed under the MIT License.









