本文共 6060 字,大约阅读时间需要 20 分钟。
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.
import halcon as ha# Read imageimg = ha.read_image('pcb')# Thresholdingregion = ha.threshold(img, 0, 122)# Count connected regionsnum_regions = ha.count_obj(ha.connection(region))# Display resultprint(f'Number of Regions: {num_regions}') import mathimport osimport halcon as haclass 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.coldef 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) # Example: Displaying Regionsthreshold(img, Region, 128, 255)connection(Region, ConnectedRegions)select_shape(ConnectedRegions, SelectedRegions1, 'area', 'and', 29000, 99999)union1(SelectedRegions1, RegionUnion)
import cv2import halcon as hafrom halcon.numpy_interop import himage_as_numpy_array, himage_from_numpy_arrayfrom PIL import Imagedef 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 xxxif __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/