当前位置:   article > 正文

一次halcon翻译成python的过程记录_halcon python

halcon python

距离上次发出记录halcon和python联合的文章已经半年过去,为了弥补上次疏漏这次做个入门级的.   详见:>https://blog.csdn.net/u014584014/article/details/127500490

众所周知halcon从20.11版本后提供了python接口,但是没有自动导出python的功能,这使人非常苦恼.

于是手工翻译一下:基本上都是语法差异:

首先说一下环境:halcon20.11是第一个支持python的版本.(同时它不支持32位操作系统||不是python不支持,而是halcon这个版本自身就不支持32位.)(python解释器版本要求3.8以及以上且必须是64位)(只有64位操作系统才支持64位的python解释器):是不是很啰嗦?没关系再总结一遍.省的有人拿别的版本折腾不到位...(在终端输入pip install mvtec-halcon==0:这时pip找不到0版本的安装包,就会报错并且返回可用包的版本号列表,)

  1. C:\Users\Administrator>pip install mvtec-halcon==0
  2. ERROR: Could not find a version that satisfies the
  3. requirement mvtec-halcon==0 (from versions:
  4. 20110.0.0, 20110.0.1, 20111.0.0, 20111.0.1,
  5. 20112.0.0, 20113.0.0, 21050.0.0, 21110.0.0,
  6. 22050.0.0, 22110.0.0, 22111.0.0, 23050.0.0)
  7. ERROR: No matching distribution
  8. found for mvtec-halcon==0

由此可见如今halcon已经有12个版本支持python了.

1.操作系统必须是windows,X64; win7_64; win10_64 都可以。
2.软件版本:halcon20.11(这个只支持64位的windows,32位低版本虽然可以pythonnet勉强写出来,但是风格非常C#,低版本还是考虑原生C#走起.
3.python版本:最低3.8.8也得是64位解释器;我用的是python-3.8.8-amd64.exe;
4.文本编辑器:我用的Notepad++编辑;调试用自带的IDLE; (根据自己喜好)vscode有微弱的自动提示.装了但用不习惯.pycharm..太专业不会配置.庞大臃肿.不想装..
如果没有安装用以下命令安装..

pip install mvtec-halcon==20111

 下来就是找一个文件夹把C/C++de那些dll拿进来,新建.py文件也放进来作为pyhalcon的工作空间:

873ab70c180b49129b95064ad22590d1.jpeg

 就是这么一张图,"display.jpg"也丢进刚才的文件夹.

先看halcon 的.hdev..

  1. dev_close_window ()
  2. read_image (Image_display, 'display.jpg')
  3. rgb1_to_gray (Image_display, GrayImage)
  4. get_image_size(Image_display,imageWidth, imageHeight)
  5. dev_open_window (0, 0, imageWidth, imageHeight, 'black', WindowHandle1)
  6. dev_display (GrayImage)
  7. XCoordCorners := []
  8. YCoordCorners := []
  9. threshold(GrayImage,DarkRegion,0, 80)
  10. connection (DarkRegion, ConnectedRegions)
  11. select_shape_std (ConnectedRegions, displayRegion, 'max_area', 70)
  12. reduce_domain (GrayImage, displayRegion, displayImage)
  13. gen_contour_region_xld (displayRegion, Contours, 'border')
  14. segment_contours_xld (Contours, ContoursSplit, 'lines', 5, 4, 2)
  15. count_obj (ContoursSplit, Number)
  16. for index:=1 to Number by 1
  17. select_obj(ContoursSplit, ObjectCurrent, index)
  18. fit_line_contour_xld (ObjectCurrent, 'tukey', -1, 0, 5, 2, RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist)
  19. tuple_concat (XCoordCorners, RowBegin, XCoordCorners)
  20. tuple_concat (YCoordCorners, ColBegin, YCoordCorners)
  21. endfor
  22. XOff:= 100
  23. YOff:= 100*imageHeight/imageWidth
  24. hom_vector_to_proj_hom_mat2d (XCoordCorners, YCoordCorners, [1,1,1,1], [YOff,YOff,imageHeight-YOff,imageHeight-YOff], [XOff,imageWidth-XOff,imageWidth-XOff,XOff], [1,1,1,1], 'normalized_dlt', HomMat2D)
  25. projective_trans_image (Image_display, Image_rectified, HomMat2D, 'bilinear', 'false', 'false')
  26. dev_display (Image_rectified)

注释就不用写了吧,代码具有自解释性.然后上python (其实几乎一样,这里主要是感受下语法差异).

  1. import os
  2. import halcon as ha
  3. def cmd(s="pause"):
  4. os.system(s)
  5. def open_window(width, height):
  6. if os.name == 'nt':
  7. ha.set_system('use_window_thread', 'true')
  8. return ha.open_window(
  9. row=0,
  10. column=0,
  11. width=width,
  12. height=height,
  13. father_window=0,
  14. mode='visible',
  15. machine=''
  16. )
  17. Image_display = ha.read_image('display.jpg')
  18. Width, Height = ha.get_image_size(Image_display)
  19. WindowHandle1 =open_window(Width[0], Height[0])
  20. WindowHandle2 =open_window(Width[0], Height[0])
  21. ha.disp_obj(Image_display, WindowHandle1);
  22. GrayImage = ha.rgb1_to_gray(Image_display)
  23. XCoordCorners = []
  24. YCoordCorners = []
  25. DarkRegion=ha.threshold(GrayImage,0, 80)
  26. ConnectedRegions=ha.connection (DarkRegion)
  27. displayRegion=ha.select_shape_std (ConnectedRegions,'max_area', 70)
  28. displayImage=ha.reduce_domain(GrayImage, displayRegion)
  29. Contours =ha.gen_contour_region_xld (displayRegion,'border')
  30. ContoursSplit=ha.segment_contours_xld (Contours, 'lines', 5, 4, 2)
  31. Number=ha.count_obj (ContoursSplit)
  32. for i in range(Number):
  33. print(i+1)
  34. ObjectCurrent=ha.select_obj(ContoursSplit, i+1)
  35. RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist=ha.fit_line_contour_xld(ObjectCurrent, 'tukey', -1, 0, 5, 2)
  36. XCoordCorners=ha.tuple_concat (XCoordCorners,RowBegin)
  37. YCoordCorners=ha.tuple_concat (YCoordCorners,ColBegin)
  38. pass
  39. XOff=100
  40. YOff=100*Height[0]/Width[0]
  41. imageHeight=Height[0]
  42. imageWidth=Width[0]
  43. HomMat2D=ha.hom_vector_to_proj_hom_mat2d (XCoordCorners, YCoordCorners, (1,1,1,1), [YOff,YOff,imageHeight-YOff,imageHeight-YOff], (XOff,imageWidth-XOff,imageWidth-XOff,XOff), (1,1,1,1), 'normalized_dlt')
  44. #方括号 圆形括号都可以 在python中并没有影响.
  45. Image_rectified=ha.projective_trans_image (Image_display, HomMat2D, 'bilinear', 'false', 'false')
  46. ha.disp_obj(Image_rectified, WindowHandle2);
  47. ha.write_image (Image_rectified, 'jpg', 0, "GG")
  48. cmd()

可以看到显示原图和透视变换后的图 左后输出"GG.jpg"

e3b6b16640cb4e3eb7b6749b7b318c2d.jpeg

 主要语法差异,不管有多少个参数python的输出总是在等号左边,输入在括号里面...而其他语言全部在括号里面....当然这是本人粗俗的认知...

结束.

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/420906
推荐阅读
相关标签
  

闽ICP备14008679号