当前位置:   article > 正文

opencv实现答题卡识别_opencv答题卡识别

opencv答题卡识别
  1. """
  2. 识别答题卡
  3. """
  4. import cv2
  5. import numpy as np
  6. def showImg(img_name, img):
  7. cv2.imshow(img_name, img)
  8. cv2.waitKey()
  9. cv2.destroyAllWindows()
  10. def get_max_rect(sorted_cnts):
  11. for cnt in sorted_cnts:
  12. # 轮廓近似
  13. possible_cnts = []
  14. epsilon = 0.1 * cv2.arcLength(cnt, True)
  15. approx = cv2.approxPolyDP(cnt, epsilon, True)
  16. if len(approx) == 4:
  17. possible_cnts.append(cnt)
  18. possible_cnts = sorted(possible_cnts, key=lambda x: cv2.arcLength(x, True))
  19. return possible_cnts
  20. def get_max_bounding_rect(possible_cnts):
  21. # for cnt in possible_cnts:
  22. # x, y, w, h = cv2.boundingRect(cnt)
  23. sorted_cnts = sorted(possible_cnts, key=lambda cnt: cv2.boundingRect(cnt)[2]*cv2.boundingRect(cnt)[3], reverse=True)
  24. print(sorted_cnts[0])
  25. def show_countour(img, cnt):
  26. img_copy = img.copy()
  27. cv2.drawContours(img_copy, cnt, -1, (0,255, 0), 3)
  28. showImg("img_copy", img_copy)
  29. # 读取答题卡图片,并显示
  30. answer_sheet_img = cv2.imread("t1.jpg")
  31. print(answer_sheet_img.shape)
  32. showImg("answer_sheet_img", answer_sheet_img)
  33. # 高斯滤波,去除噪音
  34. blur = cv2.GaussianBlur(answer_sheet_img,(5,5),0)
  35. showImg("blur", blur)
  36. # 图像转灰度值
  37. sheet_gray = cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY)
  38. showImg("sheet_gray", sheet_gray)
  39. # 二值化
  40. retval, sheet_threshold = cv2.threshold(sh
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/812006
推荐阅读
相关标签
  

闽ICP备14008679号