How do detect a QR code pattern in an image?

16,219

You don't need to use xcorr2. You should look for a 1:1:3:1:1 (widths of dark-light-dark-light-dark) pattern in 1d using scanlines.

There is a description of a reference detection algorithm on page 60 of the standard.

Also, ZXing is an open-source library that implements QR code detection/recognition. You can go over their code for reference.

Edit: At each scanline, count subsequent dark and subsequent light pixels. You will get a list of integers representing sequence lengths.

Then start from the largest dark subsequence, and look to its sides. If the dark subsequence length is 300, then its adjacent light subsequences should be of length 50-150, and their adjacent dark subsequences should be of length 50-150 as well (this is due to the tolerance of 0.5 proposed in the standard.).

So if you find such a sequence, you mark it with size 300. Then you try the next largest dark subsequence and so on.

Just to clarify, the above method should be used to find the 3 marked corners.

Share:
16,219
alvaro.delaserna
Author by

alvaro.delaserna

Masters in Multimedia and Communications student. Bachelor in Audiovisual Systems Engineering.

Updated on June 13, 2022

Comments

  • alvaro.delaserna
    alvaro.delaserna over 1 year

    I'm working on a QR detector code and I need to locate the Finding Patterns (FP) on an image. I have created a binary template resembling the squares you find on the corners of QR codes as:

    FP = ones(9);
    FP(2:8,2:8)=0;
    FP(3:7,3:7)=1;
    FP(4:6,4:6)=0;
    figure;imshow(FP)
    

    And I have tried looking for the points in the image with maximum correlation with this template using xcorr2. My problem is obvious: My template is very small compared to the actual sizes QR codes might have on the images.

    Is there a way of looking for a pattern/mask without having to resize it? Is there another approach to this problem?

    As an example, here's an image with a QR code

    enter image description here

  • alvaro.delaserna
    alvaro.delaserna over 9 years
    OK I'll try this, but what if I'm working with an image like the one I uploaded? In that case I can't look for that pattern (assuming it's pixel-based), since there are more pixels involved in the sequence, right?
  • Michael Litvin
    Michael Litvin over 9 years
    I think that you will have to scale either your pattern or the reference image. But since you do this in 1d rather than 2d, it will be much faster.
  • kkuilla
    kkuilla over 9 years
    @alvaro.delaserna I think that looking for the QR code in an image and finding the corners within the QR code are two different questions. You might progress more if you first tried to detect the QR code within an image (and then ask specific questions related to your code if you are stuck), crop out the QR code and then detect the corners within the cropped image. It might not be the fastest algorithm to detect the corners but it might be easier to get help.
  • alvaro.delaserna
    alvaro.delaserna over 9 years
    that's exactly what I need, to be able to crop the QR code