{"id":2509,"date":"2011-11-25T17:13:57","date_gmt":"2011-11-25T08:13:57","guid":{"rendered":"http:\/\/www.moonmile.net\/blog\/archives\/2509"},"modified":"2011-11-25T17:14:53","modified_gmt":"2011-11-25T08:14:53","slug":"%e7%9b%b8%e9%96%a2%e4%bf%82%e6%95%b0%e3%82%92%e8%a8%88%e7%ae%97%e3%82%92%e8%87%aa%e4%bd%9c%e3%81%99%e3%82%8b%e9%80%94%e4%b8%ad","status":"publish","type":"post","link":"http:\/\/www.moonmile.net\/blog\/archives\/2509","title":{"rendered":"\u76f8\u95a2\u4fc2\u6570\u3092\u8a08\u7b97\u3092\u81ea\u4f5c\u3059\u308b(\u9014\u4e2d)"},"content":{"rendered":"<p>\n\u7d50\u5c40\u3001\u81ea\u4f5c\u3057\u3066\u307f\u308b&#8230; templmatch.cpp \u5185\u306b\u3042\u308b crossCorr \u95a2\u6570\u3067\u76f8\u95a2\u5e73\u5747\u3092\u8a08\u7b97\u3057\u3066\u3044\u308b\u306e\u3060\u304c\u3001\u30d5\u30fc\u30ea\u30a8\u89e3\u6790\u3092\u4f7f\u3063\u3066\u3044\u308b\u305f\u3081\u304b\u5024\u304c\u9055\u3046\u3002\n<\/p>\n<p>\n\u5f8c\u3067\u30b3\u30fc\u30c9\u3092\u898b\u76f4\u305d\u3046\u3002\n<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n\/\/ \u5b9f\u753b\u50cf\u306e\u4f4d\u7f6e(dx,dy)\u306b\u5bfe\u3057\u3066\u3001\u76f8\u95a2\u4fc2\u6570\u3092\u8a08\u7b97\u3059\u308b\ndouble crossCorr( const cv::Mat&amp; img, const cv::Mat&amp; templ, const cv::Mat&amp; mask, int dx, int dy )\n{\n\tint cnt = 0;\n\n\tdouble templMean = 0.0;\t\/\/ \u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306e\u76f8\u52a0\u5e73\u5747\n\tdouble imgMean   = 0.0;\t\/\/ \u5b9f\u753b\u50cf\u306e\u76f8\u52a0\u5e73\u5747 \n\t\n\tfor ( int y=0; y&lt;templ.rows; ++y ) {\n\t\tfor ( int x=0; x&lt;templ.cols; ++x ) {\n\t\t\tif ( mask.at&lt;char&gt;(y,x) == 0 ) \n\t\t\t\tcontinue;\n\t\t\tcv::Vec3b v1 = templ.at&lt;cv::Vec3b&gt;(y,x);\n\t\t\tcv::Vec3b v2 = img.at&lt;cv::Vec3b&gt;(y+dy,x+dx);\n\t\t\ttemplMean += v1&#x5B;0] + v1&#x5B;1] + v1&#x5B;2];\n\t\t\timgMean += v2&#x5B;0] + v2&#x5B;1] + v2&#x5B;2];\n\t\t\tcnt += 3;\n\t\t}\n\t}\n\ttemplMean = templMean\/(double)cnt;\n\timgMean = imgMean\/(double)cnt;\n\n\t\/\/ \u5206\u5b50(numerator)\n\tdouble numerator = 0.0;\n\tfor ( int y=0; y&lt;templ.rows; ++y ) {\n\t\tfor ( int x=0; x&lt;templ.cols; ++x ) {\n\t\t\tif ( mask.at&lt;char&gt;(y,x) == 0 ) \n\t\t\t\tcontinue;\n\n\t\t\tcv::Vec3b v1 = templ.at&lt;cv::Vec3b&gt;(y,x);\n\t\t\tcv::Vec3b v2 = img.at&lt;cv::Vec3b&gt;(y+dy,x+dx);\n\t\t\tnumerator += (v1&#x5B;0] - templMean)*(v2&#x5B;0] - imgMean);\n\t\t\tnumerator += (v1&#x5B;1] - templMean)*(v2&#x5B;1] - imgMean);\n\t\t\tnumerator += (v1&#x5B;2] - templMean)*(v2&#x5B;2] - imgMean);\n\t\t}\n\t}\n\t\/\/ \u5206\u6bcd(denominator)\n\tdouble denominator_templ = 0.0;\n\tdouble denominator_img   = 0.0;\n\tfor ( int y=0; y&lt;templ.rows; ++y ) {\n\t\tfor ( int x=0; x&lt;templ.cols; ++x ) {\n\t\t\tif ( mask.at&lt;char&gt;(y,x) == 0 ) \n\t\t\t\tcontinue;\n\t\t\tcv::Vec3b v1 = templ.at&lt;cv::Vec3b&gt;(y,x);\n\t\t\tcv::Vec3b v2 = img.at&lt;cv::Vec3b&gt;(y+dy,x+dx);\n\n\t\t\tdenominator_templ += (v1&#x5B;0] - templMean)*(v1&#x5B;0] - templMean);\n\t\t\tdenominator_templ += (v1&#x5B;1] - templMean)*(v1&#x5B;1] - templMean);\n\t\t\tdenominator_templ += (v1&#x5B;2] - templMean)*(v1&#x5B;2] - templMean);\n\t\t\tdenominator_img += (v2&#x5B;0] - imgMean)*(v2&#x5B;0] - imgMean);\n\t\t\tdenominator_img += (v2&#x5B;1] - imgMean)*(v2&#x5B;1] - imgMean);\n\t\t\tdenominator_img += (v2&#x5B;2] - imgMean)*(v2&#x5B;2] - imgMean);\n\t\t}\n\t}\n\tdouble denominator = sqrt(denominator_templ)*sqrt(denominator_img);\n\n\treturn numerator\/denominator;\n}\n\n\/\/ \u5b9f\u753b\u50cf\u306e\u3059\u3079\u3066\u306b\u5bfe\u3057\u3066\u76f8\u95a2\u4fc2\u6570\u3092\u8a08\u7b97\u3059\u308b\nvoid MatchTemplate( const cv::Mat&amp; img, const cv::Mat&amp; templ, cv::Mat&amp; result, const cv::Mat&amp; mask )\n{\n\tcv::Size corrsize(img.cols-templ.cols, img.rows-templ.rows);\n\tresult.create(corrsize, CV_64F); \n\n\tfor ( int y=0; y&lt;corrsize.height; ++y ) {\n\t\tfor ( int x=0; x&lt;corrsize.width; ++x ) {\n\t\t\tdouble corr = crossCorr( img, templ, mask,x,y );\n\t\t\tresult.at&lt;double&gt;(y,x) = corr ;\n\t\t}\n\t}\n}\n<\/pre>\n<p>\n\u3061\u306a\u307f\u306b\u3001Mat:at<> \u3092\u4f7f\u3046\u3068\u30c7\u30d0\u30c3\u30b0\u5b9f\u884c\u3067\u306f\u3072\u3069\u304f\u9045\u3044\u306e\u3060\u304c\u3001Release \u30d3\u30eb\u30c9\u3092\u3059\u308b\u3068\u7d50\u69cb\u901f\u3044\u3002<br \/>\n\u304a\u305d\u3089\u304f\u3001mat.hpp \u306b\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u308b\n<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\ntemplate&lt;typename _Tp&gt; inline _Tp&amp; Mat::at(int i0, int i1)\n{\n    CV_DbgAssert( dims &lt;= 2 &amp;&amp; data &amp;&amp; (unsigned)i0 &lt; (unsigned)size.p&#x5B;0] &amp;&amp;\n        (unsigned)(i1*DataType&lt;_Tp&gt;::channels) &lt; (unsigned)(size.p&#x5B;1]*channels()) &amp;&amp;\n        CV_ELEM_SIZE1(DataType&lt;_Tp&gt;::depth) == elemSize1());\n    return ((_Tp*)(data + step.p&#x5B;0]*i0))&#x5B;i1];\n}\n<\/pre>\n<p>\n\u3067\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u308b CV_DbgAssert \u306e\u95a2\u6570\u304c\u9045\u3044\u308f\u3051\u3067\u3001\u3053\u308c\u306f\u30ea\u30ea\u30fc\u30b9\u30e2\u30fc\u30c9\u3067\u306f\u5916\u308c\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u308b\u3002<br \/>\n\u3053\u306e\u90e8\u5206\u3092\u3001CV_DbgAssert \u3092\u4f7f\u308f\u306a\u3044\u3088\u3046\u306b\u5c55\u958b\u3057\u3066\u3084\u308c\u3070\u3001\u65e9\u304f\u306a\u308b\u306e\u3067\u306f\u306a\u3044\u304b\u306a\u3041\u3068\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7d50\u5c40\u3001\u81ea\u4f5c\u3057\u3066\u307f\u308b&#8230; templmatch.cpp \u5185\u306b\u3042\u308b crossCorr \u95a2\u6570\u3067\u76f8\u95a2\u5e73\u5747\u3092\u8a08\u7b97\u3057\u3066\u3044\u308b\u306e\u3060\u304c\u3001\u30d5\u30fc\u30ea\u30a8\u89e3\u6790\u3092\u4f7f\u3063\u3066\u3044\u308b\u305f\u3081\u304b\u5024\u304c\u9055\u3046\u3002 \u5f8c\u3067\u30b3\u30fc\u30c9\u3092\u898b\u76f4\u305d\u3046\u3002 \/\/ \u5b9f\u753b\u50cf\u306e\u4f4d\u7f6e(dx &hellip; <a href=\"http:\/\/www.moonmile.net\/blog\/archives\/2509\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3,5,22,35],"tags":[],"class_list":["post-2509","post","type-post","status-publish","format-standard","hentry","category-dev","category-etc","category-c","category-opencv"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/2509","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/comments?post=2509"}],"version-history":[{"count":1,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/2509\/revisions"}],"predecessor-version":[{"id":2511,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/2509\/revisions\/2511"}],"wp:attachment":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/media?parent=2509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/categories?post=2509"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/tags?post=2509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}