matlab - Matlab中的手写字符模板匹配

标签 matlab image-processing template-matching

使用手写数据输入的模板匹配,但在 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);

对于绘图盒..

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% 然后我将给出找到的结果,否则找不到。
这就是我想做的。如果有人可以通过编辑或提供建议来帮助处理上述代码,我将不胜感激。

最佳答案

首先我想警告你,size(image1)>size(image2) 是一个矢量比较,通常你不会想那样做。 (可能使用 allany)。

话虽这么说:

在这种特定情况下,弄清楚为什么您的代码没有按照您的预期执行操作的唯一方法是加载它应该匹配但没有匹配的输入。然后逐行执行代码,直到看到任何意外行为。


当然你也可以尝试搜索 matlab 的模式匹配函数,应该有一些你可以在 google 上找到,或者甚至可以在 stackoverflow 上找到。

关于matlab - Matlab中的手写字符模板匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16574569/

相关文章:

Python OpenCV - 一个模板匹配函数

python - 如何使用 matchTemplate 基于共同特征对齐两个图像

matlab - `GLIBCXX_3.4.11' 未找到,从链接到 glibc 的 MATLAB 运行系统调用不同于 matlab bin 路径中的内容

math - 二维离散函数上多线积分的快速算法

Java 图像连接组件

python - 如何在模板匹配中使用 cv2.minMaxLoc()

matlab - 在matlab中改变冲浪比例而不改变绘图

android - 有没有办法在 android studio 中使用 SVM 模型(在 matlab 中训练)?

matlab - isequal() 和 == 用于比较矩阵无法正常工作 matlab

android - 在react-native中裁剪后的图像非常小