{"id":1448,"date":"2016-11-10T12:18:02","date_gmt":"2016-11-10T03:18:02","guid":{"rendered":"http:\/\/www.digifie.jp\/blog\/?p=1448"},"modified":"2016-11-10T12:18:02","modified_gmt":"2016-11-10T03:18:02","slug":"python-opencv-%e3%82%ab%e3%83%a1%e3%83%a9%e7%94%bb%e5%83%8f%e3%81%8b%e3%82%89%e3%82%aa%e3%83%97%e3%83%86%e3%82%a3%e3%82%ab%e3%83%ab%e3%83%95%e3%83%ad%e3%83%bc%e3%82%92%e5%8f%96%e5%be%97%e3%81%97","status":"publish","type":"post","link":"https:\/\/www.digifie.jp\/blog\/archives\/1448","title":{"rendered":"Python OpenCV \u30ab\u30e1\u30e9\u753b\u50cf\u304b\u3089\u30aa\u30d7\u30c6\u30a3\u30ab\u30eb\u30d5\u30ed\u30fc\u3092\u53d6\u5f97\u3057\u3066\u904a\u3093\u3067\u307f\u307e\u3057\u305f"},"content":{"rendered":"<div class='wp_social_bookmarking_light'>        <div class=\"wsbl_facebook\"><a href='http:\/\/www.facebook.com\/share.php?u=https%3A%2F%2Fwww.digifie.jp%2Fblog%2Farchives%2F1448&t=Python%20OpenCV%20%E3%82%AB%E3%83%A1%E3%83%A9%E7%94%BB%E5%83%8F%E3%81%8B%E3%82%89%E3%82%AA%E3%83%97%E3%83%86%E3%82%A3%E3%82%AB%E3%83%AB%E3%83%95%E3%83%AD%E3%83%BC%E3%82%92%E5%8F%96%E5%BE%97%E3%81%97%E3%81%A6%E9%81%8A%E3%82%93%E3%81%A7%E3%81%BF%E3%81%BE%E3%81%97%E3%81%9F' title='Facebook \u306b\u30b7\u30a7\u30a2' rel=nofollow class='wp_social_bookmarking_light_a' target=_blank><img src='https:\/\/www.digifie.jp\/blog\/wp-content\/plugins\/wp-social-bookmarking-light\/public\/images\/facebook.png' alt='Facebook \u306b\u30b7\u30a7\u30a2' title='Facebook \u306b\u30b7\u30a7\u30a2' width='16' height='16' class='wp_social_bookmarking_light_img' \/><\/a><\/div>        <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.digifie.jp\/blog\/archives\/1448\" data-text=\"Python OpenCV \u30ab\u30e1\u30e9\u753b\u50cf\u304b\u3089\u30aa\u30d7\u30c6\u30a3\u30ab\u30eb\u30d5\u30ed\u30fc\u3092\u53d6\u5f97\u3057\u3066\u904a\u3093\u3067\u307f\u307e\u3057\u305f\" data-via=\"LLC DigiFie\" data-lang=\"ja\">Tweet<\/a><\/div><\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n<p>Python + Opencv3.1 \u3067 \u30ab\u30e1\u30e9\u753b\u50cf\u304b\u3089\u30aa\u30d7\u30c6\u30a3\u30ab\u30eb\u30d5\u30ed\u30fc\uff08\u79fb\u52d5\u30d9\u30af\u30c8\u30eb\uff09\u3092\u53d6\u5f97\u3057\u3001\u3053\u308c\u3092\u30d1\u30e9\u30e1\u30fc\u30bf\u3068\u3057\u3066\u5229\u7528\u3057\u3066\u3061\u3087\u3063\u3068\u904a\u3093\u3067\u307f\u307e\u3057\u305f\u3002<\/p>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/yXSye1l1C0M\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<p>\u79fb\u52d5\u30d9\u30af\u30c8\u30eb\u3092\u53d6\u5f97\u3057\u3066\u3082\u3058\u3083\u3082\u3058\u3083\u3059\u308b\u30b3\u30fc\u30c9\u306f\u3001OpenCV 3.1 \u306e\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9 <a href=\"https:\/\/github.com\/opencv\/opencv\/blob\/master\/samples\/python\/opt_flow.py\" target=\"_blank\">opt_flow.py<\/a> \u3092\u53c2\u8003\u306b\u3057\u3066\u66f8\u3044\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<p>\u25a0 \u30aa\u30d7\u30c6\u30a3\u30ab\u30eb\u30d5\u30ed\u30fc\u3092\u5229\u7528\u3057\u3066\u63cf\u753b\u3059\u308b\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef draw_flow(img, gray, flow, step=16):\r\n    h, w = img.shape[:2]\r\n    y, x = np.mgrid[step\/2:h:step, step\/2:w:step].reshape(2,-1).astype(int)\r\n    fx, fy = flow[y,x].T\r\n    lines = np.vstack([x, y, x+fx, y+fy]).T.reshape(-1, 2, 2)\r\n    lines = np.int32(lines)\r\n\r\n    # vis = np.zeros((img.shape[0], img.shape[1], 3), np.uint8)\r\n    vis = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR) # \u30b0\u30ec\u30fc\u753b\u50cf\u3092\u80cc\u666f\u306b\u4f7f\u7528\r\n    vis = 255 - vis # \u30cd\u30ac\u53cd\u8ee2\r\n    rad = int(step\/2)\r\n\r\n    i = 0 # \u30eb\u30fc\u30d7\u30ab\u30a6\u30f3\u30bf\r\n    for (x1, y1), (x2, y2) in lines:\r\n        pv = img[y1, x1]\r\n        col = (int(pv[0]), int(pv[1]), int(pv[2]))\r\n        r = rad - int(rad * abs(fx[i]) *.05) # \u30c9\u30c3\u30c8\u306e\u534a\u5f84\u3092\u79fb\u52d5\u30d9\u30af\u30c8\u30eb\u306e\u91cf\u306b\u5fdc\u3058\u3066\u5c0f\u3055\u304f\u3059\u308b\r\n        cv2.circle(vis, (x1, y1), abs(r), col, -1)\r\n        i+=1\r\n    cv2.polylines(vis, lines, False, (255, 255, 0))\r\n    return vis\r\n<\/pre>\n<p>\u25a0 \u30ab\u30e1\u30e9\u306e\u30d5\u30ec\u30fc\u30e0\u304b\u3089\u30aa\u30d7\u30c6\u30a3\u30ab\u30eb\u30d5\u30ed\u30fc\u3092\u53d6\u5f97\u3057\u3066\u63cf\u753b\u95a2\u6570\u306b\u6e21\u3057\u3066\u753b\u50cf\u3092\u8868\u793a\u3059\u308b\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nwhile(cap.isOpened()):\r\n    # \u30ab\u30e1\u30e9\u306e\u30d5\u30ec\u30fc\u30e0\u3092\u30ad\u30e3\u30d7\u30c1\u30e3\r\n    ret, frame = cap.read()\r\n    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)\r\n    flow = cv2.calcOpticalFlowFarneback(prevgray, gray, None, 0.5, 3, 15, 3, 5, 1.2, 0)\r\n    # flow[\u7e26\u8ef8\u30c7\u30fc\u30bf, \u6a2a\u8ef8\u30c7\u30fc\u30bf, (x,y)]\r\n    # print flow[0,0,:] # \uff20 x,y \u3068\u3082\u306b 0 \u756a\u76ee\u306e\u30d9\u30af\u30c8\u30eb\u306e\u5927\u304d\u3055, \u89d2\u5ea6\uff08\u3067\u3042\u3063\u3066\u308b\uff1f\uff09\r\n    prevgray = gray\r\n\r\n    # \u30d7\u30ec\u30d3\u30e5\u30fc\r\n    cv2.imshow('detect preview', draw_flow(frame, gray, flow, 16))\r\n\r\n    if cv2.waitKey(1) &amp; 0xFF == ord('q'):\r\n        break\r\n        cap.release()\r\n     \r\n     cv2.destroyAllWindows()\r\n<\/pre>\n<p>\u4ee5\u524d ActionScript3 \u3067\u540c\u3058\u3088\u3046\u306a\u3053\u3068\u3092\u3084\u308d\u3046\u3068\u3057\u305f\u3068\u304d\u306f\u9014\u4e2d\u3067\u632b\u6298\u3057\u305f\u306e\u3067\u3059\u304c(\u7b11)\u3001Python + OpenCV \u3060\u3068 NumPy \u306e\u914d\u5217\u64cd\u4f5c\u304c\u5f37\u529b\u306a\u3053\u3068\u3082\u3042\u3063\u3066\u77ed\u3044\u30b3\u30fc\u30c9\u3067\u6e08\u3080\u3057\u7c21\u5358\u3067\u697d\u3057\u3044\u3067\u3059\u306d\u3002<\/p>\n<div class='wp_social_bookmarking_light'>        <div class=\"wsbl_facebook\"><a href='http:\/\/www.facebook.com\/share.php?u=https%3A%2F%2Fwww.digifie.jp%2Fblog%2Farchives%2F1448&t=Python%20OpenCV%20%E3%82%AB%E3%83%A1%E3%83%A9%E7%94%BB%E5%83%8F%E3%81%8B%E3%82%89%E3%82%AA%E3%83%97%E3%83%86%E3%82%A3%E3%82%AB%E3%83%AB%E3%83%95%E3%83%AD%E3%83%BC%E3%82%92%E5%8F%96%E5%BE%97%E3%81%97%E3%81%A6%E9%81%8A%E3%82%93%E3%81%A7%E3%81%BF%E3%81%BE%E3%81%97%E3%81%9F' title='Facebook \u306b\u30b7\u30a7\u30a2' rel=nofollow class='wp_social_bookmarking_light_a' target=_blank><img src='https:\/\/www.digifie.jp\/blog\/wp-content\/plugins\/wp-social-bookmarking-light\/public\/images\/facebook.png' alt='Facebook \u306b\u30b7\u30a7\u30a2' title='Facebook \u306b\u30b7\u30a7\u30a2' width='16' height='16' class='wp_social_bookmarking_light_img' \/><\/a><\/div>        <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.digifie.jp\/blog\/archives\/1448\" data-text=\"Python OpenCV \u30ab\u30e1\u30e9\u753b\u50cf\u304b\u3089\u30aa\u30d7\u30c6\u30a3\u30ab\u30eb\u30d5\u30ed\u30fc\u3092\u53d6\u5f97\u3057\u3066\u904a\u3093\u3067\u307f\u307e\u3057\u305f\" data-via=\"LLC DigiFie\" data-lang=\"ja\">Tweet<\/a><\/div><\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>Python + Opencv3.1 \u3067 \u30ab\u30e1\u30e9\u753b\u50cf\u304b\u3089\u30aa\u30d7\u30c6\u30a3\u30ab\u30eb\u30d5\u30ed\u30fc\uff08\u79fb\u52d5\u30d9\u30af\u30c8\u30eb\uff09\u3092\u53d6\u5f97\u3057\u3001\u3053\u308c\u3092\u30d1\u30e9\u30e1\u30fc\u30bf\u3068\u3057\u3066\u5229\u7528\u3057\u3066\u3061\u3087\u3063\u3068\u904a\u3093\u3067\u307f\u307e\u3057\u305f\u3002 \u79fb\u52d5\u30d9\u30af\u30c8\u30eb\u3092\u53d6\u5f97\u3057\u3066\u3082\u3058\u3083\u3082\u3058\u3083\u3059\u308b\u30b3\u30fc\u30c9\u306f\u3001OpenCV 3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[50,49,38],"tags":[],"_links":{"self":[{"href":"https:\/\/www.digifie.jp\/blog\/wp-json\/wp\/v2\/posts\/1448"}],"collection":[{"href":"https:\/\/www.digifie.jp\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.digifie.jp\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.digifie.jp\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.digifie.jp\/blog\/wp-json\/wp\/v2\/comments?post=1448"}],"version-history":[{"count":3,"href":"https:\/\/www.digifie.jp\/blog\/wp-json\/wp\/v2\/posts\/1448\/revisions"}],"predecessor-version":[{"id":1451,"href":"https:\/\/www.digifie.jp\/blog\/wp-json\/wp\/v2\/posts\/1448\/revisions\/1451"}],"wp:attachment":[{"href":"https:\/\/www.digifie.jp\/blog\/wp-json\/wp\/v2\/media?parent=1448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.digifie.jp\/blog\/wp-json\/wp\/v2\/categories?post=1448"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.digifie.jp\/blog\/wp-json\/wp\/v2\/tags?post=1448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}