Halcon机器视觉校准报错9001:修复方法及RGB图像转换
问题与解决方案
问题描述
使用Halcon开发机器视觉校准程序,通过图像采集助手连接Zivid外部相机并插入生成的采集代码后,调用find_calib_object时触发报错:
Image to process has wrong gray value type (HALCON error code: 9001)
同时需要实现将采集到的图像转为RGB格式的需求。
用户原始代码片段:
write_string(WindowHandle, 'Generating a position and sending it to the robot...') * Using procedure to generate the position and send the robot PositionGeneration (Socket, WindowHandle, TupleWithPositions) * Image Acquisition 01: Code generated by Image Acquisition 01 open_framegrabber ('GenICamTL', 0, 0, 0, 0, 0, 0, 'default', -1, 'default', -1, 'false', 'default', 'Zivid_DEV_21513D73', 0, -1, AcqHandle) grab_image_start (AcqHandle, -1) grab_image_async (Image, AcqHandle, -1) * Image Acquisition 01: Do something close_framegrabber (AcqHandle) dev_display (Image) * Saving the picture write_image (Image, 'png none', 0, 'Image'+I) find_calib_object (Image, CalibDataID, 0, 0, I, 'alpha', 0.2) get_calib_data_observ_contours (Caltab, CalibDataID, 'caltab', 0, 0, I) get_calib_data_observ_points (CalibDataID, 0, 0, I, Row, Column, Index, Pose) dev_set_color ('green') dev_display(Image) dev_display (Caltab) tuple_number (TupleWithPositions, PoseCoordinates) X := PoseCoordinates[0] Y := PoseCoordinates[1] Z := PoseCoordinates[2] A := PoseCoordinates[3] B := PoseCoordinates[4] C := PoseCoordinates[5] create_pose (X, Y, Z, A, B, C, 'Rp+T', 'abg', 'point', Pose) set_calib_data (CalibDataID, 'tool', I, 'tool_in_base_pose', Pose)
用户已尝试修改相机参数,但未解决问题。
解决方案
1. 修复错误代码9001
错误核心原因:find_calib_object仅支持单通道灰度图像作为输入,而Zivid相机采集的是3通道彩色图像,灰度值类型不匹配导致报错。
解决步骤:
- 在调用
find_calib_object前,使用rgb1_to_gray算子将采集到的彩色图像转换为灰度图像; - 后续校准相关操作均使用转换后的灰度图像。
2. 将采集图像转为RGB格式
Zivid相机通过GenICam采集的图像默认多为BGR格式,需转换为标准RGB:
- 使用
trans_from_rgb算子,指定从'bgr'转换为'rgb'格式; - 若不确定图像格式,可先调用
get_image_type查看图像类型,再选择对应转换方式。
修改后的完整代码示例
write_string(WindowHandle, 'Generating a position and sending it to the robot...') * Using procedure to generate the position and send the robot PositionGeneration (Socket, WindowHandle, TupleWithPositions) * Image Acquisition 01: Code generated by Image Acquisition 01 open_framegrabber ('GenICamTL', 0, 0, 0, 0, 0, 0, 'default', -1, 'default', -1, 'false', 'default', 'Zivid_DEV_21513D73', 0, -1, AcqHandle) grab_image_start (AcqHandle, -1) grab_image_async (Image, AcqHandle, -1) * Image Acquisition 01: Do something close_framegrabber (AcqHandle) * 将采集的BGR图像转换为标准RGB格式 trans_from_rgb(Image, ImageRGB, 'bgr', 'rgb') * 将RGB图像转换为灰度图像,用于校准算子 rgb1_to_gray(ImageRGB, ImageGray) dev_display (ImageRGB) * 保存RGB格式的图像 write_image (ImageRGB, 'png none', 0, 'Image'+I) * 使用灰度图像调用校准算子,解决类型不匹配问题 find_calib_object (ImageGray, CalibDataID, 0, 0, I, 'alpha', 0.2) get_calib_data_observ_contours (Caltab, CalibDataID, 'caltab', 0, 0, I) get_calib_data_observ_points (CalibDataID, 0, 0, I, Row, Column, Index, Pose) dev_set_color ('green') dev_display(ImageRGB) dev_display (Caltab) tuple_number (TupleWithPositions, PoseCoordinates) X := PoseCoordinates[0] Y := PoseCoordinates[1] Z := PoseCoordinates[2] A := PoseCoordinates[3] B := PoseCoordinates[4] C := PoseCoordinates[5] create_pose (X, Y, Z, A, B, C, 'Rp+T', 'abg', 'point', Pose) set_calib_data (CalibDataID, 'tool', I, 'tool_in_base_pose', Pose)
内容的提问来源于stack exchange,提问作者Viktor Petrov
相关产品推荐
相关产品推荐

