{"id":9079,"date":"2016-11-13T11:28:51","date_gmt":"2016-11-13T16:28:51","guid":{"rendered":"httpss:\/\/www.powenko.com\/wordpress\/?p=9079"},"modified":"2016-11-13T11:37:06","modified_gmt":"2016-11-13T16:37:06","slug":"02-%e5%87%b8%e6%ae%bcconvexhull","status":"publish","type":"post","link":"https:\/\/www.powenko.com\/wordpress\/?p=9079","title":{"rendered":"02 \u51f8\u6bbc(convexHull)"},"content":{"rendered":"<header class=\"entry-header\">\n<h1 class=\"entry-title\">\u51f8\u6bbc(convexHull)<\/h1>\n<\/header>\n<div class=\"entry-content\">\n<p>\u51f8\u6bbc(Convex Hull)\u662f\u4e00\u500b\u8a08\u7b97\u5e7e\u4f55\u4e2d\u7684\u6982\u5ff5\uff0c\u5728\u7d66\u5b9a\u4e8c\u7dad\u5e73\u9762\u4e0a\u7684\u9ede\u96c6\u5408\uff0c\u51f8\u6bbc\u5c31\u662f\u5c07\u6700\u5916\u5c64\u7684\u9ede\u9023\u63a5\u8d77\u4f86\u7684\u51f8\u591a\u908a\u578b\uff0c<br \/>\n\u80fd\u5305\u542b\u9ede\u96c6\u5408\u4e2d\u7684\u6240\u6709\u9ede\uff0c\u5728\u5f71\u50cf\u8655\u7406\u4e2d\uff0c\u901a\u5e38\u662f\u627e\u5230\u67d0\u500b\u7269\u4ef6\u5f8c\uff0c\u7528\u4f86\u586b\u88dc\u7a7a\u9699\uff0c\u6216\u8005\u662f\u9032\u4e00\u6b65\u7684\u9032\u884c\u7269\u4ef6\u8fa8\u8b58\u3002<\/p>\n<h4>OpenCV\u51f8\u6bbc<\/h4>\n<p>void convexHull(InputArray points, OutputArray hull, bool clockwise=false, bool returnPoints=true)<\/p>\n<ul>\n<li>points\uff1a\u8f38\u5165\u8cc7\u8a0a\uff0c\u53ef\u4ee5\u70ba\u5305\u542b\u9ede\u7684\u5bb9\u5668(vector)\u6216\u662fMat\u3002<\/li>\n<li>hull\uff1a\u8f38\u51fa\u8cc7\u8a0a\uff0c\u5305\u542b\u9ede\u7684\u5bb9\u5668(vector)\u3002<\/li>\n<li>lockwise\uff1a\u65b9\u5411\u65d7\u6a19\uff0c\u5982\u679ctrue\u662f\u9806\u6642\u91dd\uff0cfalse\u662f\u9006\u6642\u91dd\u3002<\/li>\n<\/ul>\n<\/div>\n<p>&nbsp;<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n#include &lt;iostream&gt;\r\n#include &lt;opencv2\/core\/core.hpp&gt; \r\n#include &lt;opencv2\/highgui\/highgui.hpp&gt;\r\n#include &lt;opencv2\/opencv.hpp&gt;\r\n\r\nusing namespace cv ;\r\nusing namespace std ;\r\n\r\nint main() {\r\n    \r\n    Mat src_gray;\r\n    cv::Mat src = cv::imread(&quot;\/Users\/powenko\/Desktop\/1.png&quot;);\r\n    if(src.data!=NULL){\r\n        cvtColor( src, src_gray, CV_BGR2GRAY );\r\n        \r\n        \/\/Dilate\r\n        int erosion_size =10;\r\n        Mat element = getStructuringElement(cv::MORPH_ELLIPSE,\r\n                                            cv::Size(2 * erosion_size + 1, 2 * erosion_size + 1),\r\n                                            cv::Point(erosion_size, erosion_size) );\r\n        \r\n        dilate(src_gray,src_gray,element);\r\n        \r\n        imshow( &quot;Source&quot;, src_gray );\r\n        \r\n        \/\/ Convex Hull implementation\r\n        Mat src_copy = src_gray.clone();\r\n        Mat threshold_output;\r\n        vector&lt;vector&lt;Point&gt; &gt; contours;\r\n        vector&lt;Vec4i&gt; hierarchy;\r\n        \r\n        \/\/ Find contours\r\n        threshold( src_gray, threshold_output, 200, 255, THRESH_BINARY );\r\n        findContours( threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );\r\n        \r\n        \/\/ Find the convex hull object for each contour\r\n        vector&lt;vector&lt;Point&gt; &gt;hull( contours.size() );\r\n        for( int i = 0; i &lt; contours.size(); i++ )\r\n        {  convexHull( Mat(contours&#x5B;i]), hull&#x5B;i], false ); }\r\n        \r\n        \/\/ Draw contours + hull results\r\n        RNG rng;\r\n        Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );\r\n        for( int i = 0; i&lt; contours.size(); i++ )\r\n        {\r\n            Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );\r\n            \/\/drawContours( drawing, contours, i, color, 1, 8, vector&lt;Vec4i&gt;(), 0, Point() );\r\n            drawContours( drawing, hull, i, color, 1, 8, vector&lt;Vec4i&gt;(), 0, Point() );\r\n        }\r\n        \r\n        \r\n        \r\n        cv::namedWindow(&quot;Image&quot;) ;\r\n        cv::imshow(&quot;Image&quot;, src) ;\r\n        cv::imshow(&quot;result&quot;, src_gray) ;\r\n        cv::waitKey() ;\r\n    }\r\n    return 0;\r\n}\r\n\r\n<\/pre>\n<p><a href=\"httpss:\/\/www.powenko.com\/wordpress\/wp-content\/uploads\/2016\/11\/Screen-Shot-2016-11-14-at-12.35.23-AM.png\"><img loading=\"lazy\" decoding=\"async\" src=\"httpss:\/\/www.powenko.com\/wordpress\/wp-content\/uploads\/2016\/11\/Screen-Shot-2016-11-14-at-12.35.23-AM-570x273.png\" alt=\"screen-shot-2016-11-14-at-12-35-23-am\" width=\"570\" height=\"273\" class=\"alignnone size-medium wp-image-9082\" srcset=\"https:\/\/www.powenko.com\/wordpress\/wp-content\/uploads\/2016\/11\/Screen-Shot-2016-11-14-at-12.35.23-AM-570x273.png 570w, https:\/\/www.powenko.com\/wordpress\/wp-content\/uploads\/2016\/11\/Screen-Shot-2016-11-14-at-12.35.23-AM-300x144.png 300w, https:\/\/www.powenko.com\/wordpress\/wp-content\/uploads\/2016\/11\/Screen-Shot-2016-11-14-at-12.35.23-AM-316x152.png 316w, https:\/\/www.powenko.com\/wordpress\/wp-content\/uploads\/2016\/11\/Screen-Shot-2016-11-14-at-12.35.23-AM-120x58.png 120w, https:\/\/www.powenko.com\/wordpress\/wp-content\/uploads\/2016\/11\/Screen-Shot-2016-11-14-at-12.35.23-AM-210x101.png 210w, https:\/\/www.powenko.com\/wordpress\/wp-content\/uploads\/2016\/11\/Screen-Shot-2016-11-14-at-12.35.23-AM-496x238.png 496w, https:\/\/www.powenko.com\/wordpress\/wp-content\/uploads\/2016\/11\/Screen-Shot-2016-11-14-at-12.35.23-AM-140x67.png 140w, https:\/\/www.powenko.com\/wordpress\/wp-content\/uploads\/2016\/11\/Screen-Shot-2016-11-14-at-12.35.23-AM.png 1088w\" sizes=\"(max-width: 570px) 100vw, 570px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u51f8\u6bbc(convexHull) \u51f8\u6bbc(Convex Hull)\u662f\u4e00\u500b\u8a08\u7b97\u5e7e\u4f55\u4e2d\u7684\u6982\u5ff5\uff0c\u5728\u7d66\u5b9a\u4e8c\u7dad\u5e73\u9762\u4e0a\u7684\u9ede\u96c6\u5408\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":9082,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[335],"tags":[],"class_list":["post-9079","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ch5-findcontoursdrawcontours"],"_links":{"self":[{"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/9079"}],"collection":[{"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9079"}],"version-history":[{"count":3,"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/9079\/revisions"}],"predecessor-version":[{"id":9083,"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/9079\/revisions\/9083"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=\/wp\/v2\/media\/9082"}],"wp:attachment":[{"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9079"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9079"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.powenko.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9079"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}