利用python调用halcon接口(halcon12.0版本)【源码篇幅少,直接在文章中附源码】

文章目录

  • 前言
  • 一、环境配置以及代码实现
    • 1.环境配置
    • 2.python文件中引入必要库
    • 3.python+halcon代码
  • 二、本人测试接口的完整代码
  • 总结


前言

对于早期的halcon版本,没有python接口,但是对于一些python爱好者来说,都想实现用python实现机器视觉的调用,用python结合halcon做一些功能,本文章会教你如何配置环境,后面会出一篇文章教你能从python+pyqt+halcon实现基本的定位功能。


一、环境配置以及代码实现

1.环境配置

pip install pythonnet

2.python文件中引入必要库

import clr
from time import sleep
clr.AddReference("HalconDotNet")
from HalconDotNet import *

这里就用python实现与dotnet的结合,因此可以使用from HalconDotNet import *

3.python+halcon代码

WindowHandle = None
AcqHandle = None
Image = None
Width = Height = None

AcqHandleHandle = HOperatorSet.OpenFramegrabber(HTuple("DirectShow"),HTuple(1),HTuple(1),HTuple(0),HTuple(0),HTuple(0),HTuple(0),HTuple('default'),HTuple(8),HTuple("gray"),HTuple(-1),HTuple('false'),HTuple('default'),HTuple('[0] '),HTuple(0),HTuple(-1),AcqHandle)
HOperatorSet.GrabImageStart(AcqHandleHandle,HTuple(-1))
Image = HOperatorSet.GrabImageAsync(Image,AcqHandleHandle,HTuple(-1))
(N,Width,Height) = HOperatorSet.GetImageSize(Image,Width,Height)
WindowHandle = HOperatorSet.OpenWindow(HTuple(0),HTuple(0),HTuple(Width),HTuple(Height),HTuple(0),HTuple('visible'),HTuple(''),WindowHandle)
while True:
    Image = HOperatorSet.GrabImageAsync(Image, AcqHandleHandle, HTuple(-1))
    HOperatorSet.DispImage(Image, WindowHandle)
HOperatorSet.CloseFramegrabber(AcqHandleHandle)

注意这的字符串,都必须要用HTuple()来引用,对于一些halcon算子的传出参数,可以先定义变量为None,然后即可实现参数的获取

二、本人测试接口的完整代码

import clr
from time import sleep
clr.AddReference("HalconDotNet")
from HalconDotNet import *

WindowHandle = None
AcqHandle = None
Image = None
Width = Height = None

# Image = HOperatorSet.ReadImage(Image,HTuple('fabrik'))
# (N,Width,Height) = HOperatorSet.GetImageSize(Image,Width,Height)
# WindowHandle =  HOperatorSet.OpenWindow(HTuple(0),HTuple(0),HTuple(Width),HTuple(Height),HTuple(0),HTuple('visible'),HTuple(''),WindowHandle)
# HOperatorSet.DispImage(Image, WindowHandle)
# while True:
#     pass

AcqHandleHandle = HOperatorSet.OpenFramegrabber(HTuple("DirectShow"),HTuple(1),HTuple(1),HTuple(0),HTuple(0),HTuple(0),HTuple(0),HTuple('default'),HTuple(8),HTuple("gray"),HTuple(-1),HTuple('false'),HTuple('default'),HTuple('[0] '),HTuple(0),HTuple(-1),AcqHandle)
HOperatorSet.GrabImageStart(AcqHandleHandle,HTuple(-1))
Image = HOperatorSet.GrabImageAsync(Image,AcqHandleHandle,HTuple(-1))
(N,Width,Height) = HOperatorSet.GetImageSize(Image,Width,Height)
WindowHandle = HOperatorSet.OpenWindow(HTuple(0),HTuple(0),HTuple(Width),HTuple(Height),HTuple(0),HTuple('visible'),HTuple(''),WindowHandle)
while True:
    Image = HOperatorSet.GrabImageAsync(Image, AcqHandleHandle, HTuple(-1))
    HOperatorSet.DispImage(Image, WindowHandle)
HOperatorSet.CloseFramegrabber(AcqHandleHandle)

# * Image Acquisition 01: Code generated by Image Acquisition 01
# open_framegrabber ('DirectShow', 1, 1, 0, 0, 0, 0, 'default', 8, 'rgb', -1, 'false', 'default', '[0] Integrated Camera', 0, -1, AcqHandle)
# grab_image_start (AcqHandle, -1)
# grab_image_async (Image, AcqHandle, -1)
# get_image_size (Image, Width, Height)
# open_window(0,0,400,-1,'root','visible','',WindowHandle)
# dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
# while (true)
#     grab_image_async (Image, AcqHandle, -1)
#     dev_display(Image)
#     * Image Acquisition 01: Do something
# endwhile
# close_framegrabber (AcqHandle)

总结

虽然此方法不是很好,效率相比C++和C#的调用可能不会很高,但是对于python爱好者来说这个就是一个比较好的实现方法,halcon新版本的已经支持python接口,所以本教程是对于低版本的halcon,我这里测试用的是halcon12版本64位

你可能感兴趣的:(机器视觉-halcon,计算机视觉,python)