Matlab中手写字符模板匹配

4

我正在处理手写数据输入的模板匹配,但由于我在Matlab中很新,所以遇到了一些问题。我想要将这个模板
enter image description here
与这个进行匹配..
enter image description here

到目前为止,我所做的是:

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);

对于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

我用以下工具进行测试...

% 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');

但是这段代码经常无法在源图像中识别出模板,不知道哪里出了问题。有人能帮忙吗?
基本上当我将2个图像输入系统时,我想让它们的高度类似。然后我想测量模板图像的宽度,然后根据该宽度扫描源图像并检查像素值。当模板的像素值与源图像匹配超过70%时,我就会给出结果说明找到了,否则找不到。
这就是我想要做的事情。非常感谢如果有人可以通过编辑或提供建议来帮助上述代码。

1
你可能应该看一下图像配准。 - Omar Wagih
1个回答

0

首先,我想警告你,size(image1)>size(image2)是一个向量比较,通常你不会想这样做。(也许使用allany)。

话虽如此:

在这种特定情况下,找出代码为什么不能按照你的期望工作的唯一方法是加载应该匹配但却没有匹配的输入。然后逐行调试代码,直到看到任何意外行为。


当然,你也可以尝试在Matlab中搜索模式匹配函数,你可以在谷歌上找到一些,或者甚至在stackoverflow上找到。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接