Working with template matching of handwritten data input but face some problems for being very new in Matlab. I want to match this template
with this one..
So far i did is:
function result=test(image1,image2)
%*********************************************************
image1=rgb2gray(image1);
image2=rgb2gray(image2);
% check which one is target and which one is template using their size
if size(image1)>size(image2)
Target=image1;
Template=image2;
else
Target=image2;
Template=image1;
end
% find both images sizes
[r1,c1]=size(Target);
[r2,c2]=size(Template);
% mean of the template
image22=Template-mean(mean(Template));
%corrolate both images
M=[];
for i=1:(r1-r2+1)
for j=1:(c1-c2+1)
Nimage=Target(i:i+r2-1,j:j+c2-1);
Nimage=Nimage-mean(mean(Nimage)); % mean of image part under mask
corr=sum(sum(Nimage.*image22));
%warning off
M(i,j)=corr/sqrt(sum(sum(Nimage.^2)));
end
end
% plot box on the target image
result=plotbox(Target,Template,M);
For plotbox..
function result=plotbox(Target,Template,M)
%*********************************************************
[r1,c1]=size(Target);
[r2,c2]=size(Template);
[r,c]=max(M);
[r3,c3]=max(max(M));
i=c(c3);
j=c3;
result=Target;
for x=i:i+r2-1
for y=j
result(x,y)=255;
end
end
for x=i:i+r2-1
for y=j+c2-1
result(x,y)=255;
end
end
for x=i
for y=j:j+c2-1
result(x,y)=255;
end
end
for x=i+r2-1
for y=j:j+c2-1
result(x,y)=255;
end
end
And for testing i use..
% read Template image
im1=imread('C:\Users\Shuvro\Desktop\New folder\1.jpg');
% read Traget Image
im2=imread('C:\Users\Shuvro\Desktop\New folder\2.jpg');
% apply templete matching using power of the image
result1=test(im1,im2);
figure,
subplot(2,2,1),imshow(im1);title('Template');
subplot(2,2,2),imshow(im2);title('Target');
subplot(2,2,3),imshow(result1);title('Matching Result using tmp');
But this code often can't identify that template in the source image,not understanding what wrong was there.Anybody can help?
Basically when i input 2 images to the system,i want to make their height similar.Then i want to measure template image width and then i want to scan the source image according to that width and examine pixel values.When those pixel values of template will match with source image over 70% then i will give the result that it is found,otherwise not found.
This is what i am thinking to do this.Highly appreciated if anybody can help with above code by editing it or giving suggestions.