首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Correction based on affine transformation

Correction based on affine transformation

原创
作者头像
Swing Dunn
发布2025-10-16 18:36:18
发布2025-10-16 18:36:18
730
举报
文章被收录于专栏:答题卡识别答题卡识别

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.

offset on scanned img
offset on scanned img

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.

template'info on scanned img
template'info on scanned img

source code:

代码语言:txt
复制
 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 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档