{"id":6617,"date":"2019-04-06T01:05:35","date_gmt":"2019-04-05T16:05:35","guid":{"rendered":"http:\/\/www.gisdeveloper.co.kr\/?p=6617"},"modified":"2020-05-28T12:29:05","modified_gmt":"2020-05-28T03:29:05","slug":"python%ea%b3%bc-opencv-16-%ec%9d%b4%eb%af%b8%ec%a7%80%ec%9d%98-%eb%93%b1%ec%b9%98%ec%84%a0contours-5-5","status":"publish","type":"post","link":"http:\/\/www.gisdeveloper.co.kr\/?p=6617","title":{"rendered":"Python\uacfc OpenCV \u2013 19 : \uc774\ubbf8\uc9c0\uc758 \ub4f1\uce58\uc120(Contours) \u2013 5\/5"},"content":{"rendered":"<p>\uc774 \uae00\uc740 https:\/\/opencv-python-tutroals.readthedocs.io\/en\/latest\/py_tutorials\/py_imgproc\/py_contours\/py_contours_hierarchy\/py_contours_hierarchy.html#contours-hierarchy \ub97c \ucc38\uc870\ub85c \ud558\uc600\uc2b5\ub2c8\ub2e4.<\/p>\n<p>\uba3c\uc800 \ub2e4\uc74c\uacfc \uac19\uc740 \ucf54\ub4dc\uac00 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\nimport numpy as np\r\nimport cv2\r\n\r\nimg = cv2.imread('.\/data\/opencv_contour_h.png')\r\nimgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\r\nret, thresh = cv2.threshold(imgray, 127, 255,0)\r\n\r\n_, contours,hierarchy = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)\r\n\r\nprint(hierarchy)\r\ncv2.imshow('img', img)\r\n\r\nfor i in range(len(contours)):\r\n    cv2.waitKey()\r\n    img = cv2.drawContours(img, contours, i, (0,0,255), 2)\r\n    cv2.imshow('img', img)\r\n\r\nprint('END')\r\ncv2.waitKey()\r\ncv2.destroyAllWindows()\r\n<\/pre>\n<p>8\ubc88 \ucf54\ub4dc\uc758 cv2.findContours \ud568\uc218\uc758 2\ubc88\uc9f8 \uc778\uc790\uc5d0 \ub530\ub77c \uadf8 \uacb0\uacfc\uac00 \ub2ec\ub77c\uc9c0\ub294\ub370, \ud2b9\ud788 \ubc18\ud658\uac12 \uc911 3\ubc88\uc9f8\uc778 hierarchy \uac12\uc774 \ud06c\uac8c \ub2ec\ub77c\uc9d1\ub2c8\ub2e4. hierarchy \uac12\uc740 \ucd94\ucd9c\ub41c \ub4f1\uce58\uc120 \uac04\uc758 \uacc4\uce35 \uc815\ubcf4\ub97c \ub098\ud0c0\ub0c5\ub2c8\ub2e4. cv2.findContours \ud568\uc218\uc758 2\ubc88\uc9f8 \uc778\uc790\uc5d0 \ub530\ub77c \uc5b4\ub5bb\uac8c \ubcc0\uacbd\ub418\ub294\uc9c0 \uc694\uc57d \uadf8\ub9bc\uc744 \uc5b8\uae09\ud558\ub294 \uc120\uc5d0\uc11c \uc815\ub9ac\ud569\ub2c8\ub2e4.<\/p>\n<p>\uba3c\uc800 cv2.RETR_LIST \uc77c \uacbd\uc6b0, \ucd94\ucd9c\ub41c \ub4f1\uce58\uc120\uc758 \uc778\ub371\uc2a4 \ubc88\ud638\uc5d0 \ub300\ud55c \uadf8\ub9bc\uc785\ub2c8\ub2e4.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gisdeveloper.co.kr\/wp-content\/uploads\/2019\/04\/opencv_RETR_LIST.png\" alt=\"\" width=\"1247\" height=\"946\" class=\"aligncenter size-full wp-image-6623\" \/><\/p>\n<p>\uadf8\ub9ac\uace0 \ubc18\ud658\ub41c \uacc4\uce35 \uc815\ubcf4\ub294 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\n[[[ 1 -1 -1 -1]\r\n  [ 2  0 -1 -1]\r\n  [ 3  1 -1 -1]\r\n  [ 4  2 -1 -1]\r\n  [ 5  3 -1 -1]\r\n  [ 6  4 -1 -1]\r\n  [ 7  5 -1 -1]\r\n  [ 8  6 -1 -1]\r\n  [-1  7 -1 -1]]]\r\n<\/pre>\n<p>\ucd1d 9\uac1c\uc758 \ub4f1\uce58\uc120\uc774 \ucd94\ucd9c\ub418\uc5c8\uc73c\ubbc0\ub85c \uc704\uc640 \uac19\uc774 \ucd1d 9\uac1c\uc758 \uacc4\uce35 \uc815\ubcf4\uac00 \ubc18\ud658\ub418\ub294\ub370 \uc704\uc758 \uac01 9\uac1c \uc694\uc18c\uc758 \uc21c\uc11c\ub294 \uc55e\uc120 \uadf8\ub9bc\uc5d0\uc11c \ud45c\uc2dc\ub41c \ub4f1\uce58\uc120\uc758 \uc778\ub371\uc2a4 \uc21c\uc11c\uc640 \ub3d9\uc77c\ud569\ub2c8\ub2e4. 0\ubd80\ud130 \uc2dc\uc791\ud558\uace0\uc694. \uadf8\ub9ac\uace0 \uac01 \uc694\uc18c\ub294 \ub2e4\uc2dc 4\uac1c\ub85c \uad6c\uc131\ub418\ub294\ub370.. [Next, Previous, First_Child, Parent]\uc640 \uac19\uc2b5\ub2c8\ub2e4. \uc989, [\ub2e4\uc74c \ub4f1\uce58\uc120\uc758 \uc778\ub371\uc2a4, \uc774\uc804 \ub4f1\uce58\uc120\uc758 \uc778\ub371\uc2a4, \uccab\ubc88\uc9f8 \uc790\uc2dd \ub4f1\uce58\uc120\uc758 \uc778\ub371\uc2a4, \ubd80\ubaa8 \ub4f1\uce58\uc120\uc758 \uc778\ub371\uc2a4] \uc785\ub2c8\ub2e4. \uc704\uc758 \uacc4\uce35 \uc815\ubcf4\uc5d0 \ub300\ud55c \ub0b4\uc6a9\uc740 \ub2e4\uc74c \uadf8\ub9bc\uc73c\ub85c \ud45c\uc2dc\ub420 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gisdeveloper.co.kr\/wp-content\/uploads\/2019\/04\/opencv_RETR_LIST_r.png\" alt=\"\" width=\"1001\" height=\"270\" class=\"aligncenter size-full wp-image-6621\" \/><\/p>\n<p>\ub2e4\uc74c\uc740 cv2.RETR_EXTERNAL \uc778\uc790\uc5d0 \ub300\ud55c \ub4f1\uce58\uc120\uc758 \uc778\ub371\uc2a4 \ubc88\ud638\uc5d0 \ub300\ud55c \uadf8\ub9bc\uc785\ub2c8\ub2e4.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gisdeveloper.co.kr\/wp-content\/uploads\/2019\/04\/opencv_RETR_EXTERNAL.png\" alt=\"\" width=\"1247\" height=\"945\" class=\"aligncenter size-full wp-image-6625\" \/><\/p>\n<p>\uacc4\uce35 \uc815\ubcf4\ub294 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\n[[[ 1 -1 -1 -1]\r\n  [ 2  0 -1 -1]\r\n  [-1  1 -1 -1]]]\r\n<\/pre>\n<p>\uc704 \uacc4\uce35\uc815\ubcf4\uc758 \uc758\ubbf8\ub294 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gisdeveloper.co.kr\/wp-content\/uploads\/2019\/04\/opencv_RETR_EXTERNAL_r.png\" alt=\"\" width=\"360\" height=\"93\" class=\"aligncenter size-full wp-image-6626\" \/><\/p>\n<p>\ub2e4\uc74c\uc740 cv2.RETR_CCOMP \uc778\uc790\uc5d0 \ub300\ud55c \ub4f1\uce58\uc120\uc758 \uc778\ub371\uc2a4 \ubc88\ud638\uc5d0 \ub300\ud55c \uadf8\ub9bc\uc785\ub2c8\ub2e4.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gisdeveloper.co.kr\/wp-content\/uploads\/2019\/04\/opencv_RETR_CCOMP.png\" alt=\"\" width=\"1247\" height=\"946\" class=\"aligncenter size-full wp-image-6628\" \/><\/p>\n<p>\uacc4\uce35 \uc815\ubcf4\ub294 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\n[[[ 3 -1  1 -1]\r\n  [ 2 -1 -1  0]\r\n  [-1  1 -1  0]\r\n  [ 5  0  4 -1]\r\n  [-1 -1 -1  3]\r\n  [ 7  3  6 -1]\r\n  [-1 -1 -1  5]\r\n  [ 8  5 -1 -1]\r\n  [-1  7 -1 -1]]]\r\n<\/pre>\n<p>\uc704 \uacc4\uce35\uc815\ubcf4\uc758 \uc758\ubbf8\ub294 \ub2e4\uc74c\uacfc \uac19\uace0\uc694.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gisdeveloper.co.kr\/wp-content\/uploads\/2019\/04\/opencv_RETR_CCOMP_r.png\" alt=\"\" width=\"681\" height=\"198\" class=\"aligncenter size-full wp-image-6629\" \/><\/p>\n<p>\ub9c8\uc9c0\ub9c9\uc73c\ub85c cv2.RETR_TREE \uc778\uc790\uc5d0 \ub300\ud55c \ub4f1\uce58\uc120\uc758 \uc778\ub371\uc2a4 \ubc88\ud638\uc5d0 \ub300\ud55c \uadf8\ub9bc\uc785\ub2c8\ub2e4.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gisdeveloper.co.kr\/wp-content\/uploads\/2019\/04\/opencv_RETR_TREE.png\" alt=\"\" width=\"1247\" height=\"945\" class=\"aligncenter size-full wp-image-6631\" \/><\/p>\n<p>\uacc4\uce35 \uc815\ubcf4\ub294 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\n[[[ 7 -1  1 -1]\r\n  [-1 -1  2  0]\r\n  [-1 -1  3  1]\r\n  [-1 -1  4  2]\r\n  [-1 -1  5  3]\r\n  [ 6 -1 -1  4]\r\n  [-1  5 -1  4]\r\n  [ 8  0 -1 -1]\r\n  [-1  7 -1 -1]]]\r\n<\/pre>\n<p>\uc704\uc758 \uacc4\uce35\uc815\ubcf4\uc758 \uc758\ubbf8\ub97c \ub3c4\uc2dd\ud654\ud558\uba74 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gisdeveloper.co.kr\/wp-content\/uploads\/2019\/04\/opencv_RETR_TREE_r.png\" alt=\"\" width=\"453\" height=\"541\" class=\"aligncenter size-full wp-image-6632\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\uc774 \uae00\uc740 https:\/\/opencv-python-tutroals.readthedocs.io\/en\/latest\/py_tutorials\/py_imgproc\/py_contours\/py_contours_hierarchy\/py_contours_hierarchy.html#contours-hierarchy \ub97c \ucc38\uc870\ub85c \ud558\uc600\uc2b5\ub2c8\ub2e4. \uba3c\uc800 \ub2e4\uc74c\uacfc \uac19\uc740 \ucf54\ub4dc\uac00 \uc788\uc2b5\ub2c8\ub2e4. import numpy as np import cv2 img = cv2.imread(&#8216;.\/data\/opencv_contour_h.png&#8217;) imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ret, thresh = cv2.threshold(imgray, 127, 255,0) _, contours,hierarchy = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) print(hierarchy) cv2.imshow(&#8216;img&#8217;, img) for i in range(len(contours)): cv2.waitKey() img = cv2.drawContours(img, contours, i, (0,0,255), 2) cv2.imshow(&#8216;img&#8217;, img) print(&#8216;END&#8217;) cv2.waitKey() &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/www.gisdeveloper.co.kr\/?p=6617\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Python\uacfc OpenCV \u2013 19 : \uc774\ubbf8\uc9c0\uc758 \ub4f1\uce58\uc120(Contours) \u2013 5\/5&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[130,131],"tags":[],"class_list":["post-6617","post","type-post","status-publish","format-standard","hentry","category-opencv","category-python"],"_links":{"self":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/6617","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6617"}],"version-history":[{"count":10,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/6617\/revisions"}],"predecessor-version":[{"id":9463,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/6617\/revisions\/9463"}],"wp:attachment":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6617"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}