{"id":1331,"date":"2021-03-20T16:44:13","date_gmt":"2021-03-20T07:44:13","guid":{"rendered":"http:\/\/www.ecobioinfo.com\/?p=1331"},"modified":"2022-06-05T22:12:22","modified_gmt":"2022-06-05T13:12:22","slug":"tensorflow-%e8%87%aa%e5%8b%95%e7%9d%80%e8%89%b2-opencv-%e8%bc%aa%e9%83%ad%e6%8a%bd%e5%87%ba%e3%81%a7%e8%8a%b1%e3%81%ae%e6%95%b0%e3%82%92%e3%82%ab%e3%82%a6%e3%83%b3%e3%83%88","status":"publish","type":"post","link":"https:\/\/www.ecobioinfo.com\/?p=1331","title":{"rendered":"tensorflow \u81ea\u52d5\u7740\u8272 + OpenCV \u8f2a\u90ed\u62bd\u51fa\u3067\u82b1\u306e\u6570\u3092\u30ab\u30a6\u30f3\u30c8"},"content":{"rendered":"<p>\u30c7\u30a3\u30fc\u30d7\u30e9\u30fc\u30cb\u30f3\u30b0\u306e\u81ea\u52d5\u7740\u8272\u3068 OpenCV \u306e\u8f2a\u90ed\u62bd\u51fa\u3092\u7d44\u307f\u5408\u308f\u305b\u3066\u82b1\u306e\u6570\u3092\u6570\u3048\u308b\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u4f5c\u3063\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<p>(1) \u30c7\u30a3\u30fc\u30d7\u30e9\u30fc\u30cb\u30f3\u30b0\u306e\u81ea\u52d5\u7740\u8272<br \/>\n\u30c7\u30a3\u30fc\u30d7\u30e9\u30fc\u30cb\u30f3\u30b0\u306e\u81ea\u52d5\u7740\u8272\u306b\u3064\u3044\u3066\u306f\u3001\u300c<a href=\"http:\/\/www.ecobioinfo.com\/?p=1286\">\u6df1\u5c64\u5b66\u7fd2 TensorFlow + keras \u690d\u7269\u5199\u771f\u306e\u81ea\u52d5\u7740\u8272\uff08\uff13\uff09<\/a>\u300d\u3092\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<\/p>\n<p>OpenCV \u306e\u8f2a\u90ed\u62bd\u51fa\u3067\u30e2\u30ce\u30af\u30ed\u753b\u50cf\u306e\u767d\u8272\u3068\u9ed2\u8272\u306e\u5883\u754c\u3092\u62bd\u51fa\u3059\u308b\u4e88\u5b9a\u306a\u306e\u3067\u3001\u5bfe\u8c61\u3092\u81ea\u52d5\u7740\u8272\u3067\u9ed2\u8272\u3067\u5857\u308a\u3064\u3076\u3057\u305f\u753b\u50cf\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<p>\u5143\u753b\u50cf<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.ecobioinfo.com\/wordpress\/wp-content\/uploads\/2021\/03\/countflower_test.jpg\" alt=\"countflower_test\" width=\"720\" height=\"144\" class=\"alignnone size-full wp-image-1333\" srcset=\"https:\/\/www.ecobioinfo.com\/wordpress\/wp-content\/uploads\/2021\/03\/countflower_test.jpg 720w, https:\/\/www.ecobioinfo.com\/wordpress\/wp-content\/uploads\/2021\/03\/countflower_test-300x60.jpg 300w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><\/p>\n<p>\u81ea\u52d5\u7740\u8272\u3057\u305f\u753b\u50cf<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.ecobioinfo.com\/wordpress\/wp-content\/uploads\/2021\/03\/test01.jpg\" alt=\"test01\" width=\"965\" height=\"201\" class=\"alignnone size-full wp-image-1332\" srcset=\"https:\/\/www.ecobioinfo.com\/wordpress\/wp-content\/uploads\/2021\/03\/test01.jpg 965w, https:\/\/www.ecobioinfo.com\/wordpress\/wp-content\/uploads\/2021\/03\/test01-300x62.jpg 300w, https:\/\/www.ecobioinfo.com\/wordpress\/wp-content\/uploads\/2021\/03\/test01-768x160.jpg 768w\" sizes=\"auto, (max-width: 965px) 100vw, 965px\" \/><\/p>\n<p>&nbsp;<br \/>\n<br \/>\n&nbsp;<br \/>\n(2) OpenCV \u30d7\u30ed\u30b0\u30e9\u30e0\uff08python\uff09<\/p>\n<p>\u30b0\u30ec\u30fc\u30b9\u30b1\u30fc\u30eb\u3092\u4e8c\u5024\u5316\u3057\u3066\u30ce\u30a4\u30ba\u9664\u53bb\u3001\u5c0f\u3055\u3044\u8f2a\u90ed\u306f\u8aa4\u691c\u51fa\u3068\u3057\u3066\u30ab\u30a6\u30f3\u30c8\u3057\u306a\u3044\u51e6\u7406\u3092\u5165\u308c\u3066\u3044\u307e\u3059\u3002<br \/>\n\uff08\u857e\u3084\u9670\u306b\u96a0\u308c\u305f\u82b1\u306f\u30ab\u30a6\u30f3\u30c8\u3057\u306a\u3044\u3053\u3068\u306b\u3057\u307e\u3059\u3002\uff09<\/p>\n<p>\uff08\u4e00\u90e8\u629c\u7c8b\uff09&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\nthreshold = 4<br \/>\ncounter   = 0<\/p>\n<p>window_name01 = &#8216;gray image&#8217;<br \/>\nwindow_name02 = &#8216;count_image&#8217;<br \/>\ncv2.namedWindow(window_name01)<br \/>\ncv2.namedWindow(window_name02)<\/p>\n<p># \u9818\u57df\u306b\u63cf\u753b<br \/>\ndef draw_contours(img, contours):<br \/>\n&nbsp;&nbsp;for i, cnt in enumerate(contours):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;        # \u8f2a\u90ed\u306e\u756a\u53f7\u3092\u63cf\u753b\u3059\u308b\u3002<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;        cnt = cnt.squeeze(axis=1)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;        cv2.putText(img, &#8216;{0}&#8217;.format(i+1), (cnt[0][0], cnt[0][1]), cv2.FONT_HERSHEY_COMPLEX, 1.0, (128,128,128))<\/p>\n<p>### \u30e1\u30a4\u30f3 ###<br \/>\n# \u81ea\u52d5\u7740\u8272\u7d50\u679c\u306e\u753b\u50cf countflowers.jp \u3092\u8aad\u307f\u8fbc\u3080\uff08\u30b0\u30ec\u30fc\u30b9\u30b1\u30fc\u30eb\u5909\u63db\uff09<br \/>\nimg_gray = cv2.imread(&#8216;countflowers.jpg&#8217;, cv2.IMREAD_GRAYSCALE)<\/p>\n<p>if img_gray is None:<br \/>\n&nbsp;&nbsp;    print(&#8216;image file error!&#8217;)<br \/>\nelse :<br \/>\n&nbsp;&nbsp;    # \u4e8c\u5024\u5316<br \/>\n&nbsp;&nbsp;    (retval, new_img) = cv2.threshold(img_gray, threshold, 255, cv2.THRESH_BINARY_INV)<br \/>\n&nbsp;&nbsp;    # \u30ce\u30a4\u30ba\u9664\u53bb<br \/>\n&nbsp;&nbsp;    new_img = cv2.morphologyEx(new_img, cv2.MORPH_OPEN,  None, iterations=2)<br \/>\n&nbsp;&nbsp;    new_img = cv2.morphologyEx(new_img, cv2.MORPH_CLOSE, None, iterations=2)<br \/>\n&nbsp;&nbsp;    # \u9818\u57df\u3092\u62bd\u51fa<br \/>\n&nbsp;&nbsp;    contours, hierarchy = cv2.findContours(new_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)<br \/>\n&nbsp;&nbsp;    # \u5c0f\u3055\u3044\u8f2a\u90ed\u306f\u8aa4\u691c\u51fa\u3068\u3059\u308b<br \/>\n&nbsp;&nbsp;    contours = list(filter(lambda x: cv2.contourArea(x) > 50, contours))<br \/>\n&nbsp;&nbsp;    counter = len(contours)<br \/>\n&nbsp;&nbsp;    print(&#8216;count={0}&#8217;.format(counter))<br \/>\n&nbsp;&nbsp;    # \u9818\u57df\u3092\u63cf\u753b<br \/>\n&nbsp;&nbsp;    draw_contours(new_img, contours)<br \/>\n&nbsp;&nbsp;    # \u30a4\u30e1\u30fc\u30b8\u3092\u8868\u793a<br \/>\n&nbsp;&nbsp;    cv2.imshow(window_name01, img_gray)<br \/>\n&nbsp;&nbsp;    cv2.imshow(window_name02, new_img)<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n<br \/>\n\u5b9f\u884c\u7d50\u679c<\/p>\n<p>(tf) >python  countflowers.py<br \/>\ncount=7<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.ecobioinfo.com\/wordpress\/wp-content\/uploads\/2021\/03\/countflower_result.jpg\" alt=\"countflower_result\" width=\"965\" height=\"465\" class=\"alignnone size-full wp-image-1334\" srcset=\"https:\/\/www.ecobioinfo.com\/wordpress\/wp-content\/uploads\/2021\/03\/countflower_result.jpg 965w, https:\/\/www.ecobioinfo.com\/wordpress\/wp-content\/uploads\/2021\/03\/countflower_result-300x145.jpg 300w, https:\/\/www.ecobioinfo.com\/wordpress\/wp-content\/uploads\/2021\/03\/countflower_result-768x370.jpg 768w\" sizes=\"auto, (max-width: 965px) 100vw, 965px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>\u540c\u3058\u3088\u3046\u306a\u3053\u3068\u306f\u7269\u4f53\u8a8d\u8b58\u3067\u3082\u3067\u304d\u308b\u3068\u601d\u3044\u307e\u3059\u304c\u3001<br \/>\n\u4eca\u56de\u306f\u6bd4\u8f03\u7684\u5b89\u304f\u7c21\u5358\u306a\u65b9\u6cd5\u3068\u3057\u3066\u81ea\u52d5\u7740\u8272\u3067\u51e6\u7406\u3057\u305f\u753b\u50cf\u3092\u8f2a\u90ed\u62bd\u51fa\u3067\u5229\u7528\u3059\u308b\u65b9\u6cd5\u3092\u8a66\u3057\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<p>\n\uff082021\/3\/20 S.Onda\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"\u30c7\u30a3\u30fc\u30d7\u30e9\u30fc\u30cb\u30f3\u30b0\u306e\u81ea\u52d5\u7740\u8272\u3068 OpenCV \u306e\u8f2a\u90ed\u62bd\u51fa\u3092\u7d44\u307f\u5408\u308f\u305b\u3066\u82b1\u306e\u6570\u3092\u6570\u3048\u308b\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u4f5c\u3063\u3066\u307f\u307e\u3057\u305f\u3002 (1) \u30c7\u30a3\u30fc\u30d7\u30e9\u30fc\u30cb\u30f3\u30b0\u306e\u81ea\u52d5\u7740\u8272 \u30c7\u30a3\u30fc\u30d7\u30e9\u30fc\u30cb\u30f3\u30b0\u306e\u81ea\u52d5\u7740\u8272\u306b\u3064\u3044\u3066\u306f\u3001\u300c\u6df1\u5c64\u5b66\u7fd2 TensorFl &#8230;","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-1331","post","type-post","status-publish","format-standard","hentry","category-ai_ecobio"],"_links":{"self":[{"href":"https:\/\/www.ecobioinfo.com\/index.php?rest_route=\/wp\/v2\/posts\/1331","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ecobioinfo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ecobioinfo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ecobioinfo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ecobioinfo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1331"}],"version-history":[{"count":10,"href":"https:\/\/www.ecobioinfo.com\/index.php?rest_route=\/wp\/v2\/posts\/1331\/revisions"}],"predecessor-version":[{"id":1344,"href":"https:\/\/www.ecobioinfo.com\/index.php?rest_route=\/wp\/v2\/posts\/1331\/revisions\/1344"}],"wp:attachment":[{"href":"https:\/\/www.ecobioinfo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1331"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ecobioinfo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1331"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ecobioinfo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1331"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}