投影变换矫正倾斜图像,识别二维码
https://blog.csdn.net/qq_33628827/article/details/103665480
打开halcon,按下ctrl+e打开halcon自带例程。方法->二维码识别->2d_data_codesrectify_symbol.hdev
*This program demonstrates how to read a slanted
* 2d data code by preprocessing with rectification
*
dev_update_off()
dev_close_window()
* Get the image and display it
*读取并显示图像
read_image(Image_slanted, 'datacode/ecc200/ecc200_to_preprocess_001')
dev_open_window_fit_image(Image_slanted, 0, 0, -1, -1, WindowHandle)
set_display_font(WindowHandle, 14, 'mono', 'true', 'false')
*设置画笔颜色与宽度
dev_set_color('green')
dev_set_line_width(3)
*定义一些显示信息变量
Message : = 'This program demonstrates how to preprocess'
Message[1] : = 'a slanted 2d data code with rectification'
Message[2] : = 'before reading the data code symbol.'
*显示信息
disp_message(WindowHandle, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message(WindowHandle, 'black', 'true')
stop()
* Initialize coordinates
*定义坐标变量,这是需要进行变换的原始图像的四个边角坐标,在实际项目中这些数据可以通过blob分析或其他方式获取到
XCoordCorners : = [130, 225, 290, 63]
YCoordCorners : = [101, 96, 289, 269]
*
* Display the slanted image and the corners of the symbol
*生成十字叉,方便显示在图像上
*第一个参数输出变量
*第二个参数十字的X坐标
*第三个参数十字的Y坐标
*第四个参数十字叉大小
*第五个参数十字叉的方向
gen_cross_contour_xld(Crosses, XCoordCorners, YCoordCorners, 6, 0.785398)
*显示
dev_display(Image_slanted)
dev_display(Crosses)
disp_message(WindowHandle, 'Slanted image', 'window', 12, 12, 'black', 'true')
Message : = 'The marked corners are used to generate a'
Message[1] : = 'homogeneous transformation matrix which'
Message[2] : = 'defines the projective transformation for'
Message[3] : = 'the rectification of the symbol.'
disp_message(WindowHandle, Message, 'window', 380, 12, 'black', 'true')
disp_continue_message(WindowHandle, 'black', 'true')
stop()
*
* First generate a transformation matrix using the given points
* of the corners of the data code symbol and the corresponding points
* of a quare.
*求出投影变换矩阵
*第一个参数原图像的X坐标
*第二个参数原图像的Y坐标
*第三个参数原图像的W坐标,因为是在同一个平面上,全设置为1,
*第四个参数目标图像上的X坐标
*第五个参数目标图像上的Y坐标
*第六个参数目标图像上的W坐标
*第七个参数变换方式选择,对于常规几何问题,选择'normalized_dlt'
*第八个参数输出的投影变化矩阵
hom_vector_to_proj_hom_mat2d(XCoordCorners, YCoordCorners, [1, 1, 1, 1], [70, 270, 270, 70], [100, 100, 300, 300], [1, 1, 1, 1], 'normalized_dlt', HomMat2D)
*
* Now rectifiy the slanted image by applying the projective transformation
*对原图像进行投影变换
*第一个参数输入的倾斜图像
*第二个参数输出矫正后的图像
*第三个参数求出的投影变换矩阵
*第四个参数投影后的图像插值方式
*第五个参数是否适应图像大小,false是变换图跟原图一样大小
*第五个参数是否对原图像所有像素进行投影变换
projective_trans_image(Image_slanted, Image_rectified, HomMat2D, 'bilinear', 'false', 'true')
*
* Create the data code model and search
* for the data code in the rectified image
create_data_code_2d_model('Data Matrix ECC 200', [], [], DataCodeHandle)
*寻找二维码轮廓与解析二维码数据
find_data_code_2d(Image_rectified, SymbolXLDs, DataCodeHandle, [], [], ResultHandles, DecodedDataStrings)
*
* Display result
dev_display(Image_slanted)
dev_display(Image_rectified)
dev_display(SymbolXLDs)
disp_message(WindowHandle, 'Decoding successful ', 'window', 12, 12, 'black', 'true')
set_display_font(WindowHandle, 12, 'mono', 'true', 'false')
disp_message(WindowHandle, DecodedDataStrings, 'window', 350, 70, 'forest green', 'true')