博客
关于我
python调用halcon
阅读量:798 次
发布时间:2023-03-06

本文共 6060 字,大约阅读时间需要 20 分钟。

Halcon Installation and Usage Guide

Installation

To install Halcon, follow these steps:

pip install mvtec-halcon==20111

If Halcon is not installed in your default directory, ensure the following DLL files are added to your environment variables.


Basic Usage Examples

Example 1: Minimal Console Example

import halcon as ha
# Read image
img = ha.read_image('pcb')
# Thresholding
region = ha.threshold(img, 0, 122)
# Count connected regions
num_regions = ha.count_obj(ha.connection(region))
# Display result
print(f'Number of Regions: {num_regions}')

Example 2: Matching Example

import math
import os
import halcon as ha
class SysParamGuard:
def __init__(self, parameter_name, active, restore):
self.parameter_name = parameter_name
self.active = active
self.restore = restore
def __enter__(self):
ha.set_system(self.parameter_name, self.active)
def __exit__(self, exc_type, exc_value, traceback):
ha.set_system(self.parameter_name, self.restore)
class RectInfo:
def __init__(self, base_rect, row_offset, col_offset):
_, self.center_row, self.center_col = ha.area_center_s(base_rect)
self.length = 170.0
self.width = 5.0
self.angle = 0.0
self.row = self.center_row + row_offset
self.col = self.center_col + col_offset
self.base_row = self.row
self.base_col = self.col
def open_framegrabber():
return ha.open_framegrabber(
name='File',
horizontal_resolution=1,
vertical_resolution=1,
image_width=0,
image_height=0,
start_row=0,
start_column=0,
field='default',
bits_per_channel=-1,
color_space='default',
generic=-1,
external_trigger='default',
camera_type='board/board.seq',
device='default',
port=1,
line_in=-1
)
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=''
)
def create_shape_model(template_image):
return ha.create_shape_model(
template_image,
num_levels=4,
angle_start=0.0,
angle_extent=2 * math.pi,
angle_step=math.pi / 180.0,
optimization='auto',
metric='use_polarity',
contrast=30.0,
min_contrast=10.0
)
def gen_free_rectangle(rect_info):
return ha.gen_rectangle2(
row=rect_info.row,
column=rect_info.col,
phi=rect_info.angle,
length_1=rect_info.length,
length_2=rect_info.width
)
def measure_pairs(acq_img, measure_rect):
return ha.measure_pairs(
acq_img,
measure_rect,
sigma=2,
threshold=90,
transition='positive',
select='all'
)
def print_summary(match_res, num_leads, measure_time):
print(f'Match time: {match_time:.2f} [ms], Score: {match_score:.2f}, Measure time: {measure_time:.2f} [ms], Num Leads: {num_leads}')
sys.stdout.flush()
if __name__ == '__main__':
acq_handle = open_framegrabber()
acq_img = ha.grab_image(acq_handle)
img_width, img_height = ha.get_image_size_s(acq_img)
window = open_window(img_width, img_height)
ha.disp_obj(acq_img, window)
base_rect = ha.gen_rectangle1(
row_1=188,
column_1=182,
row_2=298,
column_2=412
)
rect_info1 = RectInfo(base_rect, row_offset=-102.0, col_offset=5.0)
rect_info2 = RectInfo(base_rect, row_offset=107.0, col_offset=5.0)
model = create_shape_model(acq_img)
ha.set_color(window, 'green')
ha.disp_obj(model, window)
for _ in range(100):
with SysParamGuard('flush_graphic', 'false', restore='true') as _:
acq_img = ha.grab_image(acq_handle)
ha.disp_obj(acq_img, window)
match_res = find_shape_model(acq_img, model)
if len(match_res[2]) == 0:
print('No matches found.')
break
disp_match_res(model_contours, match_res, rect_info1, rect_info2, window)
measure_res1, measure_res2, num_leads, measure_time = measure(
acq_img,
rect_info1,
rect_info2,
img_width,
img_height
)
disp_measure_res(rect_info1.width, measure_res1)
disp_measure_res(rect_info1.width, measure_res2)
print_summary(match_res, num_leads, measure_time)
ha.disp_line(window, row_1=-1, column_1=-1, row_2=-1, column_2=-1)
ha.wait_seconds(0.1)

Python Display Operations

# Example: Displaying Regions
threshold(img, Region, 128, 255)
connection(Region, ConnectedRegions)
select_shape(ConnectedRegions, SelectedRegions1, 'area', 'and', 29000, 99999)
union1(SelectedRegions1, RegionUnion)

Realization Example

import cv2
import halcon as ha
from halcon.numpy_interop import himage_as_numpy_array, himage_from_numpy_array
from PIL import Image
def halconProcess(cvImg, hdev, algofunc, inputPara, outPara):
image = himage_from_numpy_array(cvImg)
program = ha.HDevProgram(hdev)
proc = ha.HDevProcedure.load_local(program, algofunc)
proc_call = ha.HDevProcedureCall(proc)
proc_call.set_input_iconic_param_by_name(inputPara, image)
proc_call.execute()
result = proc_call.get_output_iconic_param_by_name(outPara)
xxx = himage_as_numpy_array(result)
img = Image.fromarray(cv2.cvtColor(xxx, cv2.COLOR_BGR2RGB))
return xxx
if __name__ == '__main__':
img = cv2.imread(r"C:\Users\Public\Documents\MVTec\HALCON-20.11-Steady\examples\images\printer_chip\printer_chip_01.png")
hdev = r'C:\Users\29939\Desktop\halconAlgo\test1.hdev'
algofunc = 'test'
inputPara = 'Image'
outPara = 'ImageResult1'
res = halconProcess(cvImg=img, hdev=hdev, algofunc=algofunc, inputPara=inputPara, outPara=outPara)
img= Image.fromarray(cv2.cvtColor(res,cv2.COLOR_BGR2RGB))
img.show()

转载地址:http://jwofk.baihongyu.com/

你可能感兴趣的文章
python + requests实现的接口自动化测试(超详细~)
查看>>
Python + selenium 如何截图?
查看>>
Python + Selenium 登录QQ邮箱
查看>>
Python + selenium如何做接口自动化测试?
查看>>
Python + selenium如何截图!
查看>>
Python + selenium自动化生成测试报告!
查看>>
Python - C 嵌入式分段错误
查看>>
Python - DM 一个用户 Discord 机器人
查看>>
python - Flask 基础 - 蓝图( Blueprint )(2)
查看>>
python - os.getenv 和 os.environ 看不到我的 bash shell 的环境变量
查看>>
Python - while循环
查看>>
Python - “if“中的逻辑评估顺序陈述
查看>>
Python - 使用 pandas 格式化 Excel 单元格
查看>>
python - 列表
查看>>
Python - 可用时从串行端口数据逐行读取到列表中
查看>>
Python - 在应用程序中显示 Web 浏览器/iframe
查看>>
Python - 如何使用管道执行shell命令,但没有‘shell = True‘?
查看>>
Python - 如何使这个不可腌制的对象可腌制?
查看>>
Python - 如何在 Windows 10 上完全卸载 Anaconda?
查看>>
python - 如何并行化python numpy中的总和计算?
查看>>