%使用归一化相关函数进行配准
%Step 1: Read Images
lily = imread('lily.tif');
flowers = imread('flowers.tif');
figure, imshow(lily)
figure, imshow(flowers)
%Step 2: Choose Subregions of Each Image
% non-interactively
rect_lily = [93 13 81 69];
rect_flowers = [190 68 235 210];
sub_lily = imcrop(lily,rect_lily);
sub_flowers = imcrop(flowers,rect_flowers);
% OR
% interactively
% [sub_lily,rect_lily] = imcrop(lily); % choose one petal of lily
% [sub_flowers,rect_flowers] = imcrop(flowers); % choose whole lily
% display sub images
figure, imshow(sub_lily)
figure, imshow(sub_flowers)
%Step 3: Do Normalized Cross-correlation and Find Coordinates of Peak
c = normxcorr2(sub_lily(:,:,1),sub_flowers(:,:,1));
figure, surf(c), shading flat
%Step 4: Find the Total Offset Between the Images
% offset found by correlation
[max_c, imax] = max(abs(c(:)));
[ypeak, xpeak] = ind2sub(size(c),imax(1));
% corr_offset = [(xpeak-size(sub_lily,2))
% (ypeak-size(sub_lily,1))];
%相关运算,参考点为模板右下角(因为进行了翻转),实际参考点应该为模板左上角
corr_offset = [(xpeak-size(sub_lily,2)+1)
(ypeak-size(sub_lily,1)+1)];
% relative offset of position of subimages
rect_offset = [(rect_flowers(1)-rect_lily(1))
(rect_flowers(2)-rect_lily(2))];
% total offset
offset = corr_offset + rect_offset;
xoffset = offset(1);
yoffset = offset(2);
%Step 5: See if the Image Lily Was Extracted from the Image Flowers
% xbegin = xoffset+1;
% xend = xoffset+ size(lily,2);
% ybegin = yoffset+1;
% yend = yoffset+size(lily,1);
%此时偏移点已经是图像左上角
xbegin = xoffset;
xend = xoffset+ size(lily,2)-1;
ybegin = yoffset;
yend = yoffset+size(lily,1)-1;
% extract region from flowers and compare to lily
extracted_lily = flowers(ybegin:yend,xbegin:xend,:);
if isequal(lily,extracted_lily)
disp('lily.tif was extracted from flowers.tif')
end
%Step 6: Pad the Lily Image to the Size of the Flowers Image
recovered_lily = uint8(zeros(size(flowers)));
recovered_lily(ybegin:yend,xbegin:xend,:) = lily;
figure, imshow(recovered_lily)
%Step 7: Transparently Overlay Lily Image on Flowers Image
[m,n,p] = size(flowers);
mask = ones(m,n);
i = find(recovered_lily(:,:,1)==0);
mask(i) = .2; % try experimenting with different levels of transparency
% overlay images with transparency
figure, imshow(flowers(:,:,1)) % show only red plane of flowers
hold on
h = imshow(recovered_lily); % overlay recovered_lily
set(h,'AlphaData',mask)
matlab图像模版匹配程序
5星 · 超过95%的资源 需积分: 9 88 浏览量
2012-07-07
20:03:14
上传
评论
收藏 435KB RAR 举报
小娃娃妮
- 粉丝: 0
- 资源: 8
最新资源
- 一个chm格式的 SQL 函数手册-SQL语言手册文档
- 计算当前月份的天数和剩余天数
- 基于ARM的指令调度和延迟分支
- 基于Vue和TypeScript的极简聊天应用设计源码 - HasChat
- 基于Vue2全家桶和Zcool数据的图片收集网站设计源码 - cool-picture
- 基于C和C++的二维绘制工具设计源码 - DrawPro
- Object.defineProperty 的 IE 补丁object-defineproperty-ie-master.zip
- 整卷预览.mhtml
- MySQL是一种广泛使用的开源关系型数据库管理系统,它提供了丰富的SQL语句用于数据库的创建、查询、更新和管理 以下是一些常见的
- MySQL是一种广泛使用的开源关系型数据库管理系统,它提供了丰富的SQL语句用于数据库的创建、查询、更新和管理 以下是一些常见
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