OpenCV-Python身份证信息识别( 二 )


epsilon = 0.02 * cv2.arcLength(contours, True)approx = cv2.approxPolyDP(contours, epsilon, True)n = []for x, y in zip(approx[:, 0, 0], approx[:, 0, 1]):n.append((x, y))n = sorted(n)sort_point = []n_point1 = n[:2]n_point1.sort(key=lambda x: x[1])sort_point.extend(n_point1)n_point2 = n[2:4]n_point2.sort(key=lambda x: x[1])n_point2.reverse()sort_point.extend(n_point2)p1 = np.array(sort_point, dtype=np.float32)h = sort_point[1][1] - sort_point[0][1]w = sort_point[2][0] - sort_point[1][0]pts2 = np.array([[0, 0], [0, h], [w, h], [w, 0]], dtype=np.float32)# 生成变换矩阵M = cv2.getPerspectiveTransform(p1, pts2)# 进行透视变换dst = cv2.warpPerspective(image, M, (w, h))# print(dst.shape)show(dst, "dst")

OpenCV-Python身份证信息识别

文章插图
 
固定图像大小将图像变正 , 通过对图像的宽高进行判断 , 如果宽<高 , 就将图像旋转90° 。并将图像resize到指定大小 。方便之后对图像进行处理 。
if w < h:dst = np.rot90(dst)resize = cv2.resize(dst, (1084, 669), interpolation=cv2.INTER_AREA)show(resize, "resize")
OpenCV-Python身份证信息识别

文章插图
 
检测身份证文本位置经过灰度 , 二值滤波和开闭运算后 , 将图像中的文本区域主键显现出来 。
temp_image = resize.copy()gray = cv2.cvtColor(resize, cv2.COLOR_BGR2GRAY)show(gray, "gray")threshold = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV +cv2.THRESH_OTSU)[1]show(threshold, "threshold")blur = cv2.medianBlur(threshold, 5)show(blur, "blur")kernel = np.ones((3, 3), np.uint8)morph_open = cv2.morphologyEx(blur, cv2.MORPH_OPEN, kernel)show(morph_open, "morph_open")
OpenCV-Python身份证信息识别

文章插图
 
极度膨胀给定一个比较大的卷积盒 , 进行膨胀处理 , 使白色的区域加深加大 。更加显现出文本的区域 。
kernel = np.ones((7, 7), np.uint8)dilate = cv2.dilate(morph_open, kernel, iterations=6)show(dilate, "dilate")
OpenCV-Python身份证信息识别

文章插图
 
轮廓查找文本区域使用轮廓查找 , 将白色块状区域查找出来 。
binary, contours, hierarchy = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)resize_copy = resize.copy()res = cv2.drawContours(resize_copy, contours, -1, (255, 0, 0), 2)show(res, "res")
OpenCV-Python身份证信息识别

文章插图
 
筛选出文本区域经过上一步轮廓检测 , 我们发现 , 选中的轮廓中有一些噪点 , 通过对图像的观察 , 使用近似轮廓 , 然后用以下逻辑筛选出文本区域 。并定义文本描述信息 , 将文本区域位置信息加入到指定集合中 。到这一步 , 可以清晰地看到 , 所需要的文本区域统统都被提取了出来 。
labels = ['姓名', '性别', '民族', '出生年', '出生月', '出生日', '住址', '公民身份证号码']positions = []data_areas = {}resize_copy = resize.copy()for contour in contours:epsilon = 0.002 * cv2.arcLength(contour, True)approx = cv2.approxPolyDP(contour, epsilon, True)x, y, w, h = cv2.boundingRect(approx)if h > 50 and x < 670:res = cv2.rectangle(resize_copy, (x, y), (x + w, y + h), (0, 255, 0), 2)area = gray[y:(y + h), x:(x + w)]blur = cv2.medianBlur(area, 3)data_area = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]positions.append((x, y))data_areas['{}-{}'.format(x, y)] = data_areashow(res, "res")
OpenCV-Python身份证信息识别

文章插图
 
对文本区域进行排序发现文本的区域是由下到上的顺序 , 并且x轴从左到右的区域是无序的 , 所以使用以下逻辑 , 对文本区域进行排序
positions.sort(key=lambda p: p[1])result = []index = 0while index < len(positions) - 1:if positions[index + 1][1] - positions[index][1] < 10:temp_list = [positions[index + 1], positions[index]]for i in range(index + 1, len(positions)):if positions[i + 1][1] - positions[i][1] < 10:temp_list.append(positions[i + 1])else:breaktemp_list.sort(key=lambda p: p[0])positions[index:(index + len(temp_list))] = temp_listindex = index + len(temp_list) - 1else:index += 1识别文本对文本区域使用CnOcr一一进行识别 , 最后将识别结果进行输出 。


推荐阅读