halcon练习小demo(一)获取图像中的一个区域并进行矫正操作

目标:根据原始图片,获取图像中的二维码区域

 

 

 

代码:

read_image(Image, 'printer_chip/printer_chip_01')
get_image_size(Image, Width, Height)
dev_close_window()
dev_open_window(0, 0, Width/2, Height/2, 'black', WindowHandle)
dev_display(Image)

*获取图片中的类似于二维码的区域,不同的图片可能会位置差异
*如果图片晃动的幅度总是哎一个区域范围内,我们就可以画一个ROI区域,然后单独对ROI区域进行操作
draw_rectangle1(WindowHandle, RowLeft, ColumnLeft, RowRight, ColumnRight)
gen_rectangle1 (RectangleRoi, RowLeft, ColumnLeft, RowRight, ColumnRight)
reduce_domain(Image, RectangleRoi, ImageReduced)

*进行二值化,获取到region
binary_threshold(ImageReduced, Region, 'max_separability', 'light', UsedThreshold)
*将区域进行打散
connection(Region, ConnectedRegions)
*通过筛选条件对region进行筛选
select_shape(ConnectedRegions, SelectedRegions, ['width','height'], 'and', [100, 200],[500, 500])
*寻找region的最小外接矩形
smallest_rectangle2(SelectedRegions, Row, Column, Phi, Length1, Length2)

ratio := 180.0 / 3.14159
*rotate_image旋转时填的是角度不是弧度。所以需要将弧度转化成角度
rotate_image(ImageReduced, ImageRotate, -ratio*Phi, 'constant')
if (Height<Width)
    *裁剪图片,以 Row-Length1, Column-Length2为左上角的坐标,2*Length2作为裁切的宽度,2*Length1作为裁切的高度
    crop_part(ImageRotate, ImagePart, Row-Length1, Column-Length2, 2*Length2, 2*Length1)
else
    crop_part(ImageRotate, ImagePart, Row-Length2, Column-Length1, 2*Length1, 2*Length2)
endif

 

posted @ 2022-07-05 16:13  小女孩真可爱  阅读(880)  评论(0编辑  收藏  举报