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.
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');



