基于相关系数的图像匹配
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
% --------------------------------------------------------
% Demo: Template Matching using Correlation Coefficients
% By Yue Wu (Rex)
% Department of Electrical and Computer Engineering
% Tufts University
% Medford, MA
% 08/30/2010
% --------------------------------------------------------
clear all
close all
%% Prepare the image for analysis
F = imread('coins.png'); % read in coins image
T = imread('templateCoin.png'); % read in template image
%% display frame and template
figure, subplot(121),imshow(F),title('Gray Coins Image');
subplot(122),imshow(T),title('Coin Template');
%% correlation matching
[corrScore, boundingBox] = corrMatching(F,T);
%% show results
figure,imagesc(abs(corrScore)),axis image, axis off, colorbar,
title('Corr Measurement Space')
bY = [boundingBox(1),boundingBox(1)+boundingBox(3),boundingBox(1)+boundingBox(3),boundingBox(1),boundingBox(1)];
bX = [boundingBox(2),boundingBox(2),boundingBox(2)+boundingBox(4),boundingBox(2)+boundingBox(4),boundingBox(2)];
figure,imshow(F),line(bX,bY),title('Detected Area');
function [corrScore, boundingBox] = corrMatching(frameImg, templateImg, threshC)
% -------------------------------------------------------------------------
% Function corrMatching: Template Matching using Correlation Coefficients
% Inputs:
% frameImg = gray or color frame image
% templateImg = gray or color template image
% threshC = threshold of rejecting detected region (default = .75)
% e.g. if the detected region has a corrCoef>threshC
% then the algorithm accepts it as a detection,
% otherwise rejects it as a false alarm.
% Output:
% corrScore = 2D matrix of correlation coefficients
% boundingBox = [upperLeftPixel.y upperLeftPixel.x height width]
%
% -------------------------------------------------------------------------
% By Yue Wu (Rex)
% Department of Electrical and Computer Engineering
% Tufts University
% Medford, MA
% 08/30/2010
% -------------------------------------------------------------------------
%% 1. initialization
if size(frameImg,3) ~=1
frameGray = rgb2gray(frameImg);
else
frameGray = frameImg;
end
frameGray = double(frameGray);
if size(templateImg,3) ~=1
templateGray = rgb2gray(templateImg);
else
templateGray = templateImg;
end
templateGray = double(templateGray);
[templateHeight,templateWidth] = size(templateGray);
%% 2. correlation calculation
frameMean = conv2(frameGray,ones(size(templateGray))./numel(templateGray),'same');
templateMean = mean(templateGray(:));
corrPartI = conv2(frameGray,fliplr(flipud(templateGray-templateMean)),'same')./numel(templateGray);
corrPartII = frameMean.*sum(templateGray(:)-templateMean);
stdFrame = sqrt(conv2(frameGray.^2,ones(size(templateGray))./numel(templateGray),'same')-frameMean.^2);
stdTemplate = std(templateGray(:));
corrScore = (corrPartI-corrPartII
)./(stdFrame.*stdTemplate);
%% 3. finding most likely region
[maxVal,maxIdx] = max(corrScore(:));
[maxR, maxC] = ind2sub([size(corrScore,1),size(corrScore,2)],maxIdx);
%% 4. hypothesis test
if ~exist('threshC','var')
threshC = .75;
end
if maxVal>=threshC
boundingBox(1,:) = [max(1,maxR-round(templateHeight/2)), max(1,maxC-round(templateWidth/2)), templateHeight, templateWidth];
else
boundingBox(1,:) = [];
end
/matlabcentral/fileexchange/28590-template-matching-using-correlation-coefficients/content/corrMatching.m