Search code examples
matlabimage-processingqr-code

How do detect a QR code pattern in an image?


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


Solution

  • 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.