当前位置:   article > 正文

C#Halcon3D点云读取显示分割处理联合编程_hoperatorset.zoomimagefactor

hoperatorset.zoomimagefactor

环境:VS2010,Halcon12x64,NETFramework 4.0

先上效果图

halcon3D程序导出C#代码。

VS21010制作winform界面

Form1_Load添加代码;并注意将项目的平台目标修改为x64。

  1. //将平台目标修改为“X64”或“ANY CPU”,否则将报“试图加载格式不正确的程序。 (异常来自 HRESULT:0x8007000B)”
  2. hwControl = new HWindowControl();
  3. hWindow = hwControl.HalconWindow;
  4. //以填充方式置于容器中
  5. hwControl.Dock = DockStyle.Fill;
  6. //添加控件到panel容器
  7. panel1.Controls.Add(hwControl);

“生成并显示3D点云”按钮控件添加代码

注意在visualize_object_model_3d代码中while死循环加入 System.Windows.Forms.Application.DoEvents()语句,否则主线程会出现卡死现象。

  1. // HOperatorSet.ScaleImage(ho_Image, out ho_Image, 0.001, .0);
  2. HOperatorSet.GenEmptyObj(out ho_Image);
  3. HOperatorSet.GenEmptyObj(out ho_X);
  4. HOperatorSet.GenEmptyObj(out ho_Y);
  5. HOperatorSet.GenEmptyObj(out ho_Z);
  6. //Load X,Y,Z-Data, scale them to meter and generate a 3D object model
  7. hv_ImagePath = "time_of_flight/";
  8. ho_Image.Dispose();
  9. HOperatorSet.ReadImage(out ho_Image, hv_ImagePath+"engine_cover_xyz_01");
  10. {
  11. HObject ExpTmpOutVar_0;
  12. HOperatorSet.ScaleImage(ho_Image, out ExpTmpOutVar_0, .001, .0);
  13. ho_Image.Dispose();
  14. ho_Image = ExpTmpOutVar_0;
  15. }
  16. {
  17. HObject ExpTmpOutVar_0;
  18. HOperatorSet.ZoomImageFactor(ho_Image, out ExpTmpOutVar_0, 2, 2, "constant");
  19. ho_Image.Dispose();
  20. ho_Image = ExpTmpOutVar_0;
  21. }
  22. ho_X.Dispose();ho_Y.Dispose();ho_Z.Dispose();
  23. HOperatorSet.Decompose3(ho_Image, out ho_X, out ho_Y, out ho_Z);
  24. HOperatorSet.XyzToObjectModel3d(ho_X, ho_Y, ho_Z, out hv_ObjectModel3DID);
  25. //Compute a mesh (Delauney triangulation) of the model
  26. HOperatorSet.PrepareObjectModel3d(hv_ObjectModel3DID, "segmentation", "true",
  27. new HTuple(), new HTuple());
  28. //
  29. //Prepare the visualization and display the 3D object model
  30. HOperatorSet.CreatePose(0.058, -0.165, 0.660, 345.0, 355.0, 356.0, "Rp+T",
  31. "gba", "point", out hv_Pose);
  32. //
  33. //Instructions for visualize_object_model_3d
  34. if (hv_Instructions == null)
  35. hv_Instructions = new HTuple();
  36. //hv_Instructions[0] = "Rotate: Left button";
  37. //if (hv_Instructions == null)
  38. // hv_Instructions = new HTuple();
  39. //hv_Instructions[1] = "Zoom: Shift + left button";
  40. //if (hv_Instructions == null)
  41. // hv_Instructions = new HTuple();
  42. //hv_Instructions[2] = "Move: Ctrl + left button";
  43. //Configuration
  44. hv_CamParam = new HTuple();
  45. hv_CamParam[0] = 0.01;
  46. hv_CamParam[1] = 0;
  47. hv_CamParam[2] = 7e-6;
  48. hv_CamParam[3] = 7e-6;
  49. hv_CamParam[4] = 352;
  50. hv_CamParam[5] = 288;
  51. hv_CamParam[6] = 710;
  52. hv_CamParam[7] = 576;
  53. hv_GenParamName = new HTuple();
  54. hv_GenParamName[0] = "color";
  55. hv_GenParamName[1] = "disp_pose";
  56. hv_GenParamName[2] = "alpha";
  57. hv_GenParamName[3] = "intensity";
  58. hv_GenParamValue = new HTuple();
  59. hv_GenParamValue[0] = "green";
  60. hv_GenParamValue[1] = "false";
  61. hv_GenParamValue[2] = 0.8;
  62. hv_GenParamValue[3] = "none";
  63. HOperatorSet.SetWindowAttr("background_color", "black");
  64. // HOperatorSet.OpenWindow(0, 0, 710, 576, 0, "", "", out hWindow);
  65. //把窗口对象推入窗口栈中
  66. HDevWindowStack.Push(hWindow);
  67. export.set_display_font(hWindow, 16, "mono", "true", "false");
  68. //显示3D模型
  69. //Task.Run需要额4.5框架以上才能使用
  70. // Task.Run(() =>
  71. // {
  72. // export.visualize_object_model_3d(hWindow, hv_ObjectModel3DID, hv_CamParam,
  73. //hv_Pose, hv_GenParamName, hv_GenParamValue, "This scene will be segmented into single objects",
  74. //new HTuple(), hv_Instructions, out hv_Pose);
  75. // });
  76. export.visualize_object_model_3d(hWindow, hv_ObjectModel3DID, hv_CamParam,
  77. hv_Pose, hv_GenParamName, hv_GenParamValue, "This scene will be segmented into single objects",
  78. new HTuple(), hv_Instructions, out hv_Pose);

