Git Product home page Git Product logo

qr-code-extractor's Introduction

QR Code Extractor

A Python based QR code extractor that uses OpenCV to detect and extract QR codes in any orientation.

Features

  • Ability to rapidly detect multiple QR codes in an image or video frame at once
  • Ability to compensate for perspective and simplify codes
  • Compatible with Model 1, Model 2, and SQRC codes

Important Notes

  • This program does not decode QR codes by itself. Decoding must be handled by an outside program (such as ZBar)
  • This code could be much more optimized. Though iterating through squares is not too intensive (as very few contours end up matching the criteria), it could better handle finding the fourth corner and determining the QR code's bounds
  • This code was made as a learning experience, and as such has some major limitations. It is unable to extract QR codes with different colours, distractions or embellishments, and QR codes with more than one alignment locator. It is not meant to be used in a production environment, and is more suited as a learning tool or first step towards making a real QR code reader.

Methodology - How it works

Step One: Remove noise from sample image

  • Convert original image to greyscale
  • Apply Gaussian blur to reduce noise
  • Apply Canny edge filter to eliminate distractions

Step Two: Narrow down search to only QR code locators

  • Find all remaining contours
  • Filter all contours with approximate vertex count of four
  • Filter all quadrilaterals that are approximately squares and have a certain number of children

Step Three: Locate the locators

  • Loop through all squares
    • Find all squares that are similar in size to the current square
  • If the closest two squares to the current square are similar distances away, estimate that this square is the upper-left locator
  • With the other squares, find the angle from the upper-left vertex to determine the orientation of the QR code

Step Four: Find the alignment pattern square (colloquially called "tiny square" in my code)

  • While searching through squares in previous step, store any squares that are less than half the size of the locator squares
  • After determining locator orientations, calculate the midpoint of the QR code
  • Select from the possible alignment pattern squares the one closest to the midpoint that is also within the bounds of the code itself
  • If alignment pattern is found:
    • Determine fourth corner of the QR code to be a rational distance from the alignment pattern (in a direction opposite of the midpoint)
  • If no alignment pattern is found (smaller QR codes lack this, or the camera may not detect it):
    • Determine edges of the locators that are along the edges of the entire QR code that would intersect to form the fourth corner
    • Find line intersection and determine that point to be the fourth corner

Step Five: Compensate for perspective warping and extract the code

  • For each code, warp the vertices into a square to fix alignment
  • Scale down with cubic interpolation into 29x29 pixel square (change dimensions for different types of codes)
  • Convert to one bit black and white by thresholding the image
  • Return formatted codes in list

Hope you find this code helpful, and if you have any questions feel free to contact me!

qr-code-extractor's People

Contributors

idreesinc avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.