{"id":1892,"date":"2008-06-22T19:52:47","date_gmt":"2008-06-22T10:52:47","guid":{"rendered":"http:\/\/r-dimension.xsrv.jp\/classes_j\/?p=1892"},"modified":"2011-05-03T02:04:32","modified_gmt":"2011-05-02T17:04:32","slug":"back_subtraction","status":"publish","type":"post","link":"https:\/\/r-dimension.xsrv.jp\/classes_j\/back_subtraction\/","title":{"rendered":"\u80cc\u666f\u753b\u50cf\u306e\u9ed2\u629c\u304d"},"content":{"rendered":"<p>\u3053\u306e\u30b5\u30f3\u30d7\u30eb\u306f\u3001\u52d5\u3044\u305f\u5834\u6240\u3092\u7279\u5b9a\u3059\u308b\u305f\u3081\u306e\u30b3\u30fc\u30c9\u3067\u3059\u3002<\/p>\n<p><!--more--><\/p>\n<pre lang=\"java\" line=\"1\">\r\n\/\/This code has been arranged by Yasushi Noguchi. Last updated on June 21, 2008.\r\n\/**\r\n * Background Subtraction \r\n * by Golan Levin. \r\n * \r\n * Detect the presence of people and objects in the frame using a simple\r\n * background-subtraction technique. To initialize the background, press a key.\r\n *\/\r\n\r\n\r\nimport processing.video.*;\r\n\r\nint numPixels;    \/\/\u753b\u50cf\u306e\u30d4\u30af\u30bb\u30eb\u306e\u7dcf\u6570\r\nint[] backgroundPixels;    \/\/\u80cc\u666f\u306e\u30d4\u30af\u30bb\u30eb\r\nint noiseFilter = 100;    \/\/\u30ce\u30a4\u30ba\u3092\u62fe\u308f\u306a\u3044\u305f\u3081\u306e\u30d5\u30a3\u30eb\u30bf\uff08\u5024\u3092\u5897\u3084\u3059\u3068\u30d5\u30a3\u30eb\u30bf\u304c\u5f37\u304f\u304b\u304b\u308b\uff09\r\nCapture video;    \/\/\u30ad\u30e3\u30d7\u30c1\u30e3\u6620\u50cf\u7528\u306e\u5909\u6570\r\n\r\nvoid setup() {\r\n  size(640, 480); \r\n  \r\n  video = new Capture(this, width, height, 24);\r\n  \/\/\u30ad\u30e3\u30d7\u30c1\u30e3\u30fc\u3059\u308b\u30d3\u30c7\u30aa\u753b\u50cf\u306e\u7dcf\u30d4\u30af\u30bb\u30eb\u6570\r\n  numPixels = video.width * video.height;\r\n  \/\/\u73fe\u5728\u306e\u30ad\u30e3\u30d7\u30c1\u30e3\u753b\u50cf\u3068\u6bd4\u3079\u308b\u305f\u3081\u306b\u80cc\u666f\u753b\u50cf\u7528\u306e\u914d\u5217\u3092\u4f5c\u308b\r\n  backgroundPixels = new int[numPixels];\r\n  loadPixels();\r\n}\r\n\r\nvoid draw() {\r\n  if (video.available()) {  \/\/\u3082\u3057\u30ad\u30e3\u30d7\u30c1\u30e3\u304c\u3067\u304d\u305f\u3089\u3001\r\n    video.read(); \/\/\u30d3\u30c7\u30aa\u30d5\u30ec\u30fc\u30e0\u306e\u8aad\u307f\u8fbc\u307f\r\n    video.loadPixels(); \/\/\u30d3\u30c7\u30aa\u306e\u30d4\u30af\u30bb\u30eb\u3092\u64cd\u4f5c\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b\r\n    \r\n    \/\/\u73fe\u5728\u306e\u30d5\u30ec\u30fc\u30e0\u3068\u80cc\u666f\u753b\u50cf\u306e\u5dee\r\n    int presenceSum = 0;\r\n    for (int i = 0; i < numPixels; i++) { \/\/\u30d5\u30ec\u30fc\u30e0\u5185\u306e\u305d\u308c\u305e\u308c\u306e\u30d4\u30af\u30bb\u30eb\u3092\u691c\u51fa\r\n\r\n      \/\/\u73fe\u5728\u306e\u30d4\u30af\u30bb\u30eb\u3068\u80cc\u666f\u306e\u30d4\u30af\u30bb\u30eb\u306e\u5024\u3092\u5909\u6570\u306b\u4ee3\u5165\r\n      color currColor = video.pixels[i];\r\n      color bkgdColor = backgroundPixels[i];\r\n      \r\n      \/\/\u73fe\u5728\u306e\u30d4\u30af\u30bb\u30eb\u306eR, G, B\u3092\u629c\u304d\u51fa\u3059\r\n      int currR = (currColor >> 16) & 0xFF;\r\n      int currG = (currColor >> 8 ) & 0xFF;\r\n      int currB = currColor & 0xFF;\r\n      \r\n      \/\/\u80cc\u666f\u753b\u50cf\u306eR, G, B\u3092\u629c\u304d\u51fa\u3059\r\n      int bkgdR = (bkgdColor >> 16) & 0xFF;\r\n      int bkgdG = (bkgdColor >> 8 ) & 0xFF;\r\n      int bkgdB = bkgdColor & 0xFF;\r\n      \r\n      \/\/\u73fe\u5728\u306e\u30d4\u30af\u30bb\u30eb\u304b\u3089\u80cc\u666f\u753b\u50cf\u306e\u30d4\u30af\u30bb\u30eb\u306e\u8272\u3092\u5f15\u3044\u305f\u7d76\u5bfe\u5024\r\n      int diffR = abs(currR - bkgdR);\r\n      int diffG = abs(currG - bkgdG);\r\n      int diffB = abs(currB - bkgdB);\r\n      \r\n      \/\/noiseFilter\u306e\u5024\u3088\u308a\u3082\u5927\u304d\u304b\u3063\u305f\u3089presenceSum\u306b\u8db3\u3057\u3066\u3044\u304f\r\n      \/\/\u305d\u3057\u3066\u3001\u73fe\u5728\u306e\u8272\u306b\u66f4\u65b0\r\n      if(diffR + diffG + diffB > noiseFilter){\r\n        presenceSum += diffR + diffG + diffB;\r\n        pixels[i] = color(currR, currG, currB);\r\n        \/\/\u6b21\u306e\u30b3\u30fc\u30c9\u306e\u65b9\u304c\u9ad8\u901f\u306b\u5b9f\u884c\u3067\u304d\u308b\u304c\u3001\u3061\u3087\u3063\u3068\u96e3\u3057\u3044\r\n        \/\/pixels[i] = 0xFF000000 | (currR < < 16) | (currG << 8 ) | currB;\r\n      }\r\n      else{\r\n        \/\/\u305d\u3046\u3067\u306a\u3044\u5834\u5408\u306f\u9ed2\r\n        pixels[i] = color(0);\r\n      }\r\n    }\r\n    updatePixels();    \/\/\u30d4\u30af\u30bb\u30eb\u3092\u66f4\u65b0\r\n    \/\/println(presenceSum);    \/\/\u5909\u5316\u3057\u305f\u30d4\u30af\u30bb\u30eb\u306e\u7dcf\u6570\u3092\u30d7\u30ea\u30f3\u30c8\r\n  }\r\n}\r\n\r\n\/\/\u30de\u30a6\u30b9\u3092\u62bc\u3057\u305f\u3068\u304d\u306b\u3001\u73fe\u5728\u306e\u30d5\u30ec\u30fc\u30e0\u306e\u753b\u50cf\u3092backgroudPixels\u306b\u30b3\u30d4\u30fc\u3059\u308b\r\nvoid mousePressed() {\r\n  video.loadPixels();\r\n  arraycopy(video.pixels, backgroundPixels);\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u306e\u30b5\u30f3\u30d7\u30eb\u306f\u3001\u52d5\u3044\u305f\u5834\u6240\u3092\u7279\u5b9a\u3059\u308b\u305f\u3081\u306e\u30b3\u30fc\u30c9\u3067\u3059\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,11],"tags":[],"class_list":["post-1892","post","type-post","status-publish","format-standard","hentry","category-processing","category-11"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/r-dimension.xsrv.jp\/classes_j\/wp-json\/wp\/v2\/posts\/1892","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/r-dimension.xsrv.jp\/classes_j\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/r-dimension.xsrv.jp\/classes_j\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/r-dimension.xsrv.jp\/classes_j\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/r-dimension.xsrv.jp\/classes_j\/wp-json\/wp\/v2\/comments?post=1892"}],"version-history":[{"count":3,"href":"https:\/\/r-dimension.xsrv.jp\/classes_j\/wp-json\/wp\/v2\/posts\/1892\/revisions"}],"predecessor-version":[{"id":2468,"href":"https:\/\/r-dimension.xsrv.jp\/classes_j\/wp-json\/wp\/v2\/posts\/1892\/revisions\/2468"}],"wp:attachment":[{"href":"https:\/\/r-dimension.xsrv.jp\/classes_j\/wp-json\/wp\/v2\/media?parent=1892"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/r-dimension.xsrv.jp\/classes_j\/wp-json\/wp\/v2\/categories?post=1892"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/r-dimension.xsrv.jp\/classes_j\/wp-json\/wp\/v2\/tags?post=1892"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}