템플릿 매칭

https://drive.google.com/uc?id=1qJsnIUVp23jODXlw2rNbvn3q8qnFK-Ia

TemplateMatchModes 설명
TM_SQDIFF 제곱차 매칭 방법
$R(x, y) = \sum_{x', y'}(T(x', y') - I(x+x', y+y'))^{2}$
TM_SQDIFF_NORMED 정규화된 제곱차 매칭 방법
$R(x, y) = { \sum_{x', y'}(T(x', y') - I(x+x', y+y'))^{2} \over \sqrt{\sum_{x', y'}T(x', y')^{2} \cdot \sum_{x', y'}I(x+x', y+y')^{2}} }$
TM_CCORR 상관관계 매칭 방법
$R(x, y) = \sum_{x', y'}T(x', y') \cdot I(x+x', y+y')$
TM_CCORR_NORMED 정규화된 상관관계 매칭 방법
$R(x, y) = { \sum_{x', y'}T(x', y') \cdot I(x+x', y+y') \over \sqrt{ \sum_{x', y'}T(x', y')^{2} \cdot I(x+x', y+y')^{2}}}$
TM_CCOEFF 상관계수 매칭 방법
$R(x, y) = \sum_{x', y'}T'(x', y') \cdot I'(x+x', y+y')$
$T'(x', y') = T(x', y') - {1 \over w \cdot h} \cdot \sum_{x'', y''}T'(x'', y'')$
$I'(x+x', y+y') = I(x+x', y+y') - {1 \over w \cdot h} \cdot \sum_{x'', y''} I(x+x'', y+y'')$
TM_CCOEFF_NORMED 정규화된 상관계수 매칭 방법
$R(x, y) = { \sum_{x', y'}T'(x', y') \cdot I'(x+x', y+y') \over \sqrt{\sum_{x', y'}T'(x', y')^{2} \cdot \sum_{x',y'}I'(x+x', y+y')^{2}} }$
void template_matching()
{
  Mat img = imread("circuit.bmp", IMREAD_COLOR);
  Mat templ = imread("crystal.bmp", IMREAD_COLOR);

  if (img.empty() || templ.empty())
  {
    cerr << "Image load failed!" << endl;
    return;
  }

  img = img + Scalar(50, 50, 50);

  Mat noise(img.size(), CV_32SC3);
  randn(noise, 0, 10);
  add(img, noise, img, Mat(), CV_8UC3);

  Mat res, res_norm;
  matchTemplate(img, templ, res, TM_CCOEFF_NORMED);
  normalize(res, res_norm, 0, 255, NORM_MINMAX, CV_8U);

  double maxv;
  Point maxloc;
  minMaxLoc(res, 0, &maxv, 0, &maxloc);
  cout << "maxv: " << maxv << endl;

  rectangle(img, Rect(maxloc.x, maxloc.y, templ.cols, templ.rows), Scalar(0, 0, 255), 2);

  imshow("templ", templ);
  imshow("res_norm", res_norm);
  imshow("img", img);

  waitKey(0);
  destroyAllWindows();
}

캐스게이드 분류기와 얼굴 검출

https://drive.google.com/uc?id=1Gh35-ZbQqRjn8pdwtxSx8RBz40Ktbq6w