OpenCV项目4-图像处理之答题卡识别判卷
项目思路:
- (1) 图片读取
- (2) 图片预处理即灰度化、滤波器、边缘检测
- (3) 图片透视变换即把答题卡视角拉正
- (4) 每个圆圈轮廓检测、遍历、绘制、排序
- (5) 通过计算非零值来判断答题是否正确
- (6) 计算及显示分数
1.图片显示函数
import numpy as np
import cv2
def cv_show(name, img):
cv2.imshow(name, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
2.图片读取
img = cv2.imread('./images/test_01.png')
cv_show('img', img)
3.图片灰度化、滤波器去噪、边缘检测
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)
4.轮廓检测、绘制、排序、遍历
cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]
contours_img = img.copy()
cv2.drawContours(contours_img, cnts, -1, (0, 0, 255), 3)
cv_show('contous_img', contours_img)
if len(cnts) > 0: # 确保我们拿到的轮廓是答题卡的轮廓
cnts = sorted(cnts, key=cv2.contourArea, reverse=True) # 把cnts放入key的面积函数再进行排序
for c in cnts:
perimeter = cv2.arcLength(c, True) # 计算周长
approx = cv2.approxPolyDP(c, 0.02 * perimeter, True) # 得到近似的轮廓 近似完之后应该只剩下4个角的坐标
# print(c) # 没做近似多边形之前轮廓有多个点
# print(approx) # 没做近似多边形之前轮廓有四个点
if len(approx) == 4:
docCnt = approx# 保存approx
break # 找到答题卡近似轮廓, 直接推荐
5.透视变换
# 透视变换要找到变换矩阵。变换矩阵要求:原图的4个点坐标和变换之后的4个点坐标-现在已找到原图的4个点坐标. 需要找到变换之后的4个坐标 先对获取到的4个角点坐标按照一定顺序(顺时针或逆时针)排序
# 5.1 排序函数
def order_points(pts):
rect = np.zeros((4, 2), dtype='float32') # 创建全是0的矩阵, 来接收等下找出来的4个角的坐标
s = pts.sum(axis=1)
rect[0] = pts[np.argmin(s)] # 左上的坐标一定是x,y加起来最小的坐标. 右下的坐标一定是x,y加起来最大的坐标
rect[2] = pts[np.argmax(s)]
diff = np.diff(pts, axis=1) # 右上角的x,y相减的差值一定是最小的 左下角的x,y相减的差值, 一定是最大
rect[1] = pts[np.argmin(diff)]# diff:后一列减去前一列
rect[3] = pts[np.argmax(diff)]
return rect
# 5.2 透视变换函数
def four_point_transform(image, pts):
rect = order_points(pts) # 对输入的4个坐标排序
(tl, tr, br, bl) = rect # 上右点、上左点、下右点、下左点
widthA = np.sqrt((br[0] - bl[0]) ** 2 + (br[1] - bl[1]) ** 2)# 下两点距离 # 空间中两点的距离
widthB = np.sqrt((tr[0] - tl[0]) ** 2 + (tr[1] - tl[1]) ** 2)# 上两点距离
max_width = max(int(widthA), int(widthB))
heightA = np.sqrt((tr[0] - br[0]) ** 2 + (tr[1] - br[1]) ** 2)# 右两点距离
heightB = np.sqrt((tl[0] - bl[0]) ** 2 + (tl[1] - bl[1]) ** 2)# 左两点距离
max_height = max(int(heightA), int(heightB))
dst = np.array([ # 构造变换之后的对应坐标位置
[0, 0],
[max_width - 1, 0],
[max_width - 1, max_height - 1],
[0, max_height - 1]], dtype='float32') # 从0开始所以减一
M = cv2.getPerspectiveTransform(rect, dst) # 计算变换矩阵
warped = cv2.warpPerspective(image, M, (max_width, max_height)) # 透视变换-图片摆正
return warped
# 5.3 透视变换
warped = four_point_transform(gray, docCnt.reshape(4, 2))
cv_show('warped', warped)
6.二值化
thresh = cv2.threshold(warped, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
cv_show('thresh', thresh)
7.轮廓检测、绘制、遍历、排序
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0] # 找到每一个圆圈轮廓
thresh_contours = thresh.copy()
cv2.drawContours(thresh_contours, cnts, -1, 255, 3)
cv_show('thresh_contours', thresh_contours)
import matplotlib.pyplot as plt
plt.imshow(thresh_contours, cmap='gray')
question_cnts = [] # 遍历所有轮廓, 找到特定宽高和特定比例轮廓, 即圆圈轮廓
for c in cnts:
(x, y, w, h) = cv2.boundingRect(c) # 找到轮廓的外接矩形
ar = w / float(h) # 计算宽高比
if w >= 20 and h >= 20 and 0.9 <= ar <= 1.1: # 根据实际情况制定标准.
question_cnts.append(c)
def sort_contours(cnts, method='left-to-right'): # 轮廓排序功能封装成函数
reverse = False
i = 0 # 排序的时候, 取x轴数据, i=0, 取y轴数据i =1
if method == 'right-to-left' or method == 'bottom-to-top':
reverse = True
if method == 'top-to-bottom' or method == 'bottom-to-top': # 按y轴坐标排序
i = 1
bounding_boxes = [cv2.boundingRect(c) for c in cnts] # 计算每个轮廓的外接矩形
(cnts, bounding_boxes) = zip(*sorted(zip(cnts, bounding_boxes), key=lambda b: b[1][i], reverse=reverse))
return cnts, bounding_boxes
# 按照从上到下顺序对question_cnts排序
question_cnts = sort_contours(question_cnts, method='top-to-bottom')[0]
8.判断是否正确答案
ANSWER_KEY = {0:1, 1:4, 2:0, 3:3, 4:1}
correct = 0
for (q, i) in enumerate(np.arange(0, 25, 5)):
# print(q, i)
cnts = sort_contours(question_cnts[i: i + 5])[0]# 每次取出5个轮廓, 再按照x轴坐标从小到大排序
bubbled = None
for (j, c) in enumerate(cnts): # 遍历每一个结果
mask = np.zeros(thresh.shape, dtype='uint8') # 使用掩膜, 即mask
cv2.drawContours(mask, [c], -1, 255, -1)
# cv_show('mask', mask)
mask = cv2.bitwise_and(thresh, thresh, mask=mask) # 先做与运算
# cv_show('mask', mask)
total = cv2.countNonZero(mask) # 计算非零个数, 选择的选项非零个数比较多, 没选中的选项非零个数少一些
if bubbled is None or total > bubbled[0]:
bubbled = (total, j)
color = (0, 0, 255)
k = ANSWER_KEY[q]
if k == bubbled[1]: # 判断是否做题正确
correct += 1
color = (0, 255, 0)
cv2.drawContours(warped, [cnts[k]], -1, color, 3)# 绘图
9.分数计算、显示
print(correct)
score = (correct / 5.0) * 100
print(f'score: {score:.2f}%')
cv2.putText(warped, str(score) + '%', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
cv_show('result', warped)
4
score: 80.00%