原文:Emgu-WPF学习使用-识别二维码的位置

参考链接:http://blog.csdn.net/gaobobo138968/article/details/47663607

我完全参照该链接实现了二维码的位置锁定,向原作者致敬。

由于我使用的为最新版本的Emgu,很多封装函数调用方法有所变化,新手接触Emgu,尝试做了调整,部分参数也做了微调!

我使用的Emgu版本:emgucv-windesktop 3.2.0.2682

最终我实现的效果图如下:

前台xaml:

<Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Image x:Name="Img1" Grid.Column="0" />
        <Image x:Name="Img2" Grid.Column="1" />
        <Image x:Name="Img3" Grid.Column="2" />
        <Image x:Name="Img4" Grid.Column="3" />
        <Image x:Name="Img5" Grid.Column="0" Grid.Row="1"/>
        <Image x:Name="Img6" Grid.Column="1" Grid.Row="1"/>
        <Image x:Name="Img7" Grid.Column="2" Grid.Row="1"/>
        <Viewbox Stretch="Fill" Grid.Column="3" Grid.Row="1">
            <Grid Width="1134" Height="850" Background="Silver">
                <Image x:Name="Img8"/>
                <Canvas x:Name="CvMainZm"/>
            </Grid>
        </Viewbox>
       
    </Grid>

后台源码:

private void ShowImage(System.Windows.Controls.Image oImage, UMat src)
        {
            this.Dispatcher.Invoke(() => {
                oImage.Source = BitmapSourceConvert.ToBitmapSource(src);
            });
        }

private void ShowBgrImage(System.Windows.Controls.Image oImage, Image<Bgr, byte> src)
        {
            this.Dispatcher.Invoke(() => {
                oImage.Source = BitmapSourceConvert.ToBitmapSource(src);
            });
        }

private void ShowGrayImage(System.Windows.Controls.Image oImage, Image<Gray, byte> src)
        {
            this.Dispatcher.Invoke(() => {
                oImage.Source = BitmapSourceConvert.ToBitmapSource(src);
            });
        }

public MainWindow()
        {
            InitializeComponent();

this.Loaded += MainWindow_Loaded;
        }

//参考链接 http://blog.csdn.net/gaobobo138968/article/details/47663607
        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            string sFile = AppDomain.CurrentDomain.BaseDirectory + "Test.jpg";

System.Drawing.Image img = System.Drawing.Image.FromFile(sFile);
            Bitmap barcodeBitmap = new Bitmap(img);
            Image<Bgr, byte> img_src = new Image<Bgr, byte>(barcodeBitmap);

this.ShowBgrImage(this.Img1, img_src);

//灰度化
            Image<Gray, byte> imput_gray = new Image<Gray, byte>(img_src.Size);
            CvInvoke.CvtColor(img_src, imput_gray, ColorConversion.Bgr2Gray);
            this.ShowGrayImage(this.Img2, imput_gray);

//计算x,y方向梯度,相加
            Image<Gray, byte> grad_x1 = new Image<Gray, byte>(img_src.Size);
            Image<Gray, byte> grad_y1 = new Image<Gray, byte>(img_src.Size);
            Image<Gray, byte> grad_all = new Image<Gray, byte>(img_src.Size);
            CvInvoke.Sobel(imput_gray, grad_x1, DepthType.Default, 0, 1, 3);
            CvInvoke.Sobel(imput_gray, grad_y1, DepthType.Default, 1, 0, 3);
            CvInvoke.Add(grad_x1, grad_y1, grad_all, null);
            this.ShowGrayImage(this.Img3, grad_all);

// 高斯模糊
            grad_all = grad_all.SmoothGaussian(9);
            this.ShowGrayImage(this.Img4, grad_all);

// 二值化
            CvInvoke.Threshold(grad_all, grad_all, 100, 255, ThresholdType.Binary);
            this.ShowGrayImage(this.Img5, grad_all);

//消除裂缝
            Mat oMat1 = CvInvoke.GetStructuringElement(Emgu.CV.CvEnum.ElementShape.Rectangle, 
                new System.Drawing.Size(15, 15), new System.Drawing.Point(0, 0));
            CvInvoke.MorphologyEx(grad_all, grad_all, Emgu.CV.CvEnum.MorphOp.Close, oMat1, 
                new System.Drawing.Point(0, 0), 1, BorderType.Default,
                new MCvScalar(255, 0, 0, 255));
            this.ShowGrayImage(this.Img6, grad_all);

//膨胀与腐蚀(消除杂点)
            Mat oMat2 = CvInvoke.GetStructuringElement(Emgu.CV.CvEnum.ElementShape.Rectangle,
               new System.Drawing.Size(5, 5), new System.Drawing.Point(0, 0));
            CvInvoke.Erode(grad_all, grad_all, oMat2, new System.Drawing.Point(0, 0), 4, 
                BorderType.Default, new MCvScalar(255, 0, 0, 255));
            CvInvoke.Dilate(grad_all, grad_all, oMat2, new System.Drawing.Point(0, 0), 4,
                BorderType.Default, new MCvScalar(255, 0, 0, 255));
            this.ShowGrayImage(this.Img7, grad_all);

//查找轮廓,绘制轮廓
            #region Find triangles and rectangles
            List<Triangle2DF> triangleList = new List<Triangle2DF>();
            List<RotatedRect> boxList = new List<RotatedRect>(); //a box is a rotated rectangle

using (VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint())
            {
                CvInvoke.FindContours(grad_all, contours, null, RetrType.List, ChainApproxMethod.ChainApproxSimple);
                int count = contours.Size;
                for (int i = 0; i < count; i++)
                {
                    using (VectorOfPoint contour = contours[i])
                    using (VectorOfPoint approxContour = new VectorOfPoint())
                    {
                        CvInvoke.ApproxPolyDP(contour, approxContour, CvInvoke.ArcLength(contour, true) * 0.05, true);
                        if (CvInvoke.ContourArea(approxContour, false) > 500)
                        {
                            if (approxContour.Size == 3)
                            {
                                System.Drawing.Point[] pts = approxContour.ToArray();
                                triangleList.Add(new Triangle2DF(
                                   pts[0],
                                   pts[1],
                                   pts[2]
                                   ));
                            }
                            else if (approxContour.Size == 4)
                            {
                                #region determine if all the angles in the contour are within [80, 100] degree
                                bool isRectangle = true;
                                System.Drawing.Point[] pts = approxContour.ToArray();
                                LineSegment2D[] edges = Emgu.CV.PointCollection.PolyLine(pts, true);

for (int j = 0; j < edges.Length; j++)
                                {
                                    double angle = Math.Abs(
                                       edges[(j + 1) % edges.Length].GetExteriorAngleDegree(edges[j]));
                                    if (angle < 80 || angle > 100)
                                    {
                                        isRectangle = false;
                                        break;
                                    }
                                }
                                #endregion
                                
                                if (isRectangle)
                                {
                                    boxList.Add(CvInvoke.MinAreaRect(approxContour));
                                }
                            }
                        }
                    }
                }

}

//this.Img8.Source = new BitmapImage(new Uri(sFile));
            this.ShowGrayImage(this.Img8, grad_all);
            foreach (RotatedRect box in boxList)
            {
                System.Drawing.Point[] pts = Array.ConvertAll(box.GetVertices(), System.Drawing.Point.Round);

for (int i = 0; i < pts.Length; i++)
                {
                    System.Drawing.Point point = pts[i];
                    System.Drawing.Point point1 = new System.Drawing.Point();
                    if (i == pts.Length-1)
                        point1 = pts[0];
                    else
                        point1 = pts[i + 1];

Line oLine = new Line();
                    oLine.Stroke = new SolidColorBrush(Colors.Red);
                    oLine.StrokeThickness = 5;
                    oLine.X1 = point.X;
                    oLine.Y1 = point.Y;

oLine.X2 = point1.X;
                    oLine.Y2 = point1.Y;
                    this.CvMainZm.Children.Add(oLine);
                }
            }
            #endregion
        }

BitmapSourceConvert 类直接使用的SDK中的示例。

Emgu-WPF学习使用-识别二维码的位置相关推荐

  1. 【转载】裸眼识别二维码

    一个开关的开合状态可以对应"0.1"两个信号,莱布尼茨三百多年前留下的这个制式影响深远.日常语言依赖于思维逻辑,与之类似,数字语言的流通则依赖于数学逻辑,但本质上仍然只是无数个与非 ...

  2. python二维码生成识别代码_Python学习案例之二维码生成识别

    前言 在 JavaWeb 开发中,一般使用 Zxing 来生成和识别二维码,但是,Zxing 的识别有点差强人意,不少相对模糊的二维码识别率很低.不过就最新版本的测试来说,识别率有了现显著提高. 对比 ...

  3. 微信iOS长按无法识别二维码

    真的是无力吐槽微信的坑真的多把人能呕死,做了个公众号开发一大堆的坑 希望你看完每个字 分享一下ios微信长按无法识别二维码的问题 在网上找了一大堆的处理方式说增加img 的padding.设置缩放的. ...

  4. Android利用zxing生成二维码,识别二维码,中间填充图片超详细、超简易教程

    gayhub上的zxing可用于生成二维码,识别二维码 gayhub地址:https://github.com/zxing/zxing 此文只是简易教程,文末附有完整代码和demo下载地址,进入正题: ...

  5. (补充)微信长按识别二维码 -- 页面多个二维码如何识别?(二)

    基于上一篇微信长按识别二维码 -- 页面多个二维码如何识别? )在部分设备上,如果图片非常多,还是会出现识别错误的bug(主要是二维码显示一半或居于底部时),修改了识别流程,改为点击图片弹窗,然后长按 ...

  6. opencv项目7---智能识别二维码和条形码

    利用opencv和一个摄像头设备即可实现智能识别二维码和条形码,用到的都是基础的opencv知识. 二维码和条形码的照片可以去网上自行搜索. 这个项目可以有很多扩展: 1:比如做成一个公司的二维码证件 ...

  7. 移动端H5(JavaScript)识别二维码功能

    前言 时隔一年多, 再次接触到H5识别二维码功能,这次直接写个demo方便大家学习和使用.(其实是方便自己抄自己代码-). 直接上代码 QRcode下载地址 长的好看的都点⭐了!!! <!DOC ...

  8. python实现二维码识别软件_用 Python 生成 识别二维码

    说到二维码大家一定不陌生,可以说现在二维码几乎渗透到了我们生活的各个角落,举例来说吧,我们到超市商场购物时扫描二维码付款,我们出行时乘坐公交地铁扫描二维码进站,我们到菜鸟驿站取件时扫描二维码取件,如果 ...

  9. Python用opencv实现动态识别二维码,以及加强版Python GUI(图像用户界面编程)

    前言 关于动态识别二维码信息,利用电脑摄像头动态扫描二维码,扫描视频中的二维码. 简易程序 import cv2 pip install opencv-pythondef start():captur ...

最新文章

  1. 聊一聊-JAVA 泛型中的通配符 T,E,K,V,?
  2. 在WinForm中通过HTTP协议向服务器端上传文件(转)
  3. 使用Selenium WebDriver测试自动化的22条实用技巧
  4. DreamFactory入门指南
  5. 机器学习实战7-sklearn集成学习和随机森林
  6. Struts2的struts.xml的配置细节
  7. Inno Setup 检测已安装的.NET Framework 版本
  8. 处理 ODBC, OLE DB, 和 SQL Server .NET Provider 中的异常
  9. 潜意识的力量:潜意识开发四大关键
  10. GestureDetector类的用法
  11. 01 hadoop介绍、架构原理
  12. AI教程视频 - 零基础玩转illustrator科研绘图-内容介绍-目录
  13. CRM系统更换服务器,CRM系统三种常见安装实施解决方式
  14. 免费网课python_Python网课推荐——免费学习Python编程
  15. 国际象棋测试软件只能支持8核,国际象棋测试超线程对性能的影响,多线程多开真的强大吗?(晒T恤)【硬件玩家】...
  16. 数据结构-2019春 07-图4 哈利·波特的考试 (25 分)
  17. 按键精灵可以实现c语言吗,按键精灵的原理和编写方法(1)
  18. 建服务器数据中心,如何构建一个服务器数据中心
  19. 你真的会解决android ANR 问题吗?
  20. MySQL 更新某个字段的值加1 是有前提条件的(非auto_increament)

热门文章

  1. 一点历史--Python
  2. SQL面试题: 数据库中有A B C三列,用SQL语句实现:当A列大于B列时选择A列否则选择B列 ,当B列大于C列时选择B列否则选择C列 ,...
  3. 基于Spring可扩展Schema提供自定义配置支持
  4. 提高 Android 代码质量的4个工具
  5. ARM裸机篇---启动代码分析
  6. 百度2011实习生招聘笔试题
  7. 阿里云“华北5”落地内蒙古,AI数据大战一触即发
  8. SQL Server 2005 For XML[学习]
  9. 熊猫直播Rancho发布系统构建之路
  10. HDU 5119 Happy Matt Friends ——(背包DP)