{"id":6594,"date":"2019-04-03T16:46:53","date_gmt":"2019-04-03T07:46:53","guid":{"rendered":"http:\/\/www.gisdeveloper.co.kr\/?p=6594"},"modified":"2020-05-28T12:29:44","modified_gmt":"2020-05-28T03:29:44","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-4-5","status":"publish","type":"post","link":"http:\/\/www.gisdeveloper.co.kr\/?p=6594","title":{"rendered":"Python\uacfc OpenCV \u2013 18 : \uc774\ubbf8\uc9c0\uc758 \ub4f1\uce58\uc120(Contours) \u2013 4\/5"},"content":{"rendered":"<p>\uc774 \uae00\uc758 \uc6d0\ubb38\uc740 https:\/\/opencv-python-tutroals.readthedocs.io\/en\/latest\/py_tutorials\/py_imgproc\/py_contours\/py_contours_more_functions\/py_contours_more_functions.html#contours-more-functions \uc785\ub2c8\ub2e4.<\/p>\n<p>\ub4f1\uce58\uc120\uc5d0 \ub300\ud55c \ubcfc\ub85d\uaecd\uc9c8(Convex Hull) \uc5f0\uc0b0\uc5d0\uc11c \uc624\ubaa9\ud55c \ubd80\ubd84(\uc989, \ube14\ub7ed\ud55c \ubd80\ubd84\uc5d0 \ub300\ud55c \uacb0\ud569)\uc744 \ubc1c\uacac\ud558\ub294 \ud568\uc218\uc640 \ub4f1\uce58\uc120\uc73c\ub85c \ub9cc\ub4e4\uc5b4\uc9c0\ub294 \ub3c4\ud615(\ub610\ub294 \uac1d\uccb4)\uc640 \uc784\uc774\uc758 \ud3ec\uc778\ud2b8\uc5d0\uc11c\uc758 \uac70\ub9ac\ub97c \uad6c\ud558\ub294 \ud568\uc218, \ub9c8\uc9c0\ub9c9\uc73c\ub85c \uac1d\uccb4\uac04\uc758 \uc720\uc0ac\uc131 \uc815\ub3c4\ub97c \ud558\ub098\uc758 \uac12\uc73c\ub85c \ud2b9\uc815\ud558\ub294 \ud568\uc218\uc5d0 \ub300\ud574 \uc815\ub9ac\ud569\ub2c8\ub2e4.<\/p>\n<p>\uba3c\uc800 \ubcfc\ub85d \uaecd\uc9c8 \uc5f0\uc0b0\uc5d0\uc11c \uc624\ubaa9\ud55c \ubd80\ubd84\uc744 \uc2dd\ubcc4\ud558\ub294 \uc608\uc81c\ub294 \uc544\ub798\uc640 \uac19\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\/thunder.png')\r\nimgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)\r\nret,thresh = cv2.threshold(imgray,127,255,0)\r\nimage, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)\r\n \r\ncnt = contours[0]\r\nhull = cv2.convexHull(cnt,returnPoints = False)\r\ndefects = cv2.convexityDefects(cnt,hull)\r\n\r\nprint(defects)\r\n\r\nfor i in range(defects.shape[0]):\r\n    s,e,f,d = defects[i,0]\r\n    start = tuple(cnt[s][0])\r\n    end = tuple(cnt[e][0])\r\n    far = tuple(cnt[f][0])\r\n    cv2.line(img,start,end,[0,255,0],2)\r\n    cv2.circle(img,far,5,[0,0,255],-1)\r\n\r\ncv2.imshow('img', img)\r\ncv2.waitKey()\r\ncv2.destroyAllWindows()\r\n<\/pre>\n<p>\uacb0\uacfc\ub294 \uc544\ub798\uc640 \uac19\uc740\ub370\uc694..<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gisdeveloper.co.kr\/wp-content\/uploads\/2019\/04\/opencv_convex_defects.png\" alt=\"\" width=\"629\" height=\"660\" class=\"aligncenter size-full wp-image-6595\" \/><\/p>\n<p>\uc791\uc740 \ube68\uac04 \uc6d0\uc73c\ub85c \ud45c\uc2dc\ub418\ub294 \ubd80\ubd84\uc774 \ubcfc\ub85d\uaecd\uc9c8 \uc5f0\uc0b0\uc5d0 \uc788\uc5b4\uc11c \uc624\ubaa9\ud55c \ubd80\ubd84\uc73c\ub85c \uc2dd\ubcc4\ub41c \uc9c0\uc810\uc785\ub2c8\ub2e4. \uadf8\ub9ac\uace0 \ucd08\ub85d\uc0c9\uc120\uc740 \uc624\ubaa9\ud55c \uc9c0\uc810\uc5d0 \ub300\ud574 \ubcfc\ub85d\ud558\uac8c \ucc98\ub9ac\ub41c \uc120\ubd84\uc785\ub2c8\ub2e4.<\/p>\n<p>\ub4f1\uce58\uc120\uc73c\ub85c \uad6c\uc131\ub41c \uac1d\uccb4\uc5d0 \ub300\ud574 \uc5b4\ub5a4 \uc88c\ud45c\uc5d0\uc11c\uc758 \uac70\ub9ac\ub97c \uc5bb\ub294 \uc608\uc81c\ub294 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\ndist = cv2.pointPolygonTest(cnt,(50,50),True)\r\n<\/pre>\n<p>\uc704\uc758 \uc608\uc81c\ub294 (50,50) \uc88c\ud45c\uc5d0\uc11c \ub4f1\uce58\uc120\uae4c\uc9c0\uc758 \uac70\ub9ac\ub97c \uc5bb\uc2b5\ub2c8\ub2e4. \ub4f1\uce58\uc120\uc73c\ub85c \uad6c\uc131\ub41c \ub3c4\ud615\uc758 \ub0b4\ubd80\uc5d0 \ud3ec\uc778\ud2b8 \uc88c\ud45c(\uc5ec\uae30\uc11c\ub294 50,50)\uc774 \uc874\uc7ac\ud558\uba74 \uc591\uc218\uac00, \ubc16\uc5d0 \uc874\uc7ac\ud558\uba74 \uc74c\uc218\uac00, \ub4f1\uce58\uc120 \uc0c1\uc5d0 \uc815\ud655\uc774 \uc704\uce58\ud558\uba74 0\uc778 \uac70\ub9ac\uac00 \ubc18\ud658\ub429\ub2c8\ub2e4. pointPolygonTest \ud568\uc218\uc758 \uc138\ubc88\uc9f8 \uc778\uc790\ub294 True\uc778\ub370, \uc774\ub97c False\ub85c \uc9c0\uc815\ud558\uba74 \uac70\ub9ac\uac12\uc774 \uc544\ub2cc -1, 0, 1 \uc911 \ud558\ub098\uc758 \uac12\uc774 \ubc18\ud658\ub429\ub2c8\ub2e4. \uc774\uac12\ub4e4\uc740 \uac01\uac01 \ub3c4\ud615\uc758 \uc678\ubd80, \uacbd\uacc4, \ub0b4\ubd80\uc778\uc9c0\uc758 \uc5ec\ubd80\ub97c \ub098\ud0c0\ub0b4\ub294 \ubd80\ud638\uac12\uc785\ub2c8\ub2e4.<\/p>\n<p>\ub9c8\uc9c0\ub9c9\uc73c\ub85c \ub3c4\ud615\uc5d0 \ub300\ud574 \uc720\uc0ac\uc131\uc744 \ud558\ub098\uc758 \uc218\uce58\uac12\uc73c\ub85c \ud2b9\uc815\ud560 \uc218\uac00 \uc788\ub294\ub370, \uc608\uc81c\ub97c \uc0b4\ud3b4\ubcf4\uba74..<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\nimport numpy as np\r\nimport cv2\r\n\r\nimg1 = cv2.imread('.\/data\/shapes\/1.png')\r\nimg1 = cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY)\r\n\r\nimg2 = cv2.imread('.\/data\/shapes\/2.png')\r\nimg2 = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)\r\n\r\nret, thresh1 = cv2.threshold(img1, 127, 255,0)\r\nret, thresh2 = cv2.threshold(img2, 127, 255,0)\r\n\r\n_, contours,hierarchy = cv2.findContours(thresh1,2,1)\r\ncnt1 = contours[0]\r\n\r\n_, contours,hierarchy = cv2.findContours(thresh2,2,1)\r\ncnt2 = contours[0]\r\n\r\nret = cv2.matchShapes(cnt1,cnt2,1,0.0)\r\nprint(ret)\r\n<\/pre>\n<p>\uc704\uc758 \uc608\uc81c\ub294 1.png \ud30c\uc77c\uacfc 2.png \ud30c\uc77c\uc5d0 \ub300\ud55c \ub4f1\uce58\uc120\uc744 \ucd94\ucd9c\ud558\uace0 \uc774 \ub4f1\uce58\uc120\uc73c\ub85c \uad6c\uc131\ub41c \ub3c4\ud615\uc5d0 \ub300\ud55c \uc720\uc0ac\uc131\uc744 \ud558\ub098\uc758 \uac12\uc73c\ub85c \ud2b9\uc815\ud558\ub294 \uc608\uc81c\uc785\ub2c8\ub2e4. \uc785\ub825 \uc774\ubbf8\uc9c0 \ud30c\uc77c\uc774 \uc544\ub798\uc640 \uac19\uc740\ub370\uc694. \ud30c\uc77c\uba85\ub9cc \ud45c\uc2dc\ud558\uace0 \ud655\uc7a5\uc790\uc778 png\ub294 \uc0dd\ub7b5\ub418\uc5c8\uc2b5\ub2c8\ub2e4.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gisdeveloper.co.kr\/wp-content\/uploads\/2019\/04\/opencv_contur_shapes.png\" alt=\"\" width=\"694\" height=\"544\" class=\"aligncenter size-full wp-image-6597\" \/><\/p>\n<p>1.png \ud30c\uc77c\uc5d0 \ub300\ud574 \ub098\uba38\uc9c0 \ud30c\uc77c\ub4e4\uc5d0 \ub300\ud55c \uc720\uc0ac\uc131 \uac12\uc744 \uc704\uc758 \ucf54\ub4dc\ub97c \ud1b5\ud574 \ucd9c\ub825\ud574\ubcf4\uba74 \uac01\uac01 \uc544\ub798\uc640 \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\n1.png\uc640 2.png \uac04\uc758 cv2.matchShapes \ubc18\ud658\uac12 = 0.16<br \/>\n1.png\uc640 3.png \uac04\uc758 cv2.matchShapes \ubc18\ud658\uac12 = 14.1<br \/>\n1.png\uc640 4.png \uac04\uc758 cv2.matchShapes \ubc18\ud658\uac12 = 0.11<br \/>\n1.png\uc640 5.png \uac04\uc758 cv2.matchShapes \ubc18\ud658\uac12 = 0.26<br \/>\n1.png\uc640 6.png \uac04\uc758 cv2.matchShapes \ubc18\ud658\uac12 = 0.26<br \/>\n1.png\uc640 7.png \uac04\uc758 cv2.matchShapes \ubc18\ud658\uac12 = 0.32<br \/>\n1.png\uc640 8.png \uac04\uc758 cv2.matchShapes \ubc18\ud658\uac12 = 0.32<br \/>\n1.png\uc640 9.png \uac04\uc758 cv2.matchShapes \ubc18\ud658\uac12 = 0.17<br \/>\n1.png\uc640 10.png \uac04\uc758 cv2.matchShapes \ubc18\ud658\uac12 = 0.005<br \/>\n1.png\uc640 11.png \uac04\uc758 cv2.matchShapes \ubc18\ud658\uac12 = 1.03<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\uc774 \uae00\uc758 \uc6d0\ubb38\uc740 https:\/\/opencv-python-tutroals.readthedocs.io\/en\/latest\/py_tutorials\/py_imgproc\/py_contours\/py_contours_more_functions\/py_contours_more_functions.html#contours-more-functions \uc785\ub2c8\ub2e4. \ub4f1\uce58\uc120\uc5d0 \ub300\ud55c \ubcfc\ub85d\uaecd\uc9c8(Convex Hull) \uc5f0\uc0b0\uc5d0\uc11c \uc624\ubaa9\ud55c \ubd80\ubd84(\uc989, \ube14\ub7ed\ud55c \ubd80\ubd84\uc5d0 \ub300\ud55c \uacb0\ud569)\uc744 \ubc1c\uacac\ud558\ub294 \ud568\uc218\uc640 \ub4f1\uce58\uc120\uc73c\ub85c \ub9cc\ub4e4\uc5b4\uc9c0\ub294 \ub3c4\ud615(\ub610\ub294 \uac1d\uccb4)\uc640 \uc784\uc774\uc758 \ud3ec\uc778\ud2b8\uc5d0\uc11c\uc758 \uac70\ub9ac\ub97c \uad6c\ud558\ub294 \ud568\uc218, \ub9c8\uc9c0\ub9c9\uc73c\ub85c \uac1d\uccb4\uac04\uc758 \uc720\uc0ac\uc131 \uc815\ub3c4\ub97c \ud558\ub098\uc758 \uac12\uc73c\ub85c \ud2b9\uc815\ud558\ub294 \ud568\uc218\uc5d0 \ub300\ud574 \uc815\ub9ac\ud569\ub2c8\ub2e4. \uba3c\uc800 \ubcfc\ub85d \uaecd\uc9c8 \uc5f0\uc0b0\uc5d0\uc11c \uc624\ubaa9\ud55c \ubd80\ubd84\uc744 \uc2dd\ubcc4\ud558\ub294 \uc608\uc81c\ub294 \uc544\ub798\uc640 \uac19\uc2b5\ub2c8\ub2e4. import numpy as np import cv2 img = &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/www.gisdeveloper.co.kr\/?p=6594\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Python\uacfc OpenCV \u2013 18 : \uc774\ubbf8\uc9c0\uc758 \ub4f1\uce58\uc120(Contours) \u2013 4\/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-6594","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\/6594","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=6594"}],"version-history":[{"count":7,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/6594\/revisions"}],"predecessor-version":[{"id":9466,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/6594\/revisions\/9466"}],"wp:attachment":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6594"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6594"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}