zoukankan      html  css  js  c++  java
  • emguCv3.x 实现字符分割,轮廓检测

     /// <summary>
            /// 获取区域
            /// </summary>
            /// <param name="bitmap"></param>
            /// <param name="graybtm"></param>
            /// <returns></returns>
            public static Bitmap FindBundingBox(Bitmap bitmap, out Bitmap graybtm,out List<Rectangle> rects)
            {
                Image<Bgr, byte> img = new Image<Bgr, byte>(bitmap);
                Image<Gray, byte> gray = new Image<Gray, byte>(img.Width, img.Height);
                Image<Bgr, byte> resuImage = new Image<Bgr, byte>(img.Width, img.Height);
                Image<Gray, byte> dnc = new Image<Gray, byte>(img.Width, img.Height);
                CvInvoke.CvtColor(img, gray, ColorConversion.Bgra2Gray);//灰度化
                //做一下膨胀,x与y方向都做,但系数不同
                var kernal = CvInvoke.GetStructuringElement(ElementShape.Rectangle, new Size(4, 4), new Point(1, 1));
                CvInvoke.Erode(gray, gray, kernal, new Point(0, 2), 1, BorderType.Default, new MCvScalar());
                //CvInvoke.Canny(gray, gray, 100, 60);
                CvInvoke.Threshold(gray, gray, 100, 255, ThresholdType.BinaryInv | ThresholdType.Otsu);//二值化
                //检测连通域,每一个连通域以一系列的点表示,FindContours方法只能得到第一个域
                graybtm = gray.ToBitmap();
                VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
                CvInvoke.FindContours(gray, contours, dnc, RetrType.Ccomp, ChainApproxMethod.ChainApproxSimple);
                var color = new MCvScalar(0, 0, 255);
                Console.WriteLine(contours.Size);
                 rects = new List<Rectangle>();
                //开始遍历
                for (int i = 0; i < contours.Size; i++)
                {
                    //得到这个连通区域的外接矩形
                    var rect = CvInvoke.BoundingRectangle(contours[i]);
                    //如果高度不足,或者长宽比太小,认为是无效数据,否则把矩形画到原图上
                    if (rect.Height > 2 && rect.Width > 2)
                    {
                        rects.Add(rect);
                        CvInvoke.DrawContours(resuImage, contours, i, color);
                    }
                }
    
                return img.ConcateVertical(resuImage).ToBitmap();
            }
     private void StandardRects()
            {
                List<Rectangle> removeList=new List<Rectangle>();
                foreach (var item in glbRect)
                {
                    if (glbRect.Exists(o => o.Contains(item) && o!=item))
                    {
                        removeList.Add(item);
                    }
                }
                glbRect.RemoveAll(o => removeList.Contains(o));
                glbRect=glbRect.OrderBy(o => o.X).ToList();
                lb_count.Text = "轮廓总数:" + glbRect.Count;
            }
  • 相关阅读:
    centos 6.4 FTP安装和配置
    常用正则表达式集锦
    同一服务器部署多个tomcat时的端口号修改详情
    介绍linux下vi命令的使用
    CentOS下安装两个或多个Tomcat7
    tomcat解析之简单web服务器(图)
    吻你
    用C++语言开发Android程序 配置开发环境
    内地开源镜像网站
    Android SDK Android NDK Android Studio 官方下载地址
  • 原文地址:https://www.cnblogs.com/daxiongblog/p/5776636.html
Copyright © 2011-2022 走看看