赞
踩
距离上次发出记录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版本的安装包,就会报错并且返回可用包的版本号列表,)
- C:\Users\Administrator>pip install mvtec-halcon==0
- ERROR: Could not find a version that satisfies the
- requirement mvtec-halcon==0 (from versions:
- 20110.0.0, 20110.0.1, 20111.0.0, 20111.0.1,
- 20112.0.0, 20113.0.0, 21050.0.0, 21110.0.0,
- 22050.0.0, 22110.0.0, 22111.0.0, 23050.0.0)
- ERROR: No matching distribution
- 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的工作空间:
就是这么一张图,"display.jpg"也丢进刚才的文件夹.
先看halcon 的.hdev..
- dev_close_window ()
- read_image (Image_display, 'display.jpg')
- rgb1_to_gray (Image_display, GrayImage)
- get_image_size(Image_display,imageWidth, imageHeight)
- dev_open_window (0, 0, imageWidth, imageHeight, 'black', WindowHandle1)
- dev_display (GrayImage)
- XCoordCorners := []
- YCoordCorners := []
- threshold(GrayImage,DarkRegion,0, 80)
- connection (DarkRegion, ConnectedRegions)
- select_shape_std (ConnectedRegions, displayRegion, 'max_area', 70)
- reduce_domain (GrayImage, displayRegion, displayImage)
- gen_contour_region_xld (displayRegion, Contours, 'border')
- segment_contours_xld (Contours, ContoursSplit, 'lines', 5, 4, 2)
- count_obj (ContoursSplit, Number)
- for index:=1 to Number by 1
- select_obj(ContoursSplit, ObjectCurrent, index)
- fit_line_contour_xld (ObjectCurrent, 'tukey', -1, 0, 5, 2, RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist)
- tuple_concat (XCoordCorners, RowBegin, XCoordCorners)
- tuple_concat (YCoordCorners, ColBegin, YCoordCorners)
- endfor
- XOff:= 100
- YOff:= 100*imageHeight/imageWidth
- 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)
- projective_trans_image (Image_display, Image_rectified, HomMat2D, 'bilinear', 'false', 'false')
- dev_display (Image_rectified)
注释就不用写了吧,代码具有自解释性.然后上python (其实几乎一样,这里主要是感受下语法差异).
- import os
- import halcon as ha
- def cmd(s="pause"):
- os.system(s)
- def open_window(width, height):
- if os.name == 'nt':
- ha.set_system('use_window_thread', 'true')
- return ha.open_window(
- row=0,
- column=0,
- width=width,
- height=height,
- father_window=0,
- mode='visible',
- machine=''
- )
- Image_display = ha.read_image('display.jpg')
- Width, Height = ha.get_image_size(Image_display)
- WindowHandle1 =open_window(Width[0], Height[0])
- WindowHandle2 =open_window(Width[0], Height[0])
- ha.disp_obj(Image_display, WindowHandle1);
- GrayImage = ha.rgb1_to_gray(Image_display)
- XCoordCorners = []
- YCoordCorners = []
- DarkRegion=ha.threshold(GrayImage,0, 80)
- ConnectedRegions=ha.connection (DarkRegion)
- displayRegion=ha.select_shape_std (ConnectedRegions,'max_area', 70)
- displayImage=ha.reduce_domain(GrayImage, displayRegion)
- Contours =ha.gen_contour_region_xld (displayRegion,'border')
- ContoursSplit=ha.segment_contours_xld (Contours, 'lines', 5, 4, 2)
- Number=ha.count_obj (ContoursSplit)
- for i in range(Number):
- print(i+1)
- ObjectCurrent=ha.select_obj(ContoursSplit, i+1)
- RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist=ha.fit_line_contour_xld(ObjectCurrent, 'tukey', -1, 0, 5, 2)
- XCoordCorners=ha.tuple_concat (XCoordCorners,RowBegin)
- YCoordCorners=ha.tuple_concat (YCoordCorners,ColBegin)
- pass
- XOff=100
- YOff=100*Height[0]/Width[0]
- imageHeight=Height[0]
- imageWidth=Width[0]
- 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')
- #方括号 圆形括号都可以 在python中并没有影响.
- Image_rectified=ha.projective_trans_image (Image_display, HomMat2D, 'bilinear', 'false', 'false')
- ha.disp_obj(Image_rectified, WindowHandle2);
- ha.write_image (Image_rectified, 'jpg', 0, "GG")
- cmd()
可以看到显示原图和透视变换后的图 左后输出"GG.jpg"
主要语法差异,不管有多少个参数python的输出总是在等号左边,输入在括号里面...而其他语言全部在括号里面....当然这是本人粗俗的认知...
结束.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。