

connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 400000, 9900000)

smallest_rectangle2 (SelectedRegions, Row, Column, Phi, Lenghts1, Lenghts2)
返回的Row,Column是区域特征的中心点坐标,Lenghts1是物体区域的长边的一半,Lenghts2是物体区域的短边的一半,Lenghts1>Lenghts2恒成立。Phi是x轴(水平)与长边的夹角的弧度,
它的值在(-π/2, π/2), 要将phi换成角度的话只用angle := -180/π * phi就好了
rotate_image(Image, ImageRotate, angle, 'constant')
在halcon中rotate_image是按逆时针方向进行旋转,而且旋转后的图片大小不变,但内容可能会有缺失。并且物体的中心在原图的位置和在旋转之后图片上的位置还会有差异。

为了解决使用rotate_image进行旋转产生的问题,可以所以建议使用旋转矩阵来进行旋转。具体参数解释在代码里
hom_mat2d_identity(HomMat2DIdentity)
hom_mat2d_rotate(HomMat2DIdentity, rad(angle), Row, Column, HomMat2DRotate)
affine_trans_image(Image, ImageAffineTrans, HomMat2DRotate, 'constant', 'false')

crop_part(ImageAffineTrans, ImagePart, Row-Lenghts2, Column-Lenghts1, 2*Lenghts1, 2*Lenghts2)

注意事项:
总体代码:
- * read_image (Image, 'C:/Users/HJ/Desktop/test_image/Image_20211023135906261.bmp')
- *读取图片
-
- read_image (Image, 'C:/Users/HJ/Desktop/test_image/Image_20211023135615498.bmp')
-
-
- *自适应二值化,当寻找的物体为偏黑时使用dark, 偏白时使用light
- binary_threshold(Image, Region, 'max_separability', 'dark', UsedThreshold)
- *将区域打散
- connection (Region, ConnectedRegions)
- *通过面积来筛选区域
- select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 15000, 9900000)
- *寻找区域的最小外接矩形
- smallest_rectangle2 (SelectedRegions, Row, Column, Phi, Lenghts1, Lenghts2)
-
- *角度换算
- ratio := 90.045/1.5708
- * angle := -ratio*Phi //长边为水平
- angle := -(ratio*Phi+90)
-
- *创建旋转矩阵
- hom_mat2d_identity(HomMat2DIdentity)
- hom_mat2d_rotate(HomMat2DIdentity, rad(angle), Row, Column, HomMat2DRotate)
- *旋转图片,false的时候图像大小不发生改变,true的时候图像大小会变化
- affine_trans_image(Image, ImageAffineTrans, HomMat2DRotate, 'constant', 'false')
- *裁切图片,当长边与x轴平行时,Row-Lenghts2, Column-Lenghts1, 2*Lenghts1, 2*Lenghts2
- *当短边与x轴平行时,Row-Lenghts1, Column-Lenghts2, 2*Lenghts2, 2*Lenghts1
- crop_part(ImageAffineTrans, ImagePart, Row-Lenghts2, Column-Lenghts1, 2*Lenghts1, 2*Lenghts2)
-
-