{"id":45847,"date":"2021-12-09T14:09:18","date_gmt":"2021-12-09T05:09:18","guid":{"rendered":"https:\/\/www.charlezz.com\/?p=45847"},"modified":"2021-12-09T14:09:20","modified_gmt":"2021-12-09T05:09:20","slug":"%ec%95%88%eb%93%9c%eb%a1%9c%ec%9d%b4%eb%93%9c%eb%a1%9c-%eb%b0%b0%ec%9a%b0%eb%8a%94-opencv-%eb%b3%bc%eb%a1%9d-%ea%b2%b0%ed%95%a8-%ec%b0%be%ea%b8%b0-convexity-defects","status":"publish","type":"post","link":"https:\/\/charlezz.com\/?p=45847","title":{"rendered":"[\uc548\ub4dc\ub85c\uc774\ub4dc\ub85c \ubc30\uc6b0\ub294 OpenCV] \ubcfc\ub85d \uacb0\ud568 \ucc3e\uae30 (convexity defects)"},"content":{"rendered":"\n<p>Prerequisite : <a href=\"https:\/\/www.charlezz.com\/?p=45795\">\uc724\uacfd\uc120 \uae38\uc774 \uad6c\ud558\uae30<\/a>, <a href=\"https:\/\/www.charlezz.com\/?p=45839\">Convex hull<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\ubcfc\ub85d \uacb0\ud568 \ucc3e\uae30<\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"400\" height=\"281\" src=\"https:\/\/www.charlezz.com\/wordpress\/wp-content\/uploads\/2021\/12\/www.charlezz.com-opencv-convexity-defeats-hand-hull1.webp\" alt=\"\" class=\"wp-image-45849\"\/><\/figure>\n\n\n\n<p>\uc9c0\ub09c \uc2dc\uac04\uc5d0 Convex hull\uc5d0 \ub300\ud574\uc11c \uc54c\uc544\ubcf4\uc558\ub2e4. \uc774 \ud3ec\uc2a4\ud305\uc5d0\uc11c\ub294 \ubcfc\ub85d \uacb0\ud568(convexity defect)\ub97c \ucc3e\ub294 \ubc29\ubc95\uc5d0 \ub300\ud574\uc11c \uc54c\uc544\ubcf8\ub2e4.<\/p>\n\n\n\n<p>\ubcfc\ub85d \uacb0\ud568\uc774\ub780 convex hull\ub85c \ubd80\ud130 \uc624\ubaa9\ud558\uac8c \ub4e4\uc5b4\uac04 \uc724\uacfd\uc120 \uc911 \uac00\uc7a5 \uba40\ub9ac \ub5a8\uc5b4\uc9c4 \ubd80\ubd84\uc744 \ucc3e\ub294 \ubc29\ubc95\uc774\ub77c\uace0 \ud560 \uc218 \uc788\ub2e4.<\/p>\n\n\n\n<p>\uc790\uc138\ud55c \ub0b4\uc6a9\uc740 <a href=\"https:\/\/stackoverflow.com\/questions\/52481157\/how-convexity-defect-is-calculated-in-opencv?answertab=active#tab-top\">stackoverflow\uc758 \ud55c \ub2f5\ubcc0<\/a>\uc73c\ub85c \ub300\uccb4\ud55c\ub2e4.<\/p>\n\n\n\n<p>OpenCV\uc5d0\uc11c\ub294 \uc774 \ubcfc\ub85d \uacb0\ud568\uc744 \ucc3e\uae30\uc704\ud574 convexityDefects\ub77c\ub294 \ud568\uc218\ub97c \uc81c\uacf5\ud55c\ub2e4.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Imgproc.convexityDefects(contour, convexHull, convexityDefects)<\/pre>\n\n\n\n<p>contour: \uc724\uacfd\uc120 \uc88c\ud45c \uc815\ubcf4<br>convexHull: convex hull \uc815\ubcf4<br>convexityDefects: \ud568\uc218 \ud638\ucd9c \ud6c4 \ucc38\uc870 \ud560 \ubcfc\ub85d \uacb0\ud568 \uc815\ubcf4<\/p>\n\n\n\n<p><meta charset=\"utf-8\"><a href=\"https:\/\/www.charlezz.com\/?p=45839\">Convex hull<\/a> \ud3ec\uc2a4\ud305\uc5d0\uc11c \ub2e4\ub918\ub358 \uc190 \ubaa8\uc591 \uc608\uc81c\ub97c \uc218\uc815\ud558\uc5ec \ub123\uc5b4\ubcfc\ub85d \uacb0\ud568\uc744 \ucc3e\uc544\ubcf4\uc790.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.charlezz.com\/wordpress\/wp-content\/uploads\/2021\/12\/www.charlezz.com-opencv-convexity-defects-screenshot-1639026417591-820x1024.png\" alt=\"\" class=\"wp-image-45851\" width=\"615\" height=\"768\" srcset=\"https:\/\/charlezz.com\/wordpress\/wp-content\/uploads\/2021\/12\/www.charlezz.com-opencv-convexity-defects-screenshot-1639026417591-820x1024.png 820w, https:\/\/charlezz.com\/wordpress\/wp-content\/uploads\/2021\/12\/www.charlezz.com-opencv-convexity-defects-screenshot-1639026417591-240x300.png 240w, https:\/\/charlezz.com\/wordpress\/wp-content\/uploads\/2021\/12\/www.charlezz.com-opencv-convexity-defects-screenshot-1639026417591-768x959.png 768w, https:\/\/charlezz.com\/wordpress\/wp-content\/uploads\/2021\/12\/www.charlezz.com-opencv-convexity-defects-screenshot-1639026417591-1230x1536.png 1230w, https:\/\/charlezz.com\/wordpress\/wp-content\/uploads\/2021\/12\/www.charlezz.com-opencv-convexity-defects-screenshot-1639026417591-1640x2048.png 1640w, https:\/\/charlezz.com\/wordpress\/wp-content\/uploads\/2021\/12\/www.charlezz.com-opencv-convexity-defects-screenshot-1639026417591.png 1768w\" sizes=\"(max-width: 615px) 100vw, 615px\" \/><figcaption>\ubcfc\ub85d\uacb0\ud568\uc744 \ud30c\ub780\uc0c9\uc6d0\uc73c\ub85c \ub098\ud0c0\ub0b4\uace0 \uc788\ub2e4.<\/figcaption><\/figure>\n\n\n\n<p>\uc608\uc81c\ucf54\ub4dc:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">val contours = ArrayList&lt;MatOfPoint>()\nval hierarchy = Mat()\n\n\/\/\uc724\uacfd\uc120 \uac80\ucd9c\nImgproc.findContours(\n    binary,\n    contours,\n    hierarchy,\n    Imgproc.<em>RETR_TREE<\/em>,\n    Imgproc.<em>CHAIN_APPROX_SIMPLE\n<\/em>)\nfor (i in 0 <em>until <\/em>contours.size) {\n    \/\/ \uc724\uacfd\uc120 \uadfc\uc0ac\ud654, (\uadfc\uc0ac\ud654 \ud558\uc9c0 \uc54a\uc73c\uba74 \uc758\ub3c4\ud588\ub358 \uacb0\uacfc\ubcf4\ub2e4 \ub9ce\uc740 \ubcfc\ub85d\uacb0\ud568\uc774 \ubc1c\uacac\ub41c\ub2e4)\n    val contour2f = MatOfPoint2f(*contours[i].toArray())\n    val approxContour = MatOfPoint2f()\n    Imgproc.approxPolyDP(\n        contour2f,\n        approxContour,\n        Imgproc.arcLength(contour2f, true)*(0.005),\n        true\n    )\n    val convexHull = MatOfInt()\n    \n    \/\/ \ucee8\ubca1\uc2a4 \ud5d0 \ucc3e\uae30\n    val contour = MatOfPoint(*approxContour.toArray())\n    Imgproc.convexHull(contour, convexHull)\n    \n    \/\/ \ucee8\ubca1\uc2a4 \ud5d0 \uc815\uc810\ub9cc \ucd94\ub824\ub0b4\uae30\n    val contourArray: Array&lt;Point> = contour.toArray()\n    val hullPoints: Array&lt;Point?> = <em>arrayOfNulls<\/em>&lt;Point>(convexHull.rows()\n    val hullContourIdxList = convexHull.toList()\n    for (j in hullContourIdxList.<em>indices<\/em>) {\n        hullPoints[j] = contourArray[hullContourIdxList[j]]\n    }\n    \n    \/\/ \ucc98\uc74c\uc5d0 \uac80\ucd9c\ud55c \uc724\uacfd\uc120 \uc815\ubcf4\ub97c \ud1a0\ub300\ub85c \uadf8\ub9ac\uae30\n    Imgproc.drawContours(src, contours, i, <em>RED<\/em>)\n    \n    \/\/ \ucee8\ubca1\uc2a4 \ud5d0 \uc815\ubcf4\ub97c \ud1a0\ub300\ub85c \uadf8\ub9ac\uae30\n    Imgproc.drawContours(src, <em>listOf<\/em>(MatOfPoint(*hullPoints)),0, <em>GREEN<\/em>)\n    \n    \/\/ \ubcfc\ub85d\uacb0\ud568 \ucc3e\uae30\n    val convexDefects = MatOfInt4()\n    Imgproc.convexityDefects(contour, convexHull, convexDefects)\n    val convexDefeatList = convexDefects.toList()\n    val vertexArray = contour.toArray()\n    for(j in 0 <em>until <\/em>convexDefeatList.size <em>step <\/em>4){\n        val start = vertexArray[convexDefeatList[j]]\n        val end = vertexArray[convexDefeatList[j+1]]\n   \n        \/\/ \ucee8\ubca1\uc2a4 \ud5d0\uc758 \uc2dc\uc791\uc810(start)\uacfc \ub05d\uc810(end) \uc0ac\uc774\uc758 \ubcfc\ub85d\uacb0\ud568\n        val def = vertexArray[convexDefeatList[j+2]]\n        Imgproc.circle(src, def, 5, <em>BLUE<\/em>, 2)\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Prerequisite : \uc724\uacfd\uc120 \uae38\uc774 \uad6c\ud558\uae30, Convex hull \ubcfc\ub85d \uacb0\ud568 \ucc3e\uae30 \uc9c0\ub09c \uc2dc\uac04\uc5d0 Convex hull\uc5d0 \ub300\ud574\uc11c \uc54c\uc544\ubcf4\uc558\ub2e4. \uc774 \ud3ec\uc2a4\ud305\uc5d0\uc11c\ub294 \ubcfc\ub85d \uacb0\ud568(convexity defect)\ub97c \ucc3e\ub294 \ubc29\ubc95\uc5d0 \ub300\ud574\uc11c \uc54c\uc544\ubcf8\ub2e4. \ubcfc\ub85d \uacb0\ud568\uc774\ub780 convex hull\ub85c \ubd80\ud130 \uc624\ubaa9\ud558\uac8c \ub4e4\uc5b4\uac04 \uc724\uacfd\uc120 \uc911 \uac00\uc7a5 \uba40\ub9ac \ub5a8\uc5b4\uc9c4 \ubd80\ubd84\uc744 \ucc3e\ub294 \ubc29\ubc95\uc774\ub77c\uace0 \ud560 \uc218 \uc788\ub2e4. \uc790\uc138\ud55c \ub0b4\uc6a9\uc740 stackoverflow\uc758 \ud55c \ub2f5\ubcc0\uc73c\ub85c \ub300\uccb4\ud55c\ub2e4. OpenCV\uc5d0\uc11c\ub294 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"inline_featured_image":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[27],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/charlezz.com\/index.php?rest_route=\/wp\/v2\/posts\/45847"}],"collection":[{"href":"https:\/\/charlezz.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/charlezz.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/charlezz.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/charlezz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=45847"}],"version-history":[{"count":1,"href":"https:\/\/charlezz.com\/index.php?rest_route=\/wp\/v2\/posts\/45847\/revisions"}],"predecessor-version":[{"id":45852,"href":"https:\/\/charlezz.com\/index.php?rest_route=\/wp\/v2\/posts\/45847\/revisions\/45852"}],"wp:attachment":[{"href":"https:\/\/charlezz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=45847"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/charlezz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=45847"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/charlezz.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=45847"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}