[python] テンプレートマッチングで使う相互相関関数(scipy: correlate2d)で画像が一致するかどうかを確認

stachoverflowにあったこれ(http://stackoverflow.com/questions/1819124/image-comparison-algorithm)をなんとなく解説。

 
実装はこんな感じ↓
 

<br />
# # -*- coding: utf-8 -*<br />
  import scipy as sp<br />
  from scipy.misc import imread<br />
  from scipy.signal.signaltools import l as c2d</p>
<p>  def get(i):<br />
   &amp;nbsp; &amp;nbsp;#get JPG image as Scipy array, RGB (3 layer)<br />
   &amp;nbsp; &amp;nbsp;data = imread(&quot;%s.jpg&quot; % i)<br />
   &amp;nbsp; &amp;nbsp;# convert to grey-scale using W3C luminance calc<br />
   &amp;nbsp; &amp;nbsp;data = sp.inner(data,[299,587,144]) / 1000.0<br />
   &amp;nbsp; &amp;nbsp;#normarilze Cross-correlation<br />
   &amp;nbsp; &amp;nbsp;return (data - data.mean()) / data.std()<br />
   &amp;nbsp; &amp;nbsp;#numpy.mean 配列内にある数値データの算術平均を求める<br />
   &amp;nbsp; &amp;nbsp;#numpy.std 配列内にある数値データの標準偏差を求める</p>
<p>  if __name__ == '__main__':<br />
   &amp;nbsp; &amp;nbsp;img1 = get(1)<br />
   &amp;nbsp; &amp;nbsp;img2 = get(2)<br />
   &amp;nbsp; &amp;nbsp;img3 = get(3)</p>
<p>  c11 = c2d(img1, img1, mode='same')<br />
  c12 = c2d(img1, img2, mode='same')<br />
  c13 = c2d(img1, img3, mode='same')<br />
  c23 = c2d(img2, img3, mode='same')</p>
<p>  print(c11.max())<br />
  print(c12.max())<br />
  print(c13.max())<br />
  print(c23.max())<br />