• (总结一)Halcon基础之寻找目标特征+转正


    • 第一步当然是读取图像了:read_image (Image, 'C:/Users/HJ/Desktop/test_image/b.jpg')        

    • 第二步:二值化。因为我这里的物体是黑色的,所以用binary_threshold来二值化的时候参数选择的‘dark’,如果特征是白色的话可以把dark改为light,效果如下:
    • 第三步:将区域打散,然后根据过滤条件来进行赛选,我这里是通过面积来进行筛选 。过滤条件在这里只用了一个,也可以使用多个过滤条件来进行过滤。

            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)

    注意事项:

    1. 在旋转的时候我们的角度是angle:=180/π  * phi,旋转之后物体的长边与X轴平行(也就是本子平着)如果想要本子竖起来就需要令angle := -(ratio*Phi+90)这样子旋转之后本子就竖起来
    2. 在使用crop_part这个函数对图像进行裁切时,如果长边与X轴平行则Row-Length2,Column-Length1。保存的图像的长宽分别为2*Lenghts1, 2*Lenghts2

    总体代码:

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


     

  • 相关阅读:
    企业电子招投标采购系统——功能模块&功能描述+数字化采购管理 采购招投标
    高中生可发表论文的学术期刊涵盖TCR历史期刊
    ceph 认证
    携手亚信安慧AntDB,在数智化浪潮中乘风破浪
    深度学习-第二章-Window下Yolov5训练打电话
    神经网络logistic回归模型,logistic回归的基本理论
    聊聊 HTTP 性能优化
    【PAT甲级 - C++题解】1063 Set Similarity
    web前端期末大作业 html+css古诗词主题网页设计
    Java Applet 类的学习笔记
  • 原文地址:https://blog.csdn.net/m0_48095841/article/details/125481975