模式匹配 - 归一化相关

时间:2013-01-18 23:17:18

标签: image matlab image-processing

作为我作业的一部分,我需要实现这种模式匹配。 我们的目标是“在图像coins4.tif中检测尽可能多的0(零)。”
我获得了NGC功能。我需要使用它
这是我的main.m文件

Image = readImage('coins4.tif');
Pattern = readImage('zero.tif');
showImage(Image);
showImage(Pattern);
message = sprintf('Pattern matching Normalized Correlation');
PatternMatching(Image , Pattern);
uiwait(msgbox(message,'Done', 'help'));
close all

这是我的PatternMatching功能。

function [ output_args ] = PatternMatching( Image , Pattern )
% Pattern matching – Normalized Correlation
% Detect as many of the 0's (zeros) as you can in image coins4.tif.
% Use the 0 of the 10 coin as pattern.
% Use NGC_pm and find good threshold. Display original image with? detected regions marked using drawRect.

% NGCpm(im,pattern);
% drawRect(rectCoors,color);
% rectCoors = [r0,c0,rsize,csize]  - r0,c0 = top-left corner of rect.
%             rsize = number of rows, csize = number of cols
%
% color = an integer >=1 representing a color in the color wheel
%                   (curerntly cycles through 8 different colors

showImage(Image);
hold on
res = NGCpm(Image, Pattern);

 for i = 1:size(res,1)
     for j = 1:size(res,2)
         if res(i,j) > 0.9999
             drawRect([i j size(Pattern,1) size(Pattern,2)], 5)
         end
     end
 end

end

这是给定的NGCpm.m文件

function res=NGC_PM(im,pattern)
[n m]=size(pattern);
[im_row,im_col]=size(im);
if ~(var(pattern(:))==0)
     res = normxcorr2(pattern, im);
     res=res(n:im_row,m:im_col);   
else
    res=zeros(size(im)-size(pattern)+1);
end;
 res = 1-abs(res);  % res = abs(res);


这是我试图找到的模式和结果,我得到了

我正在尝试使用硬币10的零模式找到尽可能多的“零”。

我想了解PatternMatching函数中的算法是否有问题。由于NGCpm功能已经给我,我需要做的只是循环的最佳阈值,对吗?
或者我需要模糊图像或图案吗?

this is the pattern i'm trying to find

this is the image and the results

1 个答案:

答案 0 :(得分:2)

这是此功能的固定版本。

function [ output_args ] = patternMatching( Image , Pattern )
% Pattern matching – Normalized Correlation
% Detect as many of the 0's (zeros) as you can in image coins4.tif.
% Use the 0 of the 10 coin as pattern.
% Use NGC_pm and find good threshold. Display original image with? detected regions marked using drawRect.

% NGCpm(im,pattern);
% drawRect(rectCoors,color);
% rectCoors = [r0,c0,rsize,csize]  - r0,c0 = top-left corner of rect.
%             rsize = number of rows, csize = number of cols
%
% color = an integer >=1 representing a color in the color wheel
%                   (curerntly cycles through 8 different colors

showImage(Image);
hold on
res = 1-NGCpm(Image, Pattern);
normalized_corellation = uint8(255*res/max(max(res)));
res_thresh = thresholdImage(normalized_corellation,100);
 for i = 1:size(res_thresh,1)
     for j = 1:size(res_thresh,2)
         if res_thresh(i,j) > 0
             drawRect([i j size(Pattern,1) size(Pattern,2)], 5)
         end
     end
 end

end