{"id":754,"date":"2015-12-16T12:22:03","date_gmt":"2015-12-16T03:22:03","guid":{"rendered":"http:\/\/nantoka.filmm.info\/blog\/?p=754"},"modified":"2015-12-16T12:24:14","modified_gmt":"2015-12-16T03:24:14","slug":"of%e3%81%a7opencv","status":"publish","type":"post","link":"https:\/\/nantoka.filmm.info\/blog\/?p=754","title":{"rendered":"oF\u3067openCV"},"content":{"rendered":"<p>open frame works\u3067openCV\u3092\u3084\u3063\u3061\u3083\u3044\u307e\u3059\uff0e<\/p>\n<p>open frame works\uff08\u4ee5\u4e0boF\uff09\u306f\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u81ea\u4f53\u304c\u3068\u3066\u3082\u7c21\u7565\u5316\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u30d7\u30ed\u30c8\u30bf\u30a4\u30d7\u306e\u4f5c\u6210\u5411\u304d\u3067\u3059\uff0e\u52d5\u4f5c\u306f\u91cd\u3044\u3068\u306e\u3053\u3068\u3067\u3059\u304c\uff0c\u30ea\u30ea\u30fc\u30b9\u3059\u308b\u3082\u306e\u3067\u306a\u3051\u308c\u3070\u305d\u308c\u307b\u3069\u96e3\u3057\u304f\u3042\u308a\u307e\u305b\u3093\uff0e<\/p>\n<p>\u306a\u304a\u304b\u3064iPhone,Android\u3088\u3046\u306b\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u304c\u3042\u308a\uff0c\u305d\u3053\u306b\u66f8\u304d\u8fbc\u3081\u3070\u30b9\u30de\u30db\u30a2\u30d7\u30ea\u304c\u4f5c\u308c\u3066\u3057\u307e\u3044\u307e\u3059\uff0e\uff08Android\u3084openCV\u306f\u672a\u78ba\u8a8d\uff09<\/p>\n<p>\u3053\u308c\u306fXCODE\u3067\u306e\u30c6\u30b9\u30c8\u3067\u3059<\/p>\n<pre class=\"lang:default decode:true\" title=\"ofApph\">#pragma once\r\n\r\n#include \"ofMain.h\"\r\n#include \"ofxOpenCv.h\"\r\n\r\nclass ofApp : public ofBaseApp{\r\n\r\n\tpublic:\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 mouseMoved(int x, int y );\r\n\t\tvoid mouseDragged(int x, int y, int button);\r\n\t\tvoid mousePressed(int x, int y, int button);\r\n\t\tvoid mouseReleased(int x, int y, int button);\r\n\t\tvoid mouseEntered(int x, int y);\r\n\t\tvoid mouseExited(int x, int y);\r\n\t\tvoid windowResized(int w, int h);\r\n\t\tvoid dragEvent(ofDragInfo dragInfo);\r\n\t\tvoid gotMessage(ofMessage msg);\r\n    \r\n    ofVideoGrabber movie;\r\n    \r\n    ofxCvColorImage rgb,hsb;\r\n    ofxCvGrayscaleImage hue,sat,bri,filtered;\r\n    ofxCvContourFinder contours;\r\n    \r\n    int w,h;\r\n    int findHue;\r\n\t\t\r\n};\r\n<\/pre>\n<p>\u305d\u3057\u3066\u3053\u3063\u3061\u3082\u5fc5\u8981<\/p>\n<pre class=\"lang:default mark:49 decode:true \" title=\"ofApp.cpp\">#include \"ofApp.h\"\r\n\r\n\/\/--------------------------------------------------------------\r\nvoid ofApp::setup(){\r\n    ofBackground(0,0,0);\r\n    \r\n    w = 320;\r\n    h = 240;\r\n    \r\n    movie.initGrabber(w, h, true);\r\n    \r\n    \/\/reserve memory for cv images\r\n    rgb.allocate(w, h);\r\n    hsb.allocate(w, h);\r\n    hue.allocate(w, h);\r\n    sat.allocate(w, h);\r\n    bri.allocate(w, h);\r\n    filtered.allocate(w, h);\r\n}\r\n\r\n\/\/--------------------------------------------------------------\r\nvoid ofApp::update(){\r\n    movie.update();\r\n    \r\n    if (movie.isFrameNew()) {\r\n        \r\n        \/\/copy webcam pixels to rgb image\r\n        rgb.setFromPixels(movie.getPixels(), w, h);\r\n        \r\n        \/\/mirror horizontal\r\n        rgb.mirror(false, true);\r\n        \r\n        \/\/duplicate rgb\r\n        hsb = rgb;\r\n        \r\n        \/\/convert to hsb\r\n        hsb.convertRgbToHsv();\r\n        \r\n        \/\/store the three channels as grayscale images\r\n        hsb.convertToGrayscalePlanarImages(hue, sat, bri);\r\n        \r\n        \/\/filter image based on the hue value were looking for\r\n        for (int i=0; i&lt;w*h; i++) {\r\n            filtered.getPixels()[i] = ofInRange(hue.getPixels()[i],findHue-5,findHue+5) ? 255 : 0;\r\n        }\r\n        \r\n        filtered.flagImageChanged();\r\n        \/\/run the contour finder on the filtered image to find blobs with a certain hue\r\n        contours.findContours(filtered, 20, w*h\/2, 2, false);\/\/last numeric 2 is find maximum cout\r\n    }\r\n}\r\n\r\n\/\/--------------------------------------------------------------\r\nvoid ofApp::draw(){\r\n    ofSetColor(255,255,255);\r\n    \r\n    \/\/draw all cv images\r\n    rgb.draw(0,0);\r\n    hsb.draw(640,0);\r\n    hue.draw(0,240);\r\n    sat.draw(320,240);\r\n    bri.draw(640,240);\r\n    filtered.draw(0,480);\r\n    contours.draw(0,480);\r\n    \r\n    ofSetColor(255, 0, 0);\r\n    ofFill();\r\n    \r\n    \/\/draw red circles for found blobs\r\n    for (int i=0; i&lt;contours.nBlobs; i++) {\r\n        ofCircle(contours.blobs[i].centroid.x, contours.blobs[i].centroid.y, 10);\/\/ last text 10 is radius od draw circle\r\n    }\r\n\r\n}\r\n\r\n\/\/--------------------------------------------------------------\r\nvoid ofApp::keyPressed(int key){\r\n\r\n}\r\n\r\n\/\/--------------------------------------------------------------\r\nvoid ofApp::keyReleased(int key){\r\n\r\n}\r\n\r\n\/\/--------------------------------------------------------------\r\nvoid ofApp::mouseMoved(int x, int y ){\r\n\r\n}\r\n\r\n\/\/--------------------------------------------------------------\r\nvoid ofApp::mouseDragged(int x, int y, int button){\r\n\r\n}\r\n\r\n\/\/--------------------------------------------------------------\r\nvoid ofApp::mousePressed(int x, int y, int button){\r\n    \/\/calculate local mouse x,y in image\r\n    int mx = x % w;\r\n    int my = y % h;\r\n    \r\n    \/\/get hue value on mouse position\r\n    findHue = hue.getPixels()[my*w+mx];\r\n}\r\n\r\n\/\/--------------------------------------------------------------\r\nvoid ofApp::mouseReleased(int x, int y, int button){\r\n\r\n}\r\n\r\n\/\/--------------------------------------------------------------\r\nvoid ofApp::mouseEntered(int x, int y){\r\n\r\n}\r\n\r\n\/\/--------------------------------------------------------------\r\nvoid ofApp::mouseExited(int x, int y){\r\n\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::gotMessage(ofMessage msg){\r\n\r\n}\r\n\r\n\/\/--------------------------------------------------------------\r\nvoid ofApp::dragEvent(ofDragInfo dragInfo){ \r\n\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>\u6df7\u4e71\u3092\u9632\u3050\u305f\u3081\u306b\uff0c\u30c6\u30f3\u30d7\u30ec\u30c7\u30d5\u30a9\u30eb\u30c8\u306e\u307e\u307e\u66f8\u304d\u8db3\u3057\u3066\u3044\u307e\u3059<\/p>\n<p>\u5b9f\u884c\u7d50\u679c\u306f\u3053\u3093\u306a\u69d8\u5b50<\/p>\n<p><a href=\"http:\/\/nantoka.filmm.info\/blog\/wp-content\/uploads\/2015\/12\/cvtest.jpg\" rel=\"attachment wp-att-755\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-755\" src=\"http:\/\/nantoka.filmm.info\/blog\/wp-content\/uploads\/2015\/12\/cvtest-300x220.jpg\" alt=\"cvtest\" width=\"300\" height=\"220\" srcset=\"https:\/\/nantoka.filmm.info\/blog\/wp-content\/uploads\/2015\/12\/cvtest-300x220.jpg 300w, https:\/\/nantoka.filmm.info\/blog\/wp-content\/uploads\/2015\/12\/cvtest-768x562.jpg 768w, https:\/\/nantoka.filmm.info\/blog\/wp-content\/uploads\/2015\/12\/cvtest.jpg 985w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>\u5b9f\u884c\u3057\u3066\uff0c\u62bd\u51fa\u3057\u305f\u3044\u8272\u3092\u30af\u30ea\u30c3\u30af\u3059\u308b\u3068\u305d\u3053\u306b\u8d64\u4e38\u304c\u8868\u793a\u3055\u308c\u307e\u3059<\/p>\n<p>\u62bd\u51fa\u6570\u306fofApp.cpp\u306e49\u884c\u76ee<\/p>\n<pre class=\"lang:default decode:true \">contours.findContours(filtered, 20, w*h\/2, 2, false);<\/pre>\n<p>\u306efalse\u306e\u524d\u306e<strong>2<\/strong>\u3067\u5909\u3048\u3089\u308c\u307e\u3059\uff0e\uff12\u306a\u30892\u500b\u691c\u51fa\uff0e<\/p>\n","protected":false},"excerpt":{"rendered":"<p>open frame works\u3067openCV\u3092\u3084\u3063\u3061\u3083\u3044\u307e\u3059\uff0e open frame works\uff08\u4ee5\u4e0boF\uff09\u306f\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u81ea\u4f53\u304c\u3068\u3066\u3082\u7c21\u7565\u5316\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u30d7\u30ed\u30c8\u30bf\u30a4\u30d7\u306e\u4f5c\u6210\u5411\u304d\u3067\u3059\uff0e\u52d5\u4f5c\u306f\u91cd\u3044\u3068\u306e\u3053\u3068\u3067\u3059\u304c\uff0c\u30ea\u30ea\u30fc\u30b9 &hellip; <a href=\"https:\/\/nantoka.filmm.info\/blog\/?p=754\" class=\"more-link\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"screen-reader-text\">oF\u3067openCV<\/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":[24],"tags":[],"class_list":["post-754","post","type-post","status-publish","format-standard","hentry","category-openframeworksof"],"_links":{"self":[{"href":"https:\/\/nantoka.filmm.info\/blog\/index.php?rest_route=\/wp\/v2\/posts\/754","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=754"}],"version-history":[{"count":3,"href":"https:\/\/nantoka.filmm.info\/blog\/index.php?rest_route=\/wp\/v2\/posts\/754\/revisions"}],"predecessor-version":[{"id":758,"href":"https:\/\/nantoka.filmm.info\/blog\/index.php?rest_route=\/wp\/v2\/posts\/754\/revisions\/758"}],"wp:attachment":[{"href":"https:\/\/nantoka.filmm.info\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=754"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nantoka.filmm.info\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=754"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nantoka.filmm.info\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=754"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}