Using the previous method, we have completed the detection of the positioning points. Now, we are displaying the template data on the scanned answer sheet image: Red represents the detected positioning blocks, green represents the positions of the positioning blocks given by the template, and blue represents the positions and sizes of the multiple-choice options in the template.
It can be observed that there is a significant offset between the scanned image and the template data.
Since the number of detected positioning blocks is greater than or equal to 3, the conditions for affine transformation are met; perform affine transformation on the scanned image, and draw on the transformed image the positions and sizes of the positioning blocks and question blocks provided by the template, as well as the positions and sizes of the detected positioning blocks: Red represents the detected positioning blocks, green represents the positions of the positioning blocks given by the template, and blue represents the positions and sizes of the multiple-choice options in the template.
It can be observed that, after correction, the position coordinates of the corresponding areas on the scanned image are basically consistent with the template information.
source code:
cv2.rectangle(ori_img,(lt_archor[0], lt_archor[1]) ,(lt_archor[0] +lt_archor[2] ,lt_archor[1] +lt_archor[3]), (0,255,0),2)
cv2.rectangle(ori_img,(rt_archor[0], rt_archor[1]) ,(rt_archor[0] +rt_archor[2] ,rt_archor[1] +rt_archor[3]), (0,255,0),2)
cv2.rectangle(ori_img,(lb_archor[0], lb_archor[1]) ,(lb_archor[0] +lb_archor[2] ,lb_archor[1] +lb_archor[3]), (0,255,0),2)
cv2.rectangle(ori_img,(rb_archor[0], rb_archor[1]) ,(rb_archor[0] +rb_archor[2] ,rb_archor[1] +rb_archor[3]), (0,255,0),2)
for i in range(len(opt_block_ltpts)):
lt_x = opt_block_ltpts[i][0]
lt_y = opt_block_ltpts[i][1]
rb_x = lt_x + opt_block_size[0]
rb_y = lt_y + opt_block_size[1]
cv2.rectangle(ori_img,(lt_x, lt_y) ,(rb_x , rb_y ), (255,0,0),2)
#show_image(ori_img)
pts1 = np.float32([[lt_archor[0] +lt_archor[2], lt_archor[1] + lt_archor[3]],
[rt_archor[0], rt_archor[1] +rt_archor[3]],
[lb_archor[0] +lb_archor[2], lb_archor[1]]])
# 定义目标图像中的三个点
pts2 = np.float32([[re_lt_rect[0] + re_lt_rect[2] ,re_lt_rect[1] + re_lt_rect[3]],
[re_rt_rect[0], re_rt_rect[1] + re_rt_rect[3]],
[re_lb_rect[0] + re_lb_rect[2], re_lb_rect[1]]])
# 计算仿射变换矩阵
M = cv2.getAffineTransform(pts2, pts1)
# 应用仿射变换
transformed_image = cv2.warpAffine(display_img, M, (display_img.shape[1], display_img.shape[0]))
cv2.rectangle(transformed_image,(lt_archor[0], lt_archor[1]) ,(lt_archor[0] +lt_archor[2] ,lt_archor[1] +lt_archor[3]), (0,255,0),2)
cv2.rectangle(transformed_image,(rt_archor[0], rt_archor[1]) ,(rt_archor[0] +rt_archor[2] ,rt_archor[1] +rt_archor[3]), (0,255,0),2)
cv2.rectangle(transformed_image,(lb_archor[0], lb_archor[1]) ,(lb_archor[0] +lb_archor[2] ,lb_archor[1] +lb_archor[3]), (0,255,0),2)
cv2.rectangle(transformed_image,(rb_archor[0], rb_archor[1]) ,(rb_archor[0] +rb_archor[2] ,rb_archor[1] +rb_archor[3]), (0,255,0),2)
cv2.rectangle(transformed_image,(re_lt_rect[0], re_lt_rect[1]) ,(re_lt_rect[0] +re_lt_rect[2] ,re_lt_rect[1] +re_lt_rect[3]), (0,0,255),2)
cv2.rectangle(transformed_image,(re_rt_rect[0], re_rt_rect[1]) ,(re_rt_rect[0] +re_rt_rect[2] ,re_rt_rect[1] +re_rt_rect[3]), (0,0,255),2)
cv2.rectangle(transformed_image,(re_lb_rect[0], re_lb_rect[1]) ,(re_lb_rect[0] +re_lb_rect[2] ,re_lb_rect[1] +re_lb_rect[3]), (0,0,255),2)
cv2.rectangle(transformed_image,(re_rb_rect[0], re_rb_rect[1]) ,(re_rb_rect[0] +re_rb_rect[2] ,re_rb_rect[1] +re_rb_rect[3]), (0,0,255),2)
for i in range(len(opt_block_ltpts)):
lt_x = opt_block_ltpts[i][0]
lt_y = opt_block_ltpts[i][1]
rb_x = lt_x + opt_block_size[0]
rb_y = lt_y + opt_block_size[1]
cv2.rectangle(transformed_image,(lt_x, lt_y) ,(rb_x , rb_y ), (255,0,0),2)
show_image(transformed_image)
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。