EmguCV方形答题卡识别

本文介绍了如何利用C#结合EmguCV库对答题卡进行识别。通过参考链接中的教程,作者实现了对简单答题卡的识别功能,并展示了最终效果,包括三个imageBox控件的显示。代码中包含主程序对答题卡各部分的测量以及调用的自定义函数。
摘要由CSDN通过智能技术生成

参考:https://www.cnblogs.com/jsxyhelu/

经过一段时间的学习,终于能利用C#结合EmguCv对简单的答题卡进行识别,样图如下:

最终效果图:

三个imageBox控件

ib_original.SizeMode = PictureBoxSizeMode.Zoom;
            ib_original.FunctionalMode = Emgu.CV.UI.ImageBox.FunctionalModeOption.Minimum;

            ib_middle.SizeMode = PictureBoxSizeMode.Zoom;
            ib_middle.FunctionalMode = Emgu.CV.UI.ImageBox.FunctionalModeOption.Minimum;

            ib_result.SizeMode = PictureBoxSizeMode.Zoom;
            ib_result.FunctionalMode = Emgu.CV.UI.ImageBox.FunctionalModeOption.Minimum;

主程序代码:(对准考证、答题区域分别测量参考像素值)

if (ib_original.Image != null)
            {
                Mat src = new Image<Bgr, byte>(ib_original.Image.Bitmap).Mat;

                //1.获取当前图像的最大矩形边界
                VectorOfVectorOfPoint max_contour = commonUse.GetBoundaryOfPic(src);

                //2.对图像进行矫正
                Mat mat_Perspective = commonUse.MyWarpPerspective(src, max_contour);
                //规范图像大小
                CvInvoke.Resize(mat_Perspective, mat_Perspective, new Size(590, 384), 0, 0, Emgu.CV.CvEnum.Inter.Cubic);
                //3.二值化处理(大于阈值取0,小于阈值取255。其中白色为0,黑色为255)
                Mat mat_threshold = new Mat();
                int myThreshold = Convert.ToInt32(num_threshold.Value);
                CvInvoke.Threshold(mat_Perspective, mat_threshold, myThreshold, 255, Emgu.CV.CvEnum.ThresholdType.BinaryInv);
                //ib_middle.Image = mat_threshold;

                //形态学膨胀
                Mat mat_dilate = commonUse.MyDilate(mat_threshold);
                //ib_middle.Image = mat_dilate;

                //筛选长宽比大于2的轮廓
                VectorOfVectorOfPoint selected_contours = commonUse.GetUsefulContours(mat_dilate, 1);
                //画出轮廓
                Mat color_mat = commonUse.DrawContours(mat_Perspective, selected_contours);
                ib_middle.Image = color_mat;


                ib_result.Image = mat_Perspective;
                //准考证号,x=230+26*5,y=40+17*10
                tb_log.Text = commonUse.GetValueAndDrawGrid(ib_result, selected_contours, 230, 26, 5, 40, 17, 10, "准考证号:");

                //答题区1-5题,x=8+25*5,y=230+16*4
                tb_log.Text += commonUse.GetValueAndDrawGrid(ib_result, selected_contours, 8, 25, 5, 230, 16, 4, "1-5:");

                //答题区6-10题,x=159+25*5,y=230+16*4
                tb_log.Text += commonUse.GetValueAndDrawGrid(ib_result, selected_contours, 159, 25, 5, 230, 16, 4, "6-10:");

                //答题区11-15题,x=310+25*5,y=230+16*4
                tb_log.Text += commonUse.GetValueAndDrawGrid(ib_result, selected_contours, 310, 25, 5, 230, 16, 4, "11-15:");

                //答题区16-20题,x=461+25*5,y=230+16*4
                tb_log.Text += commonUse.GetValueAndDrawGrid(ib_result, selected_contours, 461, 25, 5, 230, 16, 4, "16-20:");

                //答题区21-25题,x=8+25*5,y=312+16*4
                tb_log.Text += commonUse.GetValueAndDrawGrid(ib_result, selected_contours, 8, 25, 5, 312, 16, 4, "21-25:");

                //答题区26-30题,x=159+25*5,y=312+16*4
                tb_log.Text += commonUse.GetValueAndDrawGrid(ib_result, selected_contours, 159, 25, 5, 312, 16, 4, "26-30:");

                //答题区31-35题,x=310+25*5,y=312+16*4
                tb_log.Text += commonUse.GetValueAndDrawGrid(ib_result, selected_contours, 310, 25, 5, 312, 16, 4, "31-35:");
            }
            else
            {
                MessageBox.Show("请先加载图片");
            }

调用函数代码(自定义CommonUse类):

 

/// <summary>
        /// 获取给定图像的最大矩形边界
        /// </summary>
        /// <param name="src"></param>
        /// <returns></returns>
        public VectorOfVectorOfPoint GetBoundaryOfPic(Mat src)
        {
            Mat dst = new Mat();
            Mat src_gray = new Mat();
            CvInvoke.CvtColor(src, src_gray, Emgu.CV.CvEnum.ColorC
  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值