- 使用第二章,背景碰撞,由多张验证码图片,碰撞得出背景图片
- 原图
- 背景图
- 使用背景图和原图对比得出文字图片
- 使用第一章,找出文字块
- 提取分隔下方文字
-
分割后
-
对分割后的文字进行校正, 使用opencv获取文字外接最小四边形,更具四边形的顶点,获取倾斜角度进行图片旋转
def font_img_angle_correct(font_image):
height = font_image.shape[0]
width = font_image.shape[1]
font_image = add_margin(font_image, (255, 255, 255), (height, width, height, width))
gray1 = cv2.cvtColor(font_image, cv2.COLOR_BGR2GRAY)
_, binary_image = cv2.threshold(gray1, 127, 255, cv2.THRESH_BINARY)
# 找到轮廓
contours, _ = cv2.findContours(binary_image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# 找到除了图片边框外,最大的轮廓
maxArea = 0
maxContour = None
index = 0
filterContours = []
for contour in contours:
if index != 0:
area = cv2.contourArea(contour)
if area > maxArea and area > 40:
maxArea = area
maxContour = contour
filterContours.append(contour)
index = index + 1
merged_contour = np.vstack(filterContours)
rect = cv2.minAreaRect(merged_contour)
# c = max(contours, key=cv2.contourArea)
# show_contour(font1Image, [merged_contour])
# 获取最大轮廓,外接最小面积的旋转矩形
# rect = cv2.minAreaRect(maxContour)
# 获取旋转矩形的四个顶点
box = cv2.boxPoints(rect)
box = np.int0(box) # 将坐标点转换为整数
print("box: {}", box)
# 获取顶点最靠上的两个,计算,倾斜角度
topPoints = sorted(box, key=lambda p: p[1])[:2]
(p1, p2) = (topPoints[0], topPoints[1]) if topPoints[0][0] < topPoints[1][0] else (topPoints[1], topPoints[0])
tanVal = (p2[1] - p1[1]) / (p2[0] - p1[0])
angle = tan2angle(tanVal)
print("angle: {}", angle)
rotate_image = rotate_bound_white_bg(font_image, angle, (255, 255, 255))
return angle, rotate_image
-
使用OCR进行识别
-
使用每个字中的黑色像素点数量排序,进行辅助识别