赞
踩
环境:VS2010,Halcon12x64,NETFramework 4.0
先上效果图
halcon3D程序导出C#代码。
VS21010制作winform界面。
Form1_Load添加代码;并注意将项目的平台目标修改为x64。
- //将平台目标修改为“X64”或“ANY CPU”,否则将报“试图加载格式不正确的程序。 (异常来自 HRESULT:0x8007000B)”
- hwControl = new HWindowControl();
-
- hWindow = hwControl.HalconWindow;
- //以填充方式置于容器中
- hwControl.Dock = DockStyle.Fill;
- //添加控件到panel容器
- panel1.Controls.Add(hwControl);
“生成并显示3D点云”按钮控件添加代码
注意在visualize_object_model_3d代码中while死循环加入 System.Windows.Forms.Application.DoEvents()语句,否则主线程会出现卡死现象。
- // HOperatorSet.ScaleImage(ho_Image, out ho_Image, 0.001, .0);
- HOperatorSet.GenEmptyObj(out ho_Image);
- HOperatorSet.GenEmptyObj(out ho_X);
- HOperatorSet.GenEmptyObj(out ho_Y);
- HOperatorSet.GenEmptyObj(out ho_Z);
-
-
- //Load X,Y,Z-Data, scale them to meter and generate a 3D object model
- hv_ImagePath = "time_of_flight/";
- ho_Image.Dispose();
- HOperatorSet.ReadImage(out ho_Image, hv_ImagePath+"engine_cover_xyz_01");
- {
- HObject ExpTmpOutVar_0;
- HOperatorSet.ScaleImage(ho_Image, out ExpTmpOutVar_0, .001, .0);
- ho_Image.Dispose();
- ho_Image = ExpTmpOutVar_0;
- }
- {
- HObject ExpTmpOutVar_0;
- HOperatorSet.ZoomImageFactor(ho_Image, out ExpTmpOutVar_0, 2, 2, "constant");
- ho_Image.Dispose();
- ho_Image = ExpTmpOutVar_0;
- }
- ho_X.Dispose();ho_Y.Dispose();ho_Z.Dispose();
- HOperatorSet.Decompose3(ho_Image, out ho_X, out ho_Y, out ho_Z);
- HOperatorSet.XyzToObjectModel3d(ho_X, ho_Y, ho_Z, out hv_ObjectModel3DID);
- //Compute a mesh (Delauney triangulation) of the model
- HOperatorSet.PrepareObjectModel3d(hv_ObjectModel3DID, "segmentation", "true",
- new HTuple(), new HTuple());
- //
- //Prepare the visualization and display the 3D object model
- HOperatorSet.CreatePose(0.058, -0.165, 0.660, 345.0, 355.0, 356.0, "Rp+T",
- "gba", "point", out hv_Pose);
- //
- //Instructions for visualize_object_model_3d
- if (hv_Instructions == null)
- hv_Instructions = new HTuple();
- //hv_Instructions[0] = "Rotate: Left button";
- //if (hv_Instructions == null)
- // hv_Instructions = new HTuple();
- //hv_Instructions[1] = "Zoom: Shift + left button";
- //if (hv_Instructions == null)
- // hv_Instructions = new HTuple();
- //hv_Instructions[2] = "Move: Ctrl + left button";
- //Configuration
- hv_CamParam = new HTuple();
- hv_CamParam[0] = 0.01;
- hv_CamParam[1] = 0;
- hv_CamParam[2] = 7e-6;
- hv_CamParam[3] = 7e-6;
- hv_CamParam[4] = 352;
- hv_CamParam[5] = 288;
- hv_CamParam[6] = 710;
- hv_CamParam[7] = 576;
- hv_GenParamName = new HTuple();
- hv_GenParamName[0] = "color";
- hv_GenParamName[1] = "disp_pose";
- hv_GenParamName[2] = "alpha";
- hv_GenParamName[3] = "intensity";
- hv_GenParamValue = new HTuple();
- hv_GenParamValue[0] = "green";
- hv_GenParamValue[1] = "false";
- hv_GenParamValue[2] = 0.8;
- hv_GenParamValue[3] = "none";
-
- HOperatorSet.SetWindowAttr("background_color", "black");
- // HOperatorSet.OpenWindow(0, 0, 710, 576, 0, "", "", out hWindow);
- //把窗口对象推入窗口栈中
- HDevWindowStack.Push(hWindow);
- export.set_display_font(hWindow, 16, "mono", "true", "false");
- //显示3D模型
- //Task.Run需要额4.5框架以上才能使用
- // Task.Run(() =>
- // {
- // export.visualize_object_model_3d(hWindow, hv_ObjectModel3DID, hv_CamParam,
- //hv_Pose, hv_GenParamName, hv_GenParamValue, "This scene will be segmented into single objects",
- //new HTuple(), hv_Instructions, out hv_Pose);
- // });
-
-
- export.visualize_object_model_3d(hWindow, hv_ObjectModel3DID, hv_CamParam,
- hv_Pose, hv_GenParamName, hv_GenParamValue, "This scene will be segmented into single objects",
- new HTuple(), hv_Instructions, out hv_Pose);
“点云阈值分割并显示连通域直径与体积”添加代码
用StringBuilder()生成用于textbox控件显示的字符串;
- //Threshold the 3D object Model
- hv_MinValue = 0.500;
- hv_MaxValue = 0.670;
- HOperatorSet.SelectPointsObjectModel3d(hv_ObjectModel3DID, "point_coord_z",
- hv_MinValue, hv_MaxValue, out hv_ObjectModel3DIDReduced);
- export.visualize_object_model_3d(hWindow, hv_ObjectModel3DIDReduced, hv_CamParam,
- hv_Pose, hv_GenParamName, hv_GenParamValue, ("Result after thresholding at z=" + (((hv_MaxValue * 1e3)).TupleString(
- ".3"))) + "mm from the camera", new HTuple(), hv_Instructions, out hv_Pose);
- //
- //Calculate the connected components and the volume and diameter
- //of each of the resulting object
- if (hv_GenParamName == null)
- hv_GenParamName = new HTuple();
- hv_GenParamName[0] = "colored";
- if (hv_GenParamValue == null)
- hv_GenParamValue = new HTuple();
- hv_GenParamValue[0] = 12;
- HOperatorSet.ConnectionObjectModel3d(hv_ObjectModel3DIDReduced, "distance_3d",
- 0.010, out hv_ObjectModel3DIDConnections);
- HOperatorSet.VolumeObjectModel3dRelativeToPlane(hv_ObjectModel3DIDConnections,
- ((((new HTuple(0)).TupleConcat(0)).TupleConcat(hv_MaxValue))).TupleConcat(
- (((new HTuple(0)).TupleConcat(0)).TupleConcat(0)).TupleConcat(0)), "signed",
- "true", out hv_Volume);
- HOperatorSet.MaxDiameterObjectModel3d(hv_ObjectModel3DIDConnections, out hv_Diameter);
- //
- //Display the results
- //HOperatorSet.SetWindowAttr("background_color", "black");
- //HOperatorSet.OpenWindow(0, 720, 400, 576, 0, "", "", out hv_WindowHandle1);
- //HDevWindowStack.Push(hv_WindowHandle1);
- //export.set_display_font(hv_WindowHandle1, 14, "mono", "true", "false");
- hv_Indices = HTuple.TupleGenSequence(0, (new HTuple(hv_ObjectModel3DIDConnections.TupleLength()
- )) - 1, 1);
- //export.disp_message(hv_WindowHandle1, ((new HTuple("Features of the connected components:")).TupleConcat(
- // " ")).TupleConcat(" "), "window", 12, 12, "white", "false");
- hv_ResultMessage = " # Max. diameter Volume";
- hv_Sequence = HTuple.TupleGenSequence(0, (new HTuple(hv_ObjectModel3DIDConnections.TupleLength()
- )) - 1, 1);
- hv_ResultMessage = hv_ResultMessage.TupleConcat((((((hv_Sequence.TupleString(
- " 3")) + " ") + (((hv_Diameter * 1e3)).TupleString("7.1f"))) + " mm ") + (((hv_Volume * 1e3)).TupleString(
- "7.3f"))) + " dm3");
- //生成字符串并显示
- var strResult = new StringBuilder();
- for (int i = 0; i < hv_ResultMessage.Length; i++)
- {
- strResult.Append(hv_ResultMessage[i].S + "\r\n");
-
- }
- textBox1.Text = strResult.ToString();
- //export.disp_message(hv_WindowHandle1, hv_ResultMessage, "window", 50, 12, "white", "false");
- HDevWindowStack.SetActive(hWindow);
- export.visualize_object_model_3d(hWindow, hv_ObjectModel3DIDConnections, hv_CamParam,
- hv_Pose, hv_GenParamName, hv_GenParamValue, new HTuple(new HTuple("Found ") + (new HTuple(hv_ObjectModel3DIDConnections.TupleLength()
- ))) + " connected components", "#" + hv_Indices, hv_Instructions, out hv_Pose);
“筛选点云直径与体积”按钮添加代码
此部分为根据组件的体积和最大直径选择组件。
- //Select components by their volume and maximal diameter
- //根据组件的体积和最大直径选择组件
- hv_MinVolume = 0.35e-003;
- hv_MaxVolume = 1.0e-003;
- hv_MinDiameter = 185.0e-003;
- hv_MaxDiameter = 300.0e-003;
- //*** Attention:
- // select_object_model_3d uses the plane [0,0,0,0,0,0]
- // for the volume calculation! Therefore we have
- // to transform the 3d object models, so that the
- // reference plane (as defined in the former call to
- // volume_object_model_3d_relative_to_plane) coincides to the
- // default plane.
- HOperatorSet.HomMat3dIdentity(out hv_HomMat3DIdentity);
- HOperatorSet.HomMat3dTranslate(hv_HomMat3DIdentity, 0, 0, -hv_MaxValue, out hv_HomMat3DTranslation);
- HOperatorSet.HomMat3dInvert(hv_HomMat3DTranslation, out hv_HomMat3DInvert);
- HOperatorSet.AffineTransObjectModel3d(hv_ObjectModel3DIDConnections, hv_HomMat3DTranslation,
- out hv_ObjectModel3DTranslated);
- //Set a label for each part
- for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_ObjectModel3DIDConnections.TupleLength()
- )) - 1); hv_Index = (int)hv_Index + 1)
- {
- HOperatorSet.SetObjectModel3dAttribMod(hv_ObjectModel3DTranslated.TupleSelect(
- hv_Index), "&Index", new HTuple(), hv_Index);
- }
- //
- HOperatorSet.VolumeObjectModel3dRelativeToPlane(hv_ObjectModel3DTranslated,
- ((((((new HTuple(0)).TupleConcat(0)).TupleConcat(0)).TupleConcat(0)).TupleConcat(
- 0)).TupleConcat(0)).TupleConcat(0), "signed", "true", out hv_Volume1);
- HOperatorSet.SelectObjectModel3d(hv_ObjectModel3DTranslated, (new HTuple("volume")).TupleConcat(
- "diameter_object"), "and", hv_MinVolume.TupleConcat(hv_MinDiameter), hv_MaxVolume.TupleConcat(
- hv_MaxDiameter), out hv_ObjectModel3DSelected);
-
- hv_Title = new HTuple();
- hv_Title[0] = "Parts selected by using the following features: ";
- hv_Title = hv_Title.TupleConcat((((" " + (((hv_MinVolume * 1e3)).TupleString(
- "3.1f"))) + " dm3 <= volume <= ") + (((hv_MaxVolume * 1e3)).TupleString("3.1f"))) + " dm3");
- hv_Title = hv_Title.TupleConcat(((("and: " + (((hv_MinDiameter * 1e3)).TupleString(
- ".1f"))) + " mm <= max. diameter <= ") + (((hv_MaxDiameter * 1e3)).TupleString(
- ".1f"))) + " mm");
- Create the labels
- hv_Label = new HTuple();
- for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_ObjectModel3DSelected.TupleLength()
- )) - 1); hv_Index = (int)hv_Index + 1)
- {
- HOperatorSet.GetObjectModel3dParams(hv_ObjectModel3DSelected.TupleSelect(
- hv_Index), "&Index", out hv_FormerIndex);
- hv_Label = hv_Label.TupleConcat("#" + hv_FormerIndex);
- }
- //
- if ((int)(new HTuple((new HTuple(hv_ObjectModel3DSelected.TupleLength())).TupleGreater(
- 1))) != 0)
- {
- export.visualize_object_model_3d(hWindow, hv_ObjectModel3DSelected, hv_CamParam,
- new HTuple(), hv_GenParamName, hv_GenParamValue, hv_Title, hv_Label,
- hv_Instructions, out hv_Pose);
- }
- else
- {
- hv_Message = "No object left after using the following features: ";
- if (hv_Message == null)
- hv_Message = new HTuple();
- hv_Message[1] = (((" " + (((hv_MinVolume * 1e3)).TupleString("3.1f"))) + " dm3 <= volume <= ") + (((hv_MaxVolume * 1e3)).TupleString(
- "3.1f"))) + " dm3";
- if (hv_Message == null)
- hv_Message = new HTuple();
- hv_Message[2] = ((("and: " + (((hv_MinDiameter * 1e3)).TupleString(".1f"))) + " mm <= max. diameter <= ") + (((hv_MaxDiameter * 1e3)).TupleString(
- ".1f"))) + " mm";
- export.disp_message(hWindow, hv_Message, "window", 12, 12, "black", "true");
- }
- //
- //Clear the 3d object models
- //HDevWindowStack.SetActive(hv_WindowHandle1);
- //if (HDevWindowStack.IsOpen())
- //{
- // HOperatorSet.CloseWindow(HDevWindowStack.Pop());
- //}
- HOperatorSet.ClearObjectModel3d(((((((hv_ObjectModel3DID.TupleConcat(hv_ObjectModel3DIDReduced))).TupleConcat(
- hv_ObjectModel3DSelected))).TupleConcat(hv_ObjectModel3DTranslated))).TupleConcat(
- hv_ObjectModel3DIDConnections));
完整项目下载链接
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。