当前位置:   article > 正文

EmguCV方形答题卡识别_emgucv答题卡识别

emgucv答题卡识别

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

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

最终效果图:

三个imageBox控件

  1. ib_original.SizeMode = PictureBoxSizeMode.Zoom;
  2. ib_original.FunctionalMode = Emgu.CV.UI.ImageBox.FunctionalModeOption.Minimum;
  3. ib_middle.SizeMode = PictureBoxSizeMode.Zoom;
  4. ib_middle.FunctionalMode = Emgu.CV.UI.ImageBox.FunctionalModeOption.Minimum;
  5. ib_result.SizeMode = PictureBoxSizeMode.Zoom;
  6. ib_result.FunctionalMode = Emgu.CV.UI.ImageBox.FunctionalModeOption.Minimum;

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

  1. if (ib_original.Image != null)
  2. {
  3. Mat src = new Image<Bgr, byte>(ib_original.Image.Bitmap).Mat;
  4. //1.获取当前图像的最大矩形边界
  5. VectorOfVectorOfPoint max_contour = commonUse.GetBoundaryOfPic(src);
  6. //2.对图像进行矫正
  7. Mat mat_Perspective = commonUse.MyWarpPerspective(src, max_contour);
  8. //规范图像大小
  9. CvInvoke.Resize(mat_Perspective, mat_Perspective, new Size(590, 384), 0, 0, Emgu.CV.CvEnum.Inter.Cubic);
  10. //3.二值化处理(大于阈值取0,小于阈值取255。其中白色为0,黑色为255)
  11. Mat mat_threshold = new Mat();
  12. int myThreshold = Convert.ToInt32(num_threshold.Value);
  13. CvInvoke.Threshold(mat_Perspective, mat_threshold, myThreshold, 255, Emgu.CV.CvEnum.ThresholdType.BinaryInv);
  14. //ib_middle.Image = mat_threshold;
  15. //形态学膨胀
  16. Mat mat_dilate = commonUse.MyDilate(mat_threshold);
  17. //ib_middle.Image = mat_dilate;
  18. //筛选长宽比大于2的轮廓
  19. VectorOfVectorOfPoint selected_contours = commonUse.GetUsefulContours(mat_dilate, 1);
  20. //画出轮廓
  21. Mat color_mat = commonUse.DrawContours(mat_Perspective, selected_contours);
  22. ib_middle.Image = color_mat;
  23. ib_result.Image = mat_Perspective;
  24. //准考证号,x=230+26*5,y=40+17*10
  25. tb_log.Text = commonUse.GetValueAndDrawGrid(ib_result, selected_contours, 230, 26, 5, 40, 17, 10, "准考证号:");
  26. //答题区1-5题,x=8+25*5,y=230+16*4
  27. tb_log.Text += commonUse.GetValueAndDrawGrid(ib_result, selected_contours, 8, 25, 5, 230, 16, 4, "1-5:");
  28. //答题区6-10题,x=159+25*5,y=230+16*4
  29. tb_log.Text += commonUse.GetValueAndDrawGrid(ib_result, selected_contours, 159, 25, 5, 230, 16, 4, "6-10:");
  30. //答题区11-15题,x=310+25*5,y=230+16*4
  31. tb_log.Text += commonUse.GetValueAndDrawGrid(ib_result, selected_contours, 310, 25, 5, 230, 16, 4, "11-15:");
  32. //答题区16-20题,x=461+25*5,y=230+16*4
  33. tb_log.Text += commonUse.GetValueAndDrawGrid(ib_result, selected_contours, 461, 25, 5, 230, 16, 4, "16-20:");
  34. //答题区21-25题,x=8+25*5,y=312+16*4
  35. tb_log.Text += commonUse.GetValueAndDrawGrid(ib_result, selected_contours, 8, 25, 5, 312, 16, 4, "21-25:");
  36. //答题区26-30题,x=159+25*5,y=312+16*4
  37. tb_log.Text += commonUse.GetValueAndDrawGrid(ib_result, selected_contours, 159, 25, 5, 312, 16, 4, "26-30:");
  38. //答题区31-35题,x=310+25*5,y=312+16*4
  39. tb_log.Text += commonUse.GetValueAndDrawGrid(ib_result, selected_contours, 310, 25, 5, 312, 16, 4, "31-35:");
  40. }
  41. else
  42. {
  43. MessageBox.Show("请先加载图片");
  44. }

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

 

  1. /// <summary>
  2. /// 获取给定图像的最大矩形边界
  3. /// </summary>
  4. /// <param name="src"></param>
  5. /// <returns></returns>
  6. public VectorOfVectorOfPoin
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/812057
推荐阅读
相关标签
  

闽ICP备14008679号