“点云阈值分割并显示连通域直径与体积”添加代码

用StringBuilder()生成用于textbox控件显示的字符串;

  1. //Threshold the 3D object Model
  2. hv_MinValue = 0.500;
  3. hv_MaxValue = 0.670;
  4. HOperatorSet.SelectPointsObjectModel3d(hv_ObjectModel3DID, "point_coord_z",
  5. hv_MinValue, hv_MaxValue, out hv_ObjectModel3DIDReduced);
  6. export.visualize_object_model_3d(hWindow, hv_ObjectModel3DIDReduced, hv_CamParam,
  7. hv_Pose, hv_GenParamName, hv_GenParamValue, ("Result after thresholding at z=" + (((hv_MaxValue * 1e3)).TupleString(
  8. ".3"))) + "mm from the camera", new HTuple(), hv_Instructions, out hv_Pose);
  9. //
  10. //Calculate the connected components and the volume and diameter
  11. //of each of the resulting object
  12. if (hv_GenParamName == null)
  13. hv_GenParamName = new HTuple();
  14. hv_GenParamName[0] = "colored";
  15. if (hv_GenParamValue == null)
  16. hv_GenParamValue = new HTuple();
  17. hv_GenParamValue[0] = 12;
  18. HOperatorSet.ConnectionObjectModel3d(hv_ObjectModel3DIDReduced, "distance_3d",
  19. 0.010, out hv_ObjectModel3DIDConnections);
  20. HOperatorSet.VolumeObjectModel3dRelativeToPlane(hv_ObjectModel3DIDConnections,
  21. ((((new HTuple(0)).TupleConcat(0)).TupleConcat(hv_MaxValue))).TupleConcat(
  22. (((new HTuple(0)).TupleConcat(0)).TupleConcat(0)).TupleConcat(0)), "signed",
  23. "true", out hv_Volume);
  24. HOperatorSet.MaxDiameterObjectModel3d(hv_ObjectModel3DIDConnections, out hv_Diameter);
  25. //
  26. //Display the results
  27. //HOperatorSet.SetWindowAttr("background_color", "black");
  28. //HOperatorSet.OpenWindow(0, 720, 400, 576, 0, "", "", out hv_WindowHandle1);
  29. //HDevWindowStack.Push(hv_WindowHandle1);
  30. //export.set_display_font(hv_WindowHandle1, 14, "mono", "true", "false");
  31. hv_Indices = HTuple.TupleGenSequence(0, (new HTuple(hv_ObjectModel3DIDConnections.TupleLength()
  32. )) - 1, 1);
  33. //export.disp_message(hv_WindowHandle1, ((new HTuple("Features of the connected components:")).TupleConcat(
  34. // " ")).TupleConcat(" "), "window", 12, 12, "white", "false");
  35. hv_ResultMessage = " # Max. diameter Volume";
  36. hv_Sequence = HTuple.TupleGenSequence(0, (new HTuple(hv_ObjectModel3DIDConnections.TupleLength()
  37. )) - 1, 1);
  38. hv_ResultMessage = hv_ResultMessage.TupleConcat((((((hv_Sequence.TupleString(
  39. " 3")) + " ") + (((hv_Diameter * 1e3)).TupleString("7.1f"))) + " mm ") + (((hv_Volume * 1e3)).TupleString(
  40. "7.3f"))) + " dm3");
  41. //生成字符串并显示
  42. var strResult = new StringBuilder();
  43. for (int i = 0; i < hv_ResultMessage.Length; i++)
  44. {
  45. strResult.Append(hv_ResultMessage[i].S + "\r\n");
  46. }
  47. textBox1.Text = strResult.ToString();
  48. //export.disp_message(hv_WindowHandle1, hv_ResultMessage, "window", 50, 12, "white", "false");
  49. HDevWindowStack.SetActive(hWindow);
  50. export.visualize_object_model_3d(hWindow, hv_ObjectModel3DIDConnections, hv_CamParam,
  51. hv_Pose, hv_GenParamName, hv_GenParamValue, new HTuple(new HTuple("Found ") + (new HTuple(hv_ObjectModel3DIDConnections.TupleLength()
  52. ))) + " connected components", "#" + hv_Indices, hv_Instructions, out hv_Pose);

