{"id":811,"date":"2021-09-11T13:35:10","date_gmt":"2021-09-11T05:35:10","guid":{"rendered":"http:\/\/www.wayln.com\/?p=811"},"modified":"2021-09-11T13:35:10","modified_gmt":"2021-09-11T05:35:10","slug":"opencvsharp%e8%af%86%e5%88%ab%e5%b9%b6%e5%ae%9a%e4%bd%8d%e4%ba%8c%e7%bb%b4%e7%a0%81","status":"publish","type":"post","link":"https:\/\/www.wayln.com\/?p=811","title":{"rendered":"OpenCVSharp\u8bc6\u522b\u5e76\u5b9a\u4f4d\u4e8c\u7ef4\u7801"},"content":{"rendered":"<pre><code class=\"language-CSharp line-numbers\">    private void FindQrPoint_1(string imagePath)\n        {\n            src = Cv2.ImRead(Environment.CurrentDirectory + \"\\\\12345.jpg\",ImreadModes.Color);\n            src_gray = new Mat();\n            Mat src_all = src.Clone();\n\n            \/\/\u5f69\u8272\u56fe\u8f6c\u7070\u5ea6\u56fe\n            Cv2.CvtColor(src, src_gray, ColorConversionCodes.BGR2GRAY);\n\n            \/\/\u5bf9\u56fe\u50cf\u8fdb\u884c\u5e73\u6ed1\u5904\u7406\n            Cv2.Blur(src_gray, src_gray, new OpenCvSharp.Size(3, 3));\n\n            Cv2.ImShow(\"src_gray\", src_gray);\n\n\n            Scalar color = new Scalar(1, 1, 255);\n\n            Mat threshold_output=new Mat();\n            OpenCvSharp.Point[][] contours;\n            List&lt;OpenCvSharp.Point[]&gt; contours2 = new List&lt;OpenCvSharp.Point[]&gt;();\n            HierarchyIndex[] hierarchy;\n            Mat drawing = Mat.Zeros(src.Size(), MatType.CV_8UC3);\n            Mat drawing2 = Mat.Zeros(src.Size(), MatType.CV_8UC3);\n            Mat drawingAllContours = Mat.Zeros(src.Size(), MatType.CV_8UC3);\n\n            \/\/\u6307\u5b9a112\u9608\u503c\u8fdb\u884c\u4e8c\u503c\u5316\n            Cv2.Threshold(src_gray, threshold_output, 112, 255, ThresholdTypes.Binary);\n            Cv2.ImShow(\"Threshold_output\", threshold_output);\n\n            Cv2.FindContours(threshold_output, out contours, out hierarchy, RetrievalModes.Tree, ContourApproximationModes.ApproxNone, new OpenCvSharp.Point(0, 0));\n            int c = 0, ic = 0, k = 0, area = 0;\n\n            \/\/\u901a\u8fc7\u9ed1\u8272\u5b9a\u4f4d\u89d2\u4f5c\u4e3a\u7236\u8f6e\u5ed3\uff0c\u6709\u4e24\u4e2a\u5b50\u8f6e\u5ed3\u7684\u7279\u70b9\uff0c\u7b5b\u9009\u51fa\u4e09\u4e2a\u5b9a\u4f4d\u89d2\n            int parentIdx = -1;\n            for (int i = 0; i &lt; contours.Length; i++)\n            {\n                \/\/\u753b\u51fa\u6240\u6709\u8f6e\u5ed3\u56fe\n                Cv2.DrawContours(drawingAllContours, contours, parentIdx, new Scalar(255, 255, 255));\n                if(hierarchy[i].Child!=-1&amp;&amp;ic==0)\n                {\n                    parentIdx = i;\n                    ic++;\n                }\n                else if(hierarchy[i].Child!=-1)\n                {\n                    ic++;\n                }\n                else if(hierarchy[i].Child==-1)\n                {\n                    ic = 0;\n                    parentIdx = -1;\n                }\n\n                \/\/\u6709\u4e24\u4e2a\u5b50\u8f6e\u5ed3\n                if(ic&gt;=2)\n                {\n                    \/\/\u4fdd\u5b58\u627e\u5230\u7684\u4e09\u4e2a\u9ed1\u8272\u5b9a\u4f4d\u89d2\n                    contours2.Add(contours[parentIdx]);\n                    \/\/\u753b\u51fa\u4e09\u4e2a\u9ed1\u8272\u5b9a\u4f4d\u89d2\u7684\u8f6e\u5ed3\n                    Cv2.DrawContours(drawing, contours, parentIdx, new Scalar(rng.Next(0, 255), rng.Next(0, 255), rng.Next(0, 255)), 1, LineTypes.Link8);\n                    ic = 0;\n                    parentIdx = -1;\n                }\n\n            }\n\n            \/\/\u586b\u5145\u7684\u65b9\u5f0f\u753b\u51fa\u4e09\u4e2a\u9ed1\u8272\u5b9a\u4f4d\u89d2\u7684\u8f6e\u5ed3\n            for (int i = 0; i &lt; contours2.Count; i++)\n            {\n                Cv2.DrawContours(drawing2, contours2, i, new Scalar(rng.Next(0, 255), rng.Next(0, 255), rng.Next(0, 255)), -1, LineTypes.Link4, hierarchy, 0, new OpenCvSharp.Point());\n            }\n\n            \/\/\u83b7\u53d6\u4e09\u4e2a\u5b9a\u4f4d\u89d2\u7684\u4e2d\u5fc3\u5750\u6807\n            OpenCvSharp.Point[] point = new OpenCvSharp.Point[3];\n            for (int i = 0; i &lt; contours2.Count; i++)\n            {\n                point[i] = Center_cal(contours2.ToArray(), i);\n            }\n\n            \/\/\u8ba1\u7b97\u8f6e\u5ed3\u7684\u9762\u79ef\uff0c\u8ba1\u7b97\u5b9a\u4f4d\u89d2\u7684\u9762\u79ef\uff0c\u4ece\u800c\u8ba1\u7b97\u51fa\u8fb9\u957f\n            area = (int)Cv2.ContourArea(contours2[1]);\n            int area_side = (int)Math.Round(Math.Sqrt((double)area));\n            for(int i=0;i&lt;contours2.Count;i++)\n            {\n                \/\/\u753b\u51fa\u4e09\u4e2a\u5b9a\u4f4d\u89d2\u7684\u4e2d\u5fc3\u8fde\u7ebf\n                Cv2.Line(drawing2, point[i % contours2.Count], point[(i + 1) % contours2.Count], color, area_side \/ 2, LineTypes.Link8);\n            }\n\n            Cv2.ImShow(\"DrawingAllContours\", drawingAllContours);\n            Cv2.ImShow(\"Drawing2\", drawing2);\n            Cv2.ImShow(\"Drawing\", drawing);\n\n\n            \/\/\u63a5\u4e0b\u6765\u8981\u6846\u5904\u6574\u4e2a\u4e8c\u7ef4\u7801\n            Mat gray_all = new Mat();\n            Mat threshold_output_all=new Mat();\n            OpenCvSharp.Point[][] contours_all;\n            HierarchyIndex[] hierarchy_all;\n            Cv2.CvtColor(drawing2, gray_all, ColorConversionCodes.BGR2GRAY);\n\n\n            Cv2.Threshold(gray_all, threshold_output_all, 45, 255, ThresholdTypes.Binary);\n            Cv2.FindContours(threshold_output_all, out contours_all, out hierarchy_all, RetrievalModes.External, ContourApproximationModes.ApproxNone, new OpenCvSharp.Point(0, 0)); \/\/External\u8868\u793a\u53ea\u5bfb\u627e\u6700\u5916\u5c42\u8f6e\u5ed3\n\n\n            Point2f[] fourPoint2f = new Point2f[4];\n            \/\/\u6c42\u6700\u5c0f\u5305\u56f4\u77e9\u5f62\n            RotatedRect rectPoint = Cv2.MinAreaRect(contours_all[0]);\n\n            \/\/\u5c06rectPoint\u53d8\u91cf\u4e2d\u5b58\u50a8\u7684\u5750\u6807\u503c\u653e\u5230fourPoint\u7684\u6570\u7ec4\u4e2d\n            fourPoint2f= rectPoint.Points();\n\n            for (int i = 0; i &lt; 4; i++)\n            {\n\n                Cv2.Line(src_all,new OpenCvSharp.Point( fourPoint2f[i % 4].X, fourPoint2f[i % 4].Y), new OpenCvSharp.Point(fourPoint2f[(i + 1) % 4].X, fourPoint2f[(i + 1) % 4].Y), new Scalar(20, 21, 237), 3);\n            }\n\n            Cv2.ImShow(\"Src_all\", src_all);\n\n\n        }\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>private void FindQrPoint_1(string imagePath) { src = Cv [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,37,2],"tags":[],"class_list":["post-811","post","type-post","status-publish","format-standard","hentry","category-c","category-opencv","category-2"],"_links":{"self":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/811","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=811"}],"version-history":[{"count":1,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/811\/revisions"}],"predecessor-version":[{"id":812,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/811\/revisions\/812"}],"wp:attachment":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=811"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=811"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=811"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}