{"id":767,"date":"2015-12-17T12:00:10","date_gmt":"2015-12-17T03:00:10","guid":{"rendered":"http:\/\/nantoka.filmm.info\/blog\/?p=767"},"modified":"2015-12-17T12:02:46","modified_gmt":"2015-12-17T03:02:46","slug":"of%e3%81%aecolor-tracking%e3%82%92android%e4%b8%8a%e3%81%a7%e5%8b%95%e3%81%8b%e3%81%99","status":"publish","type":"post","link":"https:\/\/nantoka.filmm.info\/blog\/?p=767","title":{"rendered":"oF\u306ecolor tracking\u3092Android\u4e0a\u3067\u52d5\u304b\u3059"},"content":{"rendered":"<p>Xcode\u306e\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u307b\u307c\u7121\u6539\u9020\u306e\u307e\u307eAndroidStudio\u3067\u3044\u3051\u307e\u3057\u305f<\/p>\n<pre class=\"lang:default decode:true \" title=\"ofApp.h\">#pragma once\r\n\r\n#include \"ofMain.h\"\r\n#include \"ofxAndroid.h\"\r\n\r\n#include \"ofxOpenCv.h\"\r\n\r\nclass ofApp : public ofxAndroidApp{\r\n\t\r\n\tpublic:\r\n\t\t\r\n\t\tvoid setup();\r\n\t\tvoid update();\r\n\t\tvoid draw();\r\n\r\n\t\tvoid keyPressed(int key);\r\n\t\tvoid keyReleased(int key);\r\n\t\tvoid windowResized(int w, int h);\r\n\r\n\t\tvoid touchDown(int x, int y, int id);\r\n\t\tvoid touchMoved(int x, int y, int id);\r\n\t\tvoid touchUp(int x, int y, int id);\r\n\t\tvoid touchDoubleTap(int x, int y, int id);\r\n\t\tvoid touchCancelled(int x, int y, int id);\r\n\t\tvoid swipe(ofxAndroidSwipeDir swipeDir, int id);\r\n\r\n\t\tvoid pause();\r\n\t\tvoid stop();\r\n\t\tvoid resume();\r\n\t\tvoid reloadTextures();\r\n\r\n\t\tbool backPressed();\r\n\t\tvoid okPressed();\r\n\t\tvoid cancelPressed();\r\n\r\n\r\n\tofVideoGrabber movie;\r\n\tofxCvColorImage rgb,hsb;\r\n\tofxCvGrayscaleImage hue,sat,bri,filtered;\r\n\tofxCvContourFinder contours;\r\n\r\n\tint w,h;\r\n\tint findHue;\r\n\r\n\r\n};\r\n<\/pre>\n<p>\u30e1\u30a4\u30f3\u306f\u3053\u3061\u3089<\/p>\n<pre class=\"lang:default mark:8,35 decode:true\" title=\"ofApp.cpp\">#include \"ofApp.h\"\r\n\r\n\/\/--------------------------------------------------------------\r\nvoid ofApp::setup(){\r\n\r\n\t\/\/add by me\r\n\tofBackground(0,0,0);\r\n\tofSetOrientation(OF_ORIENTATION_90_LEFT);\r\n\tw = 640;\/\/default 320\r\n\th = 480;\/\/default 240\r\n\r\n\tmovie.initGrabber(w, h, true);\r\n\r\n\t\/\/reserve memory for cv images\r\n\trgb.allocate(w, h);\r\n\thsb.allocate(w, h);\r\n\thue.allocate(w, h);\r\n\tsat.allocate(w, h);\r\n\tbri.allocate(w, h);\r\n\tfiltered.allocate(w, h);\r\n}\r\n\r\n\/\/--------------------------------------------------------------\r\nvoid ofApp::update(){\r\n\r\n\t\/\/ add by me\r\n\tmovie.update();\r\n\r\n\tif (movie.isFrameNew()) {\r\n\r\n\t\t\/\/copy webcam pixels to rgb image\r\n\t\trgb.setFromPixels(movie.getPixels(), w, h);\r\n\r\n\t\t\/\/mirror horizontal\r\n\t\trgb.mirror(false, false);\/\/default (false, true)\r\n\r\n\t\t\/\/duplicate rgb\r\n\t\thsb = rgb;\r\n\r\n\t\t\/\/convert to hsb\r\n\t\thsb.convertRgbToHsv();\r\n\r\n\t\t\/\/store the three channels as grayscale images\r\n\t\thsb.convertToGrayscalePlanarImages(hue, sat, bri);\r\n\r\n\t\t\/\/filter image based on the hue value were looking for\r\n\t\tfor (int i=0; i&lt;w*h; i++) {\r\n\t\t\tfiltered.getPixels()[i] = ofInRange(hue.getPixels()[i],findHue-5,findHue+5) ? 255 : 0;\r\n\t\t}\r\n\r\n\t\tfiltered.flagImageChanged();\r\n\t\t\/\/run the contour finder on the filtered image to find blobs with a certain hue\r\n\t\tcontours.findContours(filtered, 20, w*h\/2, 2, false);\/\/last numeric 2 is find maximum cout\r\n\t}\r\n\r\n}\r\n\r\n\/\/--------------------------------------------------------------\r\nvoid ofApp::draw(){\r\n\t\/\/add by me\r\n\tofSetColor(255,255,255);\r\n\r\n\t\/\/draw all cv images\r\n\trgb.draw(0,0);\r\n\t\/\/hsb.draw(640,0);\r\n\t\/\/hue.draw(0,240);\r\n\t\/\/sat.draw(320,240);\r\n\t\/\/bri.draw(640,240);\r\n\t\/\/filtered.draw(0,480);\r\n\t\/\/contours.draw(0,480);\r\n\r\n\tofSetColor(255, 0, 0);\r\n\tofFill();\r\n\r\n\t\/\/draw red circles for found blobs\r\n\tfor (int i=0; i&lt;contours.nBlobs; i++) {\r\n\t\tofCircle(contours.blobs[i].centroid.x, contours.blobs[i].centroid.y, 10);\/\/ last text 10 is radius od draw circle\r\n\t}\r\n\r\n}\r\n\r\n\/\/--------------------------------------------------------------\r\nvoid ofApp::keyPressed  (int key){ \r\n\t\r\n}\r\n\r\n\/\/--------------------------------------------------------------\r\nvoid ofApp::keyReleased(int key){ \r\n\t\r\n}\r\n\r\n\/\/--------------------------------------------------------------\r\nvoid ofApp::windowResized(int w, int h){\r\n\r\n}\r\n\r\n\/\/--------------------------------------------------------------\r\nvoid ofApp::touchDown(int x, int y, int id){\r\n\r\n\/\/ add by me\r\n\/\/calculate local mouse x,y in image\r\n\r\nint mx = x % w;\r\nint my = y % h;\r\n\r\n\/\/get hue value on mouse position\r\nfindHue = hue.getPixels()[my*w+mx];\r\n\r\n}\r\n\r\n\/\/--------------------------------------------------------------\r\nvoid ofApp::touchMoved(int x, int y, int id){\r\n\r\n}\r\n\r\n\/\/--------------------------------------------------------------\r\nvoid ofApp::touchUp(int x, int y, int id){\r\n\r\n}\r\n\r\n\/\/--------------------------------------------------------------\r\nvoid ofApp::touchDoubleTap(int x, int y, int id){\r\n\r\n}\r\n\r\n\/\/--------------------------------------------------------------\r\nvoid ofApp::touchCancelled(int x, int y, int id){\r\n\r\n}\r\n\r\n\/\/--------------------------------------------------------------\r\nvoid ofApp::swipe(ofxAndroidSwipeDir swipeDir, int id){\r\n\r\n}\r\n\r\n\/\/--------------------------------------------------------------\r\nvoid ofApp::pause(){\r\n\r\n}\r\n\r\n\/\/--------------------------------------------------------------\r\nvoid ofApp::stop(){\r\n\r\n}\r\n\r\n\/\/--------------------------------------------------------------\r\nvoid ofApp::resume(){\r\n\r\n}\r\n\r\n\/\/--------------------------------------------------------------\r\nvoid ofApp::reloadTextures(){\r\n\r\n}\r\n\r\n\/\/--------------------------------------------------------------\r\nbool ofApp::backPressed(){\r\n\treturn false;\r\n}\r\n\r\n\/\/--------------------------------------------------------------\r\nvoid ofApp::okPressed(){\r\n\r\n}\r\n\r\n\/\/--------------------------------------------------------------\r\nvoid ofApp::cancelPressed(){\r\n\r\n}\r\n<\/pre>\n<p>8\u884c\u76ee\u3067\u6a2a\u5411\u304d\u30e2\u30fc\u30c9\u306b\u3057\u3066\u3044\u307e\u3059<\/p>\n<p>35\u884c\u76ee\u3067\u30ab\u30e1\u30e9\u306e\u30df\u30e9\u30fc\u8a2d\u5b9a\u3092\u7121\u52b9\u306b\u3057\u3066\u3044\u307e\u3059\uff0e\u5b9f\u9a13\u304c\u30a4\u30f3\u30ab\u30e1\u30e9\u3067\u3057\u305f\u304c\uff0c\u30d0\u30c3\u30af\u30ab\u30e1\u30e9\u306e\u5834\u5408\u306f\u4e0d\u8981\u3067\u3059\u306d\uff0e<\/p>\n<p>\u5b9f\u884c\u7d50\u679c\u306f\u4ee5\u4e0b<\/p>\n<p><a href=\"http:\/\/nantoka.filmm.info\/blog\/wp-content\/uploads\/2015\/12\/ofcvtest3.jpg\" rel=\"attachment wp-att-768\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-768\" src=\"http:\/\/nantoka.filmm.info\/blog\/wp-content\/uploads\/2015\/12\/ofcvtest3-300x202.jpg\" alt=\"ofcvtest3\" width=\"300\" height=\"202\" srcset=\"https:\/\/nantoka.filmm.info\/blog\/wp-content\/uploads\/2015\/12\/ofcvtest3-300x202.jpg 300w, https:\/\/nantoka.filmm.info\/blog\/wp-content\/uploads\/2015\/12\/ofcvtest3-768x516.jpg 768w, https:\/\/nantoka.filmm.info\/blog\/wp-content\/uploads\/2015\/12\/ofcvtest3.jpg 996w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Xcode\u306e\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u307b\u307c\u7121\u6539\u9020\u306e\u307e\u307eAndroidStudio\u3067\u3044\u3051\u307e\u3057\u305f #pragma once #include &#8220;ofMain.h&#8221; #include &#8220;ofxAndroid.h&#8221; #include &#8220;ofx &hellip; <a href=\"https:\/\/nantoka.filmm.info\/blog\/?p=767\" class=\"more-link\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"screen-reader-text\">oF\u306ecolor tracking\u3092Android\u4e0a\u3067\u52d5\u304b\u3059<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25,24],"tags":[],"class_list":["post-767","post","type-post","status-publish","format-standard","hentry","category-android","category-openframeworksof"],"_links":{"self":[{"href":"https:\/\/nantoka.filmm.info\/blog\/index.php?rest_route=\/wp\/v2\/posts\/767","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nantoka.filmm.info\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nantoka.filmm.info\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nantoka.filmm.info\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nantoka.filmm.info\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=767"}],"version-history":[{"count":3,"href":"https:\/\/nantoka.filmm.info\/blog\/index.php?rest_route=\/wp\/v2\/posts\/767\/revisions"}],"predecessor-version":[{"id":771,"href":"https:\/\/nantoka.filmm.info\/blog\/index.php?rest_route=\/wp\/v2\/posts\/767\/revisions\/771"}],"wp:attachment":[{"href":"https:\/\/nantoka.filmm.info\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=767"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nantoka.filmm.info\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=767"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nantoka.filmm.info\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=767"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}