任务要求:
下图中的二维码进行校正。(HALCON10.0自带例图,路径:“images/datacode/ecc200/ecc200_to_preprocess_001.png”)
任务分析:
图中的二维码存在畸变,需对其进行透射变换。首先获得图中二维码四个顶点的坐标,设置好变换后的顶点坐标,再应用vector_to_proj_hom_mat2d算子,获得透射变换的变换矩阵,最后应用projective_trans_region算子实现图中二维码的校正。
代码分析:
dev_set_draw ('margin')//设置填充模式
read_image (Image, 'ecc200_to_preprocess_001.png')
threshold (Image, Region, 0,90)//二值化获取二维码的区域
*获得二维码区域轮廓
shape_trans (Region, RegionTrans, 'convex')
gen_contour_region_xld (RegionTrans, Contours, 'border')//区域转轮廓
segment_contours_xld (Contours, ContoursSplit, ‘lines’, 5, 10, 1) //获得轮廓的四条边
*定义数组,保存四边形的四个顶点坐标
XCoordCorners := []//保存四边形的顶点X坐标
YCoordCorners := []//保存四边形的顶点Y坐标
*对轮廓进行排序
sort_contours_xld (ContoursSplit, SortedRegions, 'lower_left', 'true', 'row')
count_obj (SortedRegions, Number)//获取区域数量
for Index := 1 to Number by 1
select_obj (SortedRegions, ObjectSelected, Index)
fit_line_contour_xld (ObjectSelected, 'tukey', -1, 0, 5, 2,
RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist)
//将轮廓拟合成直线
tuple_concat (XCoordCorners, RowBegin, XCoordCorners)
//保存顶点x坐标
tuple_concat (YCoordCorners, ColBegin, YCoordCorners)
//保存顶点的y坐标
endfor
*显示出顶点
gen_cross_contour_xld (Crosses, XCoordCorners, YCoordCorners, 6, 0.785398)
*获取投射变换参数HomMat2D
vector_to_proj_hom_mat2d (XCoordCorners, YCoordCorners, [px1,px2,px2,px1] , [py1,py1,py2,py2] , 'normalized_dlt', [], [], [], [], [], [], HomMat2D1, Covariance)//计算透射变换矩阵
projective_trans_image (Image, TransImage1, HomMat2D1, 'bilinear', 'false', 'false') //对图像进行透射变换。
项目结果: