首页 > 其他分享 >Halcon 相机外部参数标定例程一(camera_calibration_external.hdev)

Halcon 相机外部参数标定例程一(camera_calibration_external.hdev)

时间:2023-12-28 11:44:56浏览次数:22  
标签:hdev WCS set calibration 例程 image pose dev WindowHandle

1.create_calib_data — Create a HALCON calibration data model   创建 一个HALCON 校准数据模型

2.read_cam_par — Read internal camera parameters from a file   从文件中读取相机内部参数

 

 3.set_calib_data_cam_param — Set type and initial parameters of a camera in a calibration data model.   在校准数据模型中设置相机的类型和初始参数

4.set_calib_data_calib_object — Define a calibration object in a calibration model    在校准模型中定义校准对象

5.find_calib_object — Find the HALCON calibration plate and set the extracted points and contours in a calibration data model  找到 HALCON 校准板,并在校准数据模型中设置提取的点和等值线

6.get_calib_data_observ_contours — Get contour-based observation data from a calibration data model  从校准数据模型中获取基于轮廓线的观测数据

7.get_calib_data_observ_points — Get point-based observation data from a calibration data model  从校准数据模型中获取基于点的观测数据

8.disp_caltab — Project and visualize the 3D model of the calibration plate in the image     在图像中投影和可视化校准板的 3D 模型

 9.set_origin_pose — Translate the origin of a 3D pose   转换3D 位姿的原点

10.vector_to_pose — Compute an absolute pose out of point correspondences between world and image coordinates   计算世界坐标和图像坐标之间的点对应关系的绝对姿态

11.image_points_to_world_plane — Transform image points into the plane z=0 of a world coordinate system     将图像点转换为世界坐标系的平面 z=0

12.affine_trans_point_3d — Apply an arbitrary affine 3D transformation to points    对点应用任意仿射 3D 变换

13.project_3d_point — Project 3D points into (sub-)pixel image coordinates     将 3D 点映射到(子)像素图像坐标中

14.gen_region_polygon_filled — Store a polygon as a “filled” region   将多边形存储为“填充”区域

15.lines_gauss — Detect lines and their width    检测线条及其宽度

16.pose_to_hom_mat3d — Convert a 3D pose into a homogeneous transformation matrix   将 3D 姿势转换为齐次变换矩阵

17.hom_mat3d_compose — Multiply two homogeneous 3D transformation matrices   将两个齐次 3D 变换矩阵相乘

 18.hom_mat3d_translate_local — Add a translation to a homogeneous 3D transformation matrix     向3D 变换矩阵添加平移

 19.hom_mat3d_rotate_local — Add a rotation to a homogeneous 3D transformation matrix   向齐次3D 变换矩阵添加旋转

20.hom_mat3d_to_pose — Convert a homogeneous transformation matrix into a 3D pose   将齐次变换矩阵转换为 3D 姿态

21.contour_to_world_plane_xld — Transform an XLD contour into the plane z=0 of a world coordinate system    将 XLD 等值线转换为世界坐标系的平面 z=0

 

* Attention:
* This program reads the internal camera parameters from the file
* 'camera_parameters.dat', which, e.g., could be generated by the program
* 'camera_calibration_internal.hdev'
* 
ImgPath := '3d_machine_vision/calib/'
dev_close_window ()
dev_open_window (0, 0, 652, 494, 'black', WindowHandle)
dev_update_off ()
dev_set_draw ('margin')
dev_set_line_width (1)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
* Read the internal camera parameters from file
try
    read_cam_par ('camera_parameters.dat', CamParam)
catch (Exception)
    * run 'camera_calibration_internal.hdev' first to generate camera
    * parameter file 'camera_parameters.dat'
    stop ()
