arguments in function

어떤 변수나 list에 정보를 추가하면서 재귀함수를 실행하려고,

함수외부에 global 변수를 선언하여 실행하였으나 코드가 지저분하고 찝찝해 했는데,

아래 예제와 같이 변수 선언을 함수의 argument에 하면 쉽게 해결된다.

함수 argument

local nonlocal global

local은  밖에 나가면 효과없고,

nonlocal은 자기 구역내에서만 영향력이 있고

global은 모듈 전체에 영향력 있다.

 

 Scopes and Namespaces Example

scope

Global, Local and nonlocal Variables

scope global

 

아래 예제 둘다 비슷한것 같지만 미묘하게 다르다.

첫번째는 변수가 모듈에서 정의되었고, 함수안에서 global로 바꿨으니 다 적용되는거고

두번째 예제는 변수가 함수 안에서 정의되었다. 그래서 global로 바꿔도 아직 함수내에선 이전 변수내용이 그대로 적용되고 함수를 빠져나와서 global이 적용되었다.

 

matchTemplate(), minMaxLoc()

Template Matching tutorials

 

matchTemplate()

 

minMaxLoc

링크

Template Matching

Goal

In this tutorial you will learn how to:

  • Use the OpenCV function matchTemplate to search for matches between an image patch and an input image
  • Use the OpenCV function minMaxLoc to find the maximum and minimum values (as well as their positions) in a given array.

Theory

What is template matching?

Template matching is a technique for finding areas of an image that match (are similar) to a template image (patch).

How does it work?

  • We need two primary components:

    1. Source image (I): The image in which we expect to find a match to the template image
    2. Template image (T): The patch image which will be compared to the template image

    our goal is to detect the highest matching area:

    ../../../../../_images/Template_Matching_Template_Theory_Summary.jpg

  • To identify the matching area, we have to compare the template image against the source image by sliding it:

    ../../../../../_images/Template_Matching_Template_Theory_Sliding.jpg

  • By sliding, we mean moving the patch one pixel at a time (left to right, up to down). At each location, a metric is calculated so it represents how “good” or “bad” the match at that location is (or how similar the patch is to that particular area of the source image).

  • For each location of T over I, you store the metric in the result matrix (R). Each location (x,y) in R contains the match metric:

    ../../../../../_images/Template_Matching_Template_Theory_Result.jpgthe image above is the result R of sliding the patch with a metric TM_CCORR_NORMED. The brightest locations indicate the highest matches. As you can see, the location marked by the red circle is probably the one with the highest value, so that location (the rectangle formed by that point as a corner and width and height equal to the patch image) is considered the match.

  • In practice, we use the function minMaxLoc to locate the highest value (or lower, depending of the type of matching method) in the R matrix.