无意间发现opencvsharp ,对于 我这种主要平台是C#的人来 说还是 很不错的。但网上这个opencvsharp的资料很少,还好之前对OPENCV 和emgucv比较熟悉,就准备转到这个平台做视觉。opencvsharp 就是opencv给C#的API ,连函数都没有变化,但方法结构有些出入,对说熟悉opencv的人转过来还是比较容易的。不多说了,CSDN上有人发过 一点基本操作,但从图像的轮廓,模板这种实用的视觉手段就没有了,应该是行业不同,这里我就从轮廓开始接起操做。

前面基本的就不说了。

 static void Main(string[] args)
        {
            Mat mat1 = new Mat(@\"棋盘格.jpg\",ImreadModes.AnyColor);     
            Mat mat2 = new Mat();
            Mat mat3 = new Mat();
            Cv2.CvtColor(mat1, mat3,ColorConversionCodes.BGR2GRAY,0);
            Cv2.Threshold(mat3, mat2, 200, 255,ThresholdTypes.Binary);
          //  Cv2.Canny(mat2,mat3,0,255);
            
        
            Point[][] contours;
            HierarchyIndex[] hierarchy;

            Cv2.FindContours(mat2, out contours, out hierarchy, RetrievalModes.CComp,ContourApproximationModes.ApproxSimple, null);
            
            for (int i = 0; i < hierarchy.Length; i++)
            {
                Cv2.DrawContours(mat1, contours, i, Scalar.Red,1, LineTypes. 8, hierarchy, 4, new Point(10,10));
            }
            Cv2.ImShow(\"mat1\",mat1);
            Cv2.ImShow(\"mat2\", mat2);
            Cv2.ImShow(\"mat3\", mat3);
            Cv2.WaitKey();
        }

收藏 打印