• HALCON的python下的使用方法(直接开发,不需要调用.hdev文件)


    一、环境配置方法

    基本要求: Python版本>=3.8 ; Halcon版本 >=20.11

    1)首先创建一个python版本大于3.8的基础环境

    2)然后查看自己的halcon的版本,在该环境下安装halcon

    如图所示,版本是20110,执行以下语句,完成halcon的安装

    pip install mvtec-halcon==20110

    安装成功后,出现如下的图像:

    3)将halcon相关的dll放在python.exe所在的文件夹下。halcon的相关dll可以在你安装的halcon的文件位置获得。拷贝以下命名的dll,进行拷贝,放置在创建的python环境中python.exe所在的位置。

    拷贝后,如图所示:

    4)验证是否成功

    import halcon

    没有报错证明安装成功,可以使用下面的代码进行测试

    1. import halcon as ha
    2. WindowHandle = ha.open_window(0, 0, 500, 400, father_window=0, mode='visible', machine='')
    3. Image = ha.read_image('die/die_03')
    4. ha.disp_obj(Image, WindowHandle)
    5. ha.wait_seconds(5)

    5)opencv和halcon基于python的图像转换的方法

    1,Python将Halcon图像转OpenCV(CV2)图像(高效)

    1. import cv2
    2. import halcon as ha
    3. from PIL import Image
    4. from halcon.numpy_interop import himage_as_numpy_array
    5. img = cv2.imread(r"C:\Users\11716\Desktop\DogCat-seg\images\train\13.jpg")
    6. image = ha.read_image(r'C:\Users\11716\Desktop\DogCat-seg\images\train\6.jpg')
    7. res = himage_as_numpy_array(image)
    8. print(type(res))
    9. img = cv2.cvtColor(res,cv2.COLOR_RGB2BGR)
    10. print(type(img))
    11. img= Image.fromarray(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))
    12. img.show()
    13. print(type(img))

     2,cv2 np array 转 HObject

    1. import cv2
    2. import halcon as ha
    3. from PIL import Image
    4. from halcon.numpy_interop import himage_as_numpy_array,himage_from_numpy_array
    5. img = cv2.imread(r"C:\Users\11716\Desktop\DogCat-seg\images\train\13.jpg")
    6. image = ha.read_image(r'C:\Users\11716\Desktop\DogCat-seg\images\train\6.jpg')
    7. res = himage_as_numpy_array(image)
    8. print(type(res))
    9. img = cv2.cvtColor(res,cv2.COLOR_RGB2BGR)
    10. print(type(img))
    11. ## cv2 np array 转 HObject
    12. hobjectImg = himage_from_numpy_array(img)
    13. print(type(hobjectImg))

    6)复杂功能的实现,测量功能的实现方法

     对于负责功能基于halcon的实现,编程方法和在halcon中的不太一样,需要将输出的结果写在功能函数的前面。如下面实现的复杂的功能测量功能的实现:

    1. import halcon as ha
    2. import cv2
    3. from halcon.numpy_interop import himage_from_numpy_array
    4. import time
    5. WindowHandle = ha.open_window(0, 0, 500, 400, father_window=0, mode='visible', machine='')
    6. mat_image=cv2.imread('143228_014.png',-1)
    7. start_time_init = time.time()
    8. hobjectImg = himage_from_numpy_array(mat_image)
    9. Width, Height=ha.get_image_size(hobjectImg)
    10. Row = 77
    11. Column = 669
    12. Phi = -1.5708
    13. print(Phi)
    14. Length1 = 31
    15. Length2 = 24
    16. Rectangle=ha.gen_rectangle2(Row, Column, Phi, Length1, Length2)
    17. print(Rectangle)
    18. Interpolation = 'nearest_neighbor'
    19. MeasureHandle=ha.gen_measure_rectangle2 (Row, Column, Phi, Length1, Length2, Width, Height, Interpolation)
    20. Sigma = 1.0
    21. Threshold = 30
    22. Transition = 'all'
    23. Select = 'all'
    24. RowEdge,ColumnEdge,Amplitude,Distance= ha.measure_pos (hobjectImg, MeasureHandle, Sigma, Threshold, Transition, Select)
    25. totall_distance =sum(Distance)
    26. end_time_init = time.time()
    27. elapsed_time_init = (end_time_init - start_time_init)*1000
    28. print("检测时间为: {} ms".format(elapsed_time_init))
    29. print(totall_distance)
    30. # ha.write_image(hobjectImg,'png',0,'F:/1.png')

  • 相关阅读:
    Linux_oops缺页异常后输出的宕机日志解读
    有哪些视频媒体?邀请视频媒体报道活动的好处
    成像光谱遥感技术中的AI革命:ChatGPT应用指南
    PX4 固件常用 QGroundControl 参数设置
    java计算机毕业设计高校微后勤服务平台源码+mysql数据库+系统+lw文档+部署
    C++构成和编码规范
    擎创技术流 | ClickHouse实用工具—ckman教程(4)
    80W美团架构师整理分享出了Spring5企业级开发实战文档
    MyBatisPlus(十一)包含查询:in
    手把手教你如何通过Java给图片添加文字和图片水印
  • 原文地址:https://blog.csdn.net/YOULANSHENGMENG/article/details/134335604