[Digital Image Processing and Application] Template Matching

topic

Given the image car.png and the template image wheel.png, correlation detection is used to detect the wheel in the car image. The position with the maximum correlation value can be interpreted as the detected wheel position. The input of the program is an image and a template, and it is required to:
(i) display the correlation value results of the image;
(ii) list the (x, y) coordinates of all targets detected in the image.

Insert image description here
Insert image description here

Template matching principle

  • Original image: f (x, y) f(x, y)f(x,y ) , template image:T ( x , y ) T(x, y)T(x,y ) , template sizeM × NM \times NM×N
  • in image ffPositioning templateTT in fThe position of T , using relevant matching
  • Algorithm: Using template TTT in imageffCalculate the correlation point by point in f , with the maximum correlation value ρ (x, y) \rho(x, y)p ( x ,y )的位置( x , y ) (x, y)(x,y ) is the templateTTT 所在 位置
    ρ ( x , y ) = ∑ k = − M 2 M 2 ∑ l = − N 2 N 2 f ( x + k , y + l ) ⋅ T ( M 2 + k , N 2 + l ) / ∑ k = − M 2 M 2 ∑ l = − N 2 N 2 f ( x + k , y + l ) 2 ∑ k = − M 2 M 2 ∑ l = − N 2 N 2 T ( M 2 + k , N 2 + l ) 2 \rho(x, y)=\sum_{k=-\frac{M}{2}}^{\frac{M}{2}} \sum_{l=-\frac{N}{2}}^{\frac{N}{2}} f(x+k, y+l) \cdot T\left(\frac{M}{2}+k, \frac{N}{2}+l\right) / \sqrt{\sum_{k=-\frac{M}{2}}^{\frac{M}{2}} \sum_{l=-\frac{N}{2}}^{\frac{N}{2}} f(x+k, y+l)^2} \sqrt{\sum_{k=-\frac{M}{2}}^{\frac{M}{2}} \sum_{l=-\frac{N}{2}}^{\frac{N}{2}} T\left(\frac{M}{2}+k, \frac{N}{2}+l\right)^2} p ( x ,y)=k=2M2Ml=2N2Nf(x+k,y+l)T(2M+k,2N+l)/k=2M2Ml=2N2Nf(x+k,y+l)2 k=2M2Ml=2N2NT(2M+k,2N+l)2

Matlab code implementation

Algorithm introduction

  • Read original image and template image
  • STEPS 2. Calculate correlation values
  • Display and save associated value result images: (requirement (i))
  • Detect the coordinates of the target
  • Draw the detected target location on the original image
  • Display detected target coordinates: (requirement (ii))

Display the matching results of the image (the best matching one)

MATLAB implementation

% 读取原图像和模板图像
image = imread('car.png');
template = imread('wheel.png');

% 将图像和模板转换为灰度图像
grayImage = im2double(im2gray(image));
grayTemplate = im2double(im2gray(template));

% 获取图像和模板的大小
imageSize = size(grayImage);
templateSize = size(grayTemplate);

% 初始化相关值结果矩阵
correlation = zeros(imageSize(1), imageSize(2));

% 计算相关值(PDF中给的图像匹配公式)
for y = 1:imageSize(1)
    for x = 1:imageSize(2)
        numerator = 0;
        denominator1 = 0;
        denominator2 = 0;
        
        % 遍历模板中的每个像素,if条件是为了排除异常值
        for l = -floor(templateSize(1)/2):floor(templateSize(1)/2)
            for k = -floor(templateSize(2)/2):floor(templateSize(2)/2)
                % 计算相关匹配公式的分子
                if y + l >= 1 && y + l <= imageSize(1) && x + k >= 1 && x + k <= imageSize(2)
                    numerator = numerator + grayImage(y + l, x + k) * grayTemplate(l + floor(templateSize(1)/2) + 1, k + floor(templateSize(2)/2) + 1);
                end
                
                % 计算相关匹配公式的分母的第一个部分
                if y + l >= 1 && y + l <= imageSize(1) && x + k >= 1 && x + k <= imageSize(2)
                    denominator1 = denominator1 + grayImage(y + l, x + k)^2;
                end
                
                % 计算相关匹配公式的分母的第二个部分
                denominator2 = denominator2 + grayTemplate(l + floor(templateSize(1)/2) + 1, k + floor(templateSize(2)/2) + 1)^2;
            end
        end
        
        % 计算相关值
        correlation(y, x) = numerator / (sqrt(denominator1) * sqrt(denominator2));
    end
end

% 显示相关值结果图像
figure;
imshow(correlation, []);
% 保存图像到本地
imwrite(correlation, '图像的相关值结果.png');

% 检测目标的坐标(相关值最大的)
[maxValue, maxIndex] = max(correlation(:));
[maxY, maxX] = ind2sub(imageSize, maxIndex);
targetCoordinates = [maxX, maxY];
disp('检测到的目标的相关值:');
disp(maxValue);


% 在原图像上绘制检测到的目标位置
figure;
imshow(image);
hold on;
rectangle('Position', [maxX-floor(templateSize(2)/2), maxY-floor(templateSize(1)/2), templateSize(2), templateSize(1)], 'EdgeColor', 'r', 'LineWidth', 2);
saveas(gcf, '检测结果(最匹配的).png');



% 显示检测到的目标坐标
disp('检测到的目标坐标:');
disp(targetCoordinates);

operation result

Correlation value results for images:

Insert image description here

Draw the detected target location on the original image:

Insert image description here

Display detected target coordinates:

Insert image description here

Display the matching results of the image (the three best matches, threshold 0.95)

MATLAB implementation

% 读取原图像和模板图像
image = imread('car.png');
template = imread('wheel.png');


% 将图像和模板转换为灰度图像
grayImage = im2double(im2gray(image));
grayTemplate = im2double(im2gray(template));



% 获取图像和模板的大小
imageSize = size(grayImage);
templateSize = size(grayTemplate);

% 初始化相关值结果矩阵
correlation = zeros(imageSize(1), imageSize(2));

% 计算相关值(PDF中给的图像匹配公式)
for y = 1:imageSize(1)
    for x = 1:imageSize(2)
        numerator = 0;
        denominator1 = 0;
        denominator2 = 0;
        
        % 遍历模板中的每个像素,if条件是为了排除异常值
        for l = -floor(templateSize(1)/2):floor(templateSize(1)/2)
            for k = -floor(templateSize(2)/2):floor(templateSize(2)/2)
                % 计算相关匹配公式的分子 
                if y + l >= 1 && y + l <= imageSize(1) && x + k >= 1 && x + k <= imageSize(2)
                    numerator = numerator + grayImage(y + l, x + k) * grayTemplate(l + floor(templateSize(1)/2) + 1, k + floor(templateSize(2)/2) + 1);
                end
                
                % 计算相关匹配公式的分母的第一个部分
                if y + l >= 1 && y + l <= imageSize(1) && x + k >= 1 && x + k <= imageSize(2)
                    denominator1 = denominator1 + grayImage(y + l, x + k)^2;
                end
                
                % 计算相关匹配公式的分母的第二个部分
                denominator2 = denominator2 + grayTemplate(l + floor(templateSize(1)/2) + 1, k + floor(templateSize(2)/2) + 1)^2;
            end
        end
        
        % 计算相关值
        correlation(y, x) = numerator / (sqrt(denominator1) * sqrt(denominator2));
    end
end

% 设置相关阈值,根据实际情况调整
threshold = 0.95;
%设置成0.95可以检测相关值最高的3个轮子(cat中长的最接近的三个轮子)
%注:第四个轮子只是整体视觉上相似,但细节上很多不一样的地方,所以相关值低,检测不到
%设置成1则可以检测相关性最高的轮子
% 检测目标的坐标
targetCoordinates = [];
disp("检测到的目标的相关值:")
for y = 1:imageSize(1)
    for x = 1:imageSize(2)
       
        if correlation(y, x) >= threshold
             disp(correlation(y, x))
            targetCoordinates = [targetCoordinates; x, y];
        end
    end
end

% 显示相关值结果图像
figure;
imshow(correlation, []);



% 在原图像上绘制检测到的目标位置
figure;
imshow(image);
hold on;

% 绘制检测到的所有目标位置
for i = 1:size(targetCoordinates, 1)
    targetX = targetCoordinates(i, 1);
    targetY = targetCoordinates(i, 2);
    
    % 计算模板的左上角和右下角坐标
    templateTopLeft = [targetX - floor(templateSize(2)/2), targetY - floor(templateSize(1)/2)];
    templateSize = [templateSize(2), templateSize(1)];  % 调整模板大小为 [宽度, 高度]
    
    rectangle('Position', [templateTopLeft, templateSize], 'EdgeColor', 'r', 'LineWidth', 2);
end
% 显示检测到的目标坐标
disp('检测到的目标坐标:');
disp(targetCoordinates);

Correlation value results for images:

Insert image description here

Draw the detected target location on the original image:

Insert image description here

Display detected target coordinates:

Insert image description here

Related code document download

https://download.csdn.net/download/weixin_66397563/88067710

Disclaimer: Please do not reproduce without permission

Guess you like

Origin blog.csdn.net/weixin_66397563/article/details/131828519