在Matlab中使用模板匹配查找相交点

时间:2018-07-09 15:45:23

标签: template-matching

我正在尝试执行模板匹配,以找到棋盘上的所有交点(仅黑白)。 我要使用的图像是:3cross2.png(背景),10by10.jpg(模板);(附件)。

对于背景,我使用了值:Lion

我使用了代码:(问题是我只能得到一个点,我如何检测所有点)

      clc, clear all, close all 
      % Read Lion
      lion = imread('3cross2.png');
      lion =rgb2gray(lion);
      % imshow(lion); figure() 

      % Read Template 
      Template = imread('10by10t.jpg');
      template = rgb2gray(Template);
      % imshow(template)

      [RowLionT, ColLionT] = size(template) 
      sprintf('The size of letter is %dx%d.',RowLionT,ColLionT)

      [RowLion, ColLion] = size(lion) 
      sprintf('The size of letter is %dx%d.',RowLion,ColLion)

      cc=normxcorr2(template,lion);
      [val, idx] = max(cc);
      [RowCC, ColCC] = size(cc) 
      sprintf('The size of letter is %dx%d.',RowCC,ColCC)
      imshow(cc) 

      [max_cc,imax] = max(abs(cc(:))); 
      sprintf('The the highest correlation value is %f.',max_cc)

      [ypeak,xpeak] = ind2sub(size(cc),imax(1)); 
      sprintf('the row col coords of the best match in the cross corr matrix are %dx%d.',ypeak,xpeak) 

      BestRow = ypeak - (RowLionT-1); 
      BestCol = xpeak - (ColLionT-1); 
      sprintf('The Row Col coordinates of the best match in orig matrix are %d %d.',BestCol,BestRow)
      figure,imshow(lion);

      hold on
      o = plot((BestCol+((ColLionT-1)/2)), (BestRow+((RowLionT-1)/2)), 'ro', 'MarkerSize', 10);

0 个答案:

没有答案