@ Fu Xianjun. All Rights Reserved.
前言
随着人工智能的不断发展,OpenCv这门技术也越来越重要,很多人都开始学习OpenCv,本文就介绍了如何使用OpenCv进行答题卡的扫描。
提示:以下是本篇文章正文内容,下面案例可供参考
一、该功能使用了哪些方法?
想要完成该功能需要用到OpenCv中的平滑处理,边缘检测,透视变换,坐标点处理。
二、使用步骤
1.预处理,轮廓检测
代码如下(示例):
import cv2
import numpy as np
#设置正确答案
ANSWER_KEY = {
0:1,1:4,2:0,3:3,4:1}
def cv_show(name,img):
cv2.imshow(name,img)
cv2.waitKey()
cv2.destroyAllWindows()
#读取输入
img = cv2.imread('test_01.png')
contours_img = img.copy()
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray,(5,5),0)
cv_show("blurred",blurred)
edged = cv2.Canny(blurred,75,200)
cv_show('edged',edged)
#轮廓检测
cnts = cv2.findContours(edged.copy(),cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)[0]
cv2.drawContours(contours_img,-1,(255,255,255),3)
cv_show("contours_img",contours_img)
2.轮廓排序,透视变换
def order_points(pts):
rect = np.zeros((4,2),dtype = 'float32')
#按顺序找到对应坐标0123分别是左上,右上,右下,左下
#计算左上,右上
s = pts.sum(axis =