{"id":28945213,"url":"https://github.com/asadiahmad/digit-binarization","last_synced_at":"2026-03-03T14:34:48.775Z","repository":{"id":290094982,"uuid":"973355868","full_name":"AsadiAhmad/Digit-Binarization","owner":"AsadiAhmad","description":"Binarization Digits of numbers and prepare digits for OCR.","archived":false,"fork":false,"pushed_at":"2025-05-07T13:33:55.000Z","size":710,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-07T14:43:58.867Z","etag":null,"topics":["binarization","digit-classification","image-processing","opencv","remove-noise","shadow-removal"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AsadiAhmad.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-04-26T20:07:21.000Z","updated_at":"2025-05-07T13:33:59.000Z","dependencies_parsed_at":"2025-04-26T21:31:20.624Z","dependency_job_id":null,"html_url":"https://github.com/AsadiAhmad/Digit-Binarization","commit_stats":null,"previous_names":["asadiahmad/digit-binarization"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/AsadiAhmad/Digit-Binarization","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AsadiAhmad%2FDigit-Binarization","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AsadiAhmad%2FDigit-Binarization/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AsadiAhmad%2FDigit-Binarization/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AsadiAhmad%2FDigit-Binarization/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AsadiAhmad","download_url":"https://codeload.github.com/AsadiAhmad/Digit-Binarization/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AsadiAhmad%2FDigit-Binarization/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261434012,"owners_count":23157197,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["binarization","digit-classification","image-processing","opencv","remove-noise","shadow-removal"],"created_at":"2025-06-23T07:02:32.672Z","updated_at":"2026-03-03T14:34:48.770Z","avatar_url":"https://github.com/AsadiAhmad.png","language":"Jupyter Notebook","readme":"# Digit-Binarization\nBinarization Digits of numbers and prepare digits for OCR or number detection and remove the noise and shadow (background) from the picture completely.\n\n\u003cdiv display=flex align=center\u003e\n    \u003cimg src=\"/Pictures/0.jpg\" width=\"400px\"/\u003e\n    \u003cimg src=\"/Pictures/5.jpg\" width=\"400px\"/\u003e\n\u003c/div\u003e\n\n\n## Tech :hammer_and_wrench: Languages and Tools :\n\n\u003cdiv\u003e\n  \u003cimg src=\"https://github.com/devicons/devicon/blob/master/icons/python/python-original.svg\" title=\"Python\" alt=\"Python\" width=\"40\" height=\"40\"/\u003e\u0026nbsp;\n  \u003cimg src=\"https://github.com/devicons/devicon/blob/master/icons/jupyter/jupyter-original.svg\" title=\"Jupyter Notebook\" alt=\"Jupyter Notebook\" width=\"40\" height=\"40\"/\u003e\u0026nbsp;\n  \u003cimg src=\"https://assets.st-note.com/img/1670632589167-x9aAV8lmnH.png\" title=\"Google Colab\" alt=\"Google Colab\" width=\"40\" height=\"40\"/\u003e\u0026nbsp;\n  \u003cimg src=\"https://github.com/devicons/devicon/blob/master/icons/opencv/opencv-original.svg\" title=\"OpenCV\" alt=\"OpenCV\" width=\"40\" height=\"40\"/\u003e\u0026nbsp;\n  \u003cimg src=\"https://github.com/devicons/devicon/blob/master/icons/numpy/numpy-original.svg\" title=\"Numpy\" alt=\"Numpy\" width=\"40\" height=\"40\"/\u003e\u0026nbsp;\n\u003c/div\u003e\n\n- Python : Popular language for implementing Neural Network\n- Jupyter Notebook : Best tool for running python cell by cell\n- Google Colab : Best Space for running Jupyter Notebook with hosted server\n- OpenCV : Best Library for working with images\n- Numpy : Best Library for working with arrays in python\n\n## Run the Notebook on Google Colab\n\nYou can easily run this code on google colab by just clicking this badge [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/AsadiAhmad/Digit-Binarization/blob/main/Code/Image_Binarization.ipynb)\n\n## Tutorial\n\n### Step 1: Import Libraries\n\nwe need to import these libraries :\n\n`cv2`, `numpy`, `cv2_imshow`\n\n```python\nimport cv2 as cv\nfrom google.colab.patches import cv2_imshow\nimport numpy as np\n```\n\n### Step 2: Download Image\n\nWe need to Download the images from my `Github` repository or you can download others that have digits by your own.\n\n```sh\n!wget https://raw.githubusercontent.com/AsadiAhmad/Digit-Binarization/main/Pictures/number0.jpg -O number0.jpg\n!wget https://raw.githubusercontent.com/AsadiAhmad/Digit-Binarization/main/Pictures/number1.jpg -O number1.jpg\n!wget https://raw.githubusercontent.com/AsadiAhmad/Digit-Binarization/main/Pictures/number2.jpg -O number2.jpg\n```\n\n### Step 3: Load Images\n\nwe need to load images into `python` variables we ues `OpenCV` library to read the images also the format of the images are `nd.array`\n\n```python\nimage = cv.imread('number0.jpg', cv.IMREAD_GRAYSCALE)\n```\n\n\u003cdiv display=flex align=center\u003e\n  \u003cimg src=\"/Pictures/0.jpg\" width=\"400px\"/\u003e\n\u003c/div\u003e\n\n### Step 4: Image Binarization\n\nthis 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.\n\n```python\nnoise_removed = cv.medianBlur(image, 5)\nbinary_image = cv.adaptiveThreshold(noise_removed, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY,11,2)\n```\n\n\u003cdiv display=flex align=center\u003e\n  \u003cimg src=\"/Pictures/1.jpg\" width=\"400px\"/\u003e\n\u003c/div\u003e\n\n### Step 5: Invert the Binary Image\n\nFor using the morpholgy in image processing we need to invert the images \n\n```python\ninverted_image = 255 - binary_image\n```\n\n\u003cdiv display=flex align=center\u003e\n  \u003cimg src=\"/Pictures/2.jpg\" width=\"400px\"/\u003e\n\u003c/div\u003e\n\n### Step 6: Opening Image for Completely remove Noise\n\nActually Opening have two section :\n\n1- Erosion for removing noise that are not eleminated by midan filter and created after biniarization the image.\n\n```python\nkernel = np.ones((2, 2), np.uint8)\nerosion = cv.erode(inverted_image, kernel, iterations = 1)\n```\n\n\u003cdiv display=flex align=center\u003e\n  \u003cimg src=\"/Pictures/3.jpg\" width=\"400\"/\u003e\n\u003c/div\u003e\n\n2- Dilation for bolding the text because after the erosion we lose some part of the text so we need to refill the text.\n\n```python\nkernel2 = np.ones((5, 5), np.uint8)\ndilation = cv.dilate(erosion, kernel2, iterations = 1)\n```\n\n\u003cdiv display=flex align=center\u003e\n  \u003cimg src=\"/Pictures/4.jpg\" width=\"400px\"/\u003e\n\u003c/div\u003e\n\n### Step 7: Invert Image again\n\nwe have an Image with white text and black background and we don't want this so we invert that again.\n\n```python\ninverted_image2 = 255 - dilation\n```\n\n\u003cdiv display=flex align=center\u003e\n  \u003cimg src=\"/Pictures/5.jpg\" width=\"400px\"/\u003e\n\u003c/div\u003e\n\n### Step 8: All together for other images\n\nso in this step we put all of things together and test that for other images.\n\n```python\ndef binarization_image(image, blur_value=5, kernel_erosion=(2, 2), kernel_dilation=(5, 5)):\n    noise_removed = cv.medianBlur(image, blur_value)\n    binary_image = cv.adaptiveThreshold(noise_removed, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY,11,2)\n    inverted_image = 255 - binary_image\n    kernel = np.ones(kernel_erosion, np.uint8)\n    erosion = cv.erode(inverted_image, kernel, iterations = 1)\n    kernel2 = np.ones(kernel_dilation, np.uint8)\n    dilation = cv.dilate(erosion, kernel2, iterations = 1)\n    inverted_image2 = 255 - dilation\n    return inverted_image2\n```\n\u003cdiv display=flex align=center\u003e\n    \u003cimg src=\"/Pictures/number1.jpg\" width=\"400px\"/\u003e\n    \u003cimg src=\"/Pictures/6.jpg\" width=\"400px\"/\u003e\n\u003c/div\u003e\n\n\u003cdiv display=flex align=center\u003e\n    \u003cimg src=\"/Pictures/number2.jpg\" width=\"400px\"/\u003e\n    \u003cimg src=\"/Pictures/7.jpg\" width=\"400px\"/\u003e\n\u003c/div\u003e\n\n## Parameters\n\nthese are parameters of the digit binarization.\n\n- blur_value : the format is like a number `5` higher value higher remove noise and sometime lose the parts of the main digit\n- 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\n- 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!\n\nSo for each image can vary depending on the size of the digit or size or shape of the noise.\n\n## License\n\nThis project is licensed under the MIT License.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fasadiahmad%2Fdigit-binarization","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fasadiahmad%2Fdigit-binarization","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fasadiahmad%2Fdigit-binarization/lists"}