1

作为我作业的一部分,我需要实现这种模式匹配。目标是“在图像 coin4.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 功能,我需要做的只是循环最佳阈值,对吗?
还是我需要模糊图像或图案?

这是我试图找到的模式

这是图像和结果

4

1 回答 1

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
于 2013-02-08T21:06:29.653 回答