{"action":"created","issue":{"url":"https://api.github.com/repos/opencv/opencv/issues/5688","repository_url":"https://api.github.com/repos/opencv/opencv","labels_url":"https://api.github.com/repos/opencv/opencv/issues/5688/labels{/name}","comments_url":"https://api.github.com/repos/opencv/opencv/issues/5688/comments","events_url":"https://api.github.com/repos/opencv/opencv/issues/5688/events","html_url":"https://github.com/opencv/opencv/issues/5688","id":118077069,"number":5688,"title":"Unexpected result in matchTemplate for TM_CCOEFF_NORMED","user":{"login":"PkLab","id":15944968,"avatar_url":"https://avatars.githubusercontent.com/u/15944968?v=3","gravatar_id":"","url":"https://api.github.com/users/PkLab","html_url":"https://github.com/PkLab","followers_url":"https://api.github.com/users/PkLab/followers","following_url":"https://api.github.com/users/PkLab/following{/other_user}","gists_url":"https://api.github.com/users/PkLab/gists{/gist_id}","starred_url":"https://api.github.com/users/PkLab/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/PkLab/subscriptions","organizations_url":"https://api.github.com/users/PkLab/orgs","repos_url":"https://api.github.com/users/PkLab/repos","events_url":"https://api.github.com/users/PkLab/events{/privacy}","received_events_url":"https://api.github.com/users/PkLab/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/opencv/opencv/labels/bug","name":"bug","color":"fc2929"},{"url":"https://api.github.com/repos/opencv/opencv/labels/category:%20imgproc","name":"category: imgproc","color":"ededed"},{"url":"https://api.github.com/repos/opencv/opencv/labels/priority:%20normal","name":"priority: normal","color":"A38700"}],"state":"open","locked":false,"assignee":{"login":"snosov1","id":2784626,"avatar_url":"https://avatars.githubusercontent.com/u/2784626?v=3","gravatar_id":"","url":"https://api.github.com/users/snosov1","html_url":"https://github.com/snosov1","followers_url":"https://api.github.com/users/snosov1/followers","following_url":"https://api.github.com/users/snosov1/following{/other_user}","gists_url":"https://api.github.com/users/snosov1/gists{/gist_id}","starred_url":"https://api.github.com/users/snosov1/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/snosov1/subscriptions","organizations_url":"https://api.github.com/users/snosov1/orgs","repos_url":"https://api.github.com/users/snosov1/repos","events_url":"https://api.github.com/users/snosov1/events{/privacy}","received_events_url":"https://api.github.com/users/snosov1/received_events","type":"User","site_admin":false},"assignees":[{"login":"snosov1","id":2784626,"avatar_url":"https://avatars.githubusercontent.com/u/2784626?v=3","gravatar_id":"","url":"https://api.github.com/users/snosov1","html_url":"https://github.com/snosov1","followers_url":"https://api.github.com/users/snosov1/followers","following_url":"https://api.github.com/users/snosov1/following{/other_user}","gists_url":"https://api.github.com/users/snosov1/gists{/gist_id}","starred_url":"https://api.github.com/users/snosov1/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/snosov1/subscriptions","organizations_url":"https://api.github.com/users/snosov1/orgs","repos_url":"https://api.github.com/users/snosov1/repos","events_url":"https://api.github.com/users/snosov1/events{/privacy}","received_events_url":"https://api.github.com/users/snosov1/received_events","type":"User","site_admin":false}],"milestone":{"url":"https://api.github.com/repos/opencv/opencv/milestones/9","html_url":"https://github.com/opencv/opencv/milestones/3.2","labels_url":"https://api.github.com/repos/opencv/opencv/milestones/9/labels","id":1619837,"number":9,"title":"3.2","description":"","creator":{"login":"mshabunin","id":3304494,"avatar_url":"https://avatars.githubusercontent.com/u/3304494?v=3","gravatar_id":"","url":"https://api.github.com/users/mshabunin","html_url":"https://github.com/mshabunin","followers_url":"https://api.github.com/users/mshabunin/followers","following_url":"https://api.github.com/users/mshabunin/following{/other_user}","gists_url":"https://api.github.com/users/mshabunin/gists{/gist_id}","starred_url":"https://api.github.com/users/mshabunin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mshabunin/subscriptions","organizations_url":"https://api.github.com/users/mshabunin/orgs","repos_url":"https://api.github.com/users/mshabunin/repos","events_url":"https://api.github.com/users/mshabunin/events{/privacy}","received_events_url":"https://api.github.com/users/mshabunin/received_events","type":"User","site_admin":false},"open_issues":28,"closed_issues":18,"state":"open","created_at":"2016-03-03T11:08:38Z","updated_at":"2016-07-18T15:53:43Z","due_on":null,"closed_at":null},"comments":11,"created_at":"2015-11-20T16:32:32Z","updated_at":"2016-07-18T18:52:45Z","closed_at":null,"body":"When the template is flat (all pixels have same value) the result of TM_CCOEFF_NORMED is 0/0 but matchTemplate returns 1.\r\nLooking at [the formula](http://docs.opencv.org/3.0.0/df/dfb/group__imgproc__object.html#ga3a7850640f1fe1f58fe91a2d7583695d), `T` is the template:\r\n`T'(x,y) = T(x,y) - mean(T)`\r\n\r\nIf T(x,y)=C for each x,y than mean(T)=C and you will have \r\n`T'(x,y) = C - C = 0` for each x,y\r\n\r\nthe formula will be:\r\n`R = 0 * I / sqrt(0 * I^2)` that is 0/0 !=1 \r\n\r\nThis is true even if the Image patch under running template is constant.\r\n\r\nIn this case matchTemplate should returns undefined even if 0 has more sense\r\n\r\nhere is [the issue](https://github.com/opencv/opencv/blob/3.1.0/modules/imgproc/src/templmatch.cpp#L931)\r\nhere is simple test code:\r\n```cpp\r\nMat img,templ;\r\nScalar C = Scalar::all(64); //some rand number\r\nimg = imread(\"some_image.png\");\r\ntempl = C + Mat::zeros(10,10,img.type());  // flat template\r\nint result_cols = img.cols - templ.cols + 1;\r\nint result_rows = img.rows - templ.rows + 1;\r\nresultMatching.create(result_rows, result_cols, CV_32FC1);\r\n// TM_CCOEFF returns all 0 as expected\r\nmatchTemplate(img, templ, resultMatching, TM_CCOEFF);\r\n// TM_CCOEFF returns all 1 despite of it has sam numerator as TM_CCOEFF\r\nmatchTemplate(img, templ, resultMatching, TM_CCOEFF_NORMED);\r\n```"},"comment":{"url":"https://api.github.com/repos/opencv/opencv/issues/comments/233422896","html_url":"https://github.com/opencv/opencv/issues/5688#issuecomment-233422896","issue_url":"https://api.github.com/repos/opencv/opencv/issues/5688","id":233422896,"user":{"login":"PkLab","id":15944968,"avatar_url":"https://avatars.githubusercontent.com/u/15944968?v=3","gravatar_id":"","url":"https://api.github.com/users/PkLab","html_url":"https://github.com/PkLab","followers_url":"https://api.github.com/users/PkLab/followers","following_url":"https://api.github.com/users/PkLab/following{/other_user}","gists_url":"https://api.github.com/users/PkLab/gists{/gist_id}","starred_url":"https://api.github.com/users/PkLab/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/PkLab/subscriptions","organizations_url":"https://api.github.com/users/PkLab/orgs","repos_url":"https://api.github.com/users/PkLab/repos","events_url":"https://api.github.com/users/PkLab/events{/privacy}","received_events_url":"https://api.github.com/users/PkLab/received_events","type":"User","site_admin":false},"created_at":"2016-07-18T18:52:45Z","updated_at":"2016-07-18T18:52:45Z","body":"Issue happens if template **OR** image region under template is flat. Thus tests should be performed with both cases.  Because `img_region` is a running ROI  a \"flat test\" would require to calculate additional N stdev or similar. N=result.Size();\r\n\r\nBTW snosov1's implementation avoid the \"flat test\" and should works fine with CCOEFF_NORMED  if `template AND img_region >> epslion` but still produce nonsense result with CCOEFF (if region or template is flat).\r\n\r\nI think that @vpisarev  is talking about regression tests.  @snosov1 why you don't prepare a branch on your fork including adequate test cases for https://github.com/opencv/opencv/blob/master/modules/imgproc/test/test_templmatch.cpp. This would help us to go on."}}
{"actor":{"display_login":"PkLab"}}