图像处理|基于边缘的模板匹配

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% %a: model RGB image% %b: target RGB image% %c: output the match image% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function [ c ] = edge_match( a,b) %UNTITLED2 Summary of this function goes here %Detailed explanation goes here %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %用sobel掩膜提取边缘并保留边缘点的梯度方向与ang数组中% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ma=im2double(rgb2gray(a)); %样本图像 mb=im2double(rgb2gray(b)); %模板%水平与竖直方向的sobel掩膜 mask_Ver= fspecial('sobel'); %垂直方向上的掩膜 mask_Lev=mask_Ver'; %水平方向掩膜 %图像在水平方向和垂直方向上对sobel算子的响应 imgSobel_Lev=imfilter(ma,double(mask_Lev),'corr','replicate','same'); imgSobel_Ver=imfilter(ma,double(mask_Ver),'corr','replicate','same'); %imwrite(imgSobel_Ver,'Ver.jpg'); %imwrite(imgSobel_Lev,'Lev.jpg'); %% %计算模板特征点角度 [Row,Col]=size(ma); for i=1:Row for j=1:Col imgEdge(i,j)=sqrt(imgSobel_Ver(i,j)*imgSobel_Ver(i,j)+imgSobel_Lev(i,j)*imgSobel_Lev(i,j)); if(imgSobel_Lev(i,j)==0) temp=0; elseif(imgSobel_Ver(i,j)==0) temp=90.0; else temp =atan(imgSobel_Lev(i,j)/imgSobel_Ver(i,j)); end if(temp<0) temp=180*(pi+temp)/(pi); else temp=180*temp/(pi); end%将角度归为0,45,90,135,四个方向 if((temp<22.5)||(temp>157.5)) ang(i,j)=0; elseif(temp>=22.5&&temp<=67.5) ang(i,j)=45; elseif(temp>67.5&&temp<112.5) ang(i,j)=90; elseif(temp>=112.5&&temp<=157.5) ang(i,j)=135; endend end %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %非最大抑制法% && %孤立点删除% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% avg=0; %边缘点强度的平局值 count=0; %边缘点个数 %摒弃图像边缘点 for i=1:Row for j=1:Col if(i==1 || i==Row || j==1 || j==Col ) imgEdge(i,j)=0; else %最大抑制法 if(imgEdge(i,j)~=0)%是边缘点 switch ang(i,j) case 0 ptLeft=imgEdge(i,j-1); ptRight=imgEdge(i,j+1); case 45 ptLeft=imgEdge(i-1,j+1); ptRight=imgEdge(i+1,j-1); case 90 ptLeft=imgEdge(i-1,j); ptRight=imgEdge(i+1,j); case 135 ptLeft=imgEdge(i-1,j-1); ptRight=imgEdge(i+1,j+1); end %switch if(imgEdge(i,j) Row || CurY> Col) continue; else %相似性判别 targDeriv_Ver=tagSobel_Ver(CurX,CurY); targDeriv_Lev=tagSobel_Lev(CurX,CurY); if((targDeriv_Ver~=0 || targDeriv_Lev~=0)) modDeriv_Ver=ModDerivative_Ver(k); modDeriv_Lev=ModDerivative_Lev(k); targMag=sqrt(targDeriv_Ver*targDeriv_Ver+targDeriv_Lev*targDeriv_Lev); if(targMag~=0) targMag=1/targMag; end; %余弦向量相似性度量 patialSum =patialSum +((modDeriv_Ver*targDeriv_Ver) +... (modDeriv_Lev*targDeriv_Lev))*(Model_mag(k)*targMag); end %退出判断 thres=[(minScore-1)+normGreediness*k,normMinScore*k]; threshold=min(thres); score=patialSum; if(score=BestScore) BestScore=score; location(1)=i; location(2)=j; end%if(score>=BestScore) end%for j=1:Col end%for i=1:Row %% %模板在目标图像中的坐标 [Row,Col]=size(ma); countt=1; location(1); location(2); for i=1:Row; for j=1:Col; if(imgEdge(i,j)~=0) x1(countt)=location(1)+i-gravity_x; y1(countt)=location(2)+j-gravity_y; countt=countt+1; end end end %% %画出输出图像 figure hold on; image(b); plot(y1,x1,'o','LineWidth',2,...%设置圆圈的线粗 'MarkerEdgeColor','r',...%边界设置为黑色 'MarkerFaceColor','r',...%内部设置白色 'MarkerSize',2)%大小设置 hold off; c=b; %%%%%%%最后一个end end

【图像处理|基于边缘的模板匹配】
模板:
图像处理|基于边缘的模板匹配
文章图片

目标:
图像处理|基于边缘的模板匹配
文章图片

结果:
图像处理|基于边缘的模板匹配
文章图片

    推荐阅读