{"id":702,"date":"2021-08-04T11:04:49","date_gmt":"2021-08-04T03:04:49","guid":{"rendered":"http:\/\/www.wayln.com\/?p=702"},"modified":"2021-08-04T11:06:22","modified_gmt":"2021-08-04T03:06:22","slug":"opencv%e5%9c%a8c%e4%b8%ad%e4%bd%bf%e7%94%a8%e5%9f%ba%e7%a1%80%ef%bc%88opencvsharp%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.wayln.com\/?p=702","title":{"rendered":"OpenCV\u5728C#\u4e2d\u4f7f\u7528\u57fa\u7840\uff08OpenCVSharp\uff09"},"content":{"rendered":"<div id=\"toc_container\" class=\"toc_transparent no_bullets\"><p class=\"toc_title\">Contents<\/p><ul class=\"toc_list\"><li><a href=\"#OpenCVSharp\"><span class=\"toc_number toc_depth_1\">1<\/span> \u5173\u4e8eOpenCVSharp<\/a><\/li><li><a href=\"#OpenCVSharp-2\"><span class=\"toc_number toc_depth_1\">2<\/span> \u5b89\u88c5OpenCVSharp<\/a><\/li><li><a href=\"#OpenCVSharp-3\"><span class=\"toc_number toc_depth_1\">3<\/span> \u6572\u7801OpenCVSharp<\/a><\/li><\/ul><\/div>\n<h1><span id=\"OpenCVSharp\">\u5173\u4e8eOpenCVSharp<\/span><\/h1>\n<p>\u4ee5\u524d\u505a\u56fe\u50cf\u5904\u7406\u7684\u7b97\u6cd5\u5f00\u53d1\u7684\u65f6\u5019\uff0c\u4e00\u76f4\u90fd\u662f\u518d\u7528C++\u548copencv\uff0c\u8bf4\u5b9e\u8bdd\u6211\u7684C++\u5b66\u7684\u4e0d\u600e\u4e48\u6837\uff0c\u5c01\u88c5dll\u7b49\u4e5f\u90fd\u633a\u9ebb\u70e6\u7684\u3002\u540e\u6765\u4e0d\u600e\u4e48\u5f00\u53d1C++\u4e86\uff0c\u5f00\u59cb\u6295\u5165Python\u3001C#\u7b49\u7684\u5f00\u53d1\u3002<\/p>\n<p>\u4e00\u76f4\u89c9\u5f97C#\u662f\u4e00\u95e8\u6bd4C++\u66f4\u7075\u6d3b\u597d\u5b66\uff0c\u6bd4Python\u66f4\u52a0\u89c4\u8303\u4e0e\u4f20\u7edf\uff0c\u611f\u89c9\u5f88\u9002\u5408\u6211\u3002\u4f46\u662f\u5728\u56fe\u50cf\u5904\u7406\u8fd9\u5757\uff0c\u4e00\u76f4\u6ca1\u6709\u5f88\u597d\u7684\u56fe\u50cf\u5904\u7406\u5e93\u3002<\/p>\n<p>\u4ee5\u524d\u641c\u8fc7\uff0c\u53d1\u73b0EmguCV\u662fopencv\u5728C#\u4e2d\u7684\u66ff\u4ee3\u54c1\uff0c\u4f46\u662f\u8c8c\u4f3c\u5b66\u8d77\u6765\u633a\u96be\u7684\uff0c\u540e\u6765\u53c8\u53d1\u73b0\u4e86 AForge.Net\uff0c\u7ed3\u5408C#\u8c8c\u4f3c\u5f88\u4e0d\u9519\u7684\uff0c\u8fd8\u53d1\u73b0\u4e86OpenCVSharp\uff0c\u5c31\u662f\u4e00\u4e2a\u7528\u5176\u662fOpenCV\u7684.NET wrapper\uff0c\u5b83\u6bd4Emgucv\u66f4\u63a5\u8fd1\u4e8e\u539f\u59cb\u7684OpenCV\u3002<\/p>\n<p>\u8fd9\u6b21\u5148\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0bOpenCVSharp\u7684\u4e00\u4e9b\u57fa\u7840\u64cd\u4f5c<\/p>\n<h1><span id=\"OpenCVSharp-2\">\u5b89\u88c5OpenCVSharp<\/span><\/h1>\n<ol>\n<li>\u4f7f\u7528VS\u7684\u8bdd\uff0c\u4f7f\u7528nuget\u8fdb\u884c\u641c\u7d22\u5b89\u88c5\u5c31\u597d\uff0c\u4e2a\u4eba\u5efa\u8bae\u5b89\u88c5OpenCvSharp3-AnyCPU<br \/>\n<a class=\"wp-editor-md-post-content-link\" href=\"http:\/\/www.wayln.com\/wp-content\/uploads\/2021\/08\/wp_editor_md_33f44236499c016c611627c7d674ce23.jpg\"><img decoding=\"async\" src=\"http:\/\/www.wayln.com\/wp-content\/uploads\/2021\/08\/wp_editor_md_33f44236499c016c611627c7d674ce23.jpg\" alt=\"\" \/><\/a><\/p>\n<\/li>\n<li>\n<p>\u53e6\u4e00\u79cd\u65b9\u6cd5\u5462\u5c31\u662f\u53bb\u5b98\u7f51\u76f4\u63a5\u4e0b\u8f7d\uff0c\u7136\u540e\u5f15\u7528\u201cOpenCvSharp.dll\u201d\u5230\u4f9d\u8d56\u4e2d<\/p>\n<\/li>\n<\/ol>\n<h1><span id=\"OpenCVSharp-3\">\u6572\u7801OpenCVSharp<\/span><\/h1>\n<p>\u547d\u540d\u7a7a\u95f4\u8981\u5f15\u7528\uff1ausing OpenCvSharp;<\/p>\n<p>\u4f8b\u5982\uff08\u6709\u70b9\u95f2\u7684\u614c\uff09\uff1a<\/p>\n<pre><code class=\"language-CSharp line-numbers\">\/\/NameSpace:    CVSharp\n\/\/FileName:     CVSharp\n\/\/Create By:    raink\n\/\/Create Time:  2019\/9\/27 14:24:08\n\nusing System;\nusing System.Collections.Generic;\nusing System.Text;\nusing OpenCvSharp;\n\nnamespace CVSharp\n{\n    class CVSImage\n    {\n        private string path;\n        \/\/\/ &lt;summary&gt;\n        \/\/\/ \u56fe\u50cf\u6587\u4ef6\u7684\u5168\u8def\u5f84\n        \/\/\/ &lt;\/summary&gt;\n        public string Path { get =&gt; path; set =&gt; path = value; }\n\n        private int width;\n        \/\/\/ &lt;summary&gt;\n        \/\/\/ \u56fe\u50cf\u7684\u5bbd\n        \/\/\/ &lt;\/summary&gt;\n        public int Width { get =&gt; width; }\n\n        private int height;\n        \/\/\/ &lt;summary&gt;\n        \/\/\/ \u56fe\u50cf\u7684\u9ad8\n        \/\/\/ &lt;\/summary&gt;\n        public int Height { get =&gt; height; }\n\n        private long size;\n        \/\/\/ &lt;summary&gt;\n        \/\/\/ \u56fe\u50cf\u7684\u5c3a\u5bf8 size = width * height\n        \/\/\/ &lt;\/summary&gt;\n        public long Size { get =&gt; size; }\n\n\n        \/*-----------------------------------------------------------------*\/\n\n        private Mat srcImg;\n\n        public CVSImage()\n        {\n            path = \"\";\n            width = 0;\n            height = 0;\n            size = 0;\n            srcImg = new Mat();\n        }\n\n        public CVSImage(string imagePath, int readMode=1)\n        {\n            path = imagePath;\n            srcImg = Cv2.ImRead(path, (ImreadModes)readMode);\n            width = srcImg.width;\n            height = srcImg.height;\n            size = width * height;\n        }\n\n        \/*-----------------------------------------------------------------*\/\n\n        \/\/\/ &lt;summary&gt;\n        \/\/\/ \u9884\u89c8\u56fe\u50cf\n        \/\/\/ &lt;\/summary&gt;\n        \/\/\/ &lt;param name=\"winName\"&gt;\u7a97\u53e3\u540d&lt;\/param&gt;\n        \/\/\/ &lt;param name=\"windowType\"&gt;0-Normal(\u53ef\u8c03\u7a97\u53e3\u5c3a\u5bf8),1-Autosize(\u56fe\u50cf\u539f\u59cb\u5927\u5c0f)&lt;\/param&gt;\n        \/\/\/ &lt;param name=\"showTime\"&gt;\u7a97\u53e3\u505c\u7559\u65f6\u95f4\uff080\u5219\u4e00\u76f4\u7b49\u5f85\u8f93\u5165\uff09&lt;\/param&gt;\n        \/\/\/ &lt;returns&gt;\u5f02\u5e38\u8fd4\u56de-1\uff0c\u6b63\u5e38\u8fd4\u56de1&lt;\/returns&gt;\n        public int showImage(string winName, int windowType=1, int showTime=0)\n        {\n            if (srcImg == null || showTime &lt; 0)\n            {\n                return -1;\n            }\n            WindowMode mode = (WindowMode)windowType;\n            Cv2.NamedWindow(winName, mode);\n            Cv2.ImShow(winName, srcImg);\n            Cv2.WaitKey(showTime);\n            return 0;\n        }\n    }\n}\n<\/code><\/pre>\n<p>\u7136\u540e\u518d\u4e3b\u51fd\u6570\u91cc\u9762\u8c03\u7528\u4e00\u4e0b\uff1a<\/p>\n<pre><code class=\"language-CSharp line-numbers\">using System;\nusing CVSharp;\n\nnamespace CVSharp\n{\n    class Program\n    {\n        static void Main(string[] args)\n        {\n            CVSImage cvImage = new CVSImage(\"D:\\\\Asuna.jpg\");\n            Console.WriteLine(\"image width: {0}\", cvImage.Width);\n            Console.WriteLine(\"image height: {0}\", cvImage.Height);\n            cvImage.showImage(\"Asuna\", 0);\n        }\n    }\n}\n<\/code><\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"http:\/\/www.wayln.com\/wp-content\/uploads\/2021\/08\/wp_editor_md_92d94ce0c5602110cf066686b543fc93.jpg\"><img decoding=\"async\" src=\"http:\/\/www.wayln.com\/wp-content\/uploads\/2021\/08\/wp_editor_md_92d94ce0c5602110cf066686b543fc93.jpg\" alt=\"\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Contents1 \u5173\u4e8eOpenCVSharp2 \u5b89\u88c5OpenCVSharp3 \u6572\u7801OpenCVSharp \u5173 [&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-702","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\/702","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=702"}],"version-history":[{"count":1,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/702\/revisions"}],"predecessor-version":[{"id":703,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/702\/revisions\/703"}],"wp:attachment":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=702"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=702"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=702"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}