Files
2024-10-24 14:41:35 +08:00

3678 lines
167 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <952Halcon3dSubAlgo.h>
#if 0
void Cal_Glue_Height(HObject ho_in_ImageH, HTuple hv_in_xResolution, HTuple hv_in_yResolution,
HTuple hv_in_zResolution, HTuple* hv_out_Height1, HTuple* hv_out_Height2, HTuple* hv_out_Point,
HTuple* hv_out_Flag, HTuple* hv_out_Ngtails)
{
// Local iconic variables
HObject ho_ImageConverted, ho_YImageSurface;
HObject ho_XImageSurface, ho_in_XImageScaled, ho_in_YImageScaled;
HObject ho_in_ZImageScaled, ho_Regions, ho_Domain, ho_ProductRegion;
HObject ho_ImageReduced, ho_RegionClosing, ho_RegionFillUp3;
HObject ho_TempRegion, ho_RegionErosion3, ho_ZeroRegion;
HObject ho_ImageReduced1, ho_ImageReduced2, ho_ImageReduced3;
HObject ho_in_ZeroRegion1, ho_out_GlueRegion, ho_in_ZeroRegion2;
HObject ho_in_EdgeRegion, ho_out_EdgeRegion, ho_InspectionRegion;
HObject ho_RegionErosion1, ho_RegionDilation2, ho_in_InspectionRegion3;
HObject ho_in_InspectionRegion4, ho_in_ProductRegion, ho_in_outerEdge;
HObject ho_in_innerEdge, ho_in_HYRegion;
// Local control variables
HTuple hv_Debug, hv_Type, hv_Width2, hv_Height2;
HTuple hv_ObjectModel3D, hv_ParFitting, hv_ValFitting;
HTuple hv_ObjectModelPlane, hv_PlaneParamValue, hv_Thread;
HTuple hv_in_Direc1, hv_in_Height1, hv_in_PlaneParamValue1;
HTuple hv_Thread1, hv_out_Flag1, hv_out_Ngtails1, hv_in_Direc2;
HTuple hv_in_Height2, hv_in_PlaneParamValue2, hv_Thread2;
HTuple hv_out_Flag2, hv_out_Ngtails2, hv_Area2, hv_Row2;
HTuple hv_Column2, hv_Area3, hv_Row3, hv_Column3, hv_Area;
HTuple hv_Row, hv_Column, hv_Area1, hv_Row1, hv_Column1;
HTuple hv_in_PlaneParamValue3, hv_Thread3, hv_out_PaintRows1;
HTuple hv_out_PaintColumns1, hv_out_Height3, hv_out_Flag3;
HTuple hv_in_PlaneParamValue4, hv_Thread4, hv_out_PaintRows2;
HTuple hv_out_PaintColumns2, hv_out_Height4, hv_out_Flag4;
HTuple hv_Thread5, hv_out_Height, hv_out_Flag5, hv_Exception;
// +++ Threading variables
HDevThread* hcppthread_handle;
HDevThreadContext hcppthread_context; // <-signals begin of procedure
//**********************************************************************
//Function Name: Cal_Glue_Height
//Author:
//Description: 基于基准面,计算点胶的高度(mm)
//Input: in_ImageH, 深度图像(tif),
// in_xResolution, X向精度(mm/pix)
// in_yResolution, y向精度(mm/pix)
// in_zResolution, z向精度(mm/pix)
//output: out_Height, 点胶高度(mm), 一般情况下,胶的高度高于电池边缘面的区域, 才有高度输出.
// out_Point: 胶的高点和基准面的高点坐标(y,x)
// out_Flag, 算法执行的标志位; 1, 算法执行成功 2, 算法执行错误
// out_Ngtails, 算法执行信息,
//verion change log: 胡汉兵 2024-06-15 V1.1 Create new function.
// 胡汉兵 2024-06-20 V1.2 软件端执行结果异常, 算法直接强行赋值为0.
// 胡汉兵 2024-07-04 V1.3 现场输出电池的最高点位置,便于客户基于当前点位进行测量.
// 胡汉兵 2024-07-09 V1.4 使用原始图像进行高度计算,
// 胡汉兵 2024-07-16 V1.5 保护, 防止程序崩溃时, 无结果图输出.
// 胡汉兵 2024-09-11 V1.6 代码二次优化,
//**********************************************************************
hv_Debug = 0;
(*hv_out_Height1) = -99;
(*hv_out_Height2) = -99;
(*hv_out_Flag) = 2;
(*hv_out_Ngtails) = "OK";
(*hv_out_Point) = HTuple();
try
{
if (HDevWindowStack::IsOpen())
SetDraw(HDevWindowStack::GetActive(), "margin");
GetImageType(ho_in_ImageH, &hv_Type);
if (0 != (hv_Type != HTuple("real")))
{
ConvertImageType(ho_in_ImageH, &ho_ImageConverted, "real");
}
else
{
ho_ImageConverted = ho_in_ImageH;
}
GetImageSize(ho_ImageConverted, &hv_Width2, &hv_Height2);
GenImageSurfaceFirstOrder(&ho_YImageSurface, "real", 1, 0, 0, 0, 0, hv_Width2,
hv_Height2);
GenImageSurfaceFirstOrder(&ho_XImageSurface, "real", 0, 1, 0, 0, 0, hv_Width2,
hv_Height2);
ScaleImage(ho_XImageSurface, &ho_in_XImageScaled, hv_in_xResolution, 0);
ScaleImage(ho_YImageSurface, &ho_in_YImageScaled, hv_in_xResolution, 0);
ScaleImage(ho_ImageConverted, &ho_in_ZImageScaled, hv_in_zResolution, 0);
Threshold(ho_ImageConverted, &ho_Regions, 0, 0);
GetDomain(ho_in_ImageH, &ho_Domain);
Difference(ho_Domain, ho_Regions, &ho_ProductRegion);
ReduceDomain(ho_in_ZImageScaled, ho_ProductRegion, &ho_ImageReduced);
if (0 != hv_Debug)
{
//reduce_domain (in_XImageScaled, ProductRegion, ImageReduced4)
//reduce_domain (in_YImageScaled, ProductRegion, ImageReduced5)
//xyz_to_object_model_3d (ImageReduced4, ImageReduced5, ImageReduced, ObjectModel3D)
//dev_get_window (WindowHandle)
//visualize_object_model_3d (WindowHandle, [ObjectModel3D], [], [], ['lut','intensity','disp_pose'], ['hsi','coord_z', 'true'], ['展示拟合的平面'], '', '', Pose)
}
//******************************************************
//step2: 获取基准面区域(新代码不使用这个区域的Region)
ClosingCircle(ho_ProductRegion, &ho_RegionClosing, 15);
FillUp(ho_RegionClosing, &ho_RegionFillUp3);
ErosionRectangle1(ho_RegionFillUp3, &ho_TempRegion, 1, 220);
//dev_display (ImageReduced)
//dev_display (TempRegion)
//区间进行内缩, 获取基准的端面.
ErosionCircle(ho_TempRegion, &ho_RegionErosion3, 80);
Difference(ho_TempRegion, ho_RegionErosion3, &ho_ZeroRegion);
//dev_display (ImageReduced)
//dev_display (ZeroRegion)
//******************************************************
//共用的
//优化, 调整基准面
ReduceDomain(ho_in_XImageScaled, ho_ZeroRegion, &ho_ImageReduced1);
ReduceDomain(ho_in_YImageScaled, ho_ZeroRegion, &ho_ImageReduced2);
ReduceDomain(ho_in_ZImageScaled, ho_ZeroRegion, &ho_ImageReduced3);
XyzToObjectModel3d(ho_ImageReduced1, ho_ImageReduced2, ho_ImageReduced3, &hv_ObjectModel3D);
//拟合平面,
hv_ParFitting.Clear();
hv_ParFitting[0] = "primitive_type";
hv_ParFitting[1] = "fitting_algorithm";
hv_ParFitting[2] = "output_xyz_mapping";
hv_ValFitting.Clear();
hv_ValFitting[0] = "plane";
hv_ValFitting[1] = "least_squares_huber";
hv_ValFitting[2] = "false";
//利用产品的边缘拟合平面,
FitPrimitivesObjectModel3d(hv_ObjectModel3D, hv_ParFitting, hv_ValFitting, &hv_ObjectModelPlane);
GetObjectModel3dParams(hv_ObjectModelPlane, "primitive_parameter", &hv_PlaneParamValue);
if (0 != 0)
{
hv_Thread = HTuple();
//线程一,
hv_in_Direc1 = "Neg";
hv_in_Height1 = 1.0;
hv_in_PlaneParamValue1 = hv_PlaneParamValue;
ho_in_ZeroRegion1 = ho_ZeroRegion;
// Create a thread instance
hcppthread_handle = new HDevThread(hcppthread_context,
(void*)HDevExportCpp::_hcppthread_Get_OutGlueRegion, 7, 3);
// Set thread procedure call arguments
hcppthread_handle->SetInputIconicParamObject(0, ho_in_XImageScaled);
hcppthread_handle->SetInputIconicParamObject(1, ho_in_YImageScaled);
hcppthread_handle->SetInputIconicParamObject(2, ho_in_ZImageScaled);
hcppthread_handle->SetInputIconicParamObject(3, ho_in_ZeroRegion1);
hcppthread_handle->SetInputCtrlParamTuple(4, hv_in_Height1);
hcppthread_handle->SetInputCtrlParamTuple(5, hv_in_Direc1);
hcppthread_handle->SetInputCtrlParamTuple(6, hv_in_PlaneParamValue1);
hcppthread_handle->BindOutputIconicParamObject(0, 0, &ho_out_GlueRegion);
hcppthread_handle->BindOutputCtrlParamTuple(1, 0, &hv_out_Flag1);
hcppthread_handle->BindOutputCtrlParamTuple(2, 0, &hv_out_Ngtails1);
// Start proc line in thread
hcppthread_handle->ParStart(&hv_Thread1);
hv_Thread = hv_Thread.TupleConcat(hv_Thread1);
//线程二,
hv_in_Direc2 = "all";
hv_in_Height2 = 0.15;
hv_in_PlaneParamValue2 = hv_PlaneParamValue;
ho_in_ZeroRegion2 = ho_ZeroRegion;
// Create a thread instance
hcppthread_handle = new HDevThread(hcppthread_context,
(void*)HDevExportCpp::_hcppthread_Get_InGlueRegion, 7, 3);
// Set thread procedure call arguments
hcppthread_handle->SetInputIconicParamObject(0, ho_in_XImageScaled);
hcppthread_handle->SetInputIconicParamObject(1, ho_in_YImageScaled);
hcppthread_handle->SetInputIconicParamObject(2, ho_in_ZImageScaled);
hcppthread_handle->SetInputIconicParamObject(3, ho_in_ZeroRegion2);
hcppthread_handle->SetInputCtrlParamTuple(4, hv_in_Height2);
hcppthread_handle->SetInputCtrlParamTuple(5, hv_in_PlaneParamValue2);
hcppthread_handle->SetInputCtrlParamTuple(6, hv_in_Direc2);
hcppthread_handle->BindOutputIconicParamObject(0, 0, &ho_in_EdgeRegion);
hcppthread_handle->BindOutputCtrlParamTuple(1, 0, &hv_out_Flag2);
hcppthread_handle->BindOutputCtrlParamTuple(2, 0, &hv_out_Ngtails2);
// Start proc line in thread
hcppthread_handle->ParStart(&hv_Thread2);
hv_Thread = hv_Thread.TupleConcat(hv_Thread2);
HDevThread::ParJoin(hv_Thread);
}
if (0 != 1)
{
//线程一,
hv_in_Direc1 = "Neg";
hv_in_Height1 = 1.0;
hv_in_PlaneParamValue1 = hv_PlaneParamValue;
ho_in_ZeroRegion1 = ho_ZeroRegion;
Get_OutGlueRegion(ho_in_XImageScaled, ho_in_YImageScaled, ho_in_ZImageScaled,
ho_in_ZeroRegion1, &ho_out_GlueRegion, hv_in_Height1, hv_in_Direc1, hv_in_PlaneParamValue1,
&hv_out_Flag1, &hv_out_Ngtails1);
//线程二,
hv_in_Direc2 = "all";
hv_in_Height2 = 0.15;
hv_in_PlaneParamValue2 = hv_PlaneParamValue;
ho_in_ZeroRegion2 = ho_ZeroRegion;
Get_InGlueRegion(ho_in_XImageScaled, ho_in_YImageScaled, ho_in_ZImageScaled,
ho_in_ZeroRegion2, &ho_in_EdgeRegion, hv_in_Height2, hv_in_PlaneParamValue2,
hv_in_Direc2, &hv_out_Flag2, &hv_out_Ngtails2);
}
if (0 != (HTuple(hv_out_Flag1 == 1).TupleAnd(hv_out_Flag2 == 1)))
{
//dev_display (ImageReduced)
//dev_display (out_GlueRegion)
//dev_display (in_EdgeRegion)
ho_out_EdgeRegion = ho_out_GlueRegion;
}
else
{
(*hv_out_Ngtails) = "点胶区域提取失败";
return;
}
AreaCenter(ho_in_EdgeRegion, &hv_Area2, &hv_Row2, &hv_Column2);
if (0 != (hv_Area2 < 10000))
{
ho_InspectionRegion = ho_out_GlueRegion;
AreaCenter(ho_out_GlueRegion, &hv_Area3, &hv_Row3, &hv_Column3);
GenCircle(&ho_in_EdgeRegion, hv_Row3, hv_Column3, 10);
}
else
{
//目前按照这个比例进行调整, 计算区域的高度.
ErosionRectangle1(ho_in_EdgeRegion, &ho_RegionErosion1, 345, 40);
//dev_display (ImageReduced)
//dev_display (RegionErosion1)
//dev_display (in_EdgeRegion)
AreaCenter(ho_RegionErosion1, &hv_Area, &hv_Row, &hv_Column);
if (0 != (hv_Area == 0))
{
AreaCenter(ho_in_EdgeRegion, &hv_Area1, &hv_Row1, &hv_Column1);
GenCircle(&ho_RegionErosion1, hv_Row1, hv_Column1, 3);
}
DilationRectangle1(ho_out_GlueRegion, &ho_RegionDilation2, 160, 20);
//dev_display (ImageReduced)
//dev_display (RegionDilation2)
Difference(ho_RegionDilation2, ho_RegionErosion1, &ho_InspectionRegion);
}
//******************************************************
if (HDevWindowStack::IsOpen())
DispObj(ho_ImageReduced, HDevWindowStack::GetActive());
if (HDevWindowStack::IsOpen())
DispObj(ho_InspectionRegion, HDevWindowStack::GetActive());
//__________________________________________________________________________
if (0 != 0)
{
hv_Thread = HTuple();
//线程三
ho_in_InspectionRegion3 = ho_InspectionRegion;
hv_in_PlaneParamValue3 = hv_PlaneParamValue;
// Create a thread instance
hcppthread_handle = new HDevThread(hcppthread_context,
(void*)HDevExportCpp::_hcppthread_Cal_Height1, 5, 4);
// Set thread procedure call arguments
hcppthread_handle->SetInputIconicParamObject(0, ho_in_XImageScaled);
hcppthread_handle->SetInputIconicParamObject(1, ho_in_YImageScaled);
hcppthread_handle->SetInputIconicParamObject(2, ho_in_ZImageScaled);
hcppthread_handle->SetInputIconicParamObject(3, ho_in_InspectionRegion3);
hcppthread_handle->SetInputCtrlParamTuple(4, hv_in_PlaneParamValue3);
hcppthread_handle->BindOutputCtrlParamTuple(0, 0, &hv_out_PaintRows1);
hcppthread_handle->BindOutputCtrlParamTuple(1, 0, &hv_out_PaintColumns1);
hcppthread_handle->BindOutputCtrlParamTuple(2, 0, &hv_out_Height3);
hcppthread_handle->BindOutputCtrlParamTuple(3, 0, &hv_out_Flag3);
// Start proc line in thread
hcppthread_handle->ParStart(&hv_Thread3);
hv_Thread = hv_Thread.TupleConcat(hv_Thread3);
//线程四,
//in_InspectionRegion4 := out_ROI1
ho_in_InspectionRegion4 = ho_ProductRegion;
hv_in_PlaneParamValue4 = hv_PlaneParamValue;
ho_in_ProductRegion = ho_ProductRegion;
// Create a thread instance
hcppthread_handle = new HDevThread(hcppthread_context,
(void*)HDevExportCpp::_hcppthread_Cal_Height2, 6, 4);
// Set thread procedure call arguments
hcppthread_handle->SetInputIconicParamObject(0, ho_in_XImageScaled);
hcppthread_handle->SetInputIconicParamObject(1, ho_in_YImageScaled);
hcppthread_handle->SetInputIconicParamObject(2, ho_in_ZImageScaled);
hcppthread_handle->SetInputIconicParamObject(3, ho_in_InspectionRegion4);
hcppthread_handle->SetInputIconicParamObject(4, ho_in_ProductRegion);
hcppthread_handle->SetInputCtrlParamTuple(5, hv_in_PlaneParamValue4);
hcppthread_handle->BindOutputCtrlParamTuple(0, 0, &hv_out_PaintRows2);
hcppthread_handle->BindOutputCtrlParamTuple(1, 0, &hv_out_PaintColumns2);
hcppthread_handle->BindOutputCtrlParamTuple(2, 0, &hv_out_Height4);
hcppthread_handle->BindOutputCtrlParamTuple(3, 0, &hv_out_Flag4);
// Start proc line in thread
hcppthread_handle->ParStart(&hv_Thread4);
ho_in_outerEdge = ho_out_GlueRegion;
ho_in_innerEdge = ho_in_EdgeRegion;
ho_in_HYRegion = ho_RegionErosion1;
// Create a thread instance
hcppthread_handle = new HDevThread(hcppthread_context,
(void*)HDevExportCpp::_hcppthread_Cal_Height3, 6, 2);
// Set thread procedure call arguments
hcppthread_handle->SetInputIconicParamObject(0, ho_in_XImageScaled);
hcppthread_handle->SetInputIconicParamObject(1, ho_in_YImageScaled);
hcppthread_handle->SetInputIconicParamObject(2, ho_in_ZImageScaled);
hcppthread_handle->SetInputIconicParamObject(3, ho_in_innerEdge);
hcppthread_handle->SetInputIconicParamObject(4, ho_in_outerEdge);
hcppthread_handle->SetInputIconicParamObject(5, ho_in_HYRegion);
hcppthread_handle->BindOutputCtrlParamTuple(0, 0, &hv_out_Height);
hcppthread_handle->BindOutputCtrlParamTuple(1, 0, &hv_out_Flag5);
// Start proc line in thread
hcppthread_handle->ParStart(&hv_Thread5);
hv_Thread = (hv_Thread.TupleConcat(hv_Thread4)).TupleConcat(hv_Thread5);
HDevThread::ParJoin(hv_Thread);
}
if (0 != 1)
{
//计算点胶高于基准面的高点
ho_in_InspectionRegion3 = ho_InspectionRegion;
hv_in_PlaneParamValue3 = hv_PlaneParamValue;
Cal_Height1(ho_in_XImageScaled, ho_in_YImageScaled, ho_in_ZImageScaled, ho_in_InspectionRegion3,
hv_in_PlaneParamValue3, &hv_out_PaintRows1, &hv_out_PaintColumns1, &hv_out_Height3,
&hv_out_Flag3);
//计算基准面的高点,
ho_in_InspectionRegion4 = ho_ProductRegion;
hv_in_PlaneParamValue4 = hv_PlaneParamValue;
ho_in_ProductRegion = ho_ProductRegion;
Cal_Height2(ho_in_XImageScaled, ho_in_YImageScaled, ho_in_ZImageScaled, ho_in_InspectionRegion4,
ho_in_ProductRegion, hv_in_PlaneParamValue4, &hv_out_PaintRows2, &hv_out_PaintColumns2,
&hv_out_Height4, &hv_out_Flag4);
ho_in_outerEdge = ho_out_GlueRegion;
ho_in_innerEdge = ho_in_EdgeRegion;
ho_in_HYRegion = ho_RegionErosion1;
Cal_Height3(ho_in_XImageScaled, ho_in_YImageScaled, ho_in_ZImageScaled, ho_in_innerEdge,
ho_in_outerEdge, ho_in_HYRegion, &hv_out_Height, &hv_out_Flag5);
}
if (0 != (HTuple(HTuple(hv_out_Flag3 == 1).TupleAnd(hv_out_Flag4 == 1)).TupleAnd(hv_out_Flag5 == 1)))
{
(*hv_out_Height1) = hv_out_Height3 - hv_out_Height4;
(*hv_out_Height2) = hv_out_Height;
(*hv_out_Point).Clear();
(*hv_out_Point).Append(hv_out_PaintRows1);
(*hv_out_Point).Append(hv_out_PaintColumns1);
(*hv_out_Point).Append(hv_out_PaintRows2);
(*hv_out_Point).Append(hv_out_PaintColumns2);
(*hv_out_Flag) = 1;
}
if (0 != 1)
{
//高点和低点的在该处进行打印.
//gen_cross_contour_xld (Cross1, out_PaintRows1*zoomFactor, out_PaintColumns1, 40, rad(45))
//gen_cross_contour_xld (Cross2, out_PaintRows2*zoomFactor, out_PaintColumns2, 40, rad(45))
//get_contour_xld (Cross1, Row4, Col4)
//get_contour_xld (Cross2, Row5, Col5)
//gen_region_polygon (Region1, Row4, Col4)
//gen_region_polygon (Region2, Row5, Col5)
//dilation_circle (Region1, RegionDilation1, 1)
//dilation_circle (Region2, RegionDilation2, 1)
//union2 (RegionDilation1, RegionDilation2, RegionUnion)
//overpaint_region (out_DisplayImage, RegionUnion, [0,0,255], 'fill')
}
//__________________________________________________________________________
//***********************************************************************
//__________________________________________________________________________________
//基于边缘的扣边区域, 获取点胶的高度;
//dev_display (ImageReduced)
//dev_display (ProductRegion)
//erosion_circle (ProductRegion, RegionErosion2, 10)
//erosion_circle (RegionErosion2, RegionErosion4, 25)
//difference (RegionErosion2, RegionErosion4, RegionDifference)
//dev_display (ImageReduced)
//dev_display (RegionDifference)
//erosion_rectangle1 (ProductRegion, RegionErosion5, 180, 1)
//dilation_rectangle1 (RegionErosion5, RegionDilation, 1, 100)
//intersection (RegionDifference, RegionDilation, BaseRegion)
//dev_display (ImageReduced)
//dev_display (BaseRegion)
//reduce_domain (in_XImageScaled, BaseRegion, ImageReduced4)
//reduce_domain (in_YImageScaled, BaseRegion, ImageReduced5)
//reduce_domain (in_ZImageScaled, BaseRegion, ImageReduced6)
//xyz_to_object_model_3d (ImageReduced4, ImageReduced5, ImageReduced6, ObjectModel3D)
//拟合平面,
//ParFitting := ['primitive_type', 'fitting_algorithm', 'output_xyz_mapping']
//ValFitting := ['plane','least_squares_huber', 'false']
//利用产品的边缘拟合平面,
//fit_primitives_object_model_3d (ObjectModel3D, ParFitting, ValFitting, ObjectModelPlane)
//get_object_model_3d_params (ObjectModelPlane, 'primitive_parameter', PlaneParamValue)
//A := PlaneParamValue[0]
//B := PlaneParamValue[1]
//C := PlaneParamValue[2]
//D := PlaneParamValue[3]
//get_region_points (InspectionRegion, Rows, Columns)
//get_grayval (in_XImageScaled, Rows, Columns, X)
//get_grayval (in_YImageScaled, Rows, Columns, Y)
//get_grayval (in_ZImageScaled, Rows, Columns, Z)
//Dist := A* X + B* Y + C * Z - D
//if (C < 0)
//Dist := -Dist
//endif
//tuple_sort (Dist, Sorted)
//******************************3***************************
//算法寻找高点, 并寻找最高点坐标
//tuple_greater_elem (Dist, 0, PosGreater)
//寻找大于0的点坐标,
//tuple_find (PosGreater, 1, PosIndices)
//if (|PosIndices|<300 or PosIndices=-1)
//tuple_less_elem (Dist, 0, NegLess)
//tuple_find (NegLess, 1, NegIndices)
//tuple_select (Dist, NegIndices, Selected2)
//该处需要对点集进行排序,
//tuple_sort (Selected2, Sorted1)
//end1 := int(|Sorted1|*0.95)
//tuple_select (Sorted1, end1, Selected3)
//tuple_find (Dist, Selected3, Indices)
//tuple_select (Rows, Indices, out_PaintRows)
//tuple_select (Columns, Indices, out_PaintColumns)
//out_Height1 := Selected3
//elseif (|PosIndices|>300)
//tuple_greater_elem (Dist, 0, PosGreater)
//tuple_find (PosGreater, 1, PosIndices)
//tuple_select (Dist, PosIndices, Selected3)
//tuple_sort (Selected3, Sorted2)
//end2 := int(|Sorted2|*0.95)
//tuple_select (Sorted2, end2, Selected4)
//tuple_find (Dist, Selected4, Indices)
//tuple_select (Rows, Indices, out_PaintRows)
//tuple_select (Columns, Indices, out_PaintColumns)
//out_Height1 := Selected4
//endif
//打印方式,
//if (1)
//高点和低点的在该处进行打印.
//gen_cross_contour_xld (Cross1, out_PaintRows*zoomFactor, out_PaintColumns, 40, rad(45))
//if (0)
//gen_cross_contour_xld (Cross11, out_PaintRows, out_PaintColumns, 40, rad(45))
//dev_display (ImageReduced)
//dev_display (Cross11)
//endif
//get_contour_xld (Cross1, Row4, Col4)
//gen_region_polygon (Region1, Row4, Col4)
//dilation_circle (Region1, RegionDilation1, 1)
//overpaint_region (out_DisplayImage, RegionDilation1, [0,0,255], 'fill')
//boundary (BaseRegion, RegionBorder2, 'inner')
//zoom_region (RegionBorder2, ZoomBaseRegion, 1, zoomFactor)
//overpaint_region (out_DisplayImage, ZoomBaseRegion, [255,255,0], 'fill')
//endif
//__________________________________________________________________________________
//difference (out_EdgeRegion, in_EdgeRegion, GlueRegion)
//boundary (GlueRegion, RegionBorder, 'inner')
//展示用的结果图,
//zoom_region (RegionBorder, ZoomGlueRegion, 1, zoomFactor)
//dilation_circle (ZoomGlueRegion, RegionDilation5, 1)
//overpaint_region (out_DisplayImage, RegionDilation5, [0,255,0], 'fill')
//boundary (InspectionRegion, RegionBorder1, 'inner')
//zoom_region (RegionBorder1, ZoomGlueRegion, 1, zoomFactor)
//overpaint_region (out_DisplayImage, ZoomGlueRegion, [255,0,0], 'fill')
//stop ()
}
// catch (Exception)
catch (HException& HDevExpDefaultException)
{
HDevExpDefaultException.ToHTuple(&hv_Exception);
//zoom_image_factor (in_ZImageScaled, ImageZoomed, 1, zoomFactor, 'constant')
//scale_image_max (ImageZoomed, ImageScaleMax1)
//copy_image (ImageScaleMax1, Image4)
//copy_image (ImageScaleMax1, Image5)
//copy_image (ImageScaleMax1, Image6)
//compose3 (Image4, Image5, Image6, out_DisplayImage)
(*hv_out_Flag) = 2;
(*hv_out_Ngtails) = "算法执行错误";
}
return;
}
void Cal_Height1(HObject ho_in_XImageScaled, HObject ho_in_YImageScaled, HObject ho_in_ZImageScaled,
HObject ho_in_InspectionRegion, HTuple hv_in_PlaneParamValue, HTuple* hv_out_PaintRows,
HTuple* hv_out_PaintColumns, HTuple* hv_out_Height, HTuple* hv_out_Flag)
{
// Local iconic variables
// Local control variables
HTuple hv_out_Ngtails, hv_A, hv_B, hv_C, hv_D;
HTuple hv_Rows, hv_Columns, hv_X, hv_Y, hv_Z, hv_Dist;
HTuple hv_Sorted, hv_PosGreater, hv_PosIndices, hv_NegLess;
HTuple hv_NegIndices, hv_Selected2, hv_Sorted1, hv_end1;
HTuple hv_Selected3, hv_Indices, hv_Sorted2, hv_end2, hv_Selected4;
HTuple hv_Exception;
(*hv_out_Height) = -99;
(*hv_out_Flag) = 0;
hv_out_Ngtails = "ok";
try
{
hv_A = ((const HTuple&)hv_in_PlaneParamValue)[0];
hv_B = ((const HTuple&)hv_in_PlaneParamValue)[1];
hv_C = ((const HTuple&)hv_in_PlaneParamValue)[2];
hv_D = ((const HTuple&)hv_in_PlaneParamValue)[3];
GetRegionPoints(ho_in_InspectionRegion, &hv_Rows, &hv_Columns);
GetGrayval(ho_in_XImageScaled, hv_Rows, hv_Columns, &hv_X);
GetGrayval(ho_in_YImageScaled, hv_Rows, hv_Columns, &hv_Y);
GetGrayval(ho_in_ZImageScaled, hv_Rows, hv_Columns, &hv_Z);
hv_Dist = (((hv_A * hv_X) + (hv_B * hv_Y)) + (hv_C * hv_Z)) - hv_D;
if (0 != (hv_C < 0))
{
hv_Dist = -hv_Dist;
}
TupleSort(hv_Dist, &hv_Sorted);
//******************************3***************************
//算法寻找高点, 并寻找最高点坐标
TupleGreaterElem(hv_Dist, 0, &hv_PosGreater);
//寻找大于0的点坐标,
TupleFind(hv_PosGreater, 1, &hv_PosIndices);
if (0 != (HTuple((hv_PosIndices.TupleLength()) < 100).TupleOr(hv_PosIndices == -1)))
{
TupleLessElem(hv_Dist, 0, &hv_NegLess);
TupleFind(hv_NegLess, 1, &hv_NegIndices);
TupleSelect(hv_Dist, hv_NegIndices, &hv_Selected2);
//该处需要对点集进行排序,
TupleSort(hv_Selected2, &hv_Sorted1);
hv_end1 = HTuple((hv_Sorted1.TupleLength()) * 0.95).TupleInt();
TupleSelect(hv_Sorted1, hv_end1, &hv_Selected3);
TupleFind(hv_Dist, hv_Selected3, &hv_Indices);
TupleSelect(hv_Rows, hv_Indices, &(*hv_out_PaintRows));
TupleSelect(hv_Columns, hv_Indices, &(*hv_out_PaintColumns));
(*hv_out_Height) = hv_Selected3;
}
else if (0 != ((hv_PosIndices.TupleLength()) > 100))
{
TupleGreaterElem(hv_Dist, 0, &hv_PosGreater);
TupleFind(hv_PosGreater, 1, &hv_PosIndices);
TupleSelect(hv_Dist, hv_PosIndices, &hv_Selected3);
//该处需要对点集进行排序,
TupleSort(hv_Selected3, &hv_Sorted2);
hv_end2 = HTuple((hv_Sorted2.TupleLength()) * 0.99).TupleInt();
TupleSelect(hv_Sorted2, hv_end2, &hv_Selected4);
TupleFind(hv_Dist, hv_Selected4, &hv_Indices);
TupleSelect(hv_Rows, hv_Indices, &(*hv_out_PaintRows));
TupleSelect(hv_Columns, hv_Indices, &(*hv_out_PaintColumns));
(*hv_out_Height) = hv_Selected4;
}
if (0 != ((*hv_out_Height) != -99))
{
(*hv_out_Flag) = 1;
}
}
// catch (Exception)
catch (HException& HDevExpDefaultException)
{
HDevExpDefaultException.ToHTuple(&hv_Exception);
hv_out_Ngtails = "算法错误";
}
return;
}
void Cal_Height2(HObject ho_in_XImageScaled, HObject ho_in_YImageScaled, HObject ho_in_ZImageScaled,
HObject ho_in_InspectionRegion, HObject ho_in_ProductRegion, HTuple hv_in_PlaneParamValue,
HTuple* hv_out_PaintRows, HTuple* hv_out_PaintColumns, HTuple* hv_out_Height,
HTuple* hv_out_Flag)
{
// Local iconic variables
HObject ho_RegionErosion, ho_RegionDifference;
HObject ho_RegionTrans, ho_RegionErosion1, ho_InspectionRegion;
// Local control variables
HTuple hv_out_Ngtails, hv_A, hv_B, hv_C, hv_D;
HTuple hv_Rows, hv_Columns, hv_X, hv_Y, hv_Z, hv_Dist;
HTuple hv_Sorted, hv_PosGreater, hv_PosIndices, hv_NegLess;
HTuple hv_NegIndices, hv_Selected2, hv_Sorted1, hv_end1;
HTuple hv_Selected3, hv_Indices, hv_Sorted2, hv_end2, hv_Selected4;
HTuple hv_Exception;
(*hv_out_Height) = -99;
(*hv_out_Flag) = 0;
hv_out_Ngtails = "ok";
try
{
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//fill_up (in_InspectionRegion, RegionFillUp)
//opening_circle (RegionFillUp, RegionOpening, 10.5)
//dev_display (in_ZImageScaled)
//dev_display (RegionOpening)
//connection (RegionOpening, ConnectedRegions)
//select_shape_std (ConnectedRegions, SelectedRegions, 'max_area', 70)
//dilation_circle (SelectedRegions, RegionDilation, 35)
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ErosionRectangle1(ho_in_InspectionRegion, &ho_RegionErosion, 10, 70);
Difference(ho_in_InspectionRegion, ho_RegionErosion, &ho_RegionDifference);
ShapeTrans(ho_RegionDifference, &ho_RegionTrans, "rectangle1");
ErosionRectangle1(ho_RegionTrans, &ho_RegionErosion1, 350, 1);
Intersection(ho_RegionDifference, ho_RegionErosion1, &ho_InspectionRegion);
//dev_display (in_ZImageScaled)
//dev_display (RegionErosion1)
//difference (in_ProductRegion, RegionDilation, InspectionRegion)
//dev_display (in_ZImageScaled)
//dev_display (InspectionRegion)
hv_A = ((const HTuple&)hv_in_PlaneParamValue)[0];
hv_B = ((const HTuple&)hv_in_PlaneParamValue)[1];
hv_C = ((const HTuple&)hv_in_PlaneParamValue)[2];
hv_D = ((const HTuple&)hv_in_PlaneParamValue)[3];
GetRegionPoints(ho_InspectionRegion, &hv_Rows, &hv_Columns);
GetGrayval(ho_in_XImageScaled, hv_Rows, hv_Columns, &hv_X);
GetGrayval(ho_in_YImageScaled, hv_Rows, hv_Columns, &hv_Y);
GetGrayval(ho_in_ZImageScaled, hv_Rows, hv_Columns, &hv_Z);
hv_Dist = (((hv_A * hv_X) + (hv_B * hv_Y)) + (hv_C * hv_Z)) - hv_D;
if (0 != (hv_C < 0))
{
hv_Dist = -hv_Dist;
}
TupleSort(hv_Dist, &hv_Sorted);
//******************************3***************************
//算法寻找高点, 并寻找最高点坐标
TupleGreaterElem(hv_Dist, 0, &hv_PosGreater);
//寻找大于0的点坐标,
TupleFind(hv_PosGreater, 1, &hv_PosIndices);
if (0 != (HTuple((hv_PosIndices.TupleLength()) < 300).TupleOr(hv_PosIndices == -1)))
{
TupleLessElem(hv_Dist, 0, &hv_NegLess);
TupleFind(hv_NegLess, 1, &hv_NegIndices);
TupleSelect(hv_Dist, hv_NegIndices, &hv_Selected2);
//该处需要对点集进行排序,
TupleSort(hv_Selected2, &hv_Sorted1);
hv_end1 = HTuple((hv_Sorted1.TupleLength()) * 0.95).TupleInt();
TupleSelect(hv_Sorted1, hv_end1, &hv_Selected3);
TupleFind(hv_Dist, hv_Selected3, &hv_Indices);
TupleSelect(hv_Rows, hv_Indices, &(*hv_out_PaintRows));
TupleSelect(hv_Columns, hv_Indices, &(*hv_out_PaintColumns));
(*hv_out_Height) = hv_Selected3;
}
else if (0 != ((hv_PosIndices.TupleLength()) > 300))
{
TupleGreaterElem(hv_Dist, 0, &hv_PosGreater);
TupleFind(hv_PosGreater, 1, &hv_PosIndices);
TupleSelect(hv_Dist, hv_PosIndices, &hv_Selected3);
TupleSort(hv_Selected3, &hv_Sorted2);
hv_end2 = HTuple((hv_Sorted2.TupleLength()) * 0.95).TupleInt();
TupleSelect(hv_Sorted2, hv_end2, &hv_Selected4);
TupleFind(hv_Dist, hv_Selected4, &hv_Indices);
TupleSelect(hv_Rows, hv_Indices, &(*hv_out_PaintRows));
TupleSelect(hv_Columns, hv_Indices, &(*hv_out_PaintColumns));
(*hv_out_Height) = hv_Selected4;
}
if (0 != ((*hv_out_Height) != -99))
{
(*hv_out_Flag) = 1;
}
}
// catch (Exception)
catch (HException& HDevExpDefaultException)
{
HDevExpDefaultException.ToHTuple(&hv_Exception);
hv_out_Ngtails = "算法错误";
}
return;
}
void Cal_Height3(HObject ho_in_XImageScaled, HObject ho_in_YImageScaled, HObject ho_in_ZImageScaled,
HObject ho_in_innerEdge, HObject ho_in_outerEdge, HObject ho_in_HYRegion, HTuple* hv_out_Height,
HTuple* hv_out_Flag)
{
// Local iconic variables
HObject ho_RegionDifference, ho_GlueRegion, ho_RegionErosion;
HObject ho_ImageReduced1, ho_ImageReduced2, ho_ImageReduced3;
// Local control variables
HTuple hv_out_Ngtails, hv_ObjectModel3D, hv_ParFitting;
HTuple hv_ValFitting, hv_ObjectModelPlane, hv_PlaneParamValue;
HTuple hv_A, hv_B, hv_C, hv_D, hv_Rows, hv_Columns, hv_X;
HTuple hv_Y, hv_Z, hv_Dist, hv_Sorted, hv_start1, hv_end1;
HTuple hv_Selected, hv_Max, hv_Exception;
(*hv_out_Height) = -99;
(*hv_out_Flag) = 0;
hv_out_Ngtails = "ok";
try
{
Difference(ho_in_innerEdge, ho_in_HYRegion, &ho_RegionDifference);
Difference(ho_in_outerEdge, ho_in_innerEdge, &ho_GlueRegion);
ErosionCircle(ho_RegionDifference, &ho_RegionErosion, 10.5);
ReduceDomain(ho_in_XImageScaled, ho_RegionErosion, &ho_ImageReduced1);
ReduceDomain(ho_in_YImageScaled, ho_RegionErosion, &ho_ImageReduced2);
ReduceDomain(ho_in_ZImageScaled, ho_RegionErosion, &ho_ImageReduced3);
XyzToObjectModel3d(ho_ImageReduced1, ho_ImageReduced2, ho_ImageReduced3, &hv_ObjectModel3D);
//拟合平面,
hv_ParFitting.Clear();
hv_ParFitting[0] = "primitive_type";
hv_ParFitting[1] = "fitting_algorithm";
hv_ParFitting[2] = "output_xyz_mapping";
hv_ValFitting.Clear();
hv_ValFitting[0] = "plane";
hv_ValFitting[1] = "least_squares_huber";
hv_ValFitting[2] = "false";
//利用产品的边缘拟合平面,
FitPrimitivesObjectModel3d(hv_ObjectModel3D, hv_ParFitting, hv_ValFitting, &hv_ObjectModelPlane);
GetObjectModel3dParams(hv_ObjectModelPlane, "primitive_parameter", &hv_PlaneParamValue);
hv_A = ((const HTuple&)hv_PlaneParamValue)[0];
hv_B = ((const HTuple&)hv_PlaneParamValue)[1];
hv_C = ((const HTuple&)hv_PlaneParamValue)[2];
hv_D = ((const HTuple&)hv_PlaneParamValue)[3];
GetRegionPoints(ho_GlueRegion, &hv_Rows, &hv_Columns);
GetGrayval(ho_in_XImageScaled, hv_Rows, hv_Columns, &hv_X);
GetGrayval(ho_in_YImageScaled, hv_Rows, hv_Columns, &hv_Y);
GetGrayval(ho_in_ZImageScaled, hv_Rows, hv_Columns, &hv_Z);
hv_Dist = (((hv_A * hv_X) + (hv_B * hv_Y)) + (hv_C * hv_Z)) - hv_D;
if (0 != (hv_C < 0))
{
hv_Dist = -hv_Dist;
}
TupleSort(hv_Dist, &hv_Sorted);
hv_start1 = HTuple((hv_Sorted.TupleLength()) * 0.02).TupleInt();
hv_end1 = HTuple((hv_Sorted.TupleLength()) * 0.98).TupleInt();
TupleSelect(hv_Sorted, HTuple::TupleGenSequence(hv_start1, hv_end1, 1), &hv_Selected);
TupleMax(hv_Selected, &hv_Max);
(*hv_out_Height) = hv_Max;
if (0 != ((*hv_out_Height) != -99))
{
(*hv_out_Flag) = 1;
}
}
// catch (Exception)
catch (HException& HDevExpDefaultException)
{
HDevExpDefaultException.ToHTuple(&hv_Exception);
hv_out_Ngtails = "算法错误";
}
return;
}
void Get_InGlueRegion(HObject ho_in_XImageScaled, HObject ho_in_YImageScaled, HObject ho_in_ZImageScaled,
HObject ho_in_ZeroRegion, HObject* ho_in_EdgeRegion, HTuple hv_in_Height, HTuple hv_in_PlaneParamValue,
HTuple hv_in_Direc, HTuple* hv_out_Flag, HTuple* hv_out_Ngtails)
{
// Local iconic variables
HObject ho_RegionFillUp, ho_Tempregion, ho_Region2;
HObject ho_RegionOpening, ho_ConnectedRegions3, ho_Regions;
HObject ho_Out_ROI, ho_ConnectedRegions4, ho_SelectedRegions3;
HObject ho_closingRegion;
// Local control variables
HTuple hv_A, hv_B, hv_C, hv_D, hv_Rows, hv_Columns;
HTuple hv_X, hv_Y, hv_Z, hv_Dist, hv_Com_Tuple, hv_Prod_X;
HTuple hv_Prod_Y, hv_HeightOut, hv_Com_Tuple2, hv_Com_Tuple1;
HTuple hv_And, hv_Number, hv_Area, hv_Row, hv_Column, hv_Exception;
(*hv_out_Flag) = 0;
(*hv_out_Ngtails) = "ok";
try
{
FillUp(ho_in_ZeroRegion, &ho_RegionFillUp);
Difference(ho_RegionFillUp, ho_in_ZeroRegion, &ho_Tempregion);
hv_A = ((const HTuple&)hv_in_PlaneParamValue)[0];
hv_B = ((const HTuple&)hv_in_PlaneParamValue)[1];
hv_C = ((const HTuple&)hv_in_PlaneParamValue)[2];
hv_D = ((const HTuple&)hv_in_PlaneParamValue)[3];
GetRegionPoints(ho_Tempregion, &hv_Rows, &hv_Columns);
GetGrayval(ho_in_XImageScaled, hv_Rows, hv_Columns, &hv_X);
GetGrayval(ho_in_YImageScaled, hv_Rows, hv_Columns, &hv_Y);
GetGrayval(ho_in_ZImageScaled, hv_Rows, hv_Columns, &hv_Z);
hv_Dist = (((hv_A * hv_X) + (hv_B * hv_Y)) + (hv_C * hv_Z)) - hv_D;
if (0 != (hv_C < 0))
{
hv_Dist = hv_Dist * -1;
}
if (0 != (hv_in_Direc == HTuple("Pos")))
{
TupleGreaterElem(hv_Dist, hv_in_Height, &hv_Com_Tuple);
TupleMult(hv_Rows, hv_Com_Tuple, &hv_Prod_X);
TupleMult(hv_Columns, hv_Com_Tuple, &hv_Prod_Y);
GenRegionPoints(&ho_Region2, hv_Prod_X, hv_Prod_Y);
ClosingCircle(ho_Region2, &ho_RegionOpening, 3.5);
Connection(ho_RegionOpening, &ho_ConnectedRegions3);
SelectShape(ho_ConnectedRegions3, &ho_Regions, "area", "and", 2, 9999999999999);
Union1(ho_Regions, &ho_Out_ROI);
}
else if (0 != (hv_in_Direc == HTuple("Neg")))
{
hv_HeightOut = hv_HeightOut * -1;
TupleLessElem(hv_Dist, hv_in_Height, &hv_Com_Tuple);
TupleMult(hv_Rows, hv_Com_Tuple, &hv_Prod_X);
TupleMult(hv_Columns, hv_Com_Tuple, &hv_Prod_Y);
GenRegionPoints(&ho_Region2, hv_Prod_X, hv_Prod_Y);
ClosingCircle(ho_Region2, &ho_RegionOpening, 3.5);
Connection(ho_RegionOpening, &ho_ConnectedRegions3);
SelectShape(ho_ConnectedRegions3, &ho_Regions, "area", "and", 2, 9999999999999);
Union1(ho_Regions, &ho_Out_ROI);
}
else if (0 != (hv_in_Direc == HTuple("all")))
{
TupleLessElem(hv_Dist, hv_in_Height, &hv_Com_Tuple2);
TupleGreaterElem(hv_Dist, -hv_in_Height, &hv_Com_Tuple1);
TupleAnd(hv_Com_Tuple1, hv_Com_Tuple2, &hv_And);
TupleMult(hv_Rows, hv_And, &hv_Prod_X);
TupleMult(hv_Columns, hv_And, &hv_Prod_Y);
GenRegionPoints(&ho_Region2, hv_Prod_X, hv_Prod_Y);
ClosingCircle(ho_Region2, &ho_RegionOpening, 3.5);
Connection(ho_RegionOpening, &ho_ConnectedRegions3);
SelectShape(ho_ConnectedRegions3, &ho_Regions, "area", "and", 2, 9999999999999);
Union1(ho_Regions, &ho_Out_ROI);
}
ClosingCircle(ho_Out_ROI, &ho_RegionOpening, 4.5);
Connection(ho_RegionOpening, &ho_ConnectedRegions4);
SelectShape(ho_ConnectedRegions4, &ho_SelectedRegions3, "max_diameter", "and",
450, 900);
CountObj(ho_SelectedRegions3, &hv_Number);
//if (Number>1)
//smallest_rectangle1 (out_EdgeRegion, Row11, Column11, Row21, Column21)
//select_shape (SelectedRegions3, SelectedRegions1, ['row','column'], 'and', [Row11,Column11], [Row21,Column21])
//SelectedRegions3 := SelectedRegions1
//endif
ClosingCircle(ho_SelectedRegions3, &ho_closingRegion, 20);
FillUp(ho_closingRegion, &(*ho_in_EdgeRegion));
AreaCenter((*ho_in_EdgeRegion), &hv_Area, &hv_Row, &hv_Column);
if (0 != (hv_Area > 0))
{
(*hv_out_Flag) = 1;
}
}
// catch (Exception)
catch (HException& HDevExpDefaultException)
{
HDevExpDefaultException.ToHTuple(&hv_Exception);
(*hv_out_Ngtails) = "算法错误";
}
return;
}
void Get_OutGlueRegion(HObject ho_in_XImageScaled, HObject ho_in_YImageScaled, HObject ho_in_ZImageScaled,
HObject ho_in_ZeroRegion, HObject* ho_out_GlueRegion, HTuple hv_in_Height, HTuple hv_in_Direc,
HTuple hv_in_PlaneParamValue, HTuple* hv_out_Flag, HTuple* hv_out_Ngtails)
{
// Local iconic variables
HObject ho_RegionFillUp, ho_Tempregion, ho_Region2;
HObject ho_RegionOpening, ho_ConnectedRegions3, ho_Regions;
HObject ho_Out_ROI, ho_SelectedRegions2, ho_RegionFillUp1;
HObject ho_RegionDifference2, ho_EdgeRegion, ho_ConnectedRegions1;
// Local control variables
HTuple hv_A, hv_B, hv_C, hv_D, hv_Rows, hv_Columns;
HTuple hv_X, hv_Y, hv_Z, hv_Dist, hv_Com_Tuple, hv_Prod_X;
HTuple hv_Prod_Y, hv_Com_Tuple2, hv_Com_Tuple1, hv_And;
HTuple hv_Area, hv_Row, hv_Column, hv_Exception;
(*hv_out_Flag) = 0;
(*hv_out_Ngtails) = "ok";
try
{
FillUp(ho_in_ZeroRegion, &ho_RegionFillUp);
Difference(ho_RegionFillUp, ho_in_ZeroRegion, &ho_Tempregion);
//dev_display (in_XIageScaled)
if (HDevWindowStack::IsOpen())
DispObj(ho_Tempregion, HDevWindowStack::GetActive());
hv_A = ((const HTuple&)hv_in_PlaneParamValue)[0];
hv_B = ((const HTuple&)hv_in_PlaneParamValue)[1];
hv_C = ((const HTuple&)hv_in_PlaneParamValue)[2];
hv_D = ((const HTuple&)hv_in_PlaneParamValue)[3];
GetRegionPoints(ho_Tempregion, &hv_Rows, &hv_Columns);
GetGrayval(ho_in_XImageScaled, hv_Rows, hv_Columns, &hv_X);
GetGrayval(ho_in_YImageScaled, hv_Rows, hv_Columns, &hv_Y);
GetGrayval(ho_in_ZImageScaled, hv_Rows, hv_Columns, &hv_Z);
hv_Dist = (((hv_A * hv_X) + (hv_B * hv_Y)) + (hv_C * hv_Z)) - hv_D;
if (0 != (hv_C < 0))
{
hv_Dist = hv_Dist * -1;
}
if (0 != (hv_in_Direc == HTuple("Pos")))
{
TupleGreaterElem(hv_Dist, hv_in_Height, &hv_Com_Tuple);
TupleMult(hv_Rows, hv_Com_Tuple, &hv_Prod_X);
TupleMult(hv_Columns, hv_Com_Tuple, &hv_Prod_Y);
GenRegionPoints(&ho_Region2, hv_Prod_X, hv_Prod_Y);
ClosingCircle(ho_Region2, &ho_RegionOpening, 3.5);
Connection(ho_RegionOpening, &ho_ConnectedRegions3);
SelectShape(ho_ConnectedRegions3, &ho_Regions, "area", "and", 2, 9999999999999);
Union1(ho_Regions, &ho_Out_ROI);
}
else if (0 != (hv_in_Direc == HTuple("Neg")))
{
//Height := Height*(-1)
TupleLessElem(hv_Dist, -hv_in_Height, &hv_Com_Tuple);
TupleMult(hv_Rows, hv_Com_Tuple, &hv_Prod_X);
TupleMult(hv_Columns, hv_Com_Tuple, &hv_Prod_Y);
GenRegionPoints(&ho_Region2, hv_Prod_X, hv_Prod_Y);
ClosingCircle(ho_Region2, &ho_RegionOpening, 3.5);
Connection(ho_RegionOpening, &ho_ConnectedRegions3);
SelectShape(ho_ConnectedRegions3, &ho_Regions, "area", "and", 2, 9999999999999);
Union1(ho_Regions, &ho_Out_ROI);
}
else if (0 != (hv_in_Direc == HTuple("all")))
{
TupleLessElem(hv_Dist, hv_in_Height, &hv_Com_Tuple2);
TupleGreaterElem(hv_Dist, -hv_in_Height, &hv_Com_Tuple1);
TupleAnd(hv_Com_Tuple1, hv_Com_Tuple2, &hv_And);
TupleMult(hv_Rows, hv_And, &hv_Prod_X);
TupleMult(hv_Columns, hv_And, &hv_Prod_Y);
GenRegionPoints(&ho_Region2, hv_Prod_X, hv_Prod_Y);
ClosingCircle(ho_Region2, &ho_RegionOpening, 3.5);
Connection(ho_RegionOpening, &ho_ConnectedRegions3);
SelectShape(ho_ConnectedRegions3, &ho_Regions, "area", "and", 2, 9999999999999);
Union1(ho_Regions, &ho_Out_ROI);
}
Connection(ho_Out_ROI, &ho_ConnectedRegions3);
SelectShapeStd(ho_ConnectedRegions3, &ho_SelectedRegions2, "max_area", 70);
FillUp(ho_SelectedRegions2, &ho_RegionFillUp1);
Difference(ho_RegionFillUp1, ho_SelectedRegions2, &ho_RegionDifference2);
OpeningCircle(ho_RegionDifference2, &ho_EdgeRegion, 5.5);
Connection(ho_EdgeRegion, &ho_ConnectedRegions1);
SelectShapeStd(ho_ConnectedRegions1, &(*ho_out_GlueRegion), "max_area", 70);
AreaCenter((*ho_out_GlueRegion), &hv_Area, &hv_Row, &hv_Column);
if (0 != (hv_Area > 0))
{
(*hv_out_Flag) = 1;
}
}
// catch (Exception)
catch (HException& HDevExpDefaultException)
{
HDevExpDefaultException.ToHTuple(&hv_Exception);
(*hv_out_Flag) = 0;
(*hv_out_Ngtails) = "算法错误";
}
return;
}
void Get_ROI1(HObject ho_XImageScaled, HObject ho_YImageScaled, HObject ho_ZImageScaled,
HObject ho_ZeroRegion, HObject ho_TmpRegion, HObject* ho_Out_ROI, HTuple hv_Direc,
HTuple hv_Height)
{
// Local iconic variables
HObject ho_ImageReduced1, ho_ImageReduced2, ho_ImageReduced3;
HObject ho_Region2, ho_RegionOpening, ho_ConnectedRegions3;
HObject ho_Regions;
// Local control variables
HTuple hv_Debug, hv_ObjectModel3D, hv_ParFitting;
HTuple hv_ValFitting, hv_ObjectModelPlane, hv_GenParamValue;
HTuple hv_A, hv_B, hv_C, hv_D, hv_WindowHandleOut, hv_PoseOut;
HTuple hv_Rows, hv_Columns, hv_X, hv_Y, hv_Z, hv_Dist;
HTuple hv_Com_Tuple, hv_Prod_X, hv_Prod_Y, hv_Com_Tuple2;
HTuple hv_Com_Tuple1, hv_And, hv_Exception;
//**********************************************************************
//Function Name: Get_ROI1
//Author: 胡汉兵,
//Description: 获取有效的检测面,
//Input:
// XImageScaled, X向图像
// YImageScaled, Y向图像
// ZImageScaled, Z向图像
// Zero_Region, 基准平面的区域,
// InspectioRegion, 提取有效的检测面的Region
// Direc, 挑选极柱面的方向, 'Pos':极柱面上+ 'Neg': 极柱面下- 'all': 极柱上下范围内的点集,
// Height, 挑选极柱面的高度值(mm)
//output: Out_ROI, 挑选的ROI区域,
//verion change log: 胡汉兵 2023-09-07 V1.1 Create new function
// 胡汉兵 2023-11-01 V1.2 新增面上下范围内的点集,
//
//**********************************************************************
hv_Debug = 0;
try
{
ReduceDomain(ho_XImageScaled, ho_ZeroRegion, &ho_ImageReduced1);
ReduceDomain(ho_YImageScaled, ho_ZeroRegion, &ho_ImageReduced2);
ReduceDomain(ho_ZImageScaled, ho_ZeroRegion, &ho_ImageReduced3);
XyzToObjectModel3d(ho_ImageReduced1, ho_ImageReduced2, ho_ImageReduced3, &hv_ObjectModel3D);
//拟合平面,
hv_ParFitting.Clear();
hv_ParFitting[0] = "primitive_type";
hv_ParFitting[1] = "fitting_algorithm";
hv_ParFitting[2] = "output_xyz_mapping";
hv_ValFitting.Clear();
hv_ValFitting[0] = "plane";
hv_ValFitting[1] = "least_squares_huber";
hv_ValFitting[2] = "false";
//利用产品的边缘拟合平面,
FitPrimitivesObjectModel3d(hv_ObjectModel3D, hv_ParFitting, hv_ValFitting, &hv_ObjectModelPlane);
GetObjectModel3dParams(hv_ObjectModelPlane, "primitive_parameter", &hv_GenParamValue);
hv_A = ((const HTuple&)hv_GenParamValue)[0];
hv_B = ((const HTuple&)hv_GenParamValue)[1];
hv_C = ((const HTuple&)hv_GenParamValue)[2];
hv_D = ((const HTuple&)hv_GenParamValue)[3];
GetRegionPoints(ho_TmpRegion, &hv_Rows, &hv_Columns);
GetGrayval(ho_XImageScaled, hv_Rows, hv_Columns, &hv_X);
GetGrayval(ho_YImageScaled, hv_Rows, hv_Columns, &hv_Y);
GetGrayval(ho_ZImageScaled, hv_Rows, hv_Columns, &hv_Z);
hv_Dist = (((hv_A * hv_X) + (hv_B * hv_Y)) + (hv_C * hv_Z)) - hv_D;
if (0 != (hv_C < 0))
{
hv_Dist = hv_Dist * -1;
}
if (0 != (hv_Direc == HTuple("Pos")))
{
TupleGreaterElem(hv_Dist, hv_Height, &hv_Com_Tuple);
TupleMult(hv_Rows, hv_Com_Tuple, &hv_Prod_X);
TupleMult(hv_Columns, hv_Com_Tuple, &hv_Prod_Y);
GenRegionPoints(&ho_Region2, hv_Prod_X, hv_Prod_Y);
ClosingCircle(ho_Region2, &ho_RegionOpening, 3.5);
Connection(ho_RegionOpening, &ho_ConnectedRegions3);
SelectShape(ho_ConnectedRegions3, &ho_Regions, "area", "and", 2, 9999999999999);
Union1(ho_Regions, &(*ho_Out_ROI));
}
else if (0 != (hv_Direc == HTuple("Neg")))
{
hv_Height = hv_Height * -1;
TupleLessElem(hv_Dist, hv_Height, &hv_Com_Tuple);
TupleMult(hv_Rows, hv_Com_Tuple, &hv_Prod_X);
TupleMult(hv_Columns, hv_Com_Tuple, &hv_Prod_Y);
GenRegionPoints(&ho_Region2, hv_Prod_X, hv_Prod_Y);
ClosingCircle(ho_Region2, &ho_RegionOpening, 3.5);
Connection(ho_RegionOpening, &ho_ConnectedRegions3);
SelectShape(ho_ConnectedRegions3, &ho_Regions, "area", "and", 2, 9999999999999);
Union1(ho_Regions, &(*ho_Out_ROI));
}
else if (0 != (hv_Direc == HTuple("all")))
{
TupleLessElem(hv_Dist, hv_Height, &hv_Com_Tuple2);
TupleGreaterElem(hv_Dist, -hv_Height, &hv_Com_Tuple1);
TupleAnd(hv_Com_Tuple1, hv_Com_Tuple2, &hv_And);
TupleMult(hv_Rows, hv_And, &hv_Prod_X);
TupleMult(hv_Columns, hv_And, &hv_Prod_Y);
GenRegionPoints(&ho_Region2, hv_Prod_X, hv_Prod_Y);
ClosingCircle(ho_Region2, &ho_RegionOpening, 3.5);
Connection(ho_RegionOpening, &ho_ConnectedRegions3);
SelectShape(ho_ConnectedRegions3, &ho_Regions, "area", "and", 2, 9999999999999);
Union1(ho_Regions, &(*ho_Out_ROI));
}
}
// catch (Exception)
catch (HException& HDevExpDefaultException)
{
HDevExpDefaultException.ToHTuple(&hv_Exception);
}
return;
}
// Generated stubs for parallel procedure calls. Wrapped in name
// space to avoid name conflicts with actual procedure names
namespace HDevExportCpp
{
// Parallel execution wrapper for Get_OutGlueRegion(...)
static void* _hcppthread_Get_OutGlueRegion(void* hcthread)
{
// +++ define thread context for this procedure
HDevThread* hcppthread = (HDevThread*)hcthread;
try
{
// Input parameters
const HObject& cbho_in_XImageScaled = hcppthread->GetInputIconicParamObject(0);
const HObject& cbho_in_YImageScaled = hcppthread->GetInputIconicParamObject(1);
const HObject& cbho_in_ZImageScaled = hcppthread->GetInputIconicParamObject(2);
const HObject& cbho_in_ZeroRegion = hcppthread->GetInputIconicParamObject(3);
const HTuple& cbhv_in_Height = hcppthread->GetInputCtrlParamTuple(4);
const HTuple& cbhv_in_Direc = hcppthread->GetInputCtrlParamTuple(5);
const HTuple& cbhv_in_PlaneParamValue = hcppthread->GetInputCtrlParamTuple(6);
// Output parameters
HObject cbho_out_GlueRegion;
HTuple cbhv_out_Flag;
HTuple cbhv_out_Ngtails;
// Call Get_OutGlueRegion
Get_OutGlueRegion(cbho_in_XImageScaled, cbho_in_YImageScaled, cbho_in_ZImageScaled,
cbho_in_ZeroRegion, &cbho_out_GlueRegion, cbhv_in_Height, cbhv_in_Direc,
cbhv_in_PlaneParamValue, &cbhv_out_Flag, &cbhv_out_Ngtails);
// Store output parameters in thread object
hcppthread->StoreOutputIconicParamObject(0, cbho_out_GlueRegion);
hcppthread->StoreOutputCtrlParamTuple(1, cbhv_out_Flag);
hcppthread->StoreOutputCtrlParamTuple(2, cbhv_out_Ngtails);
// Reduce reference counter of thread object
hcppthread->Exit();
delete hcppthread;
}
catch (HException& exc)
{
// No exceptions may be raised from stub in parallel case,
// so we need to store this information prior to cleanup
bool is_direct_call = hcppthread->IsDirectCall();
// Attempt to clean up in error case, too
hcppthread->Exit();
delete hcppthread;
// Propagate exception if called directly
if (is_direct_call)
throw exc;
}
return NULL;
}
// Parallel execution wrapper for Get_InGlueRegion(...)
static void* _hcppthread_Get_InGlueRegion(void* hcthread)
{
// +++ define thread context for this procedure
HDevThread* hcppthread = (HDevThread*)hcthread;
try
{
// Input parameters
const HObject& cbho_in_XImageScaled = hcppthread->GetInputIconicParamObject(0);
const HObject& cbho_in_YImageScaled = hcppthread->GetInputIconicParamObject(1);
const HObject& cbho_in_ZImageScaled = hcppthread->GetInputIconicParamObject(2);
const HObject& cbho_in_ZeroRegion = hcppthread->GetInputIconicParamObject(3);
const HTuple& cbhv_in_Height = hcppthread->GetInputCtrlParamTuple(4);
const HTuple& cbhv_in_PlaneParamValue = hcppthread->GetInputCtrlParamTuple(5);
const HTuple& cbhv_in_Direc = hcppthread->GetInputCtrlParamTuple(6);
// Output parameters
HObject cbho_in_EdgeRegion;
HTuple cbhv_out_Flag;
HTuple cbhv_out_Ngtails;
// Call Get_InGlueRegion
Get_InGlueRegion(cbho_in_XImageScaled, cbho_in_YImageScaled, cbho_in_ZImageScaled,
cbho_in_ZeroRegion, &cbho_in_EdgeRegion, cbhv_in_Height, cbhv_in_PlaneParamValue,
cbhv_in_Direc, &cbhv_out_Flag, &cbhv_out_Ngtails);
// Store output parameters in thread object
hcppthread->StoreOutputIconicParamObject(0, cbho_in_EdgeRegion);
hcppthread->StoreOutputCtrlParamTuple(1, cbhv_out_Flag);
hcppthread->StoreOutputCtrlParamTuple(2, cbhv_out_Ngtails);
// Reduce reference counter of thread object
hcppthread->Exit();
delete hcppthread;
}
catch (HException& exc)
{
// No exceptions may be raised from stub in parallel case,
// so we need to store this information prior to cleanup
bool is_direct_call = hcppthread->IsDirectCall();
// Attempt to clean up in error case, too
hcppthread->Exit();
delete hcppthread;
// Propagate exception if called directly
if (is_direct_call)
throw exc;
}
return NULL;
}
// Parallel execution wrapper for Cal_Height1(...)
static void* _hcppthread_Cal_Height1(void* hcthread)
{
// +++ define thread context for this procedure
HDevThread* hcppthread = (HDevThread*)hcthread;
try
{
// Input parameters
const HObject& cbho_in_XImageScaled = hcppthread->GetInputIconicParamObject(0);
const HObject& cbho_in_YImageScaled = hcppthread->GetInputIconicParamObject(1);
const HObject& cbho_in_ZImageScaled = hcppthread->GetInputIconicParamObject(2);
const HObject& cbho_in_InspectionRegion = hcppthread->GetInputIconicParamObject(3);
const HTuple& cbhv_in_PlaneParamValue = hcppthread->GetInputCtrlParamTuple(4);
// Output parameters
HTuple cbhv_out_PaintRows;
HTuple cbhv_out_PaintColumns;
HTuple cbhv_out_Height;
HTuple cbhv_out_Flag;
// Call Cal_Height1
Cal_Height1(cbho_in_XImageScaled, cbho_in_YImageScaled, cbho_in_ZImageScaled,
cbho_in_InspectionRegion, cbhv_in_PlaneParamValue, &cbhv_out_PaintRows, &cbhv_out_PaintColumns,
&cbhv_out_Height, &cbhv_out_Flag);
// Store output parameters in thread object
hcppthread->StoreOutputCtrlParamTuple(0, cbhv_out_PaintRows);
hcppthread->StoreOutputCtrlParamTuple(1, cbhv_out_PaintColumns);
hcppthread->StoreOutputCtrlParamTuple(2, cbhv_out_Height);
hcppthread->StoreOutputCtrlParamTuple(3, cbhv_out_Flag);
// Reduce reference counter of thread object
hcppthread->Exit();
delete hcppthread;
}
catch (HException& exc)
{
// No exceptions may be raised from stub in parallel case,
// so we need to store this information prior to cleanup
bool is_direct_call = hcppthread->IsDirectCall();
// Attempt to clean up in error case, too
hcppthread->Exit();
delete hcppthread;
// Propagate exception if called directly
if (is_direct_call)
throw exc;
}
return NULL;
}
// Parallel execution wrapper for Cal_Height2(...)
static void* _hcppthread_Cal_Height2(void* hcthread)
{
// +++ define thread context for this procedure
HDevThread* hcppthread = (HDevThread*)hcthread;
try
{
// Input parameters
const HObject& cbho_in_XImageScaled = hcppthread->GetInputIconicParamObject(0);
const HObject& cbho_in_YImageScaled = hcppthread->GetInputIconicParamObject(1);
const HObject& cbho_in_ZImageScaled = hcppthread->GetInputIconicParamObject(2);
const HObject& cbho_in_InspectionRegion = hcppthread->GetInputIconicParamObject(3);
const HObject& cbho_in_ProductRegion = hcppthread->GetInputIconicParamObject(4);
const HTuple& cbhv_in_PlaneParamValue = hcppthread->GetInputCtrlParamTuple(5);
// Output parameters
HTuple cbhv_out_PaintRows;
HTuple cbhv_out_PaintColumns;
HTuple cbhv_out_Height;
HTuple cbhv_out_Flag;
// Call Cal_Height2
Cal_Height2(cbho_in_XImageScaled, cbho_in_YImageScaled, cbho_in_ZImageScaled,
cbho_in_InspectionRegion, cbho_in_ProductRegion, cbhv_in_PlaneParamValue,
&cbhv_out_PaintRows, &cbhv_out_PaintColumns, &cbhv_out_Height, &cbhv_out_Flag);
// Store output parameters in thread object
hcppthread->StoreOutputCtrlParamTuple(0, cbhv_out_PaintRows);
hcppthread->StoreOutputCtrlParamTuple(1, cbhv_out_PaintColumns);
hcppthread->StoreOutputCtrlParamTuple(2, cbhv_out_Height);
hcppthread->StoreOutputCtrlParamTuple(3, cbhv_out_Flag);
// Reduce reference counter of thread object
hcppthread->Exit();
delete hcppthread;
}
catch (HException& exc)
{
// No exceptions may be raised from stub in parallel case,
// so we need to store this information prior to cleanup
bool is_direct_call = hcppthread->IsDirectCall();
// Attempt to clean up in error case, too
hcppthread->Exit();
delete hcppthread;
// Propagate exception if called directly
if (is_direct_call)
throw exc;
}
return NULL;
}
// Parallel execution wrapper for Cal_Height3(...)
static void* _hcppthread_Cal_Height3(void* hcthread)
{
// +++ define thread context for this procedure
HDevThread* hcppthread = (HDevThread*)hcthread;
try
{
// Input parameters
const HObject& cbho_in_XImageScaled = hcppthread->GetInputIconicParamObject(0);
const HObject& cbho_in_YImageScaled = hcppthread->GetInputIconicParamObject(1);
const HObject& cbho_in_ZImageScaled = hcppthread->GetInputIconicParamObject(2);
const HObject& cbho_in_innerEdge = hcppthread->GetInputIconicParamObject(3);
const HObject& cbho_in_outerEdge = hcppthread->GetInputIconicParamObject(4);
const HObject& cbho_in_HYRegion = hcppthread->GetInputIconicParamObject(5);
// Output parameters
HTuple cbhv_out_Height;
HTuple cbhv_out_Flag;
// Call Cal_Height3
Cal_Height3(cbho_in_XImageScaled, cbho_in_YImageScaled, cbho_in_ZImageScaled,
cbho_in_innerEdge, cbho_in_outerEdge, cbho_in_HYRegion, &cbhv_out_Height,
&cbhv_out_Flag);
// Store output parameters in thread object
hcppthread->StoreOutputCtrlParamTuple(0, cbhv_out_Height);
hcppthread->StoreOutputCtrlParamTuple(1, cbhv_out_Flag);
// Reduce reference counter of thread object
hcppthread->Exit();
delete hcppthread;
}
catch (HException& exc)
{
// No exceptions may be raised from stub in parallel case,
// so we need to store this information prior to cleanup
bool is_direct_call = hcppthread->IsDirectCall();
// Attempt to clean up in error case, too
hcppthread->Exit();
delete hcppthread;
// Propagate exception if called directly
if (is_direct_call)
throw exc;
}
return NULL;
}
}
#endif
#if 0
void Get_ROI1(HObject ho_XImageScaled, HObject ho_YImageScaled, HObject ho_ZImageScaled,
HObject ho_ZeroRegion, HObject ho_TmpRegion, HObject* ho_Out_ROI, HTuple hv_Direc,
HTuple hv_Height);
void Cal_Glue_Height(HObject ho_in_ImageH, HTuple hv_in_xResolution, HTuple hv_in_yResolution,
HTuple hv_in_zResolution, HTuple* hv_out_Height1, HTuple* hv_out_Height2, HTuple* hv_out_Point,
HTuple* hv_out_Flag, HTuple* hv_out_Ngtails);
void Cal_Height1(HObject ho_in_XImageScaled, HObject ho_in_YImageScaled, HObject ho_in_ZImageScaled,
HObject ho_in_InspectionRegion, HTuple hv_in_PlaneParamValue, HTuple* hv_out_PaintRows,
HTuple* hv_out_PaintColumns, HTuple* hv_out_Height, HTuple* hv_out_Flag);
void Cal_Height2(HObject ho_in_XImageScaled, HObject ho_in_YImageScaled, HObject ho_in_ZImageScaled,
HObject ho_in_InspectionRegion, HObject ho_in_ProductRegion, HTuple hv_in_PlaneParamValue,
HTuple* hv_out_PaintRows, HTuple* hv_out_PaintColumns, HTuple* hv_out_Height,
HTuple* hv_out_Flag);
void Cal_Height3(HObject ho_in_XImageScaled, HObject ho_in_YImageScaled, HObject ho_in_ZImageScaled,
HObject ho_in_innerEdge, HObject ho_in_outerEdge, HObject ho_in_HYRegion, HTuple* hv_out_Height,
HTuple* hv_out_Flag);
void Get_InGlueRegion(HObject ho_in_XImageScaled, HObject ho_in_YImageScaled, HObject ho_in_ZImageScaled,
HObject ho_in_ZeroRegion, HObject* ho_in_EdgeRegion, HTuple hv_in_Height, HTuple hv_in_PlaneParamValue,
HTuple hv_in_Direc, HTuple* hv_out_Flag, HTuple* hv_out_Ngtails);
void Get_OutGlueRegion(HObject ho_in_XImageScaled, HObject ho_in_YImageScaled, HObject ho_in_ZImageScaled,
HObject ho_in_ZeroRegion, HObject* ho_out_GlueRegion, HTuple hv_in_Height, HTuple hv_in_Direc,
HTuple hv_in_PlaneParamValue, HTuple* hv_out_Flag, HTuple* hv_out_Ngtails);
namespace HDevExportCpp
{
// Parallel execution wrapper for Get_OutGlueRegion(...)
static void* _hcppthread_Get_OutGlueRegion(void* hcthread);
// Parallel execution wrapper for Get_InGlueRegion(...)
static void* _hcppthread_Get_InGlueRegion(void* hcthread);
// Parallel execution wrapper for Cal_Height1(...)
static void* _hcppthread_Cal_Height1(void* hcthread);
// Parallel execution wrapper for Cal_Height2(...)
static void* _hcppthread_Cal_Height2(void* hcthread);
// Parallel execution wrapper for Cal_Height3(...)
static void* _hcppthread_Cal_Height3(void* hcthread);
}
void Get_ROI1(HObject ho_XImageScaled, HObject ho_YImageScaled, HObject ho_ZImageScaled,
HObject ho_ZeroRegion, HObject ho_TmpRegion, HObject* ho_Out_ROI, HTuple hv_Direc,
HTuple hv_Height)
{
// Local iconic variables
HObject ho_ImageReduced1, ho_ImageReduced2, ho_ImageReduced3;
HObject ho_Region2, ho_RegionOpening, ho_ConnectedRegions3;
HObject ho_Regions;
// Local control variables
HTuple hv_Debug, hv_ObjectModel3D, hv_ParFitting;
HTuple hv_ValFitting, hv_ObjectModelPlane, hv_GenParamValue;
HTuple hv_A, hv_B, hv_C, hv_D, hv_WindowHandleOut, hv_PoseOut;
HTuple hv_Rows, hv_Columns, hv_X, hv_Y, hv_Z, hv_Dist;
HTuple hv_Com_Tuple, hv_Prod_X, hv_Prod_Y, hv_Com_Tuple2;
HTuple hv_Com_Tuple1, hv_And, hv_Exception;
//**********************************************************************
//Function Name: Get_ROI1
//Author: 胡汉兵,
//Description: 获取有效的检测面,
//Input:
// XImageScaled, X向图像
// YImageScaled, Y向图像
// ZImageScaled, Z向图像
// Zero_Region, 基准平面的区域,
// InspectioRegion, 提取有效的检测面的Region
// Direc, 挑选极柱面的方向, 'Pos':极柱面上+ 'Neg': 极柱面下- 'all': 极柱上下范围内的点集,
// Height, 挑选极柱面的高度值(mm)
//output: Out_ROI, 挑选的ROI区域,
//verion change log: 胡汉兵 2023-09-07 V1.1 Create new function
// 胡汉兵 2023-11-01 V1.2 新增面上下范围内的点集,
//
//**********************************************************************
hv_Debug = 0;
try
{
ReduceDomain(ho_XImageScaled, ho_ZeroRegion, &ho_ImageReduced1);
ReduceDomain(ho_YImageScaled, ho_ZeroRegion, &ho_ImageReduced2);
ReduceDomain(ho_ZImageScaled, ho_ZeroRegion, &ho_ImageReduced3);
XyzToObjectModel3d(ho_ImageReduced1, ho_ImageReduced2, ho_ImageReduced3, &hv_ObjectModel3D);
//拟合平面,
hv_ParFitting.Clear();
hv_ParFitting[0] = "primitive_type";
hv_ParFitting[1] = "fitting_algorithm";
hv_ParFitting[2] = "output_xyz_mapping";
hv_ValFitting.Clear();
hv_ValFitting[0] = "plane";
hv_ValFitting[1] = "least_squares_huber";
hv_ValFitting[2] = "false";
//利用产品的边缘拟合平面,
FitPrimitivesObjectModel3d(hv_ObjectModel3D, hv_ParFitting, hv_ValFitting, &hv_ObjectModelPlane);
GetObjectModel3dParams(hv_ObjectModelPlane, "primitive_parameter", &hv_GenParamValue);
hv_A = ((const HTuple&)hv_GenParamValue)[0];
hv_B = ((const HTuple&)hv_GenParamValue)[1];
hv_C = ((const HTuple&)hv_GenParamValue)[2];
hv_D = ((const HTuple&)hv_GenParamValue)[3];
GetRegionPoints(ho_TmpRegion, &hv_Rows, &hv_Columns);
GetGrayval(ho_XImageScaled, hv_Rows, hv_Columns, &hv_X);
GetGrayval(ho_YImageScaled, hv_Rows, hv_Columns, &hv_Y);
GetGrayval(ho_ZImageScaled, hv_Rows, hv_Columns, &hv_Z);
hv_Dist = (((hv_A * hv_X) + (hv_B * hv_Y)) + (hv_C * hv_Z)) - hv_D;
if (0 != (hv_C < 0))
{
hv_Dist = hv_Dist * -1;
}
if (0 != (hv_Direc == HTuple("Pos")))
{
TupleGreaterElem(hv_Dist, hv_Height, &hv_Com_Tuple);
TupleMult(hv_Rows, hv_Com_Tuple, &hv_Prod_X);
TupleMult(hv_Columns, hv_Com_Tuple, &hv_Prod_Y);
GenRegionPoints(&ho_Region2, hv_Prod_X, hv_Prod_Y);
ClosingCircle(ho_Region2, &ho_RegionOpening, 3.5);
Connection(ho_RegionOpening, &ho_ConnectedRegions3);
SelectShape(ho_ConnectedRegions3, &ho_Regions, "area", "and", 2, 9999999999999);
Union1(ho_Regions, &(*ho_Out_ROI));
}
else if (0 != (hv_Direc == HTuple("Neg")))
{
hv_Height = hv_Height * -1;
TupleLessElem(hv_Dist, hv_Height, &hv_Com_Tuple);
TupleMult(hv_Rows, hv_Com_Tuple, &hv_Prod_X);
TupleMult(hv_Columns, hv_Com_Tuple, &hv_Prod_Y);
GenRegionPoints(&ho_Region2, hv_Prod_X, hv_Prod_Y);
ClosingCircle(ho_Region2, &ho_RegionOpening, 3.5);
Connection(ho_RegionOpening, &ho_ConnectedRegions3);
SelectShape(ho_ConnectedRegions3, &ho_Regions, "area", "and", 2, 9999999999999);
Union1(ho_Regions, &(*ho_Out_ROI));
}
else if (0 != (hv_Direc == HTuple("all")))
{
TupleLessElem(hv_Dist, hv_Height, &hv_Com_Tuple2);
TupleGreaterElem(hv_Dist, -hv_Height, &hv_Com_Tuple1);
TupleAnd(hv_Com_Tuple1, hv_Com_Tuple2, &hv_And);
TupleMult(hv_Rows, hv_And, &hv_Prod_X);
TupleMult(hv_Columns, hv_And, &hv_Prod_Y);
GenRegionPoints(&ho_Region2, hv_Prod_X, hv_Prod_Y);
ClosingCircle(ho_Region2, &ho_RegionOpening, 3.5);
Connection(ho_RegionOpening, &ho_ConnectedRegions3);
SelectShape(ho_ConnectedRegions3, &ho_Regions, "area", "and", 2, 9999999999999);
Union1(ho_Regions, &(*ho_Out_ROI));
}
}
// catch (Exception)
catch (HException& HDevExpDefaultException)
{
HDevExpDefaultException.ToHTuple(&hv_Exception);
}
return;
}
void Cal_Glue_Height(HObject ho_in_ImageH, HTuple hv_in_xResolution, HTuple hv_in_yResolution,
HTuple hv_in_zResolution, HTuple* hv_out_Height1, HTuple* hv_out_Height2, HTuple* hv_out_Point,
HTuple* hv_out_Flag, HTuple* hv_out_Ngtails)
{
// Local iconic variables
HObject ho_ImageConverted, ho_YImageSurface;
HObject ho_XImageSurface, ho_in_XImageScaled, ho_in_YImageScaled;
HObject ho_in_ZImageScaled, ho_Regions, ho_Domain, ho_ProductRegion;
HObject ho_ImageReduced, ho_RegionClosing, ho_RegionFillUp3;
HObject ho_TempRegion, ho_RegionErosion3, ho_ZeroRegion;
HObject ho_ImageReduced1, ho_ImageReduced2, ho_ImageReduced3;
HObject ho_in_ZeroRegion1, ho_out_GlueRegion, ho_in_ZeroRegion2;
HObject ho_in_EdgeRegion, ho_out_EdgeRegion, ho_InspectionRegion;
HObject ho_RegionErosion1, ho_RegionDilation2, ho_in_InspectionRegion3;
HObject ho_in_InspectionRegion4, ho_in_ProductRegion, ho_in_outerEdge;
HObject ho_in_innerEdge, ho_in_HYRegion, ho_RegionDifference;
HObject ho_Partitioned, ho_SortedRegions, ho_ObjectSelected;
HObject ho_RegionTrans, ho_Region, ho_Cross, ho_RegionIntersection;
// Local control variables
HTuple hv_Debug, hv_Type, hv_Width2, hv_Height2;
HTuple hv_ObjectModel3D, hv_ParFitting, hv_ValFitting;
HTuple hv_ObjectModelPlane, hv_PlaneParamValue, hv_Thread;
HTuple hv_in_Direc1, hv_in_Height1, hv_in_PlaneParamValue1;
HTuple hv_Thread1, hv_out_Flag1, hv_out_Ngtails1, hv_in_Direc2;
HTuple hv_in_Height2, hv_in_PlaneParamValue2, hv_Thread2;
HTuple hv_out_Flag2, hv_out_Ngtails2, hv_Area2, hv_Row2;
HTuple hv_Column2, hv_Area3, hv_Row3, hv_Column3, hv_Area;
HTuple hv_Row, hv_Column, hv_Area1, hv_Row1, hv_Column1;
HTuple hv_in_PlaneParamValue3, hv_Thread3, hv_out_PaintRows1;
HTuple hv_out_PaintColumns1, hv_out_Height3, hv_out_Flag3;
HTuple hv_in_PlaneParamValue4, hv_Thread4, hv_out_PaintRows2;
HTuple hv_out_PaintColumns2, hv_out_Height4, hv_out_Flag4;
HTuple hv_Thread5, hv_out_Height, hv_out_Flag5, hv_Value;
HTuple hv_Area4, hv_Row4, hv_Column4, hv_Exception;
// +++ Threading variables
HDevThread* hcppthread_handle;
HDevThreadContext hcppthread_context; // <-signals begin of procedure
//**********************************************************************
//Function Name: Cal_Glue_Height
//Author:
//Description: 基于基准面,计算点胶的高度(mm)
//Input: in_ImageH, 深度图像(tif),
// in_xResolution, X向精度(mm/pix)
// in_yResolution, y向精度(mm/pix)
// in_zResolution, z向精度(mm/pix)
//output: out_Height, 点胶高度(mm), 一般情况下,胶的高度高于电池边缘面的区域, 才有高度输出.
// out_Point: 胶的高点和基准面的高点坐标(y,x)
// out_Flag, 算法执行的标志位; 1, 算法执行成功 2, 算法执行错误
// out_Ngtails, 算法执行信息,
//verion change log: 胡汉兵 2024-06-15 V1.1 Create new function.
// 胡汉兵 2024-06-20 V1.2 软件端执行结果异常, 算法直接强行赋值为0.
// 胡汉兵 2024-07-04 V1.3 现场输出电池的最高点位置,便于客户基于当前点位进行测量.
// 胡汉兵 2024-07-09 V1.4 使用原始图像进行高度计算,
// 胡汉兵 2024-07-16 V1.5 保护, 防止程序崩溃时, 无结果图输出.
// 胡汉兵 2024-09-11 V1.6 代码优化, 中间
//**********************************************************************
hv_Debug = 0;
(*hv_out_Height1) = -99;
(*hv_out_Height2) = -99;
(*hv_out_Flag) = 2;
(*hv_out_Ngtails) = "OK";
(*hv_out_Point) = HTuple();
try
{
if (HDevWindowStack::IsOpen())
SetDraw(HDevWindowStack::GetActive(), "margin");
GetImageType(ho_in_ImageH, &hv_Type);
if (0 != (hv_Type != HTuple("real")))
{
ConvertImageType(ho_in_ImageH, &ho_ImageConverted, "real");
}
else
{
ho_ImageConverted = ho_in_ImageH;
}
GetImageSize(ho_ImageConverted, &hv_Width2, &hv_Height2);
GenImageSurfaceFirstOrder(&ho_YImageSurface, "real", 1, 0, 0, 0, 0, hv_Width2,
hv_Height2);
GenImageSurfaceFirstOrder(&ho_XImageSurface, "real", 0, 1, 0, 0, 0, hv_Width2,
hv_Height2);
ScaleImage(ho_XImageSurface, &ho_in_XImageScaled, hv_in_xResolution, 0);
ScaleImage(ho_YImageSurface, &ho_in_YImageScaled, hv_in_xResolution, 0);
ScaleImage(ho_ImageConverted, &ho_in_ZImageScaled, hv_in_zResolution, 0);
Threshold(ho_ImageConverted, &ho_Regions, 0, 0);
GetDomain(ho_in_ImageH, &ho_Domain);
Difference(ho_Domain, ho_Regions, &ho_ProductRegion);
ReduceDomain(ho_in_ZImageScaled, ho_ProductRegion, &ho_ImageReduced);
if (0 != hv_Debug)
{
//reduce_domain (in_XImageScaled, ProductRegion, ImageReduced4)
//reduce_domain (in_YImageScaled, ProductRegion, ImageReduced5)
//xyz_to_object_model_3d (ImageReduced4, ImageReduced5, ImageReduced, ObjectModel3D)
//dev_get_window (WindowHandle)
//visualize_object_model_3d (WindowHandle, [ObjectModel3D], [], [], ['lut','intensity','disp_pose'], ['hsi','coord_z', 'true'], ['展示拟合的平面'], '', '', Pose)
}
//******************************************************
//step2: 获取基准面区域(新代码不使用这个区域的Region)
ClosingCircle(ho_ProductRegion, &ho_RegionClosing, 15);
FillUp(ho_RegionClosing, &ho_RegionFillUp3);
ErosionRectangle1(ho_RegionFillUp3, &ho_TempRegion, 1, 220);
//dev_display (ImageReduced)
//dev_display (TempRegion)
//区间进行内缩, 获取基准的端面.
ErosionCircle(ho_TempRegion, &ho_RegionErosion3, 80);
Difference(ho_TempRegion, ho_RegionErosion3, &ho_ZeroRegion);
//dev_display (ImageReduced)
//dev_display (ZeroRegion)
//******************************************************
//共用的
//优化, 调整基准面
ReduceDomain(ho_in_XImageScaled, ho_ZeroRegion, &ho_ImageReduced1);
ReduceDomain(ho_in_YImageScaled, ho_ZeroRegion, &ho_ImageReduced2);
ReduceDomain(ho_in_ZImageScaled, ho_ZeroRegion, &ho_ImageReduced3);
XyzToObjectModel3d(ho_ImageReduced1, ho_ImageReduced2, ho_ImageReduced3, &hv_ObjectModel3D);
//拟合平面,
hv_ParFitting.Clear();
hv_ParFitting[0] = "primitive_type";
hv_ParFitting[1] = "fitting_algorithm";
hv_ParFitting[2] = "output_xyz_mapping";
hv_ValFitting.Clear();
hv_ValFitting[0] = "plane";
hv_ValFitting[1] = "least_squares_huber";
hv_ValFitting[2] = "false";
//利用产品的边缘拟合平面,
FitPrimitivesObjectModel3d(hv_ObjectModel3D, hv_ParFitting, hv_ValFitting, &hv_ObjectModelPlane);
GetObjectModel3dParams(hv_ObjectModelPlane, "primitive_parameter", &hv_PlaneParamValue);
if (0 != 0)
{
hv_Thread = HTuple();
//线程一,
hv_in_Direc1 = "Neg";
hv_in_Height1 = 1.0;
hv_in_PlaneParamValue1 = hv_PlaneParamValue;
ho_in_ZeroRegion1 = ho_ZeroRegion;
// Create a thread instance
hcppthread_handle = new HDevThread(hcppthread_context,
(void*)HDevExportCpp::_hcppthread_Get_OutGlueRegion, 7, 3);
// Set thread procedure call arguments
hcppthread_handle->SetInputIconicParamObject(0, ho_in_XImageScaled);
hcppthread_handle->SetInputIconicParamObject(1, ho_in_YImageScaled);
hcppthread_handle->SetInputIconicParamObject(2, ho_in_ZImageScaled);
hcppthread_handle->SetInputIconicParamObject(3, ho_in_ZeroRegion1);
hcppthread_handle->SetInputCtrlParamTuple(4, hv_in_Height1);
hcppthread_handle->SetInputCtrlParamTuple(5, hv_in_Direc1);
hcppthread_handle->SetInputCtrlParamTuple(6, hv_in_PlaneParamValue1);
hcppthread_handle->BindOutputIconicParamObject(0, 0, &ho_out_GlueRegion);
hcppthread_handle->BindOutputCtrlParamTuple(1, 0, &hv_out_Flag1);
hcppthread_handle->BindOutputCtrlParamTuple(2, 0, &hv_out_Ngtails1);
// Start proc line in thread
hcppthread_handle->ParStart(&hv_Thread1);
hv_Thread = hv_Thread.TupleConcat(hv_Thread1);
//线程二,
hv_in_Direc2 = "all";
hv_in_Height2 = 0.15;
hv_in_PlaneParamValue2 = hv_PlaneParamValue;
ho_in_ZeroRegion2 = ho_ZeroRegion;
// Create a thread instance
hcppthread_handle = new HDevThread(hcppthread_context,
(void*)HDevExportCpp::_hcppthread_Get_InGlueRegion, 7, 3);
// Set thread procedure call arguments
hcppthread_handle->SetInputIconicParamObject(0, ho_in_XImageScaled);
hcppthread_handle->SetInputIconicParamObject(1, ho_in_YImageScaled);
hcppthread_handle->SetInputIconicParamObject(2, ho_in_ZImageScaled);
hcppthread_handle->SetInputIconicParamObject(3, ho_in_ZeroRegion2);
hcppthread_handle->SetInputCtrlParamTuple(4, hv_in_Height2);
hcppthread_handle->SetInputCtrlParamTuple(5, hv_in_PlaneParamValue2);
hcppthread_handle->SetInputCtrlParamTuple(6, hv_in_Direc2);
hcppthread_handle->BindOutputIconicParamObject(0, 0, &ho_in_EdgeRegion);
hcppthread_handle->BindOutputCtrlParamTuple(1, 0, &hv_out_Flag2);
hcppthread_handle->BindOutputCtrlParamTuple(2, 0, &hv_out_Ngtails2);
// Start proc line in thread
hcppthread_handle->ParStart(&hv_Thread2);
hv_Thread = hv_Thread.TupleConcat(hv_Thread2);
HDevThread::ParJoin(hv_Thread);
}
if (0 != 1)
{
//线程一,
hv_in_Direc1 = "Neg";
hv_in_Height1 = 1.0;
hv_in_PlaneParamValue1 = hv_PlaneParamValue;
ho_in_ZeroRegion1 = ho_ZeroRegion;
Get_OutGlueRegion(ho_in_XImageScaled, ho_in_YImageScaled, ho_in_ZImageScaled,
ho_in_ZeroRegion1, &ho_out_GlueRegion, hv_in_Height1, hv_in_Direc1, hv_in_PlaneParamValue1,
&hv_out_Flag1, &hv_out_Ngtails1);
//线程二,
hv_in_Direc2 = "all";
hv_in_Height2 = 0.15;
hv_in_PlaneParamValue2 = hv_PlaneParamValue;
ho_in_ZeroRegion2 = ho_ZeroRegion;
Get_InGlueRegion(ho_in_XImageScaled, ho_in_YImageScaled, ho_in_ZImageScaled,
ho_in_ZeroRegion2, &ho_in_EdgeRegion, hv_in_Height2, hv_in_PlaneParamValue2,
hv_in_Direc2, &hv_out_Flag2, &hv_out_Ngtails2);
}
if (0 != (HTuple(hv_out_Flag1 == 1).TupleAnd(hv_out_Flag2 == 1)))
{
//dev_display (ImageReduced)
//dev_display (out_GlueRegion)
//dev_display (in_EdgeRegion)
ho_out_EdgeRegion = ho_out_GlueRegion;
}
else
{
(*hv_out_Ngtails) = "点胶区域提取失败";
return;
}
AreaCenter(ho_in_EdgeRegion, &hv_Area2, &hv_Row2, &hv_Column2);
if (0 != (hv_Area2 < 10000))
{
ho_InspectionRegion = ho_out_GlueRegion;
AreaCenter(ho_out_GlueRegion, &hv_Area3, &hv_Row3, &hv_Column3);
GenCircle(&ho_in_EdgeRegion, hv_Row3, hv_Column3, 10);
}
else
{
//目前按照这个比例进行调整, 计算区域的高度.
ErosionRectangle1(ho_in_EdgeRegion, &ho_RegionErosion1, 345, 40);
//dev_display (ImageReduced)
//dev_display (RegionErosion1)
//dev_display (in_EdgeRegion)
AreaCenter(ho_RegionErosion1, &hv_Area, &hv_Row, &hv_Column);
if (0 != (hv_Area == 0))
{
AreaCenter(ho_in_EdgeRegion, &hv_Area1, &hv_Row1, &hv_Column1);
GenCircle(&ho_RegionErosion1, hv_Row1, hv_Column1, 3);
}
DilationRectangle1(ho_out_GlueRegion, &ho_RegionDilation2, 160, 20);
//dev_display (ImageReduced)
//dev_display (RegionDilation2)
Difference(ho_RegionDilation2, ho_RegionErosion1, &ho_InspectionRegion);
}
//******************************************************
if (HDevWindowStack::IsOpen())
DispObj(ho_ImageReduced, HDevWindowStack::GetActive());
if (HDevWindowStack::IsOpen())
DispObj(ho_InspectionRegion, HDevWindowStack::GetActive());
//__________________________________________________________________________
if (0 != 0)
{
hv_Thread = HTuple();
//线程三
ho_in_InspectionRegion3 = ho_InspectionRegion;
hv_in_PlaneParamValue3 = hv_PlaneParamValue;
// Create a thread instance
hcppthread_handle = new HDevThread(hcppthread_context,
(void*)HDevExportCpp::_hcppthread_Cal_Height1, 5, 4);
// Set thread procedure call arguments
hcppthread_handle->SetInputIconicParamObject(0, ho_in_XImageScaled);
hcppthread_handle->SetInputIconicParamObject(1, ho_in_YImageScaled);
hcppthread_handle->SetInputIconicParamObject(2, ho_in_ZImageScaled);
hcppthread_handle->SetInputIconicParamObject(3, ho_in_InspectionRegion3);
hcppthread_handle->SetInputCtrlParamTuple(4, hv_in_PlaneParamValue3);
hcppthread_handle->BindOutputCtrlParamTuple(0, 0, &hv_out_PaintRows1);
hcppthread_handle->BindOutputCtrlParamTuple(1, 0, &hv_out_PaintColumns1);
hcppthread_handle->BindOutputCtrlParamTuple(2, 0, &hv_out_Height3);
hcppthread_handle->BindOutputCtrlParamTuple(3, 0, &hv_out_Flag3);
// Start proc line in thread
hcppthread_handle->ParStart(&hv_Thread3);
hv_Thread = hv_Thread.TupleConcat(hv_Thread3);
//线程四,
//in_InspectionRegion4 := out_ROI1
ho_in_InspectionRegion4 = ho_ProductRegion;
hv_in_PlaneParamValue4 = hv_PlaneParamValue;
ho_in_ProductRegion = ho_ProductRegion;
// Create a thread instance
hcppthread_handle = new HDevThread(hcppthread_context,
(void*)HDevExportCpp::_hcppthread_Cal_Height2, 6, 4);
// Set thread procedure call arguments
hcppthread_handle->SetInputIconicParamObject(0, ho_in_XImageScaled);
hcppthread_handle->SetInputIconicParamObject(1, ho_in_YImageScaled);
hcppthread_handle->SetInputIconicParamObject(2, ho_in_ZImageScaled);
hcppthread_handle->SetInputIconicParamObject(3, ho_in_InspectionRegion4);
hcppthread_handle->SetInputIconicParamObject(4, ho_in_ProductRegion);
hcppthread_handle->SetInputCtrlParamTuple(5, hv_in_PlaneParamValue4);
hcppthread_handle->BindOutputCtrlParamTuple(0, 0, &hv_out_PaintRows2);
hcppthread_handle->BindOutputCtrlParamTuple(1, 0, &hv_out_PaintColumns2);
hcppthread_handle->BindOutputCtrlParamTuple(2, 0, &hv_out_Height4);
hcppthread_handle->BindOutputCtrlParamTuple(3, 0, &hv_out_Flag4);
// Start proc line in thread
hcppthread_handle->ParStart(&hv_Thread4);
ho_in_outerEdge = ho_out_GlueRegion;
ho_in_innerEdge = ho_in_EdgeRegion;
ho_in_HYRegion = ho_RegionErosion1;
// Create a thread instance
hcppthread_handle = new HDevThread(hcppthread_context,
(void*)HDevExportCpp::_hcppthread_Cal_Height3, 6, 2);
// Set thread procedure call arguments
hcppthread_handle->SetInputIconicParamObject(0, ho_in_XImageScaled);
hcppthread_handle->SetInputIconicParamObject(1, ho_in_YImageScaled);
hcppthread_handle->SetInputIconicParamObject(2, ho_in_ZImageScaled);
hcppthread_handle->SetInputIconicParamObject(3, ho_in_innerEdge);
hcppthread_handle->SetInputIconicParamObject(4, ho_in_outerEdge);
hcppthread_handle->SetInputIconicParamObject(5, ho_in_HYRegion);
hcppthread_handle->BindOutputCtrlParamTuple(0, 0, &hv_out_Height);
hcppthread_handle->BindOutputCtrlParamTuple(1, 0, &hv_out_Flag5);
// Start proc line in thread
hcppthread_handle->ParStart(&hv_Thread5);
hv_Thread = (hv_Thread.TupleConcat(hv_Thread4)).TupleConcat(hv_Thread5);
HDevThread::ParJoin(hv_Thread);
}
if (0 != 1)
{
//计算点胶高于基准面的高点
ho_in_InspectionRegion3 = ho_InspectionRegion;
hv_in_PlaneParamValue3 = hv_PlaneParamValue;
Cal_Height1(ho_in_XImageScaled, ho_in_YImageScaled, ho_in_ZImageScaled, ho_in_InspectionRegion3,
hv_in_PlaneParamValue3, &hv_out_PaintRows1, &hv_out_PaintColumns1, &hv_out_Height3,
&hv_out_Flag3);
//计算基准面的高点,
ho_in_InspectionRegion4 = ho_ProductRegion;
hv_in_PlaneParamValue4 = hv_PlaneParamValue;
ho_in_ProductRegion = ho_ProductRegion;
Cal_Height2(ho_in_XImageScaled, ho_in_YImageScaled, ho_in_ZImageScaled, ho_in_InspectionRegion4,
ho_in_ProductRegion, hv_in_PlaneParamValue4, &hv_out_PaintRows2, &hv_out_PaintColumns2,
&hv_out_Height4, &hv_out_Flag4);
//特殊区域补偿,
//dev_display (InspectionRegion)
Difference(ho_out_GlueRegion, ho_in_EdgeRegion, &ho_RegionDifference);
//dev_display (ImageReduced)
//dev_display (RegionDifference)
RegionFeatures(ho_RegionDifference, (HTuple("width").Append("height")), &hv_Value);
//region_features (RegionDifference, 'width', Value1)
PartitionRectangle(ho_RegionDifference, &ho_Partitioned, HTuple(hv_Value[0]) / 3.0,
HTuple(hv_Value[1]) + 30);
SortRegion(ho_Partitioned, &ho_SortedRegions, "character", "true", "column");
SelectObj(ho_SortedRegions, &ho_ObjectSelected, 2);
ShapeTrans(ho_ObjectSelected, &ho_RegionTrans, "rectangle2");
GenRegionPoints(&ho_Region, hv_out_PaintRows1, hv_out_PaintColumns1);
GenCrossContourXld(&ho_Cross, hv_out_PaintRows1, hv_out_PaintColumns1, 6, HTuple(45).TupleRad());
//dev_display (RegionTrans)
//dev_display (Cross)
Intersection(ho_Region, ho_RegionTrans, &ho_RegionIntersection);
AreaCenter(ho_RegionIntersection, &hv_Area4, &hv_Row4, &hv_Column4);
if (0 != (HTuple(hv_out_Height3 > hv_out_Height4).TupleAnd(hv_Area4 > 0)))
{
hv_out_Height3 += 0.05;
}
//stop ()
//调试用,
//if (0)
//erosion_rectangle1 (in_InspectionRegion4, RegionErosion, 10, 70)
//* difference (in_InspectionRegion4, RegionErosion, RegionDifference)
//* shape_trans (RegionDifference, RegionTrans, 'rectangle1')
//* erosion_rectangle1 (RegionTrans, RegionErosion1, 350, 1)
//* intersection (RegionDifference, RegionErosion1, InspectionRegion11)
//* connection (InspectionRegion11, ConnectedRegions)
//* select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 999, 999999999)
//* union1 (SelectedRegions, RegionUnion)
//dev_display (ImageReduced)
//* dev_display (RegionUnion)
//* reduce_domain (in_XImageScaled, RegionUnion, ImageReduced1)
//* reduce_domain (in_YImageScaled, RegionUnion, ImageReduced2)
//* reduce_domain (in_ZImageScaled, RegionUnion, ImageReduced3)
//xyz_to_object_model_3d (ImageReduced1, ImageReduced2, ImageReduced3, ObjectModel3D)
//ParFitting := ['primitive_type', 'fitting_algorithm', 'output_xyz_mapping']
//ValFitting := ['plane','least_squares_huber', 'false']
//利用产品的边缘拟合平面,
//fit_primitives_object_model_3d (ObjectModel3D, ParFitting, ValFitting, ObjectModelPlane)
//get_object_model_3d_params (ObjectModelPlane, 'primitive_parameter', PlaneParamValue)
//if (1)
//dev_get_window (WindowHandle)
//reduce_domain (in_XImageScaled, InspectionRegion, ImageReduced4)
//reduce_domain (in_YImageScaled, InspectionRegion, ImageReduced5)
//reduce_domain (in_ZImageScaled, InspectionRegion, ImageReduced6)
//* xyz_to_object_model_3d (ImageReduced4, ImageReduced5, ImageReduced6, ObjectModel3D1)
//* visualize_object_model_3d (WindowHandle, [ObjectModel3D1,ObjectModelPlane], [], [], ['lut','intensity','disp_pose'], ['hsi','coord_z', 'true'], ['展示拟合的平面'], '', '', Pose)
//endif
//endif
ho_in_outerEdge = ho_out_GlueRegion;
ho_in_innerEdge = ho_in_EdgeRegion;
ho_in_HYRegion = ho_RegionErosion1;
Cal_Height3(ho_in_XImageScaled, ho_in_YImageScaled, ho_in_ZImageScaled, ho_in_innerEdge,
ho_in_outerEdge, ho_in_HYRegion, &hv_out_Height, &hv_out_Flag5);
}
if (0 != (HTuple(HTuple(hv_out_Flag3 == 1).TupleAnd(hv_out_Flag4 == 1)).TupleAnd(hv_out_Flag5 == 1)))
{
(*hv_out_Height1) = hv_out_Height3 - hv_out_Height4;
(*hv_out_Height2) = hv_out_Height;
(*hv_out_Point).Clear();
(*hv_out_Point).Append(hv_out_PaintRows1);
(*hv_out_Point).Append(hv_out_PaintColumns1);
(*hv_out_Point).Append(hv_out_PaintRows2);
(*hv_out_Point).Append(hv_out_PaintColumns2);
(*hv_out_Flag) = 1;
}
if (0 != 1)
{
//高点和低点的在该处进行打印.
//gen_cross_contour_xld (Cross1, out_PaintRows1*zoomFactor, out_PaintColumns1, 40, rad(45))
//gen_cross_contour_xld (Cross2, out_PaintRows2*zoomFactor, out_PaintColumns2, 40, rad(45))
//get_contour_xld (Cross1, Row4, Col4)
//get_contour_xld (Cross2, Row5, Col5)
//gen_region_polygon (Region1, Row4, Col4)
//gen_region_polygon (Region2, Row5, Col5)
//dilation_circle (Region1, RegionDilation1, 1)
//dilation_circle (Region2, RegionDilation2, 1)
//union2 (RegionDilation1, RegionDilation2, RegionUnion)
//overpaint_region (out_DisplayImage, RegionUnion, [0,0,255], 'fill')
}
//__________________________________________________________________________
//***********************************************************************
//__________________________________________________________________________________
//基于边缘的扣边区域, 获取点胶的高度;
//dev_display (ImageReduced)
//dev_display (ProductRegion)
//erosion_circle (ProductRegion, RegionErosion2, 10)
//erosion_circle (RegionErosion2, RegionErosion4, 25)
//difference (RegionErosion2, RegionErosion4, RegionDifference)
//dev_display (ImageReduced)
//dev_display (RegionDifference)
//erosion_rectangle1 (ProductRegion, RegionErosion5, 180, 1)
//dilation_rectangle1 (RegionErosion5, RegionDilation, 1, 100)
//intersection (RegionDifference, RegionDilation, BaseRegion)
//dev_display (ImageReduced)
//dev_display (BaseRegion)
//reduce_domain (in_XImageScaled, BaseRegion, ImageReduced4)
//reduce_domain (in_YImageScaled, BaseRegion, ImageReduced5)
//reduce_domain (in_ZImageScaled, BaseRegion, ImageReduced6)
//xyz_to_object_model_3d (ImageReduced4, ImageReduced5, ImageReduced6, ObjectModel3D)
//拟合平面,
//ParFitting := ['primitive_type', 'fitting_algorithm', 'output_xyz_mapping']
//ValFitting := ['plane','least_squares_huber', 'false']
//利用产品的边缘拟合平面,
//fit_primitives_object_model_3d (ObjectModel3D, ParFitting, ValFitting, ObjectModelPlane)
//get_object_model_3d_params (ObjectModelPlane, 'primitive_parameter', PlaneParamValue)
//A := PlaneParamValue[0]
//B := PlaneParamValue[1]
//C := PlaneParamValue[2]
//D := PlaneParamValue[3]
//get_region_points (InspectionRegion, Rows, Columns)
//get_grayval (in_XImageScaled, Rows, Columns, X)
//get_grayval (in_YImageScaled, Rows, Columns, Y)
//get_grayval (in_ZImageScaled, Rows, Columns, Z)
//Dist := A* X + B* Y + C * Z - D
//if (C < 0)
//Dist := -Dist
//endif
//tuple_sort (Dist, Sorted)
//******************************3***************************
//算法寻找高点, 并寻找最高点坐标
//tuple_greater_elem (Dist, 0, PosGreater)
//寻找大于0的点坐标,
//tuple_find (PosGreater, 1, PosIndices)
//if (|PosIndices|<300 or PosIndices=-1)
//tuple_less_elem (Dist, 0, NegLess)
//tuple_find (NegLess, 1, NegIndices)
//tuple_select (Dist, NegIndices, Selected2)
//该处需要对点集进行排序,
//tuple_sort (Selected2, Sorted1)
//end1 := int(|Sorted1|*0.95)
//tuple_select (Sorted1, end1, Selected3)
//tuple_find (Dist, Selected3, Indices)
//tuple_select (Rows, Indices, out_PaintRows)
//tuple_select (Columns, Indices, out_PaintColumns)
//out_Height1 := Selected3
//elseif (|PosIndices|>300)
//tuple_greater_elem (Dist, 0, PosGreater)
//tuple_find (PosGreater, 1, PosIndices)
//tuple_select (Dist, PosIndices, Selected3)
//tuple_sort (Selected3, Sorted2)
//end2 := int(|Sorted2|*0.95)
//tuple_select (Sorted2, end2, Selected4)
//tuple_find (Dist, Selected4, Indices)
//tuple_select (Rows, Indices, out_PaintRows)
//tuple_select (Columns, Indices, out_PaintColumns)
//out_Height1 := Selected4
//endif
//打印方式,
//if (1)
//高点和低点的在该处进行打印.
//gen_cross_contour_xld (Cross1, out_PaintRows*zoomFactor, out_PaintColumns, 40, rad(45))
//if (0)
//gen_cross_contour_xld (Cross11, out_PaintRows, out_PaintColumns, 40, rad(45))
//dev_display (ImageReduced)
//dev_display (Cross11)
//endif
//get_contour_xld (Cross1, Row4, Col4)
//gen_region_polygon (Region1, Row4, Col4)
//dilation_circle (Region1, RegionDilation1, 1)
//overpaint_region (out_DisplayImage, RegionDilation1, [0,0,255], 'fill')
//boundary (BaseRegion, RegionBorder2, 'inner')
//zoom_region (RegionBorder2, ZoomBaseRegion, 1, zoomFactor)
//overpaint_region (out_DisplayImage, ZoomBaseRegion, [255,255,0], 'fill')
//endif
//__________________________________________________________________________________
//difference (out_EdgeRegion, in_EdgeRegion, GlueRegion)
//boundary (GlueRegion, RegionBorder, 'inner')
//展示用的结果图,
//zoom_region (RegionBorder, ZoomGlueRegion, 1, zoomFactor)
//dilation_circle (ZoomGlueRegion, RegionDilation5, 1)
//overpaint_region (out_DisplayImage, RegionDilation5, [0,255,0], 'fill')
//boundary (InspectionRegion, RegionBorder1, 'inner')
//zoom_region (RegionBorder1, ZoomGlueRegion, 1, zoomFactor)
//overpaint_region (out_DisplayImage, ZoomGlueRegion, [255,0,0], 'fill')
//stop ()
}
// catch (Exception)
catch (HException& HDevExpDefaultException)
{
HDevExpDefaultException.ToHTuple(&hv_Exception);
//zoom_image_factor (in_ZImageScaled, ImageZoomed, 1, zoomFactor, 'constant')
//scale_image_max (ImageZoomed, ImageScaleMax1)
//copy_image (ImageScaleMax1, Image4)
//copy_image (ImageScaleMax1, Image5)
//copy_image (ImageScaleMax1, Image6)
//compose3 (Image4, Image5, Image6, out_DisplayImage)
(*hv_out_Flag) = 2;
(*hv_out_Ngtails) = "算法执行错误";
}
return;
}
void Cal_Height1(HObject ho_in_XImageScaled, HObject ho_in_YImageScaled, HObject ho_in_ZImageScaled,
HObject ho_in_InspectionRegion, HTuple hv_in_PlaneParamValue, HTuple* hv_out_PaintRows,
HTuple* hv_out_PaintColumns, HTuple* hv_out_Height, HTuple* hv_out_Flag)
{
// Local iconic variables
// Local control variables
HTuple hv_out_Ngtails, hv_A, hv_B, hv_C, hv_D;
HTuple hv_Rows, hv_Columns, hv_X, hv_Y, hv_Z, hv_Dist;
HTuple hv_Sorted, hv_PosGreater, hv_PosIndices, hv_NegLess;
HTuple hv_NegIndices, hv_Selected2, hv_Sorted1, hv_end1;
HTuple hv_Selected3, hv_Indices, hv_Sorted2, hv_Selected4;
HTuple hv_Exception;
(*hv_out_Height) = -99;
(*hv_out_Flag) = 0;
hv_out_Ngtails = "ok";
try
{
hv_A = ((const HTuple&)hv_in_PlaneParamValue)[0];
hv_B = ((const HTuple&)hv_in_PlaneParamValue)[1];
hv_C = ((const HTuple&)hv_in_PlaneParamValue)[2];
hv_D = ((const HTuple&)hv_in_PlaneParamValue)[3];
GetRegionPoints(ho_in_InspectionRegion, &hv_Rows, &hv_Columns);
GetGrayval(ho_in_XImageScaled, hv_Rows, hv_Columns, &hv_X);
GetGrayval(ho_in_YImageScaled, hv_Rows, hv_Columns, &hv_Y);
GetGrayval(ho_in_ZImageScaled, hv_Rows, hv_Columns, &hv_Z);
hv_Dist = (((hv_A * hv_X) + (hv_B * hv_Y)) + (hv_C * hv_Z)) - hv_D;
if (0 != (hv_C < 0))
{
hv_Dist = -hv_Dist;
}
TupleSort(hv_Dist, &hv_Sorted);
//******************************3***************************
//算法寻找高点, 并寻找最高点坐标
TupleGreaterElem(hv_Dist, 0, &hv_PosGreater);
//寻找大于0的点坐标,
TupleFind(hv_PosGreater, 1, &hv_PosIndices);
if (0 != (HTuple((hv_PosIndices.TupleLength()) < 100).TupleOr(hv_PosIndices == -1)))
{
TupleLessElem(hv_Dist, 0, &hv_NegLess);
TupleFind(hv_NegLess, 1, &hv_NegIndices);
TupleSelect(hv_Dist, hv_NegIndices, &hv_Selected2);
//该处需要对点集进行排序,
TupleSort(hv_Selected2, &hv_Sorted1);
hv_end1 = HTuple((hv_Sorted1.TupleLength()) * 0.95).TupleInt();
TupleSelect(hv_Sorted1, hv_end1, &hv_Selected3);
TupleFind(hv_Dist, hv_Selected3, &hv_Indices);
TupleSelect(hv_Rows, hv_Indices, &(*hv_out_PaintRows));
TupleSelect(hv_Columns, hv_Indices, &(*hv_out_PaintColumns));
(*hv_out_Height) = hv_Selected3;
}
else if (0 != ((hv_PosIndices.TupleLength()) > 100))
{
TupleGreaterElem(hv_Dist, 0, &hv_PosGreater);
TupleFind(hv_PosGreater, 1, &hv_PosIndices);
TupleSelect(hv_Dist, hv_PosIndices, &hv_Selected3);
//该处需要对点集进行排序,
TupleSort(hv_Selected3, &hv_Sorted2);
//end2 := int(|Sorted2|*0.99)
TupleSelect(hv_Sorted2, (hv_Sorted2.TupleLength()) - 10, &hv_Selected4);
TupleFind(hv_Dist, hv_Selected4, &hv_Indices);
TupleSelect(hv_Rows, hv_Indices, &(*hv_out_PaintRows));
TupleSelect(hv_Columns, hv_Indices, &(*hv_out_PaintColumns));
(*hv_out_Height) = hv_Selected4;
}
if (0 != ((*hv_out_Height) != -99))
{
(*hv_out_Flag) = 1;
}
}
// catch (Exception)
catch (HException& HDevExpDefaultException)
{
HDevExpDefaultException.ToHTuple(&hv_Exception);
hv_out_Ngtails = "算法错误";
}
return;
}
void Cal_Height2(HObject ho_in_XImageScaled, HObject ho_in_YImageScaled, HObject ho_in_ZImageScaled,
HObject ho_in_InspectionRegion, HObject ho_in_ProductRegion, HTuple hv_in_PlaneParamValue,
HTuple* hv_out_PaintRows, HTuple* hv_out_PaintColumns, HTuple* hv_out_Height,
HTuple* hv_out_Flag)
{
// Local iconic variables
HObject ho_RegionErosion, ho_RegionDifference;
HObject ho_RegionTrans, ho_RegionErosion1, ho_InspectionRegion;
// Local control variables
HTuple hv_out_Ngtails, hv_A, hv_B, hv_C, hv_D;
HTuple hv_Rows, hv_Columns, hv_X, hv_Y, hv_Z, hv_Dist;
HTuple hv_Sorted, hv_PosGreater, hv_PosIndices, hv_NegLess;
HTuple hv_NegIndices, hv_Selected2, hv_Sorted1, hv_end1;
HTuple hv_Selected3, hv_Indices, hv_Sorted2, hv_end2, hv_Selected4;
HTuple hv_Exception;
(*hv_out_Height) = -99;
(*hv_out_Flag) = 0;
hv_out_Ngtails = "ok";
try
{
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//fill_up (in_InspectionRegion, RegionFillUp)
//opening_circle (RegionFillUp, RegionOpening, 10.5)
//dev_display (in_ZImageScaled)
//dev_display (RegionOpening)
//connection (RegionOpening, ConnectedRegions)
//select_shape_std (ConnectedRegions, SelectedRegions, 'max_area', 70)
//dilation_circle (SelectedRegions, RegionDilation, 35)
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ErosionRectangle1(ho_in_InspectionRegion, &ho_RegionErosion, 10, 70);
Difference(ho_in_InspectionRegion, ho_RegionErosion, &ho_RegionDifference);
ShapeTrans(ho_RegionDifference, &ho_RegionTrans, "rectangle1");
ErosionRectangle1(ho_RegionTrans, &ho_RegionErosion1, 350, 1);
Intersection(ho_RegionDifference, ho_RegionErosion1, &ho_InspectionRegion);
//dev_display (in_ZImageScaled)
//dev_display (RegionErosion1)
//difference (in_ProductRegion, RegionDilation, InspectionRegion)
if (HDevWindowStack::IsOpen())
DispObj(ho_in_ZImageScaled, HDevWindowStack::GetActive());
if (HDevWindowStack::IsOpen())
DispObj(ho_InspectionRegion, HDevWindowStack::GetActive());
hv_A = ((const HTuple&)hv_in_PlaneParamValue)[0];
hv_B = ((const HTuple&)hv_in_PlaneParamValue)[1];
hv_C = ((const HTuple&)hv_in_PlaneParamValue)[2];
hv_D = ((const HTuple&)hv_in_PlaneParamValue)[3];
GetRegionPoints(ho_InspectionRegion, &hv_Rows, &hv_Columns);
GetGrayval(ho_in_XImageScaled, hv_Rows, hv_Columns, &hv_X);
GetGrayval(ho_in_YImageScaled, hv_Rows, hv_Columns, &hv_Y);
GetGrayval(ho_in_ZImageScaled, hv_Rows, hv_Columns, &hv_Z);
hv_Dist = (((hv_A * hv_X) + (hv_B * hv_Y)) + (hv_C * hv_Z)) - hv_D;
if (0 != (hv_C < 0))
{
hv_Dist = -hv_Dist;
}
TupleSort(hv_Dist, &hv_Sorted);
//******************************3***************************
//算法寻找高点, 并寻找最高点坐标
TupleGreaterElem(hv_Dist, 0, &hv_PosGreater);
//寻找大于0的点坐标,
TupleFind(hv_PosGreater, 1, &hv_PosIndices);
if (0 != (HTuple((hv_PosIndices.TupleLength()) < 300).TupleOr(hv_PosIndices == -1)))
{
TupleLessElem(hv_Dist, 0, &hv_NegLess);
TupleFind(hv_NegLess, 1, &hv_NegIndices);
TupleSelect(hv_Dist, hv_NegIndices, &hv_Selected2);
//该处需要对点集进行排序,
TupleSort(hv_Selected2, &hv_Sorted1);
hv_end1 = HTuple((hv_Sorted1.TupleLength()) * 0.95).TupleInt();
TupleSelect(hv_Sorted1, hv_end1, &hv_Selected3);
TupleFind(hv_Dist, hv_Selected3, &hv_Indices);
TupleSelect(hv_Rows, hv_Indices, &(*hv_out_PaintRows));
TupleSelect(hv_Columns, hv_Indices, &(*hv_out_PaintColumns));
(*hv_out_Height) = hv_Selected3;
}
else if (0 != ((hv_PosIndices.TupleLength()) > 300))
{
TupleGreaterElem(hv_Dist, 0, &hv_PosGreater);
TupleFind(hv_PosGreater, 1, &hv_PosIndices);
TupleSelect(hv_Dist, hv_PosIndices, &hv_Selected3);
TupleSort(hv_Selected3, &hv_Sorted2);
hv_end2 = HTuple((hv_Sorted2.TupleLength()) * 0.7).TupleInt();
TupleSelect(hv_Sorted2, hv_end2, &hv_Selected4);
TupleFind(hv_Dist, hv_Selected4, &hv_Indices);
TupleSelect(hv_Rows, hv_Indices, &(*hv_out_PaintRows));
TupleSelect(hv_Columns, hv_Indices, &(*hv_out_PaintColumns));
(*hv_out_Height) = hv_Selected4;
}
if (0 != ((*hv_out_Height) != -99))
{
(*hv_out_Flag) = 1;
}
}
// catch (Exception)
catch (HException& HDevExpDefaultException)
{
HDevExpDefaultException.ToHTuple(&hv_Exception);
hv_out_Ngtails = "算法错误";
}
return;
}
void Cal_Height3(HObject ho_in_XImageScaled, HObject ho_in_YImageScaled, HObject ho_in_ZImageScaled,
HObject ho_in_innerEdge, HObject ho_in_outerEdge, HObject ho_in_HYRegion, HTuple* hv_out_Height,
HTuple* hv_out_Flag)
{
// Local iconic variables
HObject ho_RegionDifference, ho_GlueRegion, ho_RegionErosion;
HObject ho_ImageReduced1, ho_ImageReduced2, ho_ImageReduced3;
// Local control variables
HTuple hv_out_Ngtails, hv_ObjectModel3D, hv_ParFitting;
HTuple hv_ValFitting, hv_ObjectModelPlane, hv_PlaneParamValue;
HTuple hv_A, hv_B, hv_C, hv_D, hv_Rows, hv_Columns, hv_X;
HTuple hv_Y, hv_Z, hv_Dist, hv_Sorted, hv_start1, hv_end1;
HTuple hv_Selected, hv_Max, hv_Exception;
(*hv_out_Height) = -99;
(*hv_out_Flag) = 0;
hv_out_Ngtails = "ok";
try
{
Difference(ho_in_innerEdge, ho_in_HYRegion, &ho_RegionDifference);
Difference(ho_in_outerEdge, ho_in_innerEdge, &ho_GlueRegion);
ErosionCircle(ho_RegionDifference, &ho_RegionErosion, 10.5);
ReduceDomain(ho_in_XImageScaled, ho_RegionErosion, &ho_ImageReduced1);
ReduceDomain(ho_in_YImageScaled, ho_RegionErosion, &ho_ImageReduced2);
ReduceDomain(ho_in_ZImageScaled, ho_RegionErosion, &ho_ImageReduced3);
XyzToObjectModel3d(ho_ImageReduced1, ho_ImageReduced2, ho_ImageReduced3, &hv_ObjectModel3D);
//拟合平面,
hv_ParFitting.Clear();
hv_ParFitting[0] = "primitive_type";
hv_ParFitting[1] = "fitting_algorithm";
hv_ParFitting[2] = "output_xyz_mapping";
hv_ValFitting.Clear();
hv_ValFitting[0] = "plane";
hv_ValFitting[1] = "least_squares_huber";
hv_ValFitting[2] = "false";
//利用产品的边缘拟合平面,
FitPrimitivesObjectModel3d(hv_ObjectModel3D, hv_ParFitting, hv_ValFitting, &hv_ObjectModelPlane);
GetObjectModel3dParams(hv_ObjectModelPlane, "primitive_parameter", &hv_PlaneParamValue);
hv_A = ((const HTuple&)hv_PlaneParamValue)[0];
hv_B = ((const HTuple&)hv_PlaneParamValue)[1];
hv_C = ((const HTuple&)hv_PlaneParamValue)[2];
hv_D = ((const HTuple&)hv_PlaneParamValue)[3];
GetRegionPoints(ho_GlueRegion, &hv_Rows, &hv_Columns);
GetGrayval(ho_in_XImageScaled, hv_Rows, hv_Columns, &hv_X);
GetGrayval(ho_in_YImageScaled, hv_Rows, hv_Columns, &hv_Y);
GetGrayval(ho_in_ZImageScaled, hv_Rows, hv_Columns, &hv_Z);
hv_Dist = (((hv_A * hv_X) + (hv_B * hv_Y)) + (hv_C * hv_Z)) - hv_D;
if (0 != (hv_C < 0))
{
hv_Dist = -hv_Dist;
}
TupleSort(hv_Dist, &hv_Sorted);
hv_start1 = HTuple((hv_Sorted.TupleLength()) * 0.02).TupleInt();
hv_end1 = HTuple((hv_Sorted.TupleLength()) * 0.98).TupleInt();
TupleSelect(hv_Sorted, HTuple::TupleGenSequence(hv_start1, hv_end1, 1), &hv_Selected);
TupleMax(hv_Selected, &hv_Max);
(*hv_out_Height) = hv_Max;
if (0 != ((*hv_out_Height) != -99))
{
(*hv_out_Flag) = 1;
}
}
// catch (Exception)
catch (HException& HDevExpDefaultException)
{
HDevExpDefaultException.ToHTuple(&hv_Exception);
hv_out_Ngtails = "算法错误";
}
return;
}
void Get_InGlueRegion(HObject ho_in_XImageScaled, HObject ho_in_YImageScaled, HObject ho_in_ZImageScaled,
HObject ho_in_ZeroRegion, HObject* ho_in_EdgeRegion, HTuple hv_in_Height, HTuple hv_in_PlaneParamValue,
HTuple hv_in_Direc, HTuple* hv_out_Flag, HTuple* hv_out_Ngtails)
{
// Local iconic variables
HObject ho_RegionFillUp, ho_Tempregion, ho_Region2;
HObject ho_RegionOpening, ho_ConnectedRegions3, ho_Regions;
HObject ho_Out_ROI, ho_ConnectedRegions4, ho_SelectedRegions3;
HObject ho_closingRegion;
// Local control variables
HTuple hv_A, hv_B, hv_C, hv_D, hv_Rows, hv_Columns;
HTuple hv_X, hv_Y, hv_Z, hv_Dist, hv_Com_Tuple, hv_Prod_X;
HTuple hv_Prod_Y, hv_HeightOut, hv_Com_Tuple2, hv_Com_Tuple1;
HTuple hv_And, hv_Number, hv_Area, hv_Row, hv_Column, hv_Exception;
(*hv_out_Flag) = 0;
(*hv_out_Ngtails) = "ok";
try
{
FillUp(ho_in_ZeroRegion, &ho_RegionFillUp);
Difference(ho_RegionFillUp, ho_in_ZeroRegion, &ho_Tempregion);
hv_A = ((const HTuple&)hv_in_PlaneParamValue)[0];
hv_B = ((const HTuple&)hv_in_PlaneParamValue)[1];
hv_C = ((const HTuple&)hv_in_PlaneParamValue)[2];
hv_D = ((const HTuple&)hv_in_PlaneParamValue)[3];
GetRegionPoints(ho_Tempregion, &hv_Rows, &hv_Columns);
GetGrayval(ho_in_XImageScaled, hv_Rows, hv_Columns, &hv_X);
GetGrayval(ho_in_YImageScaled, hv_Rows, hv_Columns, &hv_Y);
GetGrayval(ho_in_ZImageScaled, hv_Rows, hv_Columns, &hv_Z);
hv_Dist = (((hv_A * hv_X) + (hv_B * hv_Y)) + (hv_C * hv_Z)) - hv_D;
if (0 != (hv_C < 0))
{
hv_Dist = hv_Dist * -1;
}
if (0 != (hv_in_Direc == HTuple("Pos")))
{
TupleGreaterElem(hv_Dist, hv_in_Height, &hv_Com_Tuple);
TupleMult(hv_Rows, hv_Com_Tuple, &hv_Prod_X);
TupleMult(hv_Columns, hv_Com_Tuple, &hv_Prod_Y);
GenRegionPoints(&ho_Region2, hv_Prod_X, hv_Prod_Y);
ClosingCircle(ho_Region2, &ho_RegionOpening, 3.5);
Connection(ho_RegionOpening, &ho_ConnectedRegions3);
SelectShape(ho_ConnectedRegions3, &ho_Regions, "area", "and", 2, 9999999999999);
Union1(ho_Regions, &ho_Out_ROI);
}
else if (0 != (hv_in_Direc == HTuple("Neg")))
{
hv_HeightOut = hv_HeightOut * -1;
TupleLessElem(hv_Dist, hv_in_Height, &hv_Com_Tuple);
TupleMult(hv_Rows, hv_Com_Tuple, &hv_Prod_X);
TupleMult(hv_Columns, hv_Com_Tuple, &hv_Prod_Y);
GenRegionPoints(&ho_Region2, hv_Prod_X, hv_Prod_Y);
ClosingCircle(ho_Region2, &ho_RegionOpening, 3.5);
Connection(ho_RegionOpening, &ho_ConnectedRegions3);
SelectShape(ho_ConnectedRegions3, &ho_Regions, "area", "and", 2, 9999999999999);
Union1(ho_Regions, &ho_Out_ROI);
}
else if (0 != (hv_in_Direc == HTuple("all")))
{
TupleLessElem(hv_Dist, hv_in_Height, &hv_Com_Tuple2);
TupleGreaterElem(hv_Dist, -hv_in_Height, &hv_Com_Tuple1);
TupleAnd(hv_Com_Tuple1, hv_Com_Tuple2, &hv_And);
TupleMult(hv_Rows, hv_And, &hv_Prod_X);
TupleMult(hv_Columns, hv_And, &hv_Prod_Y);
GenRegionPoints(&ho_Region2, hv_Prod_X, hv_Prod_Y);
ClosingCircle(ho_Region2, &ho_RegionOpening, 3.5);
Connection(ho_RegionOpening, &ho_ConnectedRegions3);
SelectShape(ho_ConnectedRegions3, &ho_Regions, "area", "and", 2, 9999999999999);
Union1(ho_Regions, &ho_Out_ROI);
}
ClosingCircle(ho_Out_ROI, &ho_RegionOpening, 4.5);
Connection(ho_RegionOpening, &ho_ConnectedRegions4);
SelectShape(ho_ConnectedRegions4, &ho_SelectedRegions3, "max_diameter", "and",
450, 900);
CountObj(ho_SelectedRegions3, &hv_Number);
//if (Number>1)
//smallest_rectangle1 (out_EdgeRegion, Row11, Column11, Row21, Column21)
//select_shape (SelectedRegions3, SelectedRegions1, ['row','column'], 'and', [Row11,Column11], [Row21,Column21])
//SelectedRegions3 := SelectedRegions1
//endif
ClosingCircle(ho_SelectedRegions3, &ho_closingRegion, 20);
FillUp(ho_closingRegion, &(*ho_in_EdgeRegion));
AreaCenter((*ho_in_EdgeRegion), &hv_Area, &hv_Row, &hv_Column);
if (0 != (hv_Area > 0))
{
(*hv_out_Flag) = 1;
}
}
// catch (Exception)
catch (HException& HDevExpDefaultException)
{
HDevExpDefaultException.ToHTuple(&hv_Exception);
(*hv_out_Ngtails) = "算法错误";
}
return;
}
void Get_OutGlueRegion(HObject ho_in_XImageScaled, HObject ho_in_YImageScaled, HObject ho_in_ZImageScaled,
HObject ho_in_ZeroRegion, HObject* ho_out_GlueRegion, HTuple hv_in_Height, HTuple hv_in_Direc,
HTuple hv_in_PlaneParamValue, HTuple* hv_out_Flag, HTuple* hv_out_Ngtails)
{
// Local iconic variables
HObject ho_RegionFillUp, ho_Tempregion, ho_Region2;
HObject ho_RegionOpening, ho_ConnectedRegions3, ho_Regions;
HObject ho_Out_ROI, ho_SelectedRegions2, ho_RegionFillUp1;
HObject ho_RegionDifference2, ho_EdgeRegion, ho_ConnectedRegions1;
// Local control variables
HTuple hv_A, hv_B, hv_C, hv_D, hv_Rows, hv_Columns;
HTuple hv_X, hv_Y, hv_Z, hv_Dist, hv_Com_Tuple, hv_Prod_X;
HTuple hv_Prod_Y, hv_Com_Tuple2, hv_Com_Tuple1, hv_And;
HTuple hv_Area, hv_Row, hv_Column, hv_Exception;
(*hv_out_Flag) = 0;
(*hv_out_Ngtails) = "ok";
try
{
FillUp(ho_in_ZeroRegion, &ho_RegionFillUp);
Difference(ho_RegionFillUp, ho_in_ZeroRegion, &ho_Tempregion);
//dev_display (in_XIageScaled)
if (HDevWindowStack::IsOpen())
DispObj(ho_Tempregion, HDevWindowStack::GetActive());
hv_A = ((const HTuple&)hv_in_PlaneParamValue)[0];
hv_B = ((const HTuple&)hv_in_PlaneParamValue)[1];
hv_C = ((const HTuple&)hv_in_PlaneParamValue)[2];
hv_D = ((const HTuple&)hv_in_PlaneParamValue)[3];
GetRegionPoints(ho_Tempregion, &hv_Rows, &hv_Columns);
GetGrayval(ho_in_XImageScaled, hv_Rows, hv_Columns, &hv_X);
GetGrayval(ho_in_YImageScaled, hv_Rows, hv_Columns, &hv_Y);
GetGrayval(ho_in_ZImageScaled, hv_Rows, hv_Columns, &hv_Z);
hv_Dist = (((hv_A * hv_X) + (hv_B * hv_Y)) + (hv_C * hv_Z)) - hv_D;
if (0 != (hv_C < 0))
{
hv_Dist = hv_Dist * -1;
}
if (0 != (hv_in_Direc == HTuple("Pos")))
{
TupleGreaterElem(hv_Dist, hv_in_Height, &hv_Com_Tuple);
TupleMult(hv_Rows, hv_Com_Tuple, &hv_Prod_X);
TupleMult(hv_Columns, hv_Com_Tuple, &hv_Prod_Y);
GenRegionPoints(&ho_Region2, hv_Prod_X, hv_Prod_Y);
ClosingCircle(ho_Region2, &ho_RegionOpening, 3.5);
Connection(ho_RegionOpening, &ho_ConnectedRegions3);
SelectShape(ho_ConnectedRegions3, &ho_Regions, "area", "and", 2, 9999999999999);
Union1(ho_Regions, &ho_Out_ROI);
}
else if (0 != (hv_in_Direc == HTuple("Neg")))
{
//Height := Height*(-1)
TupleLessElem(hv_Dist, -hv_in_Height, &hv_Com_Tuple);
TupleMult(hv_Rows, hv_Com_Tuple, &hv_Prod_X);
TupleMult(hv_Columns, hv_Com_Tuple, &hv_Prod_Y);
GenRegionPoints(&ho_Region2, hv_Prod_X, hv_Prod_Y);
ClosingCircle(ho_Region2, &ho_RegionOpening, 3.5);
Connection(ho_RegionOpening, &ho_ConnectedRegions3);
SelectShape(ho_ConnectedRegions3, &ho_Regions, "area", "and", 2, 9999999999999);
Union1(ho_Regions, &ho_Out_ROI);
}
else if (0 != (hv_in_Direc == HTuple("all")))
{
TupleLessElem(hv_Dist, hv_in_Height, &hv_Com_Tuple2);
TupleGreaterElem(hv_Dist, -hv_in_Height, &hv_Com_Tuple1);
TupleAnd(hv_Com_Tuple1, hv_Com_Tuple2, &hv_And);
TupleMult(hv_Rows, hv_And, &hv_Prod_X);
TupleMult(hv_Columns, hv_And, &hv_Prod_Y);
GenRegionPoints(&ho_Region2, hv_Prod_X, hv_Prod_Y);
ClosingCircle(ho_Region2, &ho_RegionOpening, 3.5);
Connection(ho_RegionOpening, &ho_ConnectedRegions3);
SelectShape(ho_ConnectedRegions3, &ho_Regions, "area", "and", 2, 9999999999999);
Union1(ho_Regions, &ho_Out_ROI);
}
Connection(ho_Out_ROI, &ho_ConnectedRegions3);
SelectShapeStd(ho_ConnectedRegions3, &ho_SelectedRegions2, "max_area", 70);
FillUp(ho_SelectedRegions2, &ho_RegionFillUp1);
Difference(ho_RegionFillUp1, ho_SelectedRegions2, &ho_RegionDifference2);
OpeningCircle(ho_RegionDifference2, &ho_EdgeRegion, 5.5);
Connection(ho_EdgeRegion, &ho_ConnectedRegions1);
SelectShapeStd(ho_ConnectedRegions1, &(*ho_out_GlueRegion), "max_area", 70);
AreaCenter((*ho_out_GlueRegion), &hv_Area, &hv_Row, &hv_Column);
if (0 != (hv_Area > 0))
{
(*hv_out_Flag) = 1;
}
}
// catch (Exception)
catch (HException& HDevExpDefaultException)
{
HDevExpDefaultException.ToHTuple(&hv_Exception);
(*hv_out_Flag) = 0;
(*hv_out_Ngtails) = "算法错误";
}
return;
}
namespace HDevExportCpp
{
// Parallel execution wrapper for Get_OutGlueRegion(...)
static void* _hcppthread_Get_OutGlueRegion(void* hcthread)
{
// +++ define thread context for this procedure
HDevThread* hcppthread = (HDevThread*)hcthread;
try
{
// Input parameters
const HObject& cbho_in_XImageScaled = hcppthread->GetInputIconicParamObject(0);
const HObject& cbho_in_YImageScaled = hcppthread->GetInputIconicParamObject(1);
const HObject& cbho_in_ZImageScaled = hcppthread->GetInputIconicParamObject(2);
const HObject& cbho_in_ZeroRegion = hcppthread->GetInputIconicParamObject(3);
const HTuple& cbhv_in_Height = hcppthread->GetInputCtrlParamTuple(4);
const HTuple& cbhv_in_Direc = hcppthread->GetInputCtrlParamTuple(5);
const HTuple& cbhv_in_PlaneParamValue = hcppthread->GetInputCtrlParamTuple(6);
// Output parameters
HObject cbho_out_GlueRegion;
HTuple cbhv_out_Flag;
HTuple cbhv_out_Ngtails;
// Call Get_OutGlueRegion
Get_OutGlueRegion(cbho_in_XImageScaled, cbho_in_YImageScaled, cbho_in_ZImageScaled,
cbho_in_ZeroRegion, &cbho_out_GlueRegion, cbhv_in_Height, cbhv_in_Direc,
cbhv_in_PlaneParamValue, &cbhv_out_Flag, &cbhv_out_Ngtails);
// Store output parameters in thread object
hcppthread->StoreOutputIconicParamObject(0, cbho_out_GlueRegion);
hcppthread->StoreOutputCtrlParamTuple(1, cbhv_out_Flag);
hcppthread->StoreOutputCtrlParamTuple(2, cbhv_out_Ngtails);
// Reduce reference counter of thread object
hcppthread->Exit();
delete hcppthread;
}
catch (HException& exc)
{
// No exceptions may be raised from stub in parallel case,
// so we need to store this information prior to cleanup
bool is_direct_call = hcppthread->IsDirectCall();
// Attempt to clean up in error case, too
hcppthread->Exit();
delete hcppthread;
// Propagate exception if called directly
if (is_direct_call)
throw exc;
}
return NULL;
}
// Parallel execution wrapper for Get_InGlueRegion(...)
static void* _hcppthread_Get_InGlueRegion(void* hcthread)
{
// +++ define thread context for this procedure
HDevThread* hcppthread = (HDevThread*)hcthread;
try
{
// Input parameters
const HObject& cbho_in_XImageScaled = hcppthread->GetInputIconicParamObject(0);
const HObject& cbho_in_YImageScaled = hcppthread->GetInputIconicParamObject(1);
const HObject& cbho_in_ZImageScaled = hcppthread->GetInputIconicParamObject(2);
const HObject& cbho_in_ZeroRegion = hcppthread->GetInputIconicParamObject(3);
const HTuple& cbhv_in_Height = hcppthread->GetInputCtrlParamTuple(4);
const HTuple& cbhv_in_PlaneParamValue = hcppthread->GetInputCtrlParamTuple(5);
const HTuple& cbhv_in_Direc = hcppthread->GetInputCtrlParamTuple(6);
// Output parameters
HObject cbho_in_EdgeRegion;
HTuple cbhv_out_Flag;
HTuple cbhv_out_Ngtails;
// Call Get_InGlueRegion
Get_InGlueRegion(cbho_in_XImageScaled, cbho_in_YImageScaled, cbho_in_ZImageScaled,
cbho_in_ZeroRegion, &cbho_in_EdgeRegion, cbhv_in_Height, cbhv_in_PlaneParamValue,
cbhv_in_Direc, &cbhv_out_Flag, &cbhv_out_Ngtails);
// Store output parameters in thread object
hcppthread->StoreOutputIconicParamObject(0, cbho_in_EdgeRegion);
hcppthread->StoreOutputCtrlParamTuple(1, cbhv_out_Flag);
hcppthread->StoreOutputCtrlParamTuple(2, cbhv_out_Ngtails);
// Reduce reference counter of thread object
hcppthread->Exit();
delete hcppthread;
}
catch (HException& exc)
{
// No exceptions may be raised from stub in parallel case,
// so we need to store this information prior to cleanup
bool is_direct_call = hcppthread->IsDirectCall();
// Attempt to clean up in error case, too
hcppthread->Exit();
delete hcppthread;
// Propagate exception if called directly
if (is_direct_call)
throw exc;
}
return NULL;
}
// Parallel execution wrapper for Cal_Height1(...)
static void* _hcppthread_Cal_Height1(void* hcthread)
{
// +++ define thread context for this procedure
HDevThread* hcppthread = (HDevThread*)hcthread;
try
{
// Input parameters
const HObject& cbho_in_XImageScaled = hcppthread->GetInputIconicParamObject(0);
const HObject& cbho_in_YImageScaled = hcppthread->GetInputIconicParamObject(1);
const HObject& cbho_in_ZImageScaled = hcppthread->GetInputIconicParamObject(2);
const HObject& cbho_in_InspectionRegion = hcppthread->GetInputIconicParamObject(3);
const HTuple& cbhv_in_PlaneParamValue = hcppthread->GetInputCtrlParamTuple(4);
// Output parameters
HTuple cbhv_out_PaintRows;
HTuple cbhv_out_PaintColumns;
HTuple cbhv_out_Height;
HTuple cbhv_out_Flag;
// Call Cal_Height1
Cal_Height1(cbho_in_XImageScaled, cbho_in_YImageScaled, cbho_in_ZImageScaled,
cbho_in_InspectionRegion, cbhv_in_PlaneParamValue, &cbhv_out_PaintRows, &cbhv_out_PaintColumns,
&cbhv_out_Height, &cbhv_out_Flag);
// Store output parameters in thread object
hcppthread->StoreOutputCtrlParamTuple(0, cbhv_out_PaintRows);
hcppthread->StoreOutputCtrlParamTuple(1, cbhv_out_PaintColumns);
hcppthread->StoreOutputCtrlParamTuple(2, cbhv_out_Height);
hcppthread->StoreOutputCtrlParamTuple(3, cbhv_out_Flag);
// Reduce reference counter of thread object
hcppthread->Exit();
delete hcppthread;
}
catch (HException& exc)
{
// No exceptions may be raised from stub in parallel case,
// so we need to store this information prior to cleanup
bool is_direct_call = hcppthread->IsDirectCall();
// Attempt to clean up in error case, too
hcppthread->Exit();
delete hcppthread;
// Propagate exception if called directly
if (is_direct_call)
throw exc;
}
return NULL;
}
// Parallel execution wrapper for Cal_Height2(...)
static void* _hcppthread_Cal_Height2(void* hcthread)
{
// +++ define thread context for this procedure
HDevThread* hcppthread = (HDevThread*)hcthread;
try
{
// Input parameters
const HObject& cbho_in_XImageScaled = hcppthread->GetInputIconicParamObject(0);
const HObject& cbho_in_YImageScaled = hcppthread->GetInputIconicParamObject(1);
const HObject& cbho_in_ZImageScaled = hcppthread->GetInputIconicParamObject(2);
const HObject& cbho_in_InspectionRegion = hcppthread->GetInputIconicParamObject(3);
const HObject& cbho_in_ProductRegion = hcppthread->GetInputIconicParamObject(4);
const HTuple& cbhv_in_PlaneParamValue = hcppthread->GetInputCtrlParamTuple(5);
// Output parameters
HTuple cbhv_out_PaintRows;
HTuple cbhv_out_PaintColumns;
HTuple cbhv_out_Height;
HTuple cbhv_out_Flag;
// Call Cal_Height2
Cal_Height2(cbho_in_XImageScaled, cbho_in_YImageScaled, cbho_in_ZImageScaled,
cbho_in_InspectionRegion, cbho_in_ProductRegion, cbhv_in_PlaneParamValue,
&cbhv_out_PaintRows, &cbhv_out_PaintColumns, &cbhv_out_Height, &cbhv_out_Flag);
// Store output parameters in thread object
hcppthread->StoreOutputCtrlParamTuple(0, cbhv_out_PaintRows);
hcppthread->StoreOutputCtrlParamTuple(1, cbhv_out_PaintColumns);
hcppthread->StoreOutputCtrlParamTuple(2, cbhv_out_Height);
hcppthread->StoreOutputCtrlParamTuple(3, cbhv_out_Flag);
// Reduce reference counter of thread object
hcppthread->Exit();
delete hcppthread;
}
catch (HException& exc)
{
// No exceptions may be raised from stub in parallel case,
// so we need to store this information prior to cleanup
bool is_direct_call = hcppthread->IsDirectCall();
// Attempt to clean up in error case, too
hcppthread->Exit();
delete hcppthread;
// Propagate exception if called directly
if (is_direct_call)
throw exc;
}
return NULL;
}
// Parallel execution wrapper for Cal_Height3(...)
static void* _hcppthread_Cal_Height3(void* hcthread)
{
// +++ define thread context for this procedure
HDevThread* hcppthread = (HDevThread*)hcthread;
try
{
// Input parameters
const HObject& cbho_in_XImageScaled = hcppthread->GetInputIconicParamObject(0);
const HObject& cbho_in_YImageScaled = hcppthread->GetInputIconicParamObject(1);
const HObject& cbho_in_ZImageScaled = hcppthread->GetInputIconicParamObject(2);
const HObject& cbho_in_innerEdge = hcppthread->GetInputIconicParamObject(3);
const HObject& cbho_in_outerEdge = hcppthread->GetInputIconicParamObject(4);
const HObject& cbho_in_HYRegion = hcppthread->GetInputIconicParamObject(5);
// Output parameters
HTuple cbhv_out_Height;
HTuple cbhv_out_Flag;
// Call Cal_Height3
Cal_Height3(cbho_in_XImageScaled, cbho_in_YImageScaled, cbho_in_ZImageScaled,
cbho_in_innerEdge, cbho_in_outerEdge, cbho_in_HYRegion, &cbhv_out_Height,
&cbhv_out_Flag);
// Store output parameters in thread object
hcppthread->StoreOutputCtrlParamTuple(0, cbhv_out_Height);
hcppthread->StoreOutputCtrlParamTuple(1, cbhv_out_Flag);
// Reduce reference counter of thread object
hcppthread->Exit();
delete hcppthread;
}
catch (HException& exc)
{
// No exceptions may be raised from stub in parallel case,
// so we need to store this information prior to cleanup
bool is_direct_call = hcppthread->IsDirectCall();
// Attempt to clean up in error case, too
hcppthread->Exit();
delete hcppthread;
// Propagate exception if called directly
if (is_direct_call)
throw exc;
}
return NULL;
}
}
#endif
void Cal_Glue_Height(HObject ho_in_ImageH, HTuple hv_in_xResolution, HTuple hv_in_yResolution,
HTuple hv_in_zResolution, HTuple* hv_out_Height1, HTuple* hv_out_Height2, HTuple* hv_out_Point,
HTuple* hv_out_Flag, HTuple* hv_out_Ngtails)
{
// Local iconic variables
HObject ho_ImageConverted, ho_YImageSurface;
HObject ho_XImageSurface, ho_in_XImageScaled, ho_in_YImageScaled;
HObject ho_in_ZImageScaled, ho_Regions, ho_Domain, ho_ProductRegion;
HObject ho_ImageReduced, ho_RegionClosing, ho_RegionFillUp3;
HObject ho_TempRegion, ho_RegionErosion3, ho_ZeroRegion;
HObject ho_ImageReduced1, ho_ImageReduced2, ho_ImageReduced3;
HObject ho_in_ZeroRegion1, ho_out_GlueRegion, ho_in_ZeroRegion2;
HObject ho_in_EdgeRegion, ho_out_EdgeRegion, ho_InspectionRegion;
HObject ho_Ellipse, ho_RegionDilation2, ho_in_InspectionRegion3;
HObject ho_in_InspectionRegion4, ho_in_ProductRegion, ho_RegionDifference;
HObject ho_Partitioned, ho_SortedRegions, ho_ObjectSelected;
HObject ho_RegionTrans, ho_Region, ho_Cross, ho_RegionIntersection;
HObject ho_in_outerEdge, ho_in_innerEdge, ho_in_HYRegion;
// Local control variables
HTuple hv_Debug, hv_Type, hv_Width2, hv_Height2;
HTuple hv_ObjectModel3D, hv_ParFitting, hv_ValFitting;
HTuple hv_ObjectModelPlane, hv_PlaneParamValue, hv_in_Direc1;
HTuple hv_in_Height1, hv_in_PlaneParamValue1, hv_out_Flag1;
HTuple hv_out_Ngtails1, hv_in_Direc2, hv_in_Height2, hv_in_PlaneParamValue2;
HTuple hv_out_Flag2, hv_out_Ngtails2, hv_Area2, hv_Row2;
HTuple hv_Column2, hv_Area3, hv_Row3, hv_Column3, hv_Area5;
HTuple hv_Row5, hv_Column5, hv_in_PlaneParamValue3, hv_out_PaintRows1;
HTuple hv_out_PaintColumns1, hv_out_Height3, hv_out_Flag3;
HTuple hv_in_PlaneParamValue4, hv_out_PaintRows2, hv_out_PaintColumns2;
HTuple hv_out_Height4, hv_out_Flag4, hv_Value, hv_Area4;
HTuple hv_Row4, hv_Column4, hv_out_Height, hv_out_Flag5;
HTuple hv_Exception;
//**********************************************************************
//Function Name: Cal_Glue_Height
//Author:
//Description: 基于边缘基准面,计算点胶的高度(mm)
//Input: in_ImageH, 深度图像(tif),
// in_xResolution, X向精度(mm/pix)
// in_yResolution, y向精度(mm/pix)
// in_zResolution, z向精度(mm/pix)
//output: out_Height, 点胶高度(mm), 一般情况下,胶的高度高于电池边缘面的区域, 才有高度输出.
// out_Point: 胶的高点和基准面的高点坐标(y,x)
// out_Flag, 算法执行的标志位; 1, 算法执行成功 2, 算法执行错误
// out_Ngtails, 算法执行信息,
//verion change log: 胡汉兵 2024-06-15 V1.1 Create new function.
// 胡汉兵 2024-06-20 V1.2 软件端执行结果异常, 算法直接强行赋值为0.
// 胡汉兵 2024-07-04 V1.3 现场输出电池的最高点位置,便于客户基于当前点位进行测量.
// 胡汉兵 2024-07-09 V1.4 使用原始图像进行高度计算,
// 胡汉兵 2024-07-16 V1.5 保护, 防止程序崩溃时, 无结果图输出.
// 胡汉兵 2024-10-21 V1.6 新的图像, 代码进行调整,
//**********************************************************************
hv_Debug = 1;
(*hv_out_Height1) = -99;
(*hv_out_Height2) = -99;
(*hv_out_Flag) = 2;
(*hv_out_Ngtails) = "OK";
(*hv_out_Point) = HTuple();
try
{
if (HDevWindowStack::IsOpen())
SetDraw(HDevWindowStack::GetActive(), "margin");
GetImageType(ho_in_ImageH, &hv_Type);
if (0 != (hv_Type != HTuple("real")))
{
ConvertImageType(ho_in_ImageH, &ho_ImageConverted, "real");
}
else
{
ho_ImageConverted = ho_in_ImageH;
}
GetImageSize(ho_ImageConverted, &hv_Width2, &hv_Height2);
GenImageSurfaceFirstOrder(&ho_YImageSurface, "real", 1, 0, 0, 0, 0, hv_Width2,
hv_Height2);
GenImageSurfaceFirstOrder(&ho_XImageSurface, "real", 0, 1, 0, 0, 0, hv_Width2,
hv_Height2);
ScaleImage(ho_XImageSurface, &ho_in_XImageScaled, hv_in_xResolution, 0);
ScaleImage(ho_YImageSurface, &ho_in_YImageScaled, hv_in_xResolution, 0);
ScaleImage(ho_ImageConverted, &ho_in_ZImageScaled, hv_in_zResolution, 0);
Threshold(ho_ImageConverted, &ho_Regions, 0, 0);
GetDomain(ho_in_ImageH, &ho_Domain);
Difference(ho_Domain, ho_Regions, &ho_ProductRegion);
ReduceDomain(ho_in_ZImageScaled, ho_ProductRegion, &ho_ImageReduced);
if (0 != hv_Debug)
{
//reduce_domain (in_XImageScaled, ProductRegion, ImageReduced4)
//reduce_domain (in_YImageScaled, ProductRegion, ImageReduced5)
//xyz_to_object_model_3d (ImageReduced4, ImageReduced5, ImageReduced, ObjectModel3D)
//dev_get_window (WindowHandle)
//visualize_object_model_3d (WindowHandle, [ObjectModel3D], [], [], ['lut','intensity','disp_pose'], ['hsi','coord_z', 'true'], ['展示拟合的平面'], '', '', Pose)
}
//******************************************************
//step2: 获取基准面区域(新代码不使用这个区域的Region)
ClosingCircle(ho_ProductRegion, &ho_RegionClosing, 15);
FillUp(ho_RegionClosing, &ho_RegionFillUp3);
ErosionRectangle1(ho_RegionFillUp3, &ho_TempRegion, 1, 220);
//dev_display (ImageReduced)
//dev_display (TempRegion)
//区间进行内缩, 获取基准的端面.
ErosionCircle(ho_TempRegion, &ho_RegionErosion3, 80);
Difference(ho_TempRegion, ho_RegionErosion3, &ho_ZeroRegion);
//dev_display (ImageReduced)
//dev_display (ZeroRegion)
//******************************************************
ReduceDomain(ho_in_XImageScaled, ho_ZeroRegion, &ho_ImageReduced1);
ReduceDomain(ho_in_YImageScaled, ho_ZeroRegion, &ho_ImageReduced2);
ReduceDomain(ho_in_ZImageScaled, ho_ZeroRegion, &ho_ImageReduced3);
XyzToObjectModel3d(ho_ImageReduced1, ho_ImageReduced2, ho_ImageReduced3, &hv_ObjectModel3D);
//拟合平面,
hv_ParFitting.Clear();
hv_ParFitting[0] = "primitive_type";
hv_ParFitting[1] = "fitting_algorithm";
hv_ParFitting[2] = "output_xyz_mapping";
hv_ValFitting.Clear();
hv_ValFitting[0] = "plane";
hv_ValFitting[1] = "least_squares_huber";
hv_ValFitting[2] = "false";
//利用产品的边缘拟合平面,
FitPrimitivesObjectModel3d(hv_ObjectModel3D, hv_ParFitting, hv_ValFitting, &hv_ObjectModelPlane);
GetObjectModel3dParams(hv_ObjectModelPlane, "primitive_parameter", &hv_PlaneParamValue);
//线程一,
hv_in_Direc1 = "Neg";
hv_in_Height1 = 1.0;
hv_in_PlaneParamValue1 = hv_PlaneParamValue;
ho_in_ZeroRegion1 = ho_ZeroRegion;
Get_OutGlueRegion(ho_in_XImageScaled, ho_in_YImageScaled, ho_in_ZImageScaled,
ho_in_ZeroRegion1, &ho_out_GlueRegion, hv_in_Height1, hv_in_Direc1, hv_in_PlaneParamValue1,
&hv_out_Flag1, &hv_out_Ngtails1);
//线程二,
hv_in_Direc2 = "all";
hv_in_Height2 = 0.15;
hv_in_PlaneParamValue2 = hv_PlaneParamValue;
ho_in_ZeroRegion2 = ho_ZeroRegion;
Get_InGlueRegion(ho_in_XImageScaled, ho_in_YImageScaled, ho_in_ZImageScaled,
ho_in_ZeroRegion2, &ho_in_EdgeRegion, hv_in_Height2, hv_in_PlaneParamValue2,
hv_in_Direc2, &hv_out_Flag2, &hv_out_Ngtails2);
if (0 != (HTuple(hv_out_Flag1 == 1).TupleAnd(hv_out_Flag2 == 1)))
{
//dev_display (ImageReduced)
//dev_display (out_GlueRegion)
//dev_display (in_EdgeRegion)
ho_out_EdgeRegion = ho_out_GlueRegion;
}
else
{
(*hv_out_Ngtails) = "点胶区域提取失败";
return;
}
AreaCenter(ho_in_EdgeRegion, &hv_Area2, &hv_Row2, &hv_Column2);
if (0 != (hv_Area2 < 10000))
{
ho_InspectionRegion = ho_out_GlueRegion;
AreaCenter(ho_out_GlueRegion, &hv_Area3, &hv_Row3, &hv_Column3);
GenCircle(&ho_in_EdgeRegion, hv_Row3, hv_Column3, 10);
}
else
{
//由于产品存在倾斜, 所以先获取内侧region的中心点,
AreaCenter(ho_in_EdgeRegion, &hv_Area5, &hv_Row5, &hv_Column5);
GenEllipse(&ho_Ellipse, hv_Row5, hv_Column5, -(HTuple(10).TupleRad()), 100,
30);
//dev_display (ImageReduced)
//dev_display (in_EdgeRegion)
//dev_display (Ellipse)
//stop ()
DilationRectangle1(ho_out_GlueRegion, &ho_RegionDilation2, 160, 20);
Difference(ho_RegionDilation2, ho_Ellipse, &ho_InspectionRegion);
}
//******************************************************
//dev_display (ImageReduced)
//dev_display (InspectionRegion)
//__________________________________________________________________________
//计算点胶高于基准面的高点
ho_in_InspectionRegion3 = ho_InspectionRegion;
hv_in_PlaneParamValue3 = hv_PlaneParamValue;
Cal_Height1(ho_in_XImageScaled, ho_in_YImageScaled, ho_in_ZImageScaled, ho_in_InspectionRegion3,
hv_in_PlaneParamValue3, &hv_out_PaintRows1, &hv_out_PaintColumns1, &hv_out_Height3,
&hv_out_Flag3);
//计算基准面的高点,
ho_in_InspectionRegion4 = ho_ProductRegion;
hv_in_PlaneParamValue4 = hv_PlaneParamValue;
ho_in_ProductRegion = ho_ProductRegion;
Cal_Height2(ho_in_XImageScaled, ho_in_YImageScaled, ho_in_ZImageScaled, ho_in_InspectionRegion4,
ho_in_ProductRegion, hv_in_PlaneParamValue4, &hv_out_PaintRows2, &hv_out_PaintColumns2,
&hv_out_Height4, &hv_out_Flag4);
//特殊区域补偿,
Difference(ho_out_GlueRegion, ho_in_EdgeRegion, &ho_RegionDifference);
//dev_display (ImageReduced)
//dev_display (RegionDifference)
RegionFeatures(ho_RegionDifference, (HTuple("width").Append("height")), &hv_Value);
PartitionRectangle(ho_RegionDifference, &ho_Partitioned, HTuple(hv_Value[0]) / 3.0,
HTuple(hv_Value[1]) + 30);
SortRegion(ho_Partitioned, &ho_SortedRegions, "character", "true", "column");
SelectObj(ho_SortedRegions, &ho_ObjectSelected, 2);
ShapeTrans(ho_ObjectSelected, &ho_RegionTrans, "rectangle2");
GenRegionPoints(&ho_Region, hv_out_PaintRows1, hv_out_PaintColumns1);
GenCrossContourXld(&ho_Cross, hv_out_PaintRows1, hv_out_PaintColumns1, 6, HTuple(45).TupleRad());
//dev_display (RegionTrans)
//dev_display (Cross)
Intersection(ho_Region, ho_RegionTrans, &ho_RegionIntersection);
AreaCenter(ho_RegionIntersection, &hv_Area4, &hv_Row4, &hv_Column4);
if (0 != (HTuple(hv_out_Height3 > hv_out_Height4).TupleAnd(hv_Area4 > 0)))
{
hv_out_Height3 += 0.05;
}
ho_in_outerEdge = ho_out_GlueRegion;
ho_in_innerEdge = ho_in_EdgeRegion;
ho_in_HYRegion = ho_Ellipse;
Cal_Height3(ho_in_XImageScaled, ho_in_YImageScaled, ho_in_ZImageScaled, ho_in_innerEdge,
ho_in_outerEdge, ho_in_HYRegion, &hv_out_Height, &hv_out_Flag5);
if (0 != (HTuple(HTuple(hv_out_Flag3 == 1).TupleAnd(hv_out_Flag4 == 1)).TupleAnd(hv_out_Flag5 == 1)))
{
(*hv_out_Height1) = hv_out_Height3 - hv_out_Height4;
(*hv_out_Height2) = hv_out_Height;
(*hv_out_Point).Clear();
(*hv_out_Point).Append(hv_out_PaintRows1);
(*hv_out_Point).Append(hv_out_PaintColumns1);
(*hv_out_Point).Append(hv_out_PaintRows2);
(*hv_out_Point).Append(hv_out_PaintColumns2);
(*hv_out_Flag) = 1;
}
//__________________________________________________________________________
//***********************************************************************
}
// catch (Exception)
catch (HException& HDevExpDefaultException)
{
HDevExpDefaultException.ToHTuple(&hv_Exception);
(*hv_out_Flag) = 2;
(*hv_out_Ngtails) = "算法执行错误";
}
return;
}
void Cal_Height1(HObject ho_in_XImageScaled, HObject ho_in_YImageScaled, HObject ho_in_ZImageScaled,
HObject ho_in_InspectionRegion, HTuple hv_in_PlaneParamValue, HTuple* hv_out_PaintRows,
HTuple* hv_out_PaintColumns, HTuple* hv_out_Height, HTuple* hv_out_Flag)
{
// Local iconic variables
// Local control variables
HTuple hv_out_Ngtails, hv_A, hv_B, hv_C, hv_D;
HTuple hv_Rows, hv_Columns, hv_X, hv_Y, hv_Z, hv_Dist;
HTuple hv_Sorted, hv_PosGreater, hv_PosIndices, hv_NegLess;
HTuple hv_NegIndices, hv_Selected2, hv_Sorted1, hv_end1;
HTuple hv_Selected3, hv_Indices, hv_Sorted2, hv_Selected4;
HTuple hv_Exception;
(*hv_out_Height) = -99;
(*hv_out_Flag) = 0;
hv_out_Ngtails = "ok";
try
{
hv_A = ((const HTuple&)hv_in_PlaneParamValue)[0];
hv_B = ((const HTuple&)hv_in_PlaneParamValue)[1];
hv_C = ((const HTuple&)hv_in_PlaneParamValue)[2];
hv_D = ((const HTuple&)hv_in_PlaneParamValue)[3];
GetRegionPoints(ho_in_InspectionRegion, &hv_Rows, &hv_Columns);
GetGrayval(ho_in_XImageScaled, hv_Rows, hv_Columns, &hv_X);
GetGrayval(ho_in_YImageScaled, hv_Rows, hv_Columns, &hv_Y);
GetGrayval(ho_in_ZImageScaled, hv_Rows, hv_Columns, &hv_Z);
hv_Dist = (((hv_A * hv_X) + (hv_B * hv_Y)) + (hv_C * hv_Z)) - hv_D;
if (0 != (hv_C < 0))
{
hv_Dist = -hv_Dist;
}
TupleSort(hv_Dist, &hv_Sorted);
//******************************3***************************
//算法寻找高点, 并寻找最高点坐标
TupleGreaterElem(hv_Dist, 0, &hv_PosGreater);
//寻找大于0的点坐标,
TupleFind(hv_PosGreater, 1, &hv_PosIndices);
if (0 != (HTuple((hv_PosIndices.TupleLength()) < 100).TupleOr(hv_PosIndices == -1)))
{
TupleLessElem(hv_Dist, 0, &hv_NegLess);
TupleFind(hv_NegLess, 1, &hv_NegIndices);
TupleSelect(hv_Dist, hv_NegIndices, &hv_Selected2);
//该处需要对点集进行排序,
TupleSort(hv_Selected2, &hv_Sorted1);
hv_end1 = HTuple((hv_Sorted1.TupleLength()) * 0.95).TupleInt();
TupleSelect(hv_Sorted1, hv_end1, &hv_Selected3);
TupleFind(hv_Dist, hv_Selected3, &hv_Indices);
TupleSelect(hv_Rows, hv_Indices, &(*hv_out_PaintRows));
TupleSelect(hv_Columns, hv_Indices, &(*hv_out_PaintColumns));
(*hv_out_Height) = hv_Selected3;
}
else if (0 != ((hv_PosIndices.TupleLength()) > 100))
{
TupleGreaterElem(hv_Dist, 0, &hv_PosGreater);
TupleFind(hv_PosGreater, 1, &hv_PosIndices);
TupleSelect(hv_Dist, hv_PosIndices, &hv_Selected3);
//该处需要对点集进行排序,
TupleSort(hv_Selected3, &hv_Sorted2);
//end2 := int(|Sorted2|*0.99)
TupleSelect(hv_Sorted2, (hv_Sorted2.TupleLength()) - 10, &hv_Selected4);
TupleFind(hv_Dist, hv_Selected4, &hv_Indices);
TupleSelect(hv_Rows, hv_Indices, &(*hv_out_PaintRows));
TupleSelect(hv_Columns, hv_Indices, &(*hv_out_PaintColumns));
(*hv_out_Height) = hv_Selected4;
}
if (0 != ((*hv_out_Height) != -99))
{
(*hv_out_Flag) = 1;
}
}
// catch (Exception)
catch (HException& HDevExpDefaultException)
{
HDevExpDefaultException.ToHTuple(&hv_Exception);
hv_out_Ngtails = "算法错误";
}
return;
}
void Cal_Height2(HObject ho_in_XImageScaled, HObject ho_in_YImageScaled, HObject ho_in_ZImageScaled,
HObject ho_in_InspectionRegion, HObject ho_in_ProductRegion, HTuple hv_in_PlaneParamValue,
HTuple* hv_out_PaintRows, HTuple* hv_out_PaintColumns, HTuple* hv_out_Height,
HTuple* hv_out_Flag)
{
// Local iconic variables
HObject ho_RegionErosion, ho_RegionDifference;
HObject ho_RegionTrans, ho_RegionErosion1, ho_InspectionRegion;
// Local control variables
HTuple hv_out_Ngtails, hv_A, hv_B, hv_C, hv_D;
HTuple hv_Rows, hv_Columns, hv_X, hv_Y, hv_Z, hv_Dist;
HTuple hv_Sorted, hv_PosGreater, hv_PosIndices, hv_NegLess;
HTuple hv_NegIndices, hv_Selected2, hv_Sorted1, hv_end1;
HTuple hv_Selected3, hv_Indices, hv_Sorted2, hv_end2, hv_Selected4;
HTuple hv_Exception;
(*hv_out_Height) = -99;
(*hv_out_Flag) = 0;
hv_out_Ngtails = "ok";
try
{
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//fill_up (in_InspectionRegion, RegionFillUp)
//opening_circle (RegionFillUp, RegionOpening, 10.5)
//dev_display (in_ZImageScaled)
//dev_display (RegionOpening)
//connection (RegionOpening, ConnectedRegions)
//select_shape_std (ConnectedRegions, SelectedRegions, 'max_area', 70)
//dilation_circle (SelectedRegions, RegionDilation, 35)
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ErosionRectangle1(ho_in_InspectionRegion, &ho_RegionErosion, 10, 70);
Difference(ho_in_InspectionRegion, ho_RegionErosion, &ho_RegionDifference);
ShapeTrans(ho_RegionDifference, &ho_RegionTrans, "rectangle1");
ErosionRectangle1(ho_RegionTrans, &ho_RegionErosion1, 350, 1);
Intersection(ho_RegionDifference, ho_RegionErosion1, &ho_InspectionRegion);
//dev_display (in_ZImageScaled)
//dev_display (RegionErosion1)
//difference (in_ProductRegion, RegionDilation, InspectionRegion)
if (HDevWindowStack::IsOpen())
DispObj(ho_in_ZImageScaled, HDevWindowStack::GetActive());
if (HDevWindowStack::IsOpen())
DispObj(ho_InspectionRegion, HDevWindowStack::GetActive());
hv_A = ((const HTuple&)hv_in_PlaneParamValue)[0];
hv_B = ((const HTuple&)hv_in_PlaneParamValue)[1];
hv_C = ((const HTuple&)hv_in_PlaneParamValue)[2];
hv_D = ((const HTuple&)hv_in_PlaneParamValue)[3];
GetRegionPoints(ho_InspectionRegion, &hv_Rows, &hv_Columns);
GetGrayval(ho_in_XImageScaled, hv_Rows, hv_Columns, &hv_X);
GetGrayval(ho_in_YImageScaled, hv_Rows, hv_Columns, &hv_Y);
GetGrayval(ho_in_ZImageScaled, hv_Rows, hv_Columns, &hv_Z);
hv_Dist = (((hv_A * hv_X) + (hv_B * hv_Y)) + (hv_C * hv_Z)) - hv_D;
if (0 != (hv_C < 0))
{
hv_Dist = -hv_Dist;
}
TupleSort(hv_Dist, &hv_Sorted);
//******************************3***************************
//算法寻找高点, 并寻找最高点坐标
TupleGreaterElem(hv_Dist, 0, &hv_PosGreater);
//寻找大于0的点坐标,
TupleFind(hv_PosGreater, 1, &hv_PosIndices);
if (0 != (HTuple((hv_PosIndices.TupleLength()) < 300).TupleOr(hv_PosIndices == -1)))
{
TupleLessElem(hv_Dist, 0, &hv_NegLess);
TupleFind(hv_NegLess, 1, &hv_NegIndices);
TupleSelect(hv_Dist, hv_NegIndices, &hv_Selected2);
//该处需要对点集进行排序,
TupleSort(hv_Selected2, &hv_Sorted1);
hv_end1 = HTuple((hv_Sorted1.TupleLength()) * 0.95).TupleInt();
TupleSelect(hv_Sorted1, hv_end1, &hv_Selected3);
TupleFind(hv_Dist, hv_Selected3, &hv_Indices);
TupleSelect(hv_Rows, hv_Indices, &(*hv_out_PaintRows));
TupleSelect(hv_Columns, hv_Indices, &(*hv_out_PaintColumns));
(*hv_out_Height) = hv_Selected3;
}
else if (0 != ((hv_PosIndices.TupleLength()) > 300))
{
TupleGreaterElem(hv_Dist, 0, &hv_PosGreater);
TupleFind(hv_PosGreater, 1, &hv_PosIndices);
TupleSelect(hv_Dist, hv_PosIndices, &hv_Selected3);
TupleSort(hv_Selected3, &hv_Sorted2);
hv_end2 = HTuple((hv_Sorted2.TupleLength()) * 0.7).TupleInt();
TupleSelect(hv_Sorted2, hv_end2, &hv_Selected4);
TupleFind(hv_Dist, hv_Selected4, &hv_Indices);
TupleSelect(hv_Rows, hv_Indices, &(*hv_out_PaintRows));
TupleSelect(hv_Columns, hv_Indices, &(*hv_out_PaintColumns));
(*hv_out_Height) = hv_Selected4;
}
if (0 != ((*hv_out_Height) != -99))
{
(*hv_out_Flag) = 1;
}
}
// catch (Exception)
catch (HException& HDevExpDefaultException)
{
HDevExpDefaultException.ToHTuple(&hv_Exception);
hv_out_Ngtails = "算法错误";
}
return;
}
void Cal_Height3(HObject ho_in_XImageScaled, HObject ho_in_YImageScaled, HObject ho_in_ZImageScaled,
HObject ho_in_innerEdge, HObject ho_in_outerEdge, HObject ho_in_HYRegion, HTuple* hv_out_Height,
HTuple* hv_out_Flag)
{
// Local iconic variables
HObject ho_RegionDifference, ho_GlueRegion, ho_RegionErosion;
HObject ho_ImageReduced1, ho_ImageReduced2, ho_ImageReduced3;
// Local control variables
HTuple hv_out_Ngtails, hv_ObjectModel3D, hv_ParFitting;
HTuple hv_ValFitting, hv_ObjectModelPlane, hv_PlaneParamValue;
HTuple hv_A, hv_B, hv_C, hv_D, hv_Rows, hv_Columns, hv_X;
HTuple hv_Y, hv_Z, hv_Dist, hv_Sorted, hv_start1, hv_end1;
HTuple hv_Selected, hv_Max, hv_Exception;
(*hv_out_Height) = -99;
(*hv_out_Flag) = 0;
hv_out_Ngtails = "ok";
try
{
Difference(ho_in_innerEdge, ho_in_HYRegion, &ho_RegionDifference);
Difference(ho_in_outerEdge, ho_in_innerEdge, &ho_GlueRegion);
ErosionCircle(ho_RegionDifference, &ho_RegionErosion, 10.5);
ReduceDomain(ho_in_XImageScaled, ho_RegionErosion, &ho_ImageReduced1);
ReduceDomain(ho_in_YImageScaled, ho_RegionErosion, &ho_ImageReduced2);
ReduceDomain(ho_in_ZImageScaled, ho_RegionErosion, &ho_ImageReduced3);
XyzToObjectModel3d(ho_ImageReduced1, ho_ImageReduced2, ho_ImageReduced3, &hv_ObjectModel3D);
//拟合平面,
hv_ParFitting.Clear();
hv_ParFitting[0] = "primitive_type";
hv_ParFitting[1] = "fitting_algorithm";
hv_ParFitting[2] = "output_xyz_mapping";
hv_ValFitting.Clear();
hv_ValFitting[0] = "plane";
hv_ValFitting[1] = "least_squares_huber";
hv_ValFitting[2] = "false";
//利用产品的边缘拟合平面,
FitPrimitivesObjectModel3d(hv_ObjectModel3D, hv_ParFitting, hv_ValFitting, &hv_ObjectModelPlane);
GetObjectModel3dParams(hv_ObjectModelPlane, "primitive_parameter", &hv_PlaneParamValue);
hv_A = ((const HTuple&)hv_PlaneParamValue)[0];
hv_B = ((const HTuple&)hv_PlaneParamValue)[1];
hv_C = ((const HTuple&)hv_PlaneParamValue)[2];
hv_D = ((const HTuple&)hv_PlaneParamValue)[3];
GetRegionPoints(ho_GlueRegion, &hv_Rows, &hv_Columns);
GetGrayval(ho_in_XImageScaled, hv_Rows, hv_Columns, &hv_X);
GetGrayval(ho_in_YImageScaled, hv_Rows, hv_Columns, &hv_Y);
GetGrayval(ho_in_ZImageScaled, hv_Rows, hv_Columns, &hv_Z);
hv_Dist = (((hv_A * hv_X) + (hv_B * hv_Y)) + (hv_C * hv_Z)) - hv_D;
if (0 != (hv_C < 0))
{
hv_Dist = -hv_Dist;
}
TupleSort(hv_Dist, &hv_Sorted);
hv_start1 = HTuple((hv_Sorted.TupleLength()) * 0.02).TupleInt();
hv_end1 = HTuple((hv_Sorted.TupleLength()) * 0.98).TupleInt();
TupleSelect(hv_Sorted, HTuple::TupleGenSequence(hv_start1, hv_end1, 1), &hv_Selected);
TupleMax(hv_Selected, &hv_Max);
(*hv_out_Height) = hv_Max;
if (0 != ((*hv_out_Height) != -99))
{
(*hv_out_Flag) = 1;
}
}
// catch (Exception)
catch (HException& HDevExpDefaultException)
{
HDevExpDefaultException.ToHTuple(&hv_Exception);
hv_out_Ngtails = "算法错误";
}
return;
}
void Get_InGlueRegion(HObject ho_in_XImageScaled, HObject ho_in_YImageScaled, HObject ho_in_ZImageScaled,
HObject ho_in_ZeroRegion, HObject* ho_in_EdgeRegion, HTuple hv_in_Height, HTuple hv_in_PlaneParamValue,
HTuple hv_in_Direc, HTuple* hv_out_Flag, HTuple* hv_out_Ngtails)
{
// Local iconic variables
HObject ho_RegionFillUp, ho_Tempregion, ho_Region2;
HObject ho_RegionOpening, ho_ConnectedRegions3, ho_Regions;
HObject ho_Out_ROI, ho_ConnectedRegions4, ho_SelectedRegions3;
HObject ho_SelectedRegions, ho_closingRegion;
// Local control variables
HTuple hv_A, hv_B, hv_C, hv_D, hv_Rows, hv_Columns;
HTuple hv_X, hv_Y, hv_Z, hv_Dist, hv_Com_Tuple, hv_Prod_X;
HTuple hv_Prod_Y, hv_HeightOut, hv_Com_Tuple2, hv_Com_Tuple1;
HTuple hv_And, hv_Number, hv_Value, hv_Area, hv_Row, hv_Column;
HTuple hv_Exception;
(*hv_out_Flag) = 0;
(*hv_out_Ngtails) = "ok";
try
{
FillUp(ho_in_ZeroRegion, &ho_RegionFillUp);
Difference(ho_RegionFillUp, ho_in_ZeroRegion, &ho_Tempregion);
hv_A = ((const HTuple&)hv_in_PlaneParamValue)[0];
hv_B = ((const HTuple&)hv_in_PlaneParamValue)[1];
hv_C = ((const HTuple&)hv_in_PlaneParamValue)[2];
hv_D = ((const HTuple&)hv_in_PlaneParamValue)[3];
GetRegionPoints(ho_Tempregion, &hv_Rows, &hv_Columns);
GetGrayval(ho_in_XImageScaled, hv_Rows, hv_Columns, &hv_X);
GetGrayval(ho_in_YImageScaled, hv_Rows, hv_Columns, &hv_Y);
GetGrayval(ho_in_ZImageScaled, hv_Rows, hv_Columns, &hv_Z);
hv_Dist = (((hv_A * hv_X) + (hv_B * hv_Y)) + (hv_C * hv_Z)) - hv_D;
if (0 != (hv_C < 0))
{
hv_Dist = hv_Dist * -1;
}
if (0 != (hv_in_Direc == HTuple("Pos")))
{
TupleGreaterElem(hv_Dist, hv_in_Height, &hv_Com_Tuple);
TupleMult(hv_Rows, hv_Com_Tuple, &hv_Prod_X);
TupleMult(hv_Columns, hv_Com_Tuple, &hv_Prod_Y);
GenRegionPoints(&ho_Region2, hv_Prod_X, hv_Prod_Y);
ClosingCircle(ho_Region2, &ho_RegionOpening, 3.5);
Connection(ho_RegionOpening, &ho_ConnectedRegions3);
SelectShape(ho_ConnectedRegions3, &ho_Regions, "area", "and", 2, 9999999999999);
Union1(ho_Regions, &ho_Out_ROI);
}
else if (0 != (hv_in_Direc == HTuple("Neg")))
{
hv_HeightOut = hv_HeightOut * -1;
TupleLessElem(hv_Dist, hv_in_Height, &hv_Com_Tuple);
TupleMult(hv_Rows, hv_Com_Tuple, &hv_Prod_X);
TupleMult(hv_Columns, hv_Com_Tuple, &hv_Prod_Y);
GenRegionPoints(&ho_Region2, hv_Prod_X, hv_Prod_Y);
ClosingCircle(ho_Region2, &ho_RegionOpening, 3.5);
Connection(ho_RegionOpening, &ho_ConnectedRegions3);
SelectShape(ho_ConnectedRegions3, &ho_Regions, "area", "and", 2, 9999999999999);
Union1(ho_Regions, &ho_Out_ROI);
}
else if (0 != (hv_in_Direc == HTuple("all")))
{
TupleLessElem(hv_Dist, hv_in_Height, &hv_Com_Tuple2);
TupleGreaterElem(hv_Dist, -hv_in_Height, &hv_Com_Tuple1);
TupleAnd(hv_Com_Tuple1, hv_Com_Tuple2, &hv_And);
TupleMult(hv_Rows, hv_And, &hv_Prod_X);
TupleMult(hv_Columns, hv_And, &hv_Prod_Y);
GenRegionPoints(&ho_Region2, hv_Prod_X, hv_Prod_Y);
ClosingCircle(ho_Region2, &ho_RegionOpening, 3.5);
Connection(ho_RegionOpening, &ho_ConnectedRegions3);
SelectShape(ho_ConnectedRegions3, &ho_Regions, "area", "and", 2, 9999999999999);
Union1(ho_Regions, &ho_Out_ROI);
}
ClosingCircle(ho_Out_ROI, &ho_RegionOpening, 4.5);
Connection(ho_RegionOpening, &ho_ConnectedRegions4);
SelectShape(ho_ConnectedRegions4, &ho_SelectedRegions3, "max_diameter", "and",
450, 900);
CountObj(ho_SelectedRegions3, &hv_Number);
if (0 != (hv_Number > 1))
{
RegionFeatures(ho_SelectedRegions3, "max_diameter", &hv_Value);
SelectShape(ho_SelectedRegions3, &ho_SelectedRegions, "max_diameter", "and",
(hv_Value.TupleMax()) - 1, (hv_Value.TupleMax()) + 1);
ho_SelectedRegions3 = ho_SelectedRegions;
}
ClosingCircle(ho_SelectedRegions3, &ho_closingRegion, 20);
FillUp(ho_closingRegion, &(*ho_in_EdgeRegion));
AreaCenter((*ho_in_EdgeRegion), &hv_Area, &hv_Row, &hv_Column);
if (0 != (hv_Area > 0))
{
(*hv_out_Flag) = 1;
}
}
// catch (Exception)
catch (HException& HDevExpDefaultException)
{
HDevExpDefaultException.ToHTuple(&hv_Exception);
(*hv_out_Ngtails) = "算法错误";
}
return;
}
void Get_OutGlueRegion(HObject ho_in_XImageScaled, HObject ho_in_YImageScaled, HObject ho_in_ZImageScaled,
HObject ho_in_ZeroRegion, HObject* ho_out_GlueRegion, HTuple hv_in_Height, HTuple hv_in_Direc,
HTuple hv_in_PlaneParamValue, HTuple* hv_out_Flag, HTuple* hv_out_Ngtails)
{
// Local iconic variables
HObject ho_RegionFillUp, ho_Tempregion, ho_Region2;
HObject ho_RegionOpening, ho_ConnectedRegions3, ho_Regions;
HObject ho_Out_ROI, ho_SelectedRegions2, ho_RegionFillUp1;
HObject ho_RegionDifference2, ho_EdgeRegion, ho_ConnectedRegions1;
// Local control variables
HTuple hv_A, hv_B, hv_C, hv_D, hv_Rows, hv_Columns;
HTuple hv_X, hv_Y, hv_Z, hv_Dist, hv_Com_Tuple, hv_Prod_X;
HTuple hv_Prod_Y, hv_Com_Tuple2, hv_Com_Tuple1, hv_And;
HTuple hv_Area, hv_Row, hv_Column, hv_Exception;
(*hv_out_Flag) = 0;
(*hv_out_Ngtails) = "ok";
try
{
FillUp(ho_in_ZeroRegion, &ho_RegionFillUp);
Difference(ho_RegionFillUp, ho_in_ZeroRegion, &ho_Tempregion);
//dev_display (in_XIageScaled)
if (HDevWindowStack::IsOpen())
DispObj(ho_Tempregion, HDevWindowStack::GetActive());
hv_A = ((const HTuple&)hv_in_PlaneParamValue)[0];
hv_B = ((const HTuple&)hv_in_PlaneParamValue)[1];
hv_C = ((const HTuple&)hv_in_PlaneParamValue)[2];
hv_D = ((const HTuple&)hv_in_PlaneParamValue)[3];
GetRegionPoints(ho_Tempregion, &hv_Rows, &hv_Columns);
GetGrayval(ho_in_XImageScaled, hv_Rows, hv_Columns, &hv_X);
GetGrayval(ho_in_YImageScaled, hv_Rows, hv_Columns, &hv_Y);
GetGrayval(ho_in_ZImageScaled, hv_Rows, hv_Columns, &hv_Z);
hv_Dist = (((hv_A * hv_X) + (hv_B * hv_Y)) + (hv_C * hv_Z)) - hv_D;
if (0 != (hv_C < 0))
{
hv_Dist = hv_Dist * -1;
}
if (0 != (hv_in_Direc == HTuple("Pos")))
{
TupleGreaterElem(hv_Dist, hv_in_Height, &hv_Com_Tuple);
TupleMult(hv_Rows, hv_Com_Tuple, &hv_Prod_X);
TupleMult(hv_Columns, hv_Com_Tuple, &hv_Prod_Y);
GenRegionPoints(&ho_Region2, hv_Prod_X, hv_Prod_Y);
ClosingCircle(ho_Region2, &ho_RegionOpening, 3.5);
Connection(ho_RegionOpening, &ho_ConnectedRegions3);
SelectShape(ho_ConnectedRegions3, &ho_Regions, "area", "and", 2, 9999999999999);
Union1(ho_Regions, &ho_Out_ROI);
}
else if (0 != (hv_in_Direc == HTuple("Neg")))
{
//Height := Height*(-1)
TupleLessElem(hv_Dist, -hv_in_Height, &hv_Com_Tuple);
TupleMult(hv_Rows, hv_Com_Tuple, &hv_Prod_X);
TupleMult(hv_Columns, hv_Com_Tuple, &hv_Prod_Y);
GenRegionPoints(&ho_Region2, hv_Prod_X, hv_Prod_Y);
ClosingCircle(ho_Region2, &ho_RegionOpening, 3.5);
Connection(ho_RegionOpening, &ho_ConnectedRegions3);
SelectShape(ho_ConnectedRegions3, &ho_Regions, "area", "and", 2, 9999999999999);
Union1(ho_Regions, &ho_Out_ROI);
}
else if (0 != (hv_in_Direc == HTuple("all")))
{
TupleLessElem(hv_Dist, hv_in_Height, &hv_Com_Tuple2);
TupleGreaterElem(hv_Dist, -hv_in_Height, &hv_Com_Tuple1);
TupleAnd(hv_Com_Tuple1, hv_Com_Tuple2, &hv_And);
TupleMult(hv_Rows, hv_And, &hv_Prod_X);
TupleMult(hv_Columns, hv_And, &hv_Prod_Y);
GenRegionPoints(&ho_Region2, hv_Prod_X, hv_Prod_Y);
ClosingCircle(ho_Region2, &ho_RegionOpening, 3.5);
Connection(ho_RegionOpening, &ho_ConnectedRegions3);
SelectShape(ho_ConnectedRegions3, &ho_Regions, "area", "and", 2, 9999999999999);
Union1(ho_Regions, &ho_Out_ROI);
}
Connection(ho_Out_ROI, &ho_ConnectedRegions3);
SelectShapeStd(ho_ConnectedRegions3, &ho_SelectedRegions2, "max_area", 70);
FillUp(ho_SelectedRegions2, &ho_RegionFillUp1);
Difference(ho_RegionFillUp1, ho_SelectedRegions2, &ho_RegionDifference2);
OpeningCircle(ho_RegionDifference2, &ho_EdgeRegion, 5.5);
Connection(ho_EdgeRegion, &ho_ConnectedRegions1);
SelectShapeStd(ho_ConnectedRegions1, &(*ho_out_GlueRegion), "max_area", 70);
AreaCenter((*ho_out_GlueRegion), &hv_Area, &hv_Row, &hv_Column);
if (0 != (hv_Area > 0))
{
(*hv_out_Flag) = 1;
}
}
// catch (Exception)
catch (HException& HDevExpDefaultException)
{
HDevExpDefaultException.ToHTuple(&hv_Exception);
(*hv_out_Flag) = 0;
(*hv_out_Ngtails) = "算法错误";
}
return;
}
void Get_ROI1(HObject ho_XImageScaled, HObject ho_YImageScaled, HObject ho_ZImageScaled,
HObject ho_ZeroRegion, HObject ho_TmpRegion, HObject* ho_Out_ROI, HTuple hv_Direc,
HTuple hv_Height)
{
// Local iconic variables
HObject ho_ImageReduced1, ho_ImageReduced2, ho_ImageReduced3;
HObject ho_Region2, ho_RegionOpening, ho_ConnectedRegions3;
HObject ho_Regions;
// Local control variables
HTuple hv_Debug, hv_ObjectModel3D, hv_ParFitting;
HTuple hv_ValFitting, hv_ObjectModelPlane, hv_GenParamValue;
HTuple hv_A, hv_B, hv_C, hv_D, hv_WindowHandleOut, hv_PoseOut;
HTuple hv_Rows, hv_Columns, hv_X, hv_Y, hv_Z, hv_Dist;
HTuple hv_Com_Tuple, hv_Prod_X, hv_Prod_Y, hv_Com_Tuple2;
HTuple hv_Com_Tuple1, hv_And, hv_Exception;
//**********************************************************************
//Function Name: Get_ROI1
//Author: 胡汉兵,
//Description: 获取有效的检测面,
//Input:
// XImageScaled, X向图像
// YImageScaled, Y向图像
// ZImageScaled, Z向图像
// Zero_Region, 基准平面的区域,
// InspectioRegion, 提取有效的检测面的Region
// Direc, 挑选极柱面的方向, 'Pos':极柱面上+ 'Neg': 极柱面下- 'all': 极柱上下范围内的点集,
// Height, 挑选极柱面的高度值(mm)
//output: Out_ROI, 挑选的ROI区域,
//verion change log: 胡汉兵 2023-09-07 V1.1 Create new function
// 胡汉兵 2023-11-01 V1.2 新增面上下范围内的点集,
//
//**********************************************************************
hv_Debug = 0;
try
{
ReduceDomain(ho_XImageScaled, ho_ZeroRegion, &ho_ImageReduced1);
ReduceDomain(ho_YImageScaled, ho_ZeroRegion, &ho_ImageReduced2);
ReduceDomain(ho_ZImageScaled, ho_ZeroRegion, &ho_ImageReduced3);
XyzToObjectModel3d(ho_ImageReduced1, ho_ImageReduced2, ho_ImageReduced3, &hv_ObjectModel3D);
//拟合平面,
hv_ParFitting.Clear();
hv_ParFitting[0] = "primitive_type";
hv_ParFitting[1] = "fitting_algorithm";
hv_ParFitting[2] = "output_xyz_mapping";
hv_ValFitting.Clear();
hv_ValFitting[0] = "plane";
hv_ValFitting[1] = "least_squares_huber";
hv_ValFitting[2] = "false";
//利用产品的边缘拟合平面,
FitPrimitivesObjectModel3d(hv_ObjectModel3D, hv_ParFitting, hv_ValFitting, &hv_ObjectModelPlane);
GetObjectModel3dParams(hv_ObjectModelPlane, "primitive_parameter", &hv_GenParamValue);
hv_A = ((const HTuple&)hv_GenParamValue)[0];
hv_B = ((const HTuple&)hv_GenParamValue)[1];
hv_C = ((const HTuple&)hv_GenParamValue)[2];
hv_D = ((const HTuple&)hv_GenParamValue)[3];
GetRegionPoints(ho_TmpRegion, &hv_Rows, &hv_Columns);
GetGrayval(ho_XImageScaled, hv_Rows, hv_Columns, &hv_X);
GetGrayval(ho_YImageScaled, hv_Rows, hv_Columns, &hv_Y);
GetGrayval(ho_ZImageScaled, hv_Rows, hv_Columns, &hv_Z);
hv_Dist = (((hv_A * hv_X) + (hv_B * hv_Y)) + (hv_C * hv_Z)) - hv_D;
if (0 != (hv_C < 0))
{
hv_Dist = hv_Dist * -1;
}
if (0 != (hv_Direc == HTuple("Pos")))
{
TupleGreaterElem(hv_Dist, hv_Height, &hv_Com_Tuple);
TupleMult(hv_Rows, hv_Com_Tuple, &hv_Prod_X);
TupleMult(hv_Columns, hv_Com_Tuple, &hv_Prod_Y);
GenRegionPoints(&ho_Region2, hv_Prod_X, hv_Prod_Y);
ClosingCircle(ho_Region2, &ho_RegionOpening, 3.5);
Connection(ho_RegionOpening, &ho_ConnectedRegions3);
SelectShape(ho_ConnectedRegions3, &ho_Regions, "area", "and", 2, 9999999999999);
Union1(ho_Regions, &(*ho_Out_ROI));
}
else if (0 != (hv_Direc == HTuple("Neg")))
{
hv_Height = hv_Height * -1;
TupleLessElem(hv_Dist, hv_Height, &hv_Com_Tuple);
TupleMult(hv_Rows, hv_Com_Tuple, &hv_Prod_X);
TupleMult(hv_Columns, hv_Com_Tuple, &hv_Prod_Y);
GenRegionPoints(&ho_Region2, hv_Prod_X, hv_Prod_Y);
ClosingCircle(ho_Region2, &ho_RegionOpening, 3.5);
Connection(ho_RegionOpening, &ho_ConnectedRegions3);
SelectShape(ho_ConnectedRegions3, &ho_Regions, "area", "and", 2, 9999999999999);
Union1(ho_Regions, &(*ho_Out_ROI));
}
else if (0 != (hv_Direc == HTuple("all")))
{
TupleLessElem(hv_Dist, hv_Height, &hv_Com_Tuple2);
TupleGreaterElem(hv_Dist, -hv_Height, &hv_Com_Tuple1);
TupleAnd(hv_Com_Tuple1, hv_Com_Tuple2, &hv_And);
TupleMult(hv_Rows, hv_And, &hv_Prod_X);
TupleMult(hv_Columns, hv_And, &hv_Prod_Y);
GenRegionPoints(&ho_Region2, hv_Prod_X, hv_Prod_Y);
ClosingCircle(ho_Region2, &ho_RegionOpening, 3.5);
Connection(ho_RegionOpening, &ho_ConnectedRegions3);
SelectShape(ho_ConnectedRegions3, &ho_Regions, "area", "and", 2, 9999999999999);
Union1(ho_Regions, &(*ho_Out_ROI));
}
}
// catch (Exception)
catch (HException& HDevExpDefaultException)
{
HDevExpDefaultException.ToHTuple(&hv_Exception);
}
return;
}