2013-01-18 29 views
1

作爲我家庭作業的一部分,我需要實現這種模式匹配。 的目標是「在圖像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 


這是我的模式匹配功能。

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零模式possiable。

我試圖瞭解,如果有東西wr在PatternMatching函數中使用我的算法。由於NGCpm函數已經提供給我,所有我需要做的只是循環的最佳閾值,是正確的?
還是我需要模糊圖像或模式?

this is the pattern i'm trying to find

this is the image and the results

+1

作業的實際目的是什麼?爲了表明互相關不足以解決這個問題?你顯然從10美分硬幣中獲得了這種模式,這就是爲什麼你有最好的匹配。如果你想使用這種方法可能匹配其他硬幣中的「0」,則需要爲模板和輸入圖像以某種方式獲得非常好的二值圖像(或同樣非常清晰的灰度圖像)。 – mmgp

+0

@mmgp你好朋友。目標是「在圖像coins4.tif中檢測儘可能多的0(零)」,以便獲得我剛裁剪圖像的圖案。並從10美分硬幣中取零。但我不明白這將如何幫助我找到50美分的零,例如更大的零,使用這種技術... – Gilad

+1

函數res = NGC_PM(im,pattern)被給出。也許我沒有正確使用它? – Gilad

回答

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