“筛选点云直径与体积”按钮添加代码

此部分为根据组件的体积和最大直径选择组件。

  1. //Select components by their volume and maximal diameter
  2. //根据组件的体积和最大直径选择组件
  3. hv_MinVolume = 0.35e-003;
  4. hv_MaxVolume = 1.0e-003;
  5. hv_MinDiameter = 185.0e-003;
  6. hv_MaxDiameter = 300.0e-003;
  7. //*** Attention:
  8. // select_object_model_3d uses the plane [0,0,0,0,0,0]
  9. // for the volume calculation! Therefore we have
  10. // to transform the 3d object models, so that the
  11. // reference plane (as defined in the former call to
  12. // volume_object_model_3d_relative_to_plane) coincides to the
  13. // default plane.
  14. HOperatorSet.HomMat3dIdentity(out hv_HomMat3DIdentity);
  15. HOperatorSet.HomMat3dTranslate(hv_HomMat3DIdentity, 0, 0, -hv_MaxValue, out hv_HomMat3DTranslation);
  16. HOperatorSet.HomMat3dInvert(hv_HomMat3DTranslation, out hv_HomMat3DInvert);
  17. HOperatorSet.AffineTransObjectModel3d(hv_ObjectModel3DIDConnections, hv_HomMat3DTranslation,
  18. out hv_ObjectModel3DTranslated);
  19. //Set a label for each part
  20. for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_ObjectModel3DIDConnections.TupleLength()
  21. )) - 1); hv_Index = (int)hv_Index + 1)
  22. {
  23. HOperatorSet.SetObjectModel3dAttribMod(hv_ObjectModel3DTranslated.TupleSelect(
  24. hv_Index), "&Index", new HTuple(), hv_Index);
  25. }
  26. //
  27. HOperatorSet.VolumeObjectModel3dRelativeToPlane(hv_ObjectModel3DTranslated,
  28. ((((((new HTuple(0)).TupleConcat(0)).TupleConcat(0)).TupleConcat(0)).TupleConcat(
  29. 0)).TupleConcat(0)).TupleConcat(0), "signed", "true", out hv_Volume1);
  30. HOperatorSet.SelectObjectModel3d(hv_ObjectModel3DTranslated, (new HTuple("volume")).TupleConcat(
  31. "diameter_object"), "and", hv_MinVolume.TupleConcat(hv_MinDiameter), hv_MaxVolume.TupleConcat(
  32. hv_MaxDiameter), out hv_ObjectModel3DSelected);
  33. hv_Title = new HTuple();
  34. hv_Title[0] = "Parts selected by using the following features: ";
  35. hv_Title = hv_Title.TupleConcat((((" " + (((hv_MinVolume * 1e3)).TupleString(
  36. "3.1f"))) + " dm3 <= volume <= ") + (((hv_MaxVolume * 1e3)).TupleString("3.1f"))) + " dm3");
  37. hv_Title = hv_Title.TupleConcat(((("and: " + (((hv_MinDiameter * 1e3)).TupleString(
  38. ".1f"))) + " mm <= max. diameter <= ") + (((hv_MaxDiameter * 1e3)).TupleString(
  39. ".1f"))) + " mm");
  40. Create the labels
  41. hv_Label = new HTuple();
  42. for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_ObjectModel3DSelected.TupleLength()
  43. )) - 1); hv_Index = (int)hv_Index + 1)
  44. {
  45. HOperatorSet.GetObjectModel3dParams(hv_ObjectModel3DSelected.TupleSelect(
  46. hv_Index), "&Index", out hv_FormerIndex);
  47. hv_Label = hv_Label.TupleConcat("#" + hv_FormerIndex);
  48. }
  49. //
  50. if ((int)(new HTuple((new HTuple(hv_ObjectModel3DSelected.TupleLength())).TupleGreater(
  51. 1))) != 0)
  52. {
  53. export.visualize_object_model_3d(hWindow, hv_ObjectModel3DSelected, hv_CamParam,
  54. new HTuple(), hv_GenParamName, hv_GenParamValue, hv_Title, hv_Label,
  55. hv_Instructions, out hv_Pose);
  56. }
  57. else
  58. {
  59. hv_Message = "No object left after using the following features: ";
  60. if (hv_Message == null)
  61. hv_Message = new HTuple();
  62. hv_Message[1] = (((" " + (((hv_MinVolume * 1e3)).TupleString("3.1f"))) + " dm3 <= volume <= ") + (((hv_MaxVolume * 1e3)).TupleString(
  63. "3.1f"))) + " dm3";
  64. if (hv_Message == null)
  65. hv_Message = new HTuple();
  66. hv_Message[2] = ((("and: " + (((hv_MinDiameter * 1e3)).TupleString(".1f"))) + " mm <= max. diameter <= ") + (((hv_MaxDiameter * 1e3)).TupleString(
  67. ".1f"))) + " mm";
  68. export.disp_message(hWindow, hv_Message, "window", 12, 12, "black", "true");
  69. }
  70. //
  71. //Clear the 3d object models
  72. //HDevWindowStack.SetActive(hv_WindowHandle1);
  73. //if (HDevWindowStack.IsOpen())
  74. //{
  75. // HOperatorSet.CloseWindow(HDevWindowStack.Pop());
  76. //}
  77. HOperatorSet.ClearObjectModel3d(((((((hv_ObjectModel3DID.TupleConcat(hv_ObjectModel3DIDReduced))).TupleConcat(
  78. hv_ObjectModel3DSelected))).TupleConcat(hv_ObjectModel3DTranslated))).TupleConcat(
  79. hv_ObjectModel3DIDConnections));

完整项目下载链接

https://download.csdn.net/download/ztzlzt/88571085

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/109042
推荐阅读