1

I'm currently working on a project that involves isolating QR codes from images that contain various background elements. But I'm struggling to figure out how to isolate the QR code from the background.

QR code image - QR01

QR code image - QR02

The images below show my attempt so far, but I've only managed to isolate the 3 corner squares and some white parts of the QR code. Although some of the isolated regions have unwanted artifacts.

I'm very new to image processing so I'm learning how to use these image processing function while working on this project, so please excuse any simple mistake I've made.

The challenges:

  • The isolated region includes unwanted artifacts.
  • I'm unsure if my structuring element is appropriate for the morphological operations for extracting QR codes.
  • I've experimented with different threshold values, but the results vary significantly.

Request for Feedback:

  • Could someone provide insights on how to improve my morphological processing? Are there any specific techniques or adjustments you would recommend to better isolate the QR code patterns from the background image?

My MATLAB code:

% Read the input image
img = imread('QR01.jpg');

% Convert to grayscale if the image is RGB
if size(img, 3) == 3
    img = rgb2gray(img);
end

% Set a threshold to create a binary image
thresholdVal = 0.5; % Adjust as necessary
threshold_img = imbinarize(img, thresholdVal);

% Morphological operations to emphasize squares
se = strel('square', 5); % Use a smaller structuring element to preserve finer details
cleaned_img = imopen(threshold_img, se); % Remove small noise

% Identify connected components in the cleaned image
bConnectedComponents = bwconncomp(cleaned_img, 4);
stats = regionprops(bConnectedComponents, 'Area', 'BoundingBox', 'Eccentricity', 'Extent');

% Initialize a blank image for the QR code
qrCodeRegion = false(size(cleaned_img));

% Loop through each component to find potential QR patterns
for k = 1:bConnectedComponents.NumObjects
    area = stats(k).Area;
    bbox = stats(k).BoundingBox;
    aspectRatio = bbox(3) / bbox(4); % Width / Height ratio
    extent = stats(k).Extent; % Ratio of area to bounding box area

    % QR finder patterns are approximately square with high extent (~1 for solid shapes)
    if area > 100 && area < 1000 && aspectRatio > 0.8 && aspectRatio < 1.2 && extent > 0.6
        % Mark this component as part of the QR code
        qrCodeRegion(bConnectedComponents.PixelIdxList{k}) = true;
    end
end

% Further dilation if needed to connect the finder patterns
%qrCodeRegion = imdilate(qrCodeRegion, strel('square', 10));

% Show the isolated QR code region
figure;
imshow(img);
title('Original QR Code image');

figure;
imshow(qrCodeRegion);
title('Isolated QR Code Region');

Original QR Code image

Isolated QR Code Region

2
  • QR codes are located, basically, by scanning across the image and looking for a pattern of edges of relative distance 1,1,3,1,1, which is characteristic of the "finder patterns". this approach is detailed in the original paper/specification. your approach looks ad-hoc. I don't see that leading far. Commented Oct 28, 2024 at 7:45
  • your question appears to be authored with AI assistance. I would not recommend using AI to "shape up" any prose. Commented Oct 28, 2024 at 7:48

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.