Skip to content

MahamadElshabory/opencv-code11-barcode-decoder

Repository files navigation

Code 11 Barcode Decoder with OpenCV

A computer-vision project that processes and decodes Code 11 barcodes from images under challenging conditions such as rotation, low contrast, extreme brightness, salt-and-pepper noise, obstruction, and complex backgrounds.

The project uses traditional image-processing techniques with Python, OpenCV, NumPy, and Matplotlib rather than a trained machine-learning model.


Project Overview

Barcode images are not always perfectly scanned. They may be:

  • Rotated
  • Blurred
  • Too dark or too bright
  • Covered by image noise
  • Partially obstructed
  • Placed over a complex background
  • Captured with weak contrast

This project applies a sequence of image-processing operations to clean the image, isolate the barcode, measure the bar widths, and decode the resulting Code 11 pattern.


Processing Pipeline

Input image
    ↓
Convert to grayscale
    ↓
Detect barcode rotation
    ↓
Correct the rotation
    ↓
Measure image brightness
    ↓
Apply suitable thresholding
    ↓
Detect and remove salt-and-pepper noise
    ↓
Apply morphological opening
    ↓
Locate and crop the barcode
    ↓
Apply morphological closing
    ↓
Remove the printed text below the barcode
    ↓
Measure narrow and wide bars
    ↓
Decode the Code 11 characters

Main Features

  • Barcode rotation detection using contours and minimum-area rectangles
  • Automatic barcode deskewing
  • Brightness analysis using the image’s mean pixel value
  • Threshold selection based on image brightness
  • Salt-and-pepper noise detection
  • Median filtering for noise reduction
  • Morphological opening and closing
  • Automatic barcode-region detection
  • Removal of the human-readable text below the barcode
  • Narrow and wide bar classification
  • Code 11 character decoding
  • Visualization of intermediate processing stages

Code 11 Decoding

Code 11 represents each character using a combination of narrow and wide bars.

In this project:

0 = Narrow bar
1 = Wide bar

Example character patterns:

Pattern Character
10001 1
01001 2
11000 3
00101 4
10100 5
01100 6
00011 7
10010 8
10000 9
00001 0
00100 -
00110 Start/Stop

The decoder reads the barcode column by column, measures consecutive black and white regions, classifies each region as narrow or wide, and maps the resulting pattern to its Code 11 character.


Test Cases

The repository contains multiple images designed to test the pipeline under different conditions.

Clean barcode Obstruction Rotation
Clean barcode Obstructed barcode Rotated barcode

Additional tests include:

  • Dark backgrounds
  • Very bright images
  • Low-contrast barcodes
  • Salt-and-pepper noise
  • Blurred images
  • Textured backgrounds
  • Partial barcode obstruction

Technologies

  • Python
  • OpenCV
  • NumPy
  • Matplotlib
  • Jupyter Notebook

Repository Files

opencv-code11-barcode-decoder/
├── barcode_processing_phase1.ipynb
├── barcode_decoder.ipynb
├── testcase1.jpg
├── testcase2.jpg
├── testcase3.jpg
├── ...
├── testcase11.jpg
└── README.md

barcode_processing_phase1.ipynb

Contains the image-preprocessing pipeline, including:

  • Rotation handling
  • Thresholding
  • Noise detection
  • Morphological processing
  • Barcode cropping

barcode_decoder.ipynb

Contains the complete pipeline and the final Code 11 decoding logic.


Installation

Clone the repository:

git clone https://github.com/MahamadElshabory/opencv-code11-barcode-decoder.git
cd opencv-code11-barcode-decoder

Install the required packages:

pip install opencv-python numpy matplotlib jupyter

Start Jupyter Notebook:

jupyter notebook

Open:

barcode_decoder.ipynb

Usage

Inside the notebook, change the image filename:

image = cv2.imread("testcase1.jpg")

For example:

image = cv2.imread("testcase9.jpg")

Then run all notebook cells:

Kernel → Restart & Run All

The notebook displays the processing stages and prints the detected Code 11 characters.


Example Output

The final decoded result is returned as a sequence of Code 11 characters:

["Stop/Start", "1", "2", "3", "-", "4", "5", "Stop/Start"]

The exact output depends on the selected test image.


Limitations

  • Threshold values are manually selected for known brightness ranges.
  • Very severe obstruction may prevent successful decoding.
  • The narrow-bar size is estimated from the first detected bar.
  • The current implementation is designed specifically for Code 11.
  • The project currently runs through Jupyter notebooks rather than a command-line or web interface.

Future Improvements

  • Convert the notebook into reusable Python modules
  • Accept image paths through command-line arguments
  • Automatically estimate narrow and wide bar sizes
  • Add checksum validation for Code 11
  • Support live camera input
  • Build a FastAPI barcode-decoding endpoint
  • Add a simple web interface
  • Add automated tests for every test image
  • Display confidence scores and failure reasons
  • Support additional barcode formats

Author

Mahamad Elshabory


License

This project was developed for educational and portfolio purposes.

About

OpenCV-based Code 11 barcode decoder that detects rotation, removes noise, adjusts contrast, isolates barcode regions, and decodes bar patterns from challenging images.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors