資源簡介
本資源是通過模板匹配,實現圖像的匹配功能。

代碼片段和文件信息
%模板匹配
function?[maxHeightmaxWidthMaxR]?=?templateMatcher(im_matchedim_template)
if??isrgb(im_matched)
????im_matched?=?rgb2gray(im_matched);
end
if??isrgb(im_template)
????im_template?=?rgb2gray(im_template);
end
dim_matched?=double(im_matched);
dim_template?=?double(im_template);
%方差
?sigmaST?=?0.0;
?sigmaS?=?0.0;
?sigmaT?=?0.0;
%相似性測度
%double?R
%最大相似性測度
?MaxR?=?0.0;
%最大相似性出現位置
?maxWidth=0;
?maxHeight=0;
%計算sigmaT
[tempheighttempwidth]?=?size(im_template);
for?n?=1:tempheight
???for?m?=1:tempwidth
???????sigmaT?=?sigmaT?+?dim_template(nm)^2;
???end
end
sigmaT?=?sqrt(sigmaT);
%找到圖像中最大相似性的出現位置
[heightwidth]?=?size(im_matched);
%?width?=?size(im_matched2);
for?j?=1:height-tempheight
????for?i?=1:width-tempwidth
????????sigmaST?=?0;
????????sigmaS?=?0;
????????for?n?=?1:tempheight
????????????for?m?=?1:tempwidth
?????????????sigmaS?=?sigmaS?+??dim_matched(j+ni+m)^2;
?????????????sigmaST?=?sigmaST?+?dim_template(nm)*dim_matched(j+ni+m);
?????????????end
????????end
????????
?????%計算相似性
?????sigmaS?=?sqrt(sigmaS);
?????R?=?sigmaST?/(sigmaS*sigmaT);
?????%與最大相似性比較
?????if(R?>MaxR)
?????????MaxR?=?R;
?????????maxWidth?=?i+1;
?????????maxHeight?=j+1;
?????end
????end
end
recWidth?=?[maxWidth?maxWidth+tempwidth-1?maxWidth+tempwidth-1?maxWidth?maxWidth];
recHeight?=?[maxHeight?maxHeight?maxHeight+tempheight-1?maxHeight+tempheight-1?maxHeight];
??figureimshow(im_matched);hold?on??plot(recWidthrecHeight‘r-‘);???
????????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????1574??2007-07-07?11:03??templateMatcher.m
-----------?---------??----------?-----??----
?????????????????1792????????????????????2
評論
共有 條評論