如何做一个C#仿Halcon Calibration插件
安装部分目标效果
安装部分实现效果
这两部分的核心为
HOperatorSet.GenCaltab(xNum, yNum, markDis, ratio, descrFilePath, psFilePath);
标定部分目标效果
标定部分实现效果
本部分主要有四个核心部分
1,导入图像
主要是创建文件夹浏览对话框
var dialog = new FolderBrowserDialog();
if (dialog.ShowDialog() == DialogResult.OK)
2,根据用户的选择,显示对应的图像
/// <summary>
/// ListView根据用户的选择,显示对应的图像
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
//进行判定,如果一项没有选中就不往下走
if (listView1.SelectedItems.Count == 0)
{
return;
}
//获取当前选择项下标
var selectIndex = listView1.SelectedItems[0].Index;
//更新显示图像
pictureBox1.Image = Image.FromFile(files[selectIndex].FullName);
}
3,设置位姿
/// <summary>
/// 设置参考位姿按键
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_SetPose_Click(object sender, EventArgs e)
{
//设置参考位姿的颜色
var NowColor = Color.DarkCyan;
try
{
//把所有的设置重置
for (int i = 0; i < listView1.Items.Count; i++)
{
if (listView1.Items[i].BackColor == NowColor)
{
listView1.Items[i].BackColor = origionColor;
}
}
//获取当前选择项下标 方式1
selectIndexPose = listView1.SelectedItems[0].Index;
//获取当前选择项下标 方式2
//var selectIndex2 = listView1.Items.IndexOf(listView1.FocusedItem);
//设置背景颜色
listView1.Items[selectIndexPose].BackColor = NowColor;
}
catch (Exception)
{
MessageBox.Show("设置参考位姿失败");
}
}
4,执行标定
//生成相机的参数
GenCamPar();
//创建标定对象
HOperatorSet.CreateCalibData("calibration_object", 1, 1, out var calibDataID);
//设置相机参数
HOperatorSet.SetCalibDataCamParam(calibDataID, 0, new HTuple(), startcamParm);
//设置标定板描述文件
HOperatorSet.SetCalibDataCalibObject(calibDataID, 0, tbx_SetPsPath.Text);
//图像映射到标定对象中
for (int i = 0; i < files.Length; i++)
{
HOperatorSet.ReadImage(out var himage, files[i].FullName);
HOperatorSet.FindCalibObject(himage, calibDataID, 0, 0, i, new HTuple(), new HTuple());
}
//执行标定
HOperatorSet.CalibrateCameras(calibDataID, out Enumber);
//获取相机标定参数
HOperatorSet.GetCalibData(calibDataID, "camera", 0, "params", out CameraParameters);
//获取参考位姿
HOperatorSet.GetCalibData(calibDataID, "calib_obj_pose", new HTuple(0, selectIndexPose), "pose", out var CameraPose1);
//添加标定板厚度
HOperatorSet.SetOriginPose(CameraPose1, 0, 0, Convert.ToDouble(up_Thickness.Text) * 0.001, out CameraPose);
MessageBox.Show("标定成功");
结果显示目标效果
结果显示实现效果
验证部分
HOperatorSet.ChangeRadialDistortionCamPar("adaptive", CameraParameters, 0, out var hv_CamParamNew);
HOperatorSet.ChangeRadialDistortionImage(HIMage, HIMage, out HObject hobj, CameraParameters, hv_CamParamNew);
位姿校正第10张图像
原图
位姿验证
HOperatorSet.SetOriginPose(CameraPose, DX, DY, DZ, out hv_PoseNewOrigin);
ho_MapSingle.Dispose();
HOperatorSet.GenImageToWorldPlaneMap(out ho_MapSingle, CameraParameters, hv_PoseNewOrigin,
768, 576, 900, 800, 0.0025, "bilinear");
ho_ImageMapped.Dispose();
HOperatorSet.MapImage(HIMage, ho_MapSingle, out HIMage);