赞
踩
参考: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 VectorOfVectorOfPoin
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。