endtry
* 
* Determine the external camera parameters and world coodinates from image points
* 
* The external camera parameters can be determined from an image, where the
* calibration plate is positioned directly on the measurement plane
read_image (Image, ImgPath + 'calib_11')
dev_display (Image)
CaltabName := 'caltab_30mm.descr'
create_calib_data ('calibration_object', 1, 1, CalibDataID)
* Here, the final camera parameters are already known and can be used instead
* of the starting values used in the program 'camera_calibration_internal.hdev'
set_calib_data_cam_param (CalibDataID, 0, [], CamParam)
set_calib_data_calib_object (CalibDataID, 0, CaltabName)
find_calib_object (Image, CalibDataID, 0, 0, 1, [], [])
get_calib_data_observ_contours (Caltab, CalibDataID, 'caltab', 0, 0, 1)
get_calib_data_observ_points (CalibDataID, 0, 0, 1, RCoord, CCoord, Index, PoseForCalibrationPlate)
dev_set_color ('green')
dev_display (Caltab)
dev_set_color ('red')
disp_caltab (WindowHandle, CaltabName, CamParam, PoseForCalibrationPlate, 1)
dev_set_line_width (3)
disp_circle (WindowHandle, RCoord, CCoord, gen_tuple_const(|RCoord|,1.5))
* caltab_points (CaltabName, X, Y, Z)
* calibrate_cameras (CalibDataID, Error)
* To take the thickness of the calibration plate into account, the z-value
* of the origin given by the camera pose has to be translated by the
* thickness of the calibration plate.
* Deactivate the following line if you do not want to add the correction.
set_origin_pose (PoseForCalibrationPlate, 0, 0, 0.00075, PoseForCalibrationPlate)
clear_calib_data (CalibDataID)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* Alternatively, the external camera parameters can  be determined from
* at least three point correspondances between the WCS and the pixel coordinate system
read_image (Image, ImgPath + 'caliper_01')
dev_display (Image)
* Set the world coordinates of three points on the rule
X := [0,50,100,80]
Y := [5,0,5,0]
Z := [0,0,0,0]
* Set the respective image plane coordinates of the three points
RCoord := [414,227,85,128]
CCoord := [119,318,550,448]
* 
disp_cross (WindowHandle, RCoord, CCoord, 6, 0)
* create_pose (-50, 25, 400, 0, 0, -30, 'Rp+T', 'gba', 'point', InitialPose)
vector_to_pose (X, Y, Z, RCoord, CCoord, CamParam, 'iterative', 'error', FinalPose, Errors)
write_pose (FinalPose, 'pose_from_three_points.dat')
* Now, transform a point measured interactively into the WCS
dev_update_window ('on')
dev_display (Image)
while (1)
    disp_message (WindowHandle, 'Measure one point: left mouse button', 'window', 12, 12, 'red', 'false')
    disp_message (WindowHandle, 'Exit measure mode: right mouse button', 'window', 36, 12, 'red', 'false')
    get_mbutton (WindowHandle, Row, Column, Button)
    if (Button == 4)
        break
    endif
    dev_display (Image)
    dev_set_color ('green')
    disp_cross (WindowHandle, Row, Column, 6, 0)
    image_points_to_world_plane (CamParam, FinalPose, Row, Column, 1, X1, Y1)
    disp_message (WindowHandle, 'X = ' + X1, 'window', 320, 400, 'red', 'false')
    disp_message (WindowHandle, 'Y = ' + Y1, 'window', 340, 400, 'red', 'false')
endwhile
* Apply the measure tool and transform the resulting point coordinates
* into the WCS
dev_set_color ('red')
dev_display (Image)
* Set the world coordinates of four points defining a ROI for the measure tool
ROI_X_WCS := [-2,-2,112,112]
ROI_Y_WCS := [0,0.5,0.5,0]
ROI_Z_WCS := [0,0,0,0]
* Determine the transformation matrix from the WCS into the CCS
pose_to_hom_mat3d (FinalPose, CCS_HomMat_WCS)
* Transform the point coordintes into the image coordinate system
affine_trans_point_3d (CCS_HomMat_WCS, ROI_X_WCS, ROI_Y_WCS, ROI_Z_WCS, CCS_RectangleX, CCS_RectangleY, CCS_RectangleZ)
project_3d_point (CCS_RectangleX, CCS_RectangleY, CCS_RectangleZ, CamParam, RectangleRow, RectangleCol)
gen_region_polygon_filled (ROI, RectangleRow, RectangleCol)
smallest_rectangle2 (ROI, RowCenterROI, ColCenterROI, PhiROI, Length1ROI, Length2ROI)
* Create a measure
gen_measure_rectangle2 (RowCenterROI, ColCenterROI, PhiROI, Length1ROI, Length2ROI, 652, 494, 'bilinear', MeasureHandle)
measure_pairs (Image, MeasureHandle, 0.4, 5, 'all_strongest', 'all', RowEdgeFirst, ColumnEdgeFirst, AmplitudeFirst, RowEdgeSecond, ColumnEdgeSecond, AmplitudeSecond, IntraDistance, InterDistance)
close_measure (MeasureHandle)
dev_display (Image)
disp_message (WindowHandle, 'Measuring the position of the pitch lines', 'window', 450, 25, 'red', 'false')
dev_set_color ('green')
RowPitchLine := (RowEdgeFirst + RowEdgeSecond) / 2.0
ColPitchLine := (ColumnEdgeFirst + ColumnEdgeSecond) / 2.0
disp_cross (WindowHandle, RowPitchLine, ColPitchLine, 6, 0)
image_points_to_world_plane (CamParam, FinalPose, RowPitchLine, ColPitchLine, 1, X1, Y1)
for I := 1 to |X1| by 1
    set_tposition (WindowHandle, RowEdgeFirst[I - 1] + 5, ColumnEdgeFirst[I - 1] - 20)
    if (I == |X1|)
        set_tposition (WindowHandle, RowEdgeFirst[I - 1], ColumnEdgeFirst[I - 2])
    endif
    write_string (WindowHandle, X1[I - 1]$'.3f' + 'mm')
endfor
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
dev_display (Image)
* Apply a line extraction and transform the resulting XLD contours
* into the WCS
* Set the world coordinates of four points defining a ROI
ROI_X_WCS := [11,11,13,13]
ROI_Y_WCS := [4,6,6,4]
ROI_Z_WCS := [0,0,0,0]
* Transform the point coordinates into the image coordinate system
affine_trans_point_3d (CCS_HomMat_WCS, ROI_X_WCS, ROI_Y_WCS, ROI_Z_WCS, CCS_RectangleX, CCS_RectangleY, CCS_RectangleZ)
project_3d_point (CCS_RectangleX, CCS_RectangleY, CCS_RectangleZ, CamParam, RectangleRow, RectangleCol)
* Visualize the square in the original image
disp_polygon (WindowHandle, [RectangleRow,RectangleRow[0]], [RectangleCol,RectangleCol[0]])
dev_display (Image)
* create the ROI
gen_region_polygon_filled (ROI, RectangleRow, RectangleCol)
reduce_domain (Image, ROI, ImageReduced)
* Extract the lines
lines_gauss (ImageReduced, Lines, 1, 3, 8, 'dark', 'true', 'bar-shaped', 'true')
* Adapt the pose of the measurement plane to the tilted plane of the vernier
RelPose := [0,3.2,0,-14,0,0,0]
pose_to_hom_mat3d (FinalPose, HomMat3D)
pose_to_hom_mat3d (RelPose, HomMat3DRel)
hom_mat3d_compose (HomMat3D, HomMat3DRel, HomMat3DAdapted)
* Alternatively, the adaption can be done using the operators
* hom_mat3d_translate_local and hom_mat3d_rotate_local
* as shown in the following two lines
hom_mat3d_translate_local (HomMat3D, 0, 3.2, 0, HomMat3DTranslate)
hom_mat3d_rotate_local (HomMat3DTranslate, rad(-14), 'x', HomMat3DAdapted)
hom_mat3d_to_pose (HomMat3DAdapted, PoseAdapted)
* Transform the XLD contour to the WCS using the adapted pose
contour_to_world_plane_xld (Lines, ContoursTrans, CamParam, PoseAdapted, 1)
get_contour_xld (ContoursTrans, YOfContour, XOfContour)
tuple_mean (XOfContour, MeterReading)
dev_display (Lines)
disp_message (WindowHandle, 'Meter reading: ' + MeterReading$'.3f' + 'mm', 'window', 400, 180, 'green', 'false')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
dev_close_inspect_ctrl (YOfContour)
dev_close_inspect_ctrl (XOfContour)
* Now, transform the whole image
WidthMappedImage := 652
HeightMappedImage := 494
dev_display (Image)
* First, determine the scale for the mapping
* (here, the scale is determined such that in the
* surroundings of the points P0 and P1, the image scale of the
* mapped image is similar to the image scale of the original image)
distance_pp (X[0], Y[0], X[1], Y[1], DistP0P1WCS)
distance_pp (RCoord[0], CCoord[0], RCoord[1], CCoord[1], DistP0P1PCS)
Scale := DistP0P1WCS / DistP0P1PCS
* Then, determine the parameter settings for set_origin_pose such
* that the point given via get_mbutton will be in the center of the
* mapped image
dev_display (Image)
disp_message (WindowHandle, 'Define the center of the mapped image', 'window', 12, 12, 'red', 'false')
get_mbutton (WindowHandle, CenterRow, CenterColumn, Button1)
image_points_to_world_plane (CamParam, FinalPose, CenterRow, CenterColumn, 1, CenterX, CenterY)
set_origin_pose (FinalPose, CenterX - Scale * WidthMappedImage / 2.0, CenterY - Scale * HeightMappedImage / 2.0, 0, PoseNewOrigin)
gen_image_to_world_plane_map (Map, CamParam, PoseNewOrigin, 652, 494, WidthMappedImage, HeightMappedImage, Scale, 'bilinear')
map_image (Image, Map, ImageMapped)
dev_clear_window ()
dev_display (ImageMapped)
* In the case that only one image has to be mapped, the operator
* image_to_world_plane can be used instead of the operators
* gen_image_to_world_plane_map and map_image.
image_to_world_plane (Image, ImageMapped, CamParam, PoseNewOrigin, WidthMappedImage, HeightMappedImage, Scale, 'bilinear')
View Code

 

标签:hdev,WCS,set,calibration,例程,image,pose,dev,WindowHandle
From: https://www.cnblogs.com/echo-efun/p/17932281.html

相关文章

  • CH32V307 DHCP例程介绍
    1、DHCP概述DHCP,全称为DynamicHostConfigurationProtocol,动态主机配置协议,该协议允许服务器向客户端动态分配IP地址和配置信息,实现了自动设置IP地址、统一管理IP地址分配,简单理解为实现即插即用。 2、例程介绍main函数内容如下: /**********************************......
  • C++ /内存文件使用例程
    ///c++内存文件使用例程classMemoryBuffer:publicstd::strstreambuf{public:usingBase=std::basic_streambuf<char>;usingchar_type=typenameBase::char_type;MemoryBuffer(char*buf,size_tlen):std::strstreambuf(buf,l......
  • CH582,CH583,CH32V208等IC低功耗蓝牙系列2.4G例程RF_PHY/RF_PHY_Hop配对示例
    RF_PHY和RF_PHY_Hop两个例程均可以采用这种思路,甚至可以组合使用,比如RF_PHY用来配对,RF_PHY_Hop用来数据通讯,思路简介:该例程只要收发双方的地址,跳频频道等基础配置项一致即可进行无线通讯,因此可以衍生出其中一种较简单的配对思路,即:1、初始化代码中使用默认地址,例如0x71764129,这样......
  • Qt之modbus_slave例程的底层串口数据分析
    一.参考网址1. qCDebug输出到日志文件2. HowtoredirectqDebug,qWarning,qCriticaletcoutput?二.问题1. 如何查看slave底层收到的主机发送的消息和回复的消息?解决办法:在main.cpp代码中取消注释 注:QLoggingCategory::setFilterRules函数不能重复使用,其只能最后......
  • 嵌入式系统开发中常用的程序例程的简要介绍
    这些例程都是嵌入式系统开发中常用的程序,它们分别属于不同的领域和功能。以下是对这些例程的简要介绍:audio_codec:音频编解码器程序,用于处理音频数据的编码和解码。cherryusb:基于CherryPi的USB设备驱动程序,用于连接USB设备。coremark:一个用于测试处理器性能的基准测试程序......
  • 嵌入式系统开发中常用的程序这些例程的简要介绍和使用示例
    这些例程都是嵌入式系统开发中常用的程序。以下是对这些例程的简要介绍和使用示例:audio_codec:音频编解码器程序,用于处理音频数据的编码和解码。使用示例:#include"audio_codec.h"intmain(){AudioCodeccodec;uint8_tinput[]={0x01,0x02,0x03,0x04};uint8_......
  • Java报表详情介绍及示例程
    Java报表详情介绍及示例程序简介Java报表是一种用于展示数据并提供可视化分析的工具。它可以将数据转化为图表、表格、图像等形式,帮助用户更直观地理解数据的含义和趋势。Java报表通常用于企业管理、数据分析、业务决策等领域,具有易于使用、灵活性高、功能强大等特点。常用的Java报......
  • CH573 CH582 CH592 CH579 看门狗WDOG例程
    看门狗功能是基于一个8位的递增计数器,计数时钟周期为131072/Fsys。当开启了看门狗超时复位功能后,一旦此计数器溢出会复位整个系统。CH579例程中主频都是32M,看门狗超时复位最长时间为131072/32000000*255=1.04448s。CH573CH582CH592例程中主频都是60M,看门狗超时复位最长时间为......
  • Halcon、HDevelop快速入门
    ​HDevelop基础一HDevelop概述    HDevelop是一款机器视觉的集成开发环境。下面将对HDevelop的界面内容做一下简单的介绍。界面介绍打开HDevelop,将看到以下画面。     如上图所示,打开HDevelop后可以看到4个窗口:图形窗口,算子窗口,变量窗口,程序窗口。其中......
  • Qt3D改变观察视角例程(二)
    本例依旧是改变3D视角。不同的是这个是视野位置不变而只改变观察方向。相当于一个人站在原地不动,旋转脑袋看周围的东西。测试的条件是VS2017和Qt5.9。主要的知识点就是欧拉角的计算。下面是效果图:头文件:classQOpenGLTexture;classQOpenGLBuffer;classMvOpenGLWidget:p......