/// <summary> /// 重定位 /// </summary> /// <param name="stdRow">訓練圖形中心點 Row</param> /// <param name="stdCol">訓練圖形中心點 Col</param> /// <param name="curModelAngle">目前圖形 Angle</param> /// <param name="hv_OffsetRow">(現在圖形中心點 Row - 訓練圖形中心點 Row</param> /// <param name="hv_OffsetCol">(現在圖形中心點 Col - 訓練圖形中心點 Col</param> /// <param name="roiRow">訓練圖形時設定的 ROI Row</param> /// <param name="roiCol">訓練圖形時設定的 ROI Col</param> /// <param name="curROI_Row">回傳值 - 重定位後的 Row</param> /// <param name="curROI_Col">回傳值 - 重定位後的 Col</param> public static void ReLocater(HTuple stdRow, HTuple stdCol, HTuple curModelAngle, HTuple offsetRow, HTuple offsetCol , HTuple roiRow, HTuple roiCol , out HTuple curROI_Row, out HTuple curROI_Col) { //roiRow = 1110; //roiCol = 630; //STD 向量 STD_A_1_1_ HTuple veterRow = roiRow - stdRow; HTuple vertCol = roiCol - stdCol; HTuple roiVeterCol = (vertCol * (curModelAngle.TupleCos())) + (veterRow * (curModelAngle.TupleSin())); HTuple roiVectorRow = (veterRow * (curModelAngle.TupleCos())) - (vertCol * (curModelAngle.TupleSin())); //目前圖形 A_1_1_ 位置 curROI_Row = (stdRow + roiVectorRow) + offsetRow; curROI_Col = (stdCol + roiVeterCol) + offsetCol; }
/// <summary> /// /// </summary> /// <param name="stdRow">訓練圖形中心點 Row</param> /// <param name="stdCol">訓練圖形中心點 Col</param> /// <param name="curModelAngle">目前圖形 Angle</param> /// <param name="hv_OffsetRow">hv_Img_Row - hv_STD_Row (現在圖形中心點 Row - 訓練圖形中心點 Row</param> /// <param name="hv_OffsetCol">hv_Img_Col - hv_STD_Col (現在圖形中心點 Col - 訓練圖形中心點 Col</param> /// <param name="roiRow">訓練圖形時設定的 ROI Row</param> /// <param name="roiCol">訓練圖形時設定的 ROI Col</param> /// <param name="curROI_Row">回傳值 - 重定位後的 Row</param> /// <param name="curROI_Col">回傳值 - 重定位後的 Col</param> private void ReLocater(HTuple stdRow, HTuple stdCol, HTuple curModelAngle, HTuple offsetRow, HTuple offsetCol , HTuple roiRow, HTuple roiCol , out HTuple curROI_Row, out HTuple curROI_Col) { //roiRow = 1110; //roiCol = 630; //STD 向量 STD_A_1_1_ HTuple veterRow = roiRow - stdRow; HTuple vertCol = roiCol - stdCol; HTuple roiVeterCol = (vertCol * (curModelAngle.TupleCos())) + (veterRow * (curModelAngle.TupleSin())); HTuple roiVectorRow = (veterRow * (curModelAngle.TupleCos())) - (vertCol * (curModelAngle.TupleSin())); //目前圖形 A_1_1_ 位置 curROI_Row = (stdRow + roiVectorRow) + offsetRow; curROI_Col = (stdCol + roiVeterCol) + offsetCol; }
public bool GetMaterialAngleAndOffset_Magnet(InputLocateParam IptParam, ref OutputLocateParam OptParam) { //计算时间,开始时间。 DateTime timeStart = System.DateTime.Now; //输出参数 //物料中心、角度输出参数 OptParam.RotateAngle = 0.0; OptParam.OffsetX = 0.0; OptParam.OffsetY = 0.0; OptParam.CenterX = 0.0; OptParam.CenterY = 0.0; //物料最小外接矩形输出参数 OptParam.RectX = 0.0; OptParam.RectY = 0.0; OptParam.RectAngle = 0.0; OptParam.RectWidth = 0.0; OptParam.RectHeight = 0.0; //程序运行输出参数 OptParam.ErrorCode = 0; OptParam.ExceptionString = ""; OptParam.SpanTime = 0; try { //错误1:判断输入图像是否传入。 if (IptParam.ImagePtr == null || IptParam.ImagePtr.Length <= 0) { OptParam.ErrorCode = 1; return(false); } //错误2:判断输入二值化阈值是否输入正确。 if ((IptParam.ThresholdMin < 0 || IptParam.ThresholdMin > 254) || (IptParam.ThresholdMax < 1 || IptParam.ThresholdMax > 255) || (IptParam.ThresholdMin >= IptParam.ThresholdMax)) { OptParam.ErrorCode = 2; return(false); } //错误3:判断图像大小或ROI区域输入是否正确。 if ((IptParam.ImageW < 4 || IptParam.ImageH < 4) || (IptParam.LeftPt < 0 || IptParam.LeftPt >= IptParam.RightPt) //多余|| (IptParam.RightPt <= IptParam.LeftPt || (IptParam.RightPt > IptParam.ImageW) || (IptParam.TopPt < 0 || IptParam.TopPt >= IptParam.BottomPt) //多余|| (IptParam.BottomPt <= IptParam.TopPt || (IptParam.BottomPt > IptParam.ImageH) ) { OptParam.ErrorCode = 3; return(false); } //////////////////////////////////////////////////////////////////////////////// /******************************HALCON图像处理模块******************************/ //定义与初始化图像、数据变量 HTuple Windowhandle = new HTuple(); HObject InputImageObj = new HObject(); HObject TestImageObj = new HObject(); HObject RectObj = new HObject(); InputImageObj.GenEmptyObj(); TestImageObj.GenEmptyObj(); RectObj.GenEmptyObj(); //将图像指针指向0地址。 IntPtr ImagePtr = IntPtr.Zero; //传入图像指针 ImagePtr = ArrayToIntptr(IptParam.ImagePtr); //错误4:判断图像指针是否还在0地址。 if (ImagePtr == IntPtr.Zero) { OptParam.ErrorCode = 4; return(false); } //生成图像变量 HOperatorSet.GenImage1(out InputImageObj, "byte", IptParam.ImageW, IptParam.ImageH, ImagePtr); FreeIntptr(ImagePtr); //生成ROI HOperatorSet.GenRectangle1(out RectObj, IptParam.TopPt, IptParam.LeftPt, IptParam.BottomPt, IptParam.RightPt); HOperatorSet.ReduceDomain(InputImageObj, RectObj, out TestImageObj); HOperatorSet.CropDomain(TestImageObj, out TestImageObj); HTuple tmpW, tmpH; HOperatorSet.GetImageSize(TestImageObj, out tmpW, out tmpH); HObject GrayImage, ThesholdRegion, DealRegion, ReduceImage; HObject Edges, ContoursSplit, SelectedXLDC, ContCircle; HTuple Row = null, Column = null, Radius = null; HTuple StartPhi = null, EndPhi = null, PointOrder = null; //二值化和进一步ROI HOperatorSet.Rgb1ToGray(TestImageObj, out GrayImage); HOperatorSet.Threshold(GrayImage, out ThesholdRegion, IptParam.ThresholdMin, IptParam.ThresholdMax); HOperatorSet.DilationCircle(ThesholdRegion, out DealRegion, 3); HOperatorSet.ReduceDomain(GrayImage, DealRegion, out ReduceImage); //提取边缘 HOperatorSet.EdgesSubPix(ReduceImage, out Edges, "canny", 1, 20, 40); HOperatorSet.SegmentContoursXld(Edges, out ContoursSplit, "lines_circles", 5, 4, 2); //选出圆弧边 HOperatorSet.SelectShapeXld(ContoursSplit, out SelectedXLDC, "ratio", "and", 1, 5); HOperatorSet.FitCircleContourXld(SelectedXLDC, "algebraic", -1, 0, 0, 3, 2, out Row, out Column, out Radius, out StartPhi, out EndPhi, out PointOrder); HOperatorSet.GenCircleContourXld(out ContCircle, Row, Column, Radius, 0, 6.28318, "positive", 1); //stop () //计算出中间圆环 HTuple Radius_Mean = null, Row_Mean = null, Column_Mean = null; HObject Center_ContCircle; HOperatorSet.TupleMean(Radius, out Radius_Mean); HOperatorSet.TupleMean(Row, out Row_Mean); HOperatorSet.TupleMean(Column, out Column_Mean); HOperatorSet.GenCircleContourXld(out Center_ContCircle, Row_Mean, Column_Mean, Radius_Mean, 0, 6.28318, "positive", 1); //显示中心圆环 //得出两直边的中间直边 HObject ContourL_center, Cross1; HTuple RowBegin = null; HTuple ColBegin = null, RowEnd = null, ColEnd = null; HTuple Row_I = null; HTuple Column_I = null, IsOverlapping = null, PhiL = null; HTuple PhiL_Mean = null, Line_center = null, Row_Cross = null; HTuple Column_Cross = null, ANGLE = null; HOperatorSet.IntersectionLines(RowBegin.TupleSelect(0), ColBegin.TupleSelect( 0), RowEnd.TupleSelect(0), ColEnd.TupleSelect(0), RowBegin.TupleSelect( 1), ColBegin.TupleSelect(1), RowEnd.TupleSelect(1), ColEnd.TupleSelect( 1), out Row_I, out Column_I, out IsOverlapping); HOperatorSet.LineOrientation(RowBegin, ColBegin, RowEnd, ColEnd, out PhiL); HOperatorSet.TupleMean(PhiL, out PhiL_Mean); Line_center = new HTuple(); Line_center = Line_center.TupleConcat(Row_I); Line_center = Line_center.TupleConcat(Column_I); Line_center = Line_center.TupleConcat(Row_I + ((Radius.TupleSelect( 0)) * (PhiL_Mean.TupleSin()))); Line_center = Line_center.TupleConcat(Column_I - ((Radius.TupleSelect( 0)) * (PhiL_Mean.TupleCos()))); HOperatorSet.GenContourPolygonXld(out ContourL_center, ((Line_center.TupleSelect( 0))).TupleConcat(Line_center.TupleSelect(2)), ((Line_center.TupleSelect( 1))).TupleConcat(Line_center.TupleSelect(3))); //显示中间直线 //中间直线角度 ANGLE = PhiL_Mean.TupleDeg(); //得到中间直线和中间圆环的中心交点。 HOperatorSet.IntersectionLineCircle(Row_I, Column_I, Row_I + ((Radius.TupleSelect(0)) * (PhiL_Mean.TupleSin())) , Column_I - ((Radius.TupleSelect(0)) * (PhiL_Mean.TupleCos())) , Row_Mean, Column_Mean, Radius_Mean, StartPhi.TupleSelect(0) , EndPhi.TupleSelect(0), "negative", out Row_Cross, out Column_Cross); HOperatorSet.GenCrossContourXld(out Cross1, Row_Cross, Column_Cross, 20, 0); //显示中心交点 //////////////////////////////////////////////////////////////////////////////// GC.Collect(); } catch (HOperatorException e) { string str, expMsg; HTuple expTuple; e.ToHTuple(out expTuple); expMsg = expTuple[1].S; str = "Halcon 错误:\r\n"; str += expMsg; //输出到输出结构参数ExceptionString。 OptParam.ExceptionString = str; OptParam.ErrorCode = 255; return(false); } //计算时间,输出运行时间 DateTime timeEnd = System.DateTime.Now; TimeSpan timeSpan = timeEnd.Subtract(timeStart); OptParam.SpanTime = timeSpan.Milliseconds; return(true); }
//DateTime t1, t2, t3, t4,t5,t6,t7; private void action() { HObject ho_Rectangle, ho_ImageReduced, imagexz, retxz; HObject ho_Border, ho_Circle, ho_ImageReduced2, ho_Region, ho_RegionLines; HObject ho_Rectangle1 = null, ho_ImageReduced1 = null, ho_Border1 = null; HObject ho_SelectedContours = null; // Local control variables HTuple hv_Row1 = null, hv_Column1 = null, hv_Angle = null, hv_l1 = null, hv_l2 = null; HTuple hv_Score = null, hv_zj = null, hv_Area = null, hv_Row3 = null; HTuple hv_Column2 = null, hv_Phi1 = null, hv_zpj = null; HTuple hv_zcj = null, hv_zpr = null, hv_zpc = null, hv_zcr = null; HTuple hv_zcc = null, hv_Tp = null, hv_Tc = null, hv_Length = null; HTuple hv_dx = null, hv_dn = null, hv_dxr = null, hv_dxc = null; HTuple hv_dnr = null, hv_dnc = null, hv_i = null; //HTuple hv_Col = new HTuple(), hv_dis = new HTuple(), hv_dxi = new HTuple(), hv_Row2 = new HTuple(); //HTuple hv_dni = new HTuple(), hv_Ix = new HTuple(), hv_In = new HTuple(); //HTuple hv_Exception = new HTuple(), hv_j = new HTuple(), hv_jh = new HTuple(); HTuple hv_disdxpx = null; HTuple hv_disdnpx = null; HTuple hv_djd = null, hv_djdr = null, hv_djdc = null, hv_djx = null; HTuple hv_djxr = null, hv_djxc = null, hv_xjd = null, hv_xjdr = null; HTuple hv_xjdc = null, hv_xjx = null, hv_xjxr = null, hv_xjxc = null; HTuple hv_ljd = null, hv_ljx = null, hv_dj = null, hv_xj = null; HTuple hv_djn = null, hv_djm = null, hv_xjn = null, hv_xjm = null; HTuple hv_djzd = null, hv_djzx = null, hv_xjzd = null; HTuple hv_xjzx = null, hv_ljtd = null, hv_ljtx = null; HTuple hv_Length3 = null, hv_Length4 = null, hv_ljdx = null; HTuple hv_ljdn = null, hv_ljxx = null, hv_ljxn = null; HTuple hv_ljdm = null, hv_ljxm = null, hv_djiaodu = null; HTuple hv_xjiaodu = null, hv_Length5 = null, hv_Length6 = null; HTuple hv_djiaodu1 = null, hv_xjiaodu1 = null, hv_jjdd = null; HTuple hv_jjdx = null, hv_jjxd = null, hv_jjxx = null; HTuple hv_jjdm = null, hv_jjxm = null; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Rectangle); HOperatorSet.GenEmptyObj(out ho_ImageReduced); HOperatorSet.GenEmptyObj(out ho_Border); HOperatorSet.GenEmptyObj(out ho_Circle); HOperatorSet.GenEmptyObj(out ho_ImageReduced2); HOperatorSet.GenEmptyObj(out ho_Region); HOperatorSet.GenEmptyObj(out ho_Rectangle1); HOperatorSet.GenEmptyObj(out ho_ImageReduced1); HOperatorSet.GenEmptyObj(out ho_Border1); HOperatorSet.GenEmptyObj(out ho_SelectedContours); HOperatorSet.GenEmptyObj(out ho_RegionLines); HOperatorSet.GenEmptyObj(out imagexz); HOperatorSet.GenEmptyObj(out retxz); HOperatorSet.Union1(algorithm.Region, out RegionToDisp); //t3 = DateTime.Now; try { if (hv_ModelID == null) { HOperatorSet.ReadShapeModel(PathHelper.currentProductPath + @"\lwspmd.shm", out hv_ModelID); } retxz.Dispose(); HOperatorSet.GenRectangle2(out retxz, hv1_Row, hv1_Column, hv1_Phi, hv1_Length1, hv1_Length2); imagexz.Dispose(); HOperatorSet.ReduceDomain(Image, retxz, out imagexz); HOperatorSet.FindShapeModel(imagexz, hv_ModelID, -3.14, 6.29, 0.7, 0, 0.1, "least_squares", 0, 0.8, out hv_Row1, out hv_Column1, out hv_Angle, out hv_Score); hv_zj = (((((hv_Row1.TupleMax()) - (hv_Row1.TupleMin())) * ((hv_Row1.TupleMax() ) - (hv_Row1.TupleMin()))) + (((hv_Column1.TupleMax()) - (hv_Column1.TupleMin() )) * ((hv_Column1.TupleMax()) - (hv_Column1.TupleMin()))))).TupleSqrt(); ho_Circle.Dispose(); HOperatorSet.GenCircle(out ho_Circle, hv_Row1.TupleMean(), hv_Column1.TupleMean() , hv_zj / 2); ho_ImageReduced2.Dispose(); HOperatorSet.ReduceDomain(Image, ho_Circle, out ho_ImageReduced2); ho_Region.Dispose(); HOperatorSet.Threshold(ho_ImageReduced2, out ho_Region, 0, thv); HOperatorSet.AreaCenter(ho_Region, out hv_Area, out hv_Row3, out hv_Column2); HOperatorSet.OrientationRegion(ho_Region, out hv_Phi1); hv_zpj = hv_Phi1.Clone(); hv_zcj = hv_Phi1 + ((new HTuple(90)).TupleRad()); hv_zpr = hv_Row3 + ((hv_zcj.TupleSin()) * hv_zj); hv_zpc = hv_Column2 - ((hv_zcj.TupleCos()) * hv_zj); hv_zcr = hv_Row3 + ((hv_zpj.TupleSin()) * hv_zj); hv_zcc = hv_Row3 - ((hv_zpj.TupleCos()) * hv_zj); HOperatorSet.TupleTan(hv_zpj, out hv_Tp); HOperatorSet.TupleTan(hv_zcj, out hv_Tc); HOperatorSet.TupleLength(hv_Row1, out hv_Length); hv_dx = new HTuple(); hv_dn = new HTuple(); hv_dxr = new HTuple(); hv_dxc = new HTuple(); hv_dnr = new HTuple(); hv_dnc = new HTuple(); HTuple end_val29 = hv_Length - 1; HTuple step_val29 = 1; for (hv_i = 0; hv_i.Continue(end_val29, step_val29); hv_i = hv_i.TupleAdd(step_val29)) { //try ho_Rectangle1.Dispose(); HOperatorSet.GenRectangle2(out ho_Rectangle1, hv_Row1.TupleSelect(hv_i), hv_Column1.TupleSelect(hv_i), hv_Phi + (hv_Angle.TupleSelect(hv_i)), hv_Length1, hv_Length2); ho_ImageReduced1.Dispose(); HOperatorSet.ReduceDomain(Image, ho_Rectangle1, out ho_ImageReduced1 ); ho_Border1.Dispose(); HOperatorSet.ThresholdSubPix(ho_ImageReduced1, out ho_Border1, thv); ho_SelectedContours.Dispose(); HOperatorSet.SelectContoursXld(ho_Border1, out ho_SelectedContours, "contour_length", 30, 2000000, -0.5, 0.5); HOperatorSet.UnionAdjacentContoursXld(ho_SelectedContours, out ho_SelectedContours, 10, 1, "attr_keep"); HOperatorSet.GetContourXld(ho_SelectedContours, out hv_Row2, out hv_Col); hv_dis = ((((((hv_Tp * hv_Col) + hv_Row2) - (hv_Tp * hv_zpc)) - hv_zpr) / (((1 + (hv_Tp * hv_Tp))).TupleSqrt() ))).TupleAbs(); hv_dxi = hv_dis.TupleMax(); hv_dni = hv_dis.TupleMin(); HOperatorSet.TupleFind(hv_dis, hv_dxi, out hv_Ix); HOperatorSet.TupleFind(hv_dis, hv_dni, out hv_In); hv_dx = hv_dx.TupleConcat(hv_dxi); hv_dn = hv_dn.TupleConcat(hv_dni); hv_dxr = hv_dxr.TupleConcat(hv_Row2.TupleSelect(hv_Ix)); hv_dxc = hv_dxc.TupleConcat(hv_Col.TupleSelect(hv_Ix)); hv_dnr = hv_dnr.TupleConcat(hv_Row2.TupleSelect(hv_In)); hv_dnc = hv_dnc.TupleConcat(hv_Col.TupleSelect(hv_In)); //catch (Exception) //endtry //stop () } hv_disdxpx = ((((((hv_Tc * hv_dxc) + hv_dxr) - (hv_Tc * hv_zcc)) - hv_zcr) / (((1 + (hv_Tc * hv_Tc))).TupleSqrt() ))).TupleAbs(); hv_disdnpx = ((((((hv_Tc * hv_dnc) + hv_dnr) - (hv_Tc * hv_zcc)) - hv_zcr) / (((1 + (hv_Tc * hv_Tc))).TupleSqrt() ))).TupleAbs(); HTuple end_val55 = hv_Length - 2; HTuple step_val55 = 1; //t1 = DateTime.Now; for (hv_i = 0; hv_i.Continue(end_val55, step_val55); hv_i = hv_i.TupleAdd(step_val55)) { //try HTuple end_val59 = hv_Length - 1; HTuple step_val59 = 1; for (hv_j = hv_i + 1; hv_j.Continue(end_val59, step_val59); hv_j = hv_j.TupleAdd(step_val59)) { if ((int)(new HTuple(((hv_disdxpx.TupleSelect(hv_i))).TupleGreater(hv_disdxpx.TupleSelect( hv_j)))) != 0) { hv_jh = hv_disdxpx.TupleSelect(hv_i); if (hv_disdxpx == null) { hv_disdxpx = new HTuple(); } hv_disdxpx[hv_i] = hv_disdxpx.TupleSelect(hv_j); if (hv_disdxpx == null) { hv_disdxpx = new HTuple(); } hv_disdxpx[hv_j] = hv_jh; hv_jh = hv_dx.TupleSelect(hv_i); if (hv_dx == null) { hv_dx = new HTuple(); } hv_dx[hv_i] = hv_dx.TupleSelect(hv_j); if (hv_dx == null) { hv_dx = new HTuple(); } hv_dx[hv_j] = hv_jh; hv_jh = hv_dxr.TupleSelect(hv_i); if (hv_dxr == null) { hv_dxr = new HTuple(); } hv_dxr[hv_i] = hv_dxr.TupleSelect(hv_j); if (hv_dxr == null) { hv_dxr = new HTuple(); } hv_dxr[hv_j] = hv_jh; hv_jh = hv_dxc.TupleSelect(hv_i); if (hv_dxc == null) { hv_dxc = new HTuple(); } hv_dxc[hv_i] = hv_dxc.TupleSelect(hv_j); if (hv_dxc == null) { hv_dxc = new HTuple(); } hv_dxc[hv_j] = hv_jh; } if ((int)(new HTuple(((hv_disdnpx.TupleSelect(hv_i))).TupleGreater(hv_disdnpx.TupleSelect( hv_j)))) != 0) { hv_jh = hv_disdnpx.TupleSelect(hv_i); if (hv_disdnpx == null) { hv_disdnpx = new HTuple(); } hv_disdnpx[hv_i] = hv_disdnpx.TupleSelect(hv_j); if (hv_disdnpx == null) { hv_disdnpx = new HTuple(); } hv_disdnpx[hv_j] = hv_jh; hv_jh = hv_dn.TupleSelect(hv_i); if (hv_dn == null) { hv_dn = new HTuple(); } hv_dn[hv_i] = hv_dn.TupleSelect(hv_j); if (hv_dn == null) { hv_dn = new HTuple(); } hv_dn[hv_j] = hv_jh; hv_jh = hv_dnr.TupleSelect(hv_i); if (hv_dnr == null) { hv_dnr = new HTuple(); } hv_dnr[hv_i] = hv_dnr.TupleSelect(hv_j); if (hv_dnr == null) { hv_dnr = new HTuple(); } hv_dnr[hv_j] = hv_jh; hv_jh = hv_dnc.TupleSelect(hv_i); if (hv_dnc == null) { hv_dnc = new HTuple(); } hv_dnc[hv_i] = hv_dnc.TupleSelect(hv_j); if (hv_dnc == null) { hv_dnc = new HTuple(); } hv_dnc[hv_j] = hv_jh; } } //catch (Exception) //endtry } //t2 = DateTime.Now; hv_djd = new HTuple(); hv_djdr = new HTuple(); hv_djdc = new HTuple(); hv_djx = new HTuple(); hv_djxr = new HTuple(); hv_djxc = new HTuple(); hv_xjd = new HTuple(); hv_xjdr = new HTuple(); hv_xjdc = new HTuple(); hv_xjx = new HTuple(); hv_xjxr = new HTuple(); hv_xjxc = new HTuple(); hv_ljd = new HTuple(); hv_ljx = new HTuple(); HTuple end_val107 = hv_Length - 1; HTuple step_val107 = 1; for (hv_i = 0; hv_i.Continue(end_val107, step_val107); hv_i = hv_i.TupleAdd(step_val107)) { if ((int)(new HTuple(((hv_dx.TupleSelect(hv_i))).TupleGreater(hv_zj))) != 0) { for (hv_j = -3; (int)hv_j <= 3; hv_j = (int)hv_j + 1) { if (((hv_i + hv_j) >= 0) && ((hv_i + hv_j) <= end_val107)) { if ((int)((new HTuple(((hv_disdnpx.TupleSelect(hv_i + hv_j))).TupleGreater( hv_disdxpx.TupleSelect(hv_i)))).TupleAnd(new HTuple(((hv_dn.TupleSelect( hv_i + hv_j))).TupleLess(hv_zj)))) != 0) { hv_ljd = hv_ljd.TupleConcat(hv_disdxpx.TupleSelect(hv_i)); hv_djd = hv_djd.TupleConcat(hv_dx.TupleSelect(hv_i)); hv_djdr = hv_djdr.TupleConcat(hv_dxr.TupleSelect(hv_i)); hv_djdc = hv_djdc.TupleConcat(hv_dxc.TupleSelect(hv_i)); hv_djx = hv_djx.TupleConcat(hv_dn.TupleSelect(hv_i + hv_j)); hv_djxr = hv_djxr.TupleConcat(hv_dnr.TupleSelect(hv_i + hv_j)); hv_djxc = hv_djxc.TupleConcat(hv_dnc.TupleSelect(hv_i + hv_j)); break; } } // catch (Exception) //catch (HalconException) //{ // //HDevExpDefaultException1.ToHTuple(out hv_Exception); //} } } if ((int)(new HTuple(((hv_dn.TupleSelect(hv_i))).TupleGreater(hv_zj))) != 0) { for (hv_j = -3; (int)hv_j <= 3; hv_j = (int)hv_j + 1) { //try if (((hv_i + hv_j) >= 0) && ((hv_i + hv_j) <= end_val107)) { if ((int)((new HTuple(((hv_disdxpx.TupleSelect(hv_i + hv_j))).TupleGreater( hv_disdnpx.TupleSelect(hv_i)))).TupleAnd(new HTuple(((hv_dx.TupleSelect( hv_i + hv_j))).TupleLess(hv_zj)))) != 0) { hv_ljx = hv_ljx.TupleConcat(hv_disdnpx.TupleSelect(hv_i)); hv_xjd = hv_xjd.TupleConcat(hv_dn.TupleSelect(hv_i)); hv_xjdr = hv_xjdr.TupleConcat(hv_dnr.TupleSelect(hv_i)); hv_xjdc = hv_xjdc.TupleConcat(hv_dnc.TupleSelect(hv_i)); hv_xjx = hv_xjx.TupleConcat(hv_dx.TupleSelect(hv_i + hv_j)); hv_xjxr = hv_xjxr.TupleConcat(hv_dxr.TupleSelect(hv_i + hv_j)); hv_xjxc = hv_xjxc.TupleConcat(hv_dxc.TupleSelect(hv_i + hv_j)); break; } } // catch (Exception) //catch (HalconException) //{ // //HDevExpDefaultException1.ToHTuple(out hv_Exception); //} } } } HOperatorSet.TupleLength(hv_ljd, out hv_l1); HTuple end_val153 = 1; HTuple step_val153 = -1; for (hv_i = hv_l1 - 1; hv_i.Continue(end_val153, step_val153); hv_i = hv_i.TupleAdd(step_val153)) { if ((int)(new HTuple(((hv_ljd.TupleSelect(hv_i))).TupleEqual(hv_ljd.TupleSelect( hv_i - 1)))) != 0) { HOperatorSet.TupleRemove(hv_ljd, hv_i, out hv_ljd); HOperatorSet.TupleRemove(hv_djd, hv_i, out hv_djd); HOperatorSet.TupleRemove(hv_djdr, hv_i, out hv_djdr); HOperatorSet.TupleRemove(hv_djdc, hv_i, out hv_djdc); HOperatorSet.TupleRemove(hv_djx, hv_i, out hv_djx); HOperatorSet.TupleRemove(hv_djxr, hv_i, out hv_djxr); HOperatorSet.TupleRemove(hv_djxc, hv_i, out hv_djxc); } } HOperatorSet.TupleLength(hv_ljx, out hv_l2); HTuple end_val165 = 1; HTuple step_val165 = -1; for (hv_i = hv_l2 - 1; hv_i.Continue(end_val165, step_val165); hv_i = hv_i.TupleAdd(step_val165)) { if ((int)(new HTuple(((hv_ljd.TupleSelect(hv_i))).TupleEqual(hv_ljd.TupleSelect( hv_i - 1)))) != 0) { HOperatorSet.TupleRemove(hv_ljx, hv_i, out hv_ljx); HOperatorSet.TupleRemove(hv_xjd, hv_i, out hv_xjd); HOperatorSet.TupleRemove(hv_xjdr, hv_i, out hv_xjdr); HOperatorSet.TupleRemove(hv_xjdc, hv_i, out hv_xjdc); HOperatorSet.TupleRemove(hv_xjx, hv_i, out hv_xjx); HOperatorSet.TupleRemove(hv_xjxr, hv_i, out hv_xjxr); HOperatorSet.TupleRemove(hv_xjxc, hv_i, out hv_xjxc); } } HOperatorSet.TupleLength(hv_ljx, out hv_l2); HOperatorSet.TupleLength(hv_ljd, out hv_l1); //t5 = DateTime.Now; hv_dj = hv_djd - hv_djx; hv_xj = hv_xjd - hv_xjx; hv_djx = hv_dj.TupleMax(); hv_djn = hv_dj.TupleMin(); hv_djm = hv_dj.TupleMean(); hv_xjx = hv_xj.TupleMax(); hv_xjn = hv_xj.TupleMin(); hv_xjm = hv_xj.TupleMean(); hv_djzd = (hv_djd.TupleMax()) - (hv_djx.TupleMin()); hv_djzx = (hv_djd.TupleMin()) - (hv_djx.TupleMax()); hv_xjzd = (hv_xjd.TupleMax()) - (hv_xjx.TupleMin()); hv_xjzx = (hv_xjd.TupleMin()) - (hv_xjx.TupleMax()); hv_ljtd = (hv_ljd.TupleConcat(0)) - ((new HTuple(0)).TupleConcat(hv_ljd)); hv_ljtx = (hv_ljx.TupleConcat(0)) - ((new HTuple(0)).TupleConcat(hv_ljx)); HOperatorSet.TupleLength(hv_ljtd, out hv_Length3); HOperatorSet.TupleLength(hv_ljtx, out hv_Length4); hv_ljtd = hv_ljtd.TupleSelectRange(1, hv_Length3 - 2); hv_ljtx = hv_ljtx.TupleSelectRange(1, hv_Length4 - 2); hv_ljdx = hv_ljtd.TupleMax(); hv_ljdn = hv_ljtd.TupleMin(); hv_ljxx = hv_ljtx.TupleMax(); hv_ljxn = hv_ljtx.TupleMin(); hv_ljdm = hv_ljtd.TupleMean(); hv_ljxm = hv_ljtx.TupleMean(); HOperatorSet.TupleAtan((hv_djxr - hv_djdr) / (hv_djdc - hv_djxc), out hv_djiaodu); HOperatorSet.TupleAtan((hv_xjxr - hv_xjdr) / (hv_xjdc - hv_xjxc), out hv_xjiaodu); HOperatorSet.TupleLength(hv_djiaodu, out hv_Length5); HOperatorSet.TupleLength(hv_xjiaodu, out hv_Length6); hv_djiaodu1 = ((hv_djiaodu - hv_zcj)).TupleAbs(); hv_xjiaodu1 = ((hv_xjiaodu - hv_zcj)).TupleAbs(); HTuple end_val177 = hv_Length5 - 1; HTuple step_val177 = 1; //t6 = DateTime.Now; for (hv_i = 0; hv_i.Continue(end_val177, step_val177); hv_i = hv_i.TupleAdd(step_val177)) { while ((int)(new HTuple(hv_djiaodu1.TupleGreater((new HTuple(90)).TupleRad() ))) != 0) { hv_djiaodu1 = hv_djiaodu1 - ((new HTuple(90)).TupleRad()); } if ((int)(new HTuple(hv_djiaodu1.TupleGreater((new HTuple(45)).TupleRad() ))) != 0) { hv_djiaodu1 = (-hv_djiaodu1) + ((new HTuple(90)).TupleRad()); } } HTuple end_val185 = hv_Length6 - 1; HTuple step_val185 = 1; for (hv_i = 0; hv_i.Continue(end_val185, step_val185); hv_i = hv_i.TupleAdd(step_val185)) { while ((int)(new HTuple(hv_xjiaodu1.TupleGreater((new HTuple(90)).TupleRad() ))) != 0) { hv_xjiaodu1 = hv_xjiaodu1 - ((new HTuple(90)).TupleRad()); } if ((int)(new HTuple(hv_xjiaodu1.TupleGreater((new HTuple(45)).TupleRad() ))) != 0) { hv_xjiaodu1 = (-hv_xjiaodu1) + ((new HTuple(90)).TupleRad()); } } hv_jjdd = hv_djiaodu1.TupleMax().TupleDeg(); hv_jjdx = hv_djiaodu1.TupleMin().TupleDeg(); hv_jjxd = hv_xjiaodu1.TupleMax().TupleDeg(); hv_jjxx = hv_xjiaodu1.TupleMin().TupleDeg(); hv_jjdm = hv_djiaodu1.TupleMean().TupleDeg(); hv_jjxm = hv_xjiaodu1.TupleMean().TupleDeg(); ho_RegionLines.Dispose(); HOperatorSet.GenRegionLine(out ho_RegionLines, hv_djdr, hv_djdc, hv_djxr, hv_djxc); HOperatorSet.Union2(ho_RegionLines, ho_RegionLines, out RegionToDisp); HTuple hv_result = GetHv_result(); hv_result = hv_result.TupleConcat("螺纹条数1"); hv_result = hv_result.TupleConcat(hv_l1); hv_result = hv_result.TupleConcat("螺纹条数2"); hv_result = hv_result.TupleConcat(hv_l2); hv_result = hv_result.TupleConcat("大径最大值"); hv_result = hv_result.TupleConcat(hv_djx.D * pixeldist); hv_result = hv_result.TupleConcat("大径最小值"); hv_result = hv_result.TupleConcat(hv_djn.D * pixeldist); hv_result = hv_result.TupleConcat("大径平均值"); hv_result = hv_result.TupleConcat(hv_djm.D * pixeldist); hv_result = hv_result.TupleConcat("小径最大值"); hv_result = hv_result.TupleConcat(hv_xjx.D * pixeldist); hv_result = hv_result.TupleConcat("小径最小值"); hv_result = hv_result.TupleConcat(hv_xjn.D * pixeldist); hv_result = hv_result.TupleConcat("小径平均值"); hv_result = hv_result.TupleConcat(hv_xjm.D * pixeldist); hv_result = hv_result.TupleConcat("整体大径最大值"); hv_result = hv_result.TupleConcat(hv_djzd.D * pixeldist); hv_result = hv_result.TupleConcat("整体大径最小值"); hv_result = hv_result.TupleConcat(hv_djzx.D * pixeldist); hv_result = hv_result.TupleConcat("整体小径最大值"); hv_result = hv_result.TupleConcat(hv_xjzd.D * pixeldist); hv_result = hv_result.TupleConcat("整体小径最小值"); hv_result = hv_result.TupleConcat(hv_xjzx.D * pixeldist); hv_result = hv_result.TupleConcat("螺距最大值"); hv_result = hv_result.TupleConcat(hv_ljdx.D * pixeldist); hv_result = hv_result.TupleConcat("螺距最小值"); hv_result = hv_result.TupleConcat(hv_ljdn.D * pixeldist); hv_result = hv_result.TupleConcat("螺距平均值"); hv_result = hv_result.TupleConcat(hv_ljdm.D * pixeldist); hv_result = hv_result.TupleConcat("角度最大值"); hv_result = hv_result.TupleConcat(hv_jjdd.D); hv_result = hv_result.TupleConcat("角度最大值"); hv_result = hv_result.TupleConcat(hv_jjdx.D); hv_result = hv_result.TupleConcat("角度平均值"); hv_result = hv_result.TupleConcat(hv_jjdm.D); result = hv_result.Clone(); ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_Border.Dispose(); ho_Circle.Dispose(); ho_ImageReduced2.Dispose(); ho_Region.Dispose(); ho_Rectangle1.Dispose(); ho_ImageReduced1.Dispose(); ho_Border1.Dispose(); ho_SelectedContours.Dispose(); imagexz.Dispose(); retxz.Dispose(); ho_RegionLines.Dispose(); algorithm.Region.Dispose(); //t4 = DateTime.Now; } catch { HTuple hv_result = GetHv_result(); hv_result = hv_result.TupleConcat("螺纹条数1"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("螺纹条数2"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("大径最大值"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("大径最小值"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("大径平均值"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("小径最大值"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("小径最小值"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("小径平均值"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("整体大径最大值"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("整体大径最小值"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("整体小径最大值"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("整体小径最小值"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("螺距最大值"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("螺距最小值"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("螺距平均值"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("角度最大值"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("角度最大值"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("角度平均值"); hv_result = hv_result.TupleConcat(0); result = hv_result.Clone(); ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_Border.Dispose(); ho_Circle.Dispose(); ho_ImageReduced2.Dispose(); ho_Region.Dispose(); ho_Rectangle1.Dispose(); ho_ImageReduced1.Dispose(); ho_Border1.Dispose(); ho_SelectedContours.Dispose(); imagexz.Dispose(); retxz.Dispose(); ho_RegionLines.Dispose(); algorithm.Region.Dispose(); } }
//DateTime t1, t2, t3, t4,t5,t6,t7; private void action() { HObject[] OTemp = new HObject[20]; // Local iconic variables HObject ho_Rectangle; HObject ho_ImageReduced, ho_Rectangle1, ho_ImageReduced1; HObject ho_Region, ho_ConnectedRegions, ho_ObjectSelected1; HObject ho_Contours, ho_ContoursSplit, ho_SelectedContours; HObject ho_UnionContours, ho_ObjectSelected = null, ho_Region1 = null; HObject ho_EmptyObject, ho_Rectangle3 = null, ho_ImageReduced2 = null; HObject ho_Edges = null, ho_UnionContours1 = null, ho_ObjectSelected2 = null; HObject ho_RegionLines = null; // Local control variables HTuple hv_ModelID = null; HTuple hv_Area = null; HTuple hv_Row5 = null, hv_Column4 = null, hv_Indices = null; HTuple hv_Length = null, hv_Mean = null, hv_Deviation = null; HTuple hv_Number = null, hv_Row3 = new HTuple(), hv_Col = new HTuple(); HTuple hv_Row4 = new HTuple(), hv_Column3 = new HTuple(); HTuple hv_Phi2 = new HTuple(), hv_Length12 = new HTuple(); HTuple hv_Length22 = new HTuple(), hv_sdr1 = null, hv_sdr2 = null; HTuple hv_sdc1 = null, hv_sdc2 = null, hv_cdr1 = null; HTuple hv_cdr2 = null, hv_cdc1 = null, hv_cdc2 = null; HTuple hv_Row2 = null, hv_Column2 = null, hv_Angle = null; HTuple hv_Score = null, hv_djj = null, hv_djy = null, hv_xjj = null; HTuple hv_xjy = null, hv_Index = null, hv_Number1 = new HTuple(); HTuple hv_min1 = new HTuple(), hv_max1 = new HTuple(); HTuple hv_d = new HTuple(), hv_r6 = new HTuple(), hv_c6 = new HTuple(); HTuple hv_Index1 = new HTuple(), hv_Row6 = new HTuple(); HTuple hv_Col1 = new HTuple(), hv_Distance = new HTuple(); HTuple hv_Mins = new HTuple(), hv_Maxs = new HTuple(); HTuple hv_Indices1 = new HTuple(), hv_Indices2 = new HTuple(); HTuple hv_djpj = null, hv_djzd = null, hv_djzx = null; HTuple hv_xjpj = null, hv_xjzd = null, hv_xjzx = null; HTuple hv_Distance1 = null, hv_lwts = null, hv_lwcd = null; HTuple hv_luoju = null; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Rectangle); HOperatorSet.GenEmptyObj(out ho_ImageReduced); HOperatorSet.GenEmptyObj(out ho_Rectangle1); HOperatorSet.GenEmptyObj(out ho_ImageReduced1); HOperatorSet.GenEmptyObj(out ho_Region); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions); HOperatorSet.GenEmptyObj(out ho_ObjectSelected1); HOperatorSet.GenEmptyObj(out ho_Contours); HOperatorSet.GenEmptyObj(out ho_ContoursSplit); HOperatorSet.GenEmptyObj(out ho_SelectedContours); HOperatorSet.GenEmptyObj(out ho_UnionContours); HOperatorSet.GenEmptyObj(out ho_ObjectSelected); HOperatorSet.GenEmptyObj(out ho_Region1); HOperatorSet.GenEmptyObj(out ho_EmptyObject); HOperatorSet.GenEmptyObj(out ho_Rectangle3); HOperatorSet.GenEmptyObj(out ho_ImageReduced2); HOperatorSet.GenEmptyObj(out ho_Edges); HOperatorSet.GenEmptyObj(out ho_UnionContours1); HOperatorSet.GenEmptyObj(out ho_ObjectSelected2); HOperatorSet.GenEmptyObj(out ho_RegionLines); HOperatorSet.Union1(algorithm.Region, out RegionToDisp); //t3 = DateTime.Now; try { if (hv_ModelID == null) { HOperatorSet.ReadNccModel(PathHelper.currentProductPath + @"\lwspmd.ncm", out hv_ModelID); } HOperatorSet.GenRectangle2(out ho_Rectangle1, hv1_Rowx, hv1_Columnx, hv1_Phix, hv1_Length1x, hv1_Length2x); ho_ImageReduced1.Dispose(); HOperatorSet.ReduceDomain(Image, ho_Rectangle1, out ho_ImageReduced1 ); ho_Region.Dispose(); HOperatorSet.Threshold(ho_ImageReduced1, out ho_Region, 0, 128); ho_ConnectedRegions.Dispose(); HOperatorSet.Connection(ho_Region, out ho_ConnectedRegions); HOperatorSet.AreaCenter(ho_ConnectedRegions, out hv_Area, out hv_Row5, out hv_Column4); HOperatorSet.TupleFind(hv_Area, hv_Area.TupleMax(), out hv_Indices); ho_ObjectSelected1.Dispose(); HOperatorSet.SelectObj(ho_ConnectedRegions, out ho_ObjectSelected1, hv_Indices + 1); ho_Contours.Dispose(); HOperatorSet.GenContourRegionXld(ho_ObjectSelected1, out ho_Contours, "border"); ho_ContoursSplit.Dispose(); HOperatorSet.SegmentContoursXld(ho_Contours, out ho_ContoursSplit, "lines_circles", 5, 4, 2); HOperatorSet.LengthXld(ho_ContoursSplit, out hv_Length); HOperatorSet.TupleMean(hv_Length, out hv_Mean); HOperatorSet.TupleDeviation(hv_Length, out hv_Deviation); ho_SelectedContours.Dispose(); HOperatorSet.SelectContoursXld(ho_ContoursSplit, out ho_SelectedContours, "contour_length", 0, hv_Mean + (hv_Deviation * 0.4), -0.5, 0.5); ho_UnionContours.Dispose(); HOperatorSet.UnionAdjacentContoursXld(ho_SelectedContours, out ho_UnionContours, 10, 1, "attr_keep"); HOperatorSet.CountObj(ho_UnionContours, out hv_Number); if ((int)(new HTuple(hv_Number.TupleEqual(2))) != 0) { ho_ObjectSelected.Dispose(); HOperatorSet.SelectObj(ho_UnionContours, out ho_ObjectSelected, 1); HOperatorSet.GetContourXld(ho_ObjectSelected, out hv_Row3, out hv_Col); ho_Region1.Dispose(); HOperatorSet.GenRegionPoints(out ho_Region1, hv_Row3, hv_Col); HOperatorSet.SmallestRectangle2(ho_Region1, out hv_Row4, out hv_Column3, out hv_Phi2, out hv_Length12, out hv_Length22); } hv_sdr1 = hv_Row4 + (10000.000 * (hv_Phi2.TupleSin())); hv_sdr2 = hv_Row4 - (10000.000 * (hv_Phi2.TupleSin())); hv_sdc1 = hv_Column3 - (10000.000 * (hv_Phi2.TupleCos())); hv_sdc2 = hv_Column3 + (10000.000 * (hv_Phi2.TupleCos())); hv_cdr1 = hv_Row4 - (10000.000 * (hv_Phi2.TupleCos())); hv_cdr2 = hv_Row4 + (10000.000 * (hv_Phi2.TupleCos())); hv_cdc1 = hv_Column3 - (10000.000 * (hv_Phi2.TupleSin())); hv_cdc2 = hv_Column3 + (10000.000 * (hv_Phi2.TupleSin())); HOperatorSet.FindNccModel(ho_ImageReduced1, hv_ModelID, -((new HTuple(5)).TupleRad() ), (new HTuple(5)).TupleRad(), 0.5, 0, 0.5, "true", 0, out hv_Row2, out hv_Column2, out hv_Angle, out hv_Score); hv_djj = new HTuple(); hv_djy = new HTuple(); hv_xjj = new HTuple(); hv_xjy = new HTuple(); ho_EmptyObject.Dispose(); HOperatorSet.GenEmptyObj(out ho_EmptyObject); for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_Row2.TupleLength())) - 1); hv_Index = (int)hv_Index + 1) { ho_Rectangle3.Dispose(); HOperatorSet.GenRectangle2(out ho_Rectangle3, hv_Row2.TupleSelect(hv_Index), hv_Column2.TupleSelect(hv_Index), hv_Phim + (hv_Angle.TupleSelect(hv_Index)), hv_Length1m, hv_Length2m); ho_ImageReduced2.Dispose(); HOperatorSet.ReduceDomain(Image, ho_Rectangle3, out ho_ImageReduced2 ); ho_Edges.Dispose(); HOperatorSet.EdgesSubPix(ho_ImageReduced2, out ho_Edges, "canny", 1, 20, 40); ho_UnionContours1.Dispose(); HOperatorSet.UnionAdjacentContoursXld(ho_Edges, out ho_UnionContours1, 10, 1, "attr_keep"); HOperatorSet.CountObj(ho_UnionContours1, out hv_Number1); if ((int)(new HTuple(hv_Number1.TupleEqual(2))) != 0) { hv_min1 = new HTuple(); hv_max1 = new HTuple(); hv_d = new HTuple(); hv_r6 = new HTuple(); hv_c6 = new HTuple(); for (hv_Index1 = 0; (int)hv_Index1 <= 1; hv_Index1 = (int)hv_Index1 + 1) { ho_ObjectSelected2.Dispose(); HOperatorSet.SelectObj(ho_UnionContours1, out ho_ObjectSelected2, hv_Index1 + 1); HOperatorSet.GetContourXld(ho_ObjectSelected2, out hv_Row6, out hv_Col1); HOperatorSet.DistancePl(hv_Row6, hv_Col1, hv_sdr1, hv_sdc1, hv_sdr2, hv_sdc2, out hv_Distance); HOperatorSet.TupleMin(hv_Distance, out hv_Mins); HOperatorSet.TupleMax(hv_Distance, out hv_Maxs); hv_min1 = hv_min1.TupleConcat(hv_Mins); hv_max1 = hv_max1.TupleConcat(hv_Maxs); hv_d = hv_d.TupleConcat(hv_Distance); hv_r6 = hv_r6.TupleConcat(hv_Row6); hv_c6 = hv_c6.TupleConcat(hv_Col1); } } hv_djj = hv_djj.TupleConcat(hv_min1.TupleMin()); hv_djy = hv_djy.TupleConcat(hv_max1.TupleMax()); hv_xjj = hv_xjj.TupleConcat(hv_min1.TupleMax()); hv_xjy = hv_xjy.TupleConcat(hv_max1.TupleMin()); HOperatorSet.TupleFind(hv_d, hv_min1.TupleMin(), out hv_Indices1); HOperatorSet.TupleFind(hv_d, hv_max1.TupleMax(), out hv_Indices2); ho_RegionLines.Dispose(); HOperatorSet.GenRegionLine(out ho_RegionLines, hv_r6.TupleSelect(hv_Indices1), hv_c6.TupleSelect(hv_Indices1), hv_r6.TupleSelect(hv_Indices2), hv_c6.TupleSelect( hv_Indices2)); { HObject ExpTmpOutVar_0; HOperatorSet.ConcatObj(ho_EmptyObject, ho_RegionLines, out ExpTmpOutVar_0); ho_EmptyObject.Dispose(); ho_EmptyObject = ExpTmpOutVar_0; } } hv_djpj = (hv_djy.TupleMean()) - (hv_djj.TupleMean()); hv_djzd = (hv_djy.TupleMax()) - (hv_djj.TupleMin()); hv_djzx = (hv_djy.TupleMin()) - (hv_djj.TupleMax()); hv_xjpj = (hv_xjy.TupleMean()) - (hv_xjj.TupleMean()); hv_xjzd = (hv_xjy.TupleMax()) - (hv_xjj.TupleMin()); hv_xjzx = (hv_xjy.TupleMin()) - (hv_xjj.TupleMax()); HOperatorSet.DistancePl(hv_Row2, hv_Column2, hv_cdr1, hv_cdc1, hv_cdr2, hv_cdc2, out hv_Distance1); hv_lwts = hv_Index.Clone(); hv_lwcd = (hv_Distance1.TupleMax()) - (hv_Distance1.TupleMin()); hv_luoju = hv_lwcd / (hv_lwts - 1); HOperatorSet.Union1(ho_EmptyObject, out RegionToDisp); HTuple hv_result = GetHv_result(); hv_result = hv_result.TupleConcat("螺纹条数1"); hv_result = hv_result.TupleConcat(hv_lwts.D); hv_result = hv_result.TupleConcat("大径最大值"); hv_result = hv_result.TupleConcat(hv_djzd.D * pixeldist); hv_result = hv_result.TupleConcat("大径最小值"); hv_result = hv_result.TupleConcat(hv_djzx.D * pixeldist); hv_result = hv_result.TupleConcat("大径平均值"); hv_result = hv_result.TupleConcat(hv_djpj.D * pixeldist); hv_result = hv_result.TupleConcat("小径最大值"); hv_result = hv_result.TupleConcat(hv_xjzd.D * pixeldist); hv_result = hv_result.TupleConcat("小径最小值"); hv_result = hv_result.TupleConcat(hv_xjzx.D * pixeldist); hv_result = hv_result.TupleConcat("小径平均值"); hv_result = hv_result.TupleConcat(hv_xjpj.D * pixeldist); hv_result = hv_result.TupleConcat("螺距"); hv_result = hv_result.TupleConcat(hv_luoju.D * pixeldist); hv_result = hv_result.TupleConcat("螺纹长度"); hv_result = hv_result.TupleConcat(hv_lwcd.D * pixeldist); result = hv_result.Clone(); ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_Rectangle1.Dispose(); ho_ImageReduced1.Dispose(); ho_Region.Dispose(); ho_ConnectedRegions.Dispose(); ho_ObjectSelected1.Dispose(); ho_Contours.Dispose(); ho_ContoursSplit.Dispose(); ho_SelectedContours.Dispose(); ho_UnionContours.Dispose(); ho_ObjectSelected.Dispose(); ho_Region1.Dispose(); ho_EmptyObject.Dispose(); ho_Rectangle3.Dispose(); ho_ImageReduced2.Dispose(); ho_Edges.Dispose(); ho_UnionContours1.Dispose(); ho_ObjectSelected2.Dispose(); ho_RegionLines.Dispose(); algorithm.Region.Dispose(); //t4 = DateTime.Now; } catch { HTuple hv_result = GetHv_result(); hv_result = hv_result.TupleConcat("螺纹条数1"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("大径最大值"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("大径最小值"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("大径平均值"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("小径最大值"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("小径最小值"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("小径平均值"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("螺距"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("螺纹长度"); hv_result = hv_result.TupleConcat(0); result = hv_result.Clone(); ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_Rectangle1.Dispose(); ho_ImageReduced1.Dispose(); ho_Region.Dispose(); ho_ConnectedRegions.Dispose(); ho_ObjectSelected1.Dispose(); ho_Contours.Dispose(); ho_ContoursSplit.Dispose(); ho_SelectedContours.Dispose(); ho_UnionContours.Dispose(); ho_ObjectSelected.Dispose(); ho_Region1.Dispose(); ho_EmptyObject.Dispose(); ho_Rectangle3.Dispose(); ho_ImageReduced2.Dispose(); ho_Edges.Dispose(); ho_UnionContours1.Dispose(); ho_ObjectSelected2.Dispose(); ho_RegionLines.Dispose(); algorithm.Region.Dispose(); } }
private bool DetectPoints() { edgeParams.hv_MeasureHandles.Clear(); edgeParams.hv_RowEdges.Clear(); edgeParams.hv_ColumnEdges.Clear(); HTuple hv_Row = null; HTuple hv_Column = null; HTuple hv_Phi = null; HTuple hv_Length1 = null; HTuple hv_Length2 = null; hv_Phi = edgeParams.hv_Phi; hv_Length1 = edgeParams.hv_Length1; HOperatorSet.TupleInt(edgeParams.hv_Length2 / edgeParams.divideParts, out hv_Length2); if (hv_Length2 < 1) { MessageBox.Show("ROI过窄,请重新选择"); return(false); } HObject ho_DspXld; HOperatorSet.GenEmptyObj(out ho_DspXld); for (int i = 0; i < edgeParams.divideParts; i++) { hv_Row = edgeParams.hv_Row - hv_Length2 * 2 * hv_Phi.TupleCos() * ((edgeParams.divideParts + 1) / 2 - i); hv_Column = edgeParams.hv_Column - hv_Length2 * 2 * hv_Phi.TupleSin() * ((edgeParams.divideParts + 1) / 2 - i); HOperatorSet.GenMeasureRectangle2(hv_Row, hv_Column, hv_Phi, hv_Length1, hv_Length2, edgeParams.hv_Width, edgeParams.hv_Height, edgeParams.hv_Interpolation, out edgeParams.hv_MeasureHandle); HOperatorSet.MeasurePos(edgeParams.ho_Image, edgeParams.hv_MeasureHandle, edgeParams.hv_Sigma, edgeParams.hv_Threshold, edgeParams.hv_Transition, edgeParams.hv_Select, out edgeParams.hv_RowEdge, out edgeParams.hv_ColumnEdge, out edgeParams.hv_Amplitude, out edgeParams.hv_Distance); if (edgeParams.hv_RowEdge.Length != 1) { continue; } edgeParams.hv_RowEdges.Add(edgeParams.hv_RowEdge); edgeParams.hv_ColumnEdges.Add(edgeParams.hv_ColumnEdge); edgeParams.hv_MeasureHandles.Add(edgeParams.hv_MeasureHandle); //HOperatorSet.CloseMeasure(hv_MeasureHandle); HObject ho_Cross; HOperatorSet.GenEmptyObj(out ho_Cross); HOperatorSet.GenCrossContourXld(out ho_Cross, edgeParams.hv_RowEdge, edgeParams.hv_ColumnEdge, 10, (new HTuple(45)).TupleRad()); HOperatorSet.ConcatObj(ho_DspXld, ho_Cross, out ho_DspXld); } HTuple hv_XldHomMat2D; HOperatorSet.HomMat2dIdentity(out hv_XldHomMat2D); HOperatorSet.HomMat2dScale(hv_XldHomMat2D, hv_ZoomFactor, hv_ZoomFactor, 0, 0, out hv_XldHomMat2D); HOperatorSet.AffineTransContourXld(ho_DspXld, out ho_DspXld, hv_XldHomMat2D); Show2HWindow(edgeParams.ho_Image); HOperatorSet.SetColor(hv_ImageWindow, "red"); HOperatorSet.DispObj(ho_DspXld, hv_ImageWindow); FitLine(edgeParams.hv_RowEdges, edgeParams.hv_ColumnEdges); return(true); }
//显示静态图像的匹配结果 public void MatchStaticImage() { //绘制矩形(可以做一个弹窗什么的) MessageBox.Show("请在右图中绘制匹配的区块,按鼠标右键结束绘制"); HOperatorSet.DrawRectangle1(hv_ExpDefaultWinHandle, out hv_LeftTopY, out hv_LeftTopX, out hv_RightBottomY, out hv_RightBottomX); //获取这个鼠标绘制出来的矩形框 ho_Rectangle.Dispose(); HOperatorSet.GenRectangle1(out ho_Rectangle, hv_LeftTopY, hv_LeftTopX, hv_RightBottomY, hv_RightBottomX); hv_pi = ((new HTuple(0.0)).TupleAcos()) * 2; hv_arrowLength = 50; ho_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_Image_Static, ho_Rectangle, out ho_ImageReduced); //从ImageReduced中创建一个该特征的唯一TemplateID用于后续匹配作为代号 HOperatorSet.CreateTemplateRot(ho_ImageReduced, 4, -hv_pi, 2 * hv_pi, hv_pi / 45, "sort", "original", out hv_TemplateID); //在图像中查找与TemplateID对应的区块的最佳匹配,并把最佳匹配的结果保存到Column,Angle,Error中 HOperatorSet.BestMatchRotMg(ho_ImageReduced, hv_TemplateID, -hv_pi, 2 * hv_pi, 40, "true", 4, out hv_Row, out hv_Column, out hv_Angle, out hv_Error); //显示箭头 HOperatorSet.DispArrow(hv_ExpDefaultWinHandle, hv_Row, hv_Column, hv_Row + ((hv_Angle.TupleSin()) * hv_arrowLength), hv_Column + ((hv_Angle.TupleCos()) * hv_arrowLength), 1); //打开开关并发送当前的匹配结果 DoMatch = true; SendMatchResult(); }
private void action() { HObject ho_Rectangle, ho_ImageReduced, ho_ConnectedRegions2, ho_ObjectSelected3, ho_cl; HObject ho_Regions, ho_ObjectSelected, ho_RegionFillUp; HObject ho_Contours, ho_Rectangle1, ho_Rectangle3, ho_RegionDifference; HObject ho_ImageReduced1, ho_Border, ho_SelectedContours = null; HObject ho_ObjectSelected1 = null, ho_ObjectSelected2 = null; HObject ho_ContoursSplit1 = null, ho_RegionLines = null, ho_Region = null; HObject ho_RegionIntersection = null, ho_ConnectedRegions = null; HObject ho_Circle = null, ho_RegionLines1 = null, ho_Region1 = null; HObject ho_RegionIntersection1 = null, ho_ConnectedRegions1 = null; HObject ho_Circle1 = null; // Local control variables HTuple hv_Row4 = null; HTuple hv_Column4 = null, hv_Phi2 = null, hv_Length12 = null; HTuple hv_Length22 = null, hv_Area2 = null, hv_Row5 = null; HTuple hv_Column5 = null, hv_p = null, hv_fx = new HTuple(); HTuple hv_cz = new HTuple(), hv_dz = new HTuple(), hv_lll = null; HTuple hv_Mean = null, hv_Deviation1 = null, hv_Length8 = null; HTuple hv_Number = new HTuple(), hv_Rowo1 = new HTuple(); HTuple hv_Colo1 = new HTuple(), hv_Rowo2 = new HTuple(); HTuple hv_Colo2 = new HTuple(), hv_as = new HTuple(), hv_ac = new HTuple(); HTuple hv_b = new HTuple(), hv_cs = new HTuple(), hv_cc = new HTuple(); HTuple hv_ds1 = new HTuple(), hv_ds2 = new HTuple(), hv_dc1 = new HTuple(); HTuple hv_dc2 = new HTuple(), hv_Length3 = new HTuple(); HTuple hv_dd = new HTuple(), hv_Floor = new HTuple(), hv_Length4 = new HTuple(); HTuple hv_Sequence1 = new HTuple(), hv_Length5 = new HTuple(); HTuple hv_Sequence = new HTuple(), hv_Sequence2 = new HTuple(); HTuple hv_Reduced1 = new HTuple(), hv_Reduced2 = new HTuple(); HTuple hv_Reduced3 = new HTuple(), hv_Reduced4 = new HTuple(); HTuple hv_d1 = new HTuple(), hv_Length6 = new HTuple(); HTuple hv_Sequence3 = new HTuple(), hv_Area = new HTuple(); HTuple hv_Row2 = new HTuple(), hv_Column2 = new HTuple(); HTuple hv_Selectedc = new HTuple(), hv_Selectedr = new HTuple(); HTuple hv_Newtuple = new HTuple(), hv_d2 = new HTuple(); HTuple hv_Length7 = new HTuple(), hv_Sequence4 = new HTuple(); HTuple hv_Area1 = new HTuple(), hv_Row3 = new HTuple(); HTuple hv_Column3 = new HTuple(), hv_Selectedc1 = new HTuple(); HTuple hv_Selectedr1 = new HTuple(), hv_Newtuple5 = new HTuple(); HTuple hv_Selectedds1 = new HTuple(), hv_Selecteddc1 = new HTuple(); HTuple hv_Selectedds2 = new HTuple(), hv_Selecteddc2 = new HTuple(); HTuple hv_hs1 = new HTuple(), hv_hs1r = new HTuple(), hv_hs1c = new HTuple(); HTuple hv_hs2 = new HTuple(), hv_hs2r = new HTuple(), hv_hs2c = new HTuple(); HTuple hv_hc1 = new HTuple(), hv_hc2 = new HTuple(), hv_jdr = new HTuple(); HTuple hv_jdc = new HTuple(), hv_jds = new HTuple(), hv_jdC = new HTuple(); HTuple hv_jxr = new HTuple(), hv_jxc = new HTuple(), hv_jxs = new HTuple(); HTuple hv_jxC = new HTuple(), hv_ydr = new HTuple(), hv_ydc = new HTuple(); HTuple hv_yds = new HTuple(), hv_ydC = new HTuple(), hv_yxr = new HTuple(); HTuple hv_yxc = new HTuple(), hv_yxs = new HTuple(), hv_yxC = new HTuple(); HTuple hv_jin = new HTuple(), hv_yu = new HTuple(), hv_jp = new HTuple(), hv_Indices = new HTuple(); HTuple hv_yp = new HTuple(), hv_j = new HTuple(), hv_luoju1 = new HTuple(), hv_Area3 = new HTuple(), hv_Row1 = new HTuple(), hv_Column1 = new HTuple(); HTuple hv_luoju2 = new HTuple(), hv_dajing = new HTuple(); HTuple hv_xiaojing = new HTuple(), hv_changdu = new HTuple(); HTuple hv_tiaoshu = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Rectangle); HOperatorSet.GenEmptyObj(out ho_ImageReduced); HOperatorSet.GenEmptyObj(out ho_Regions); HOperatorSet.GenEmptyObj(out ho_ObjectSelected); HOperatorSet.GenEmptyObj(out ho_RegionFillUp); HOperatorSet.GenEmptyObj(out ho_Contours); HOperatorSet.GenEmptyObj(out ho_Rectangle1); HOperatorSet.GenEmptyObj(out ho_Rectangle3); HOperatorSet.GenEmptyObj(out ho_RegionDifference); HOperatorSet.GenEmptyObj(out ho_ImageReduced1); HOperatorSet.GenEmptyObj(out ho_Border); HOperatorSet.GenEmptyObj(out ho_SelectedContours); HOperatorSet.GenEmptyObj(out ho_ObjectSelected1); HOperatorSet.GenEmptyObj(out ho_ObjectSelected2); HOperatorSet.GenEmptyObj(out ho_ContoursSplit1); HOperatorSet.GenEmptyObj(out ho_RegionLines); HOperatorSet.GenEmptyObj(out ho_Region); HOperatorSet.GenEmptyObj(out ho_RegionIntersection); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions); HOperatorSet.GenEmptyObj(out ho_Circle); HOperatorSet.GenEmptyObj(out ho_RegionLines1); HOperatorSet.GenEmptyObj(out ho_Region1); HOperatorSet.GenEmptyObj(out ho_RegionIntersection1); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1); HOperatorSet.GenEmptyObj(out ho_Circle1); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions2); HOperatorSet.GenEmptyObj(out ho_ObjectSelected3); HOperatorSet.GenEmptyObj(out ho_cl); HOperatorSet.Union1(algorithm.Region, out RegionToDisp); try { ho_Rectangle.Dispose(); HOperatorSet.GenRectangle2(out ho_Rectangle, hv_centerRowm, hv_centerColumnm, hv_Phim, hv_Length1m, hv_Length2m); ho_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(Image, ho_Rectangle, out ho_ImageReduced); ho_Regions.Dispose(); HOperatorSet.AutoThreshold(ho_ImageReduced, out ho_Regions, 3); ho_ObjectSelected.Dispose(); HOperatorSet.SelectObj(ho_Regions, out ho_ObjectSelected, 1); ho_ObjectSelected.Dispose(); HOperatorSet.SelectObj(ho_Regions, out ho_ObjectSelected, 1); ho_ConnectedRegions2.Dispose(); HOperatorSet.Connection(ho_ObjectSelected, out ho_ConnectedRegions2); HOperatorSet.AreaCenter(ho_ConnectedRegions2, out hv_Area3, out hv_Row1, out hv_Column1); HOperatorSet.TupleFind(hv_Area3, hv_Area3.TupleMax(), out hv_Indices); ho_ObjectSelected3.Dispose(); HOperatorSet.SelectObj(ho_ConnectedRegions2, out ho_ObjectSelected3, hv_Indices + 1); ho_cl.Dispose(); HOperatorSet.ClosingCircle(ho_ObjectSelected3, out ho_cl, 3.5); ho_RegionFillUp.Dispose(); HOperatorSet.FillUp(ho_ObjectSelected3, out ho_RegionFillUp); ho_Contours.Dispose(); HOperatorSet.GenContourRegionXld(ho_RegionFillUp, out ho_Contours, "border"); HOperatorSet.SmallestRectangle2Xld(ho_Contours, out hv_Row4, out hv_Column4, out hv_Phi2, out hv_Length12, out hv_Length22); HOperatorSet.AreaCenter(ho_RegionFillUp, out hv_Area2, out hv_Row5, out hv_Column5); hv_p = ((hv_Phi2 - hv_Phim)).TupleAbs(); while ((int)(new HTuple(hv_p.TupleGreater((new HTuple(90)).TupleRad()))) != 0) { hv_p = ((hv_p - ((new HTuple(180)).TupleRad()))).TupleAbs(); } if ((int)(new HTuple(hv_p.TupleGreater((new HTuple(45)).TupleRad()))) != 0) { hv_fx = hv_Phi2.Clone(); hv_cz = hv_Length12.Clone(); hv_dz = hv_Length22.Clone(); } else { hv_fx = hv_Phi2 + ((new HTuple(90)).TupleRad()); hv_cz = hv_Length22.Clone(); hv_dz = hv_Length12.Clone(); } hv_lll = ((((hv_cz * hv_dz) * 4) - hv_Area2) / 2) / hv_cz; ho_Rectangle1.Dispose(); HOperatorSet.GenRectangle2(out ho_Rectangle1, hv_Row4, hv_Column4, hv_fx, hv_cz, hv_dz + 5); ho_Rectangle3.Dispose(); HOperatorSet.GenRectangle2(out ho_Rectangle3, hv_Row4, hv_Column4, hv_Phi2, hv_Length12 + 200, hv_lll); ho_RegionDifference.Dispose(); HOperatorSet.Difference(ho_Rectangle1, ho_Rectangle3, out ho_RegionDifference ); ho_ImageReduced1.Dispose(); HOperatorSet.ReduceDomain(ho_ImageReduced, ho_RegionDifference, out ho_ImageReduced1 ); HOperatorSet.Intensity(ho_RegionDifference, ho_ImageReduced1, out hv_Mean, out hv_Deviation1); ho_Border.Dispose(); HOperatorSet.ThresholdSubPix(ho_ImageReduced1, out ho_Border, hv_Mean); HOperatorSet.LengthXld(ho_Border, out hv_Length8); if ((int)(new HTuple((new HTuple(hv_Length8.TupleLength())).TupleGreater(1))) != 0) { ho_SelectedContours.Dispose(); HOperatorSet.SelectContoursXld(ho_Border, out ho_SelectedContours, "contour_length", 0.5 * (hv_Length8.TupleMax()), 1.5 * (hv_Length8.TupleMax()), -0.5, 0.5); HOperatorSet.CountObj(ho_SelectedContours, out hv_Number); if ((int)(new HTuple(hv_Number.TupleEqual(2))) != 0) { ho_ObjectSelected1.Dispose(); HOperatorSet.SelectObj(ho_SelectedContours, out ho_ObjectSelected1, 1); ho_ObjectSelected2.Dispose(); HOperatorSet.SelectObj(ho_SelectedContours, out ho_ObjectSelected2, 2); HOperatorSet.GetContourXld(ho_ObjectSelected1, out hv_Rowo1, out hv_Colo1); HOperatorSet.GetContourXld(ho_ObjectSelected2, out hv_Rowo2, out hv_Colo2); HOperatorSet.TupleTan(hv_fx + ((new HTuple(0.000001)).TupleRad()), out hv_as); HOperatorSet.TupleTan(hv_fx + ((new HTuple(90.000001)).TupleRad()), out hv_ac); hv_b = -1; hv_cs = (-(hv_Row4 + (5000 * (((hv_fx + ((new HTuple(90)).TupleRad()))).TupleSin() )))) - (hv_as * (hv_Column4 - (5000 * (((hv_fx + ((new HTuple(90)).TupleRad()))).TupleCos() )))); hv_cc = (-(hv_Row4 + (5000 * (hv_fx.TupleSin())))) - (hv_ac * (hv_Column4 - (5000 * (hv_fx.TupleCos() )))); hv_ds1 = (((((hv_as * hv_Colo1) + (hv_b * (-hv_Rowo1))) + hv_cs)).TupleAbs()) / (((1 + (hv_as * hv_as))).TupleSqrt() ); hv_ds2 = (((((hv_as * hv_Colo2) + (hv_b * (-hv_Rowo2))) + hv_cs)).TupleAbs()) / (((1 + (hv_as * hv_as))).TupleSqrt() ); hv_dc1 = (((((hv_ac * hv_Colo1) + (hv_b * (-hv_Rowo1))) + hv_cc)).TupleAbs()) / (((1 + (hv_ac * hv_ac))).TupleSqrt() ); hv_dc2 = (((((hv_ac * hv_Colo2) + (hv_b * (-hv_Rowo2))) + hv_cc)).TupleAbs()) / (((1 + (hv_ac * hv_ac))).TupleSqrt() ); ho_ContoursSplit1.Dispose(); HOperatorSet.SegmentContoursXld(ho_ObjectSelected2, out ho_ContoursSplit1, "lines_circles", 5, 4, 2); HOperatorSet.LengthXld(ho_ContoursSplit1, out hv_Length3); hv_dd = hv_Length3.TupleMean(); HOperatorSet.TupleFloor(hv_dd, out hv_Floor); HOperatorSet.TupleLength(hv_Colo1, out hv_Length4); HOperatorSet.TupleGenSequence(hv_Length4 - hv_Floor, hv_Length4 - 1, 1, out hv_Sequence1); HOperatorSet.TupleLength(hv_Colo2, out hv_Length5); HOperatorSet.TupleGenSequence(0, hv_Floor - 1, 1, out hv_Sequence); HOperatorSet.TupleGenSequence(hv_Length5 - hv_Floor, hv_Length5 - 1, 1, out hv_Sequence2); HOperatorSet.TupleRemove(hv_ds1, hv_Sequence, out hv_Reduced1); HOperatorSet.TupleRemove(hv_ds1, hv_Sequence1, out hv_Reduced2); HOperatorSet.TupleRemove(hv_ds2, hv_Sequence, out hv_Reduced3); HOperatorSet.TupleRemove(hv_ds2, hv_Sequence2, out hv_Reduced4); hv_d1 = hv_Reduced1 - hv_Reduced2; HOperatorSet.TupleLength(hv_d1, out hv_Length6); HOperatorSet.TupleGenSequence(1, hv_Length6, 1, out hv_Sequence3); ho_RegionLines.Dispose(); HOperatorSet.GenRegionLine(out ho_RegionLines, 0, 0, 0, hv_Length6); ho_Region.Dispose(); HOperatorSet.GenRegionPoints(out ho_Region, hv_d1, hv_Sequence3); ho_RegionIntersection.Dispose(); HOperatorSet.Intersection(ho_RegionLines, ho_Region, out ho_RegionIntersection ); ho_ConnectedRegions.Dispose(); HOperatorSet.Connection(ho_RegionIntersection, out ho_ConnectedRegions); HOperatorSet.AreaCenter(ho_ConnectedRegions, out hv_Area, out hv_Row2, out hv_Column2); HOperatorSet.TupleSelect(hv_Colo1, ((hv_Column2 + (hv_dd / 2))).TupleFloor(), out hv_Selectedc); HOperatorSet.TupleSelect(hv_Rowo1, ((hv_Column2 + (hv_dd / 2))).TupleFloor(), out hv_Selectedr); HOperatorSet.TupleGenConst(new HTuple(hv_Selectedr.TupleLength()), 1, out hv_Newtuple); ho_Circle.Dispose(); HOperatorSet.GenCircle(out ho_Circle, hv_Selectedr, hv_Selectedc, hv_Newtuple); hv_d2 = hv_Reduced3 - hv_Reduced4; HOperatorSet.TupleLength(hv_d2, out hv_Length7); HOperatorSet.TupleGenSequence(1, hv_Length7, 1, out hv_Sequence4); ho_RegionLines1.Dispose(); HOperatorSet.GenRegionLine(out ho_RegionLines1, 0, 0, 0, hv_Length7); ho_Region1.Dispose(); HOperatorSet.GenRegionPoints(out ho_Region1, hv_d2, hv_Sequence4); ho_RegionIntersection1.Dispose(); HOperatorSet.Intersection(ho_Region1, ho_RegionLines1, out ho_RegionIntersection1 ); ho_ConnectedRegions1.Dispose(); HOperatorSet.Connection(ho_RegionIntersection1, out ho_ConnectedRegions1); HOperatorSet.AreaCenter(ho_ConnectedRegions1, out hv_Area1, out hv_Row3, out hv_Column3); HOperatorSet.TupleSelect(hv_Colo2, ((hv_Column3 + (hv_dd / 2))).TupleFloor(), out hv_Selectedc1); HOperatorSet.TupleSelect(hv_Rowo2, ((hv_Column3 + (hv_dd / 2))).TupleFloor(), out hv_Selectedr1); HOperatorSet.TupleGenConst(new HTuple(hv_Selectedr1.TupleLength()), 1, out hv_Newtuple5); ho_Circle1.Dispose(); HOperatorSet.GenCircle(out ho_Circle1, hv_Selectedr1, hv_Selectedc1, hv_Newtuple5); HOperatorSet.TupleSelect(hv_ds1, ((hv_Column2 + (hv_dd / 2))).TupleFloor(), out hv_Selectedds1); HOperatorSet.TupleSelect(hv_dc1, ((hv_Column2 + (hv_dd / 2))).TupleFloor(), out hv_Selecteddc1); HOperatorSet.TupleSelect(hv_ds2, ((hv_Column3 + (hv_dd / 2))).TupleFloor(), out hv_Selectedds2); HOperatorSet.TupleSelect(hv_dc2, ((hv_Column3 + (hv_dd / 2))).TupleFloor(), out hv_Selecteddc2); if ((int)(new HTuple((new HTuple(hv_Selectedds1.TupleMean())).TupleLess(hv_Selectedds2.TupleMean() ))) != 0) { hv_hs1 = hv_Selectedds1.Clone(); hv_hs1r = hv_Selectedr.Clone(); hv_hs1c = hv_Selectedc.Clone(); hv_hs2 = hv_Selectedds2.Clone(); hv_hs2r = hv_Selectedr1.Clone(); hv_hs2c = hv_Selectedc1.Clone(); hv_hc1 = hv_Selecteddc1.Clone(); hv_hc2 = hv_Selecteddc2.Clone(); } else { hv_hs1 = hv_Selectedds2.Clone(); hv_hs2r = hv_Selectedr.Clone(); hv_hs2c = hv_Selectedc.Clone(); hv_hs2 = hv_Selectedds1.Clone(); hv_hs1r = hv_Selectedr1.Clone(); hv_hs1c = hv_Selectedc1.Clone(); hv_hc2 = hv_Selecteddc1.Clone(); hv_hc1 = hv_Selecteddc2.Clone(); } hv_jdr = new HTuple(); hv_jdc = new HTuple(); hv_jds = new HTuple(); hv_jdC = new HTuple(); hv_jxr = new HTuple(); hv_jxc = new HTuple(); hv_jxs = new HTuple(); hv_jxC = new HTuple(); hv_ydr = new HTuple(); hv_ydc = new HTuple(); hv_yds = new HTuple(); hv_ydC = new HTuple(); hv_yxr = new HTuple(); hv_yxc = new HTuple(); hv_yxs = new HTuple(); hv_yxC = new HTuple(); hv_jin = new HTuple(hv_hs1.TupleLength()); hv_yu = new HTuple(hv_hs2.TupleLength()); hv_jp = hv_hs1.TupleMean(); hv_yp = hv_hs2.TupleMean(); HTuple end_val128 = hv_jin - 1; HTuple step_val128 = 1; for (hv_j = 0; hv_j.Continue(end_val128, step_val128); hv_j = hv_j.TupleAdd(step_val128)) { if ((int)(new HTuple(((hv_hs1.TupleSelect(hv_j))).TupleLess(hv_jp))) != 0) { hv_jdr = hv_jdr.TupleConcat(hv_hs1r.TupleSelect(hv_j)); hv_jdc = hv_jdc.TupleConcat(hv_hs1c.TupleSelect(hv_j)); hv_jds = hv_jds.TupleConcat(hv_hs1.TupleSelect(hv_j)); hv_jdC = hv_jdC.TupleConcat(hv_hc1.TupleSelect(hv_j)); } else { hv_jxr = hv_jxr.TupleConcat(hv_hs1r.TupleSelect(hv_j)); hv_jxc = hv_jxc.TupleConcat(hv_hs1c.TupleSelect(hv_j)); hv_jxs = hv_jxs.TupleConcat(hv_hs1.TupleSelect(hv_j)); hv_jxC = hv_jxC.TupleConcat(hv_hc1.TupleSelect(hv_j)); } } HTuple end_val141 = hv_yu - 1; HTuple step_val141 = 1; for (hv_j = 0; hv_j.Continue(end_val141, step_val141); hv_j = hv_j.TupleAdd(step_val141)) { if ((int)(new HTuple(((hv_hs2.TupleSelect(hv_j))).TupleGreater(hv_yp))) != 0) { hv_ydr = hv_ydr.TupleConcat(hv_hs2r.TupleSelect(hv_j)); hv_ydc = hv_ydc.TupleConcat(hv_hs2c.TupleSelect(hv_j)); hv_yds = hv_yds.TupleConcat(hv_hs2.TupleSelect(hv_j)); hv_ydC = hv_ydC.TupleConcat(hv_hc2.TupleSelect(hv_j)); } else { hv_yxr = hv_yxr.TupleConcat(hv_hs2r.TupleSelect(hv_j)); hv_yxc = hv_yxc.TupleConcat(hv_hs2c.TupleSelect(hv_j)); hv_yxs = hv_yxs.TupleConcat(hv_hs2.TupleSelect(hv_j)); hv_yxC = hv_yxC.TupleConcat(hv_hc2.TupleSelect(hv_j)); } } hv_luoju1 = (((hv_hc1.TupleMax()) - (hv_hc1.TupleMin())) / ((new HTuple(hv_hc1.TupleLength() )) - 1)) * 2; hv_luoju2 = (((hv_hc2.TupleMax()) - (hv_hc2.TupleMin())) / ((new HTuple(hv_hc2.TupleLength() )) - 1)) * 2; hv_dajing = (hv_yds.TupleMean()) - (hv_jds.TupleMean()); hv_xiaojing = (hv_yxs.TupleMean()) - (hv_jxs.TupleMean()); hv_changdu = (((hv_hc1.TupleConcat(hv_hc2))).TupleMax()) - (((hv_hc1.TupleConcat( hv_hc2))).TupleMin()); hv_tiaoshu = (((((((new HTuple(hv_jds.TupleLength())).TupleConcat(new HTuple(hv_jxs.TupleLength() )))).TupleConcat(new HTuple(hv_yds.TupleLength())))).TupleConcat(new HTuple(hv_yxs.TupleLength() )))).TupleMax(); } } HOperatorSet.Union2(ho_Circle, ho_Circle1, out RegionToDisp); HTuple hv_result = GetHv_result(); hv_result = hv_result.TupleConcat("大径平均值"); hv_result = hv_result.TupleConcat(hv_dajing.D * pixeldist); hv_result = hv_result.TupleConcat("螺纹长度"); hv_result = hv_result.TupleConcat(hv_changdu.D * pixeldist); hv_result = hv_result.TupleConcat("螺纹条数"); hv_result = hv_result.TupleConcat(hv_tiaoshu.D); hv_result = hv_result.TupleConcat("螺距1"); hv_result = hv_result.TupleConcat(hv_luoju1.D * pixeldist); hv_result = hv_result.TupleConcat("螺距2"); hv_result = hv_result.TupleConcat(hv_luoju2.D * pixeldist); result = hv_result.Clone(); ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_Regions.Dispose(); ho_ObjectSelected.Dispose(); ho_RegionFillUp.Dispose(); ho_Contours.Dispose(); ho_Rectangle1.Dispose(); ho_Rectangle3.Dispose(); ho_RegionDifference.Dispose(); ho_ImageReduced1.Dispose(); ho_Border.Dispose(); ho_SelectedContours.Dispose(); ho_ObjectSelected1.Dispose(); ho_ObjectSelected2.Dispose(); ho_ContoursSplit1.Dispose(); ho_RegionLines.Dispose(); ho_Region.Dispose(); ho_RegionIntersection.Dispose(); ho_ConnectedRegions.Dispose(); ho_Circle.Dispose(); ho_RegionLines1.Dispose(); ho_Region1.Dispose(); ho_RegionIntersection1.Dispose(); ho_ConnectedRegions1.Dispose(); ho_Circle1.Dispose(); ho_ConnectedRegions2.Dispose(); ho_ObjectSelected3.Dispose(); ho_cl.Dispose(); algorithm.Region.Dispose(); } catch { HTuple hv_result = GetHv_result(); hv_result = hv_result.TupleConcat("大径平均值"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("螺纹长度"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("螺纹条数"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("螺距1"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("螺距2"); hv_result = hv_result.TupleConcat(0); result = hv_result.Clone(); ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_Regions.Dispose(); ho_ObjectSelected.Dispose(); ho_RegionFillUp.Dispose(); ho_Contours.Dispose(); ho_Rectangle1.Dispose(); ho_Rectangle3.Dispose(); ho_RegionDifference.Dispose(); ho_ImageReduced1.Dispose(); ho_Border.Dispose(); ho_SelectedContours.Dispose(); ho_ObjectSelected1.Dispose(); ho_cl.Dispose(); ho_ObjectSelected2.Dispose(); ho_ContoursSplit1.Dispose(); ho_RegionLines.Dispose(); ho_Region.Dispose(); ho_RegionIntersection.Dispose(); ho_ConnectedRegions.Dispose(); ho_Circle.Dispose(); ho_RegionLines1.Dispose(); ho_Region1.Dispose(); ho_RegionIntersection1.Dispose(); ho_ConnectedRegions1.Dispose(); ho_Circle1.Dispose(); ho_ConnectedRegions2.Dispose(); ho_ObjectSelected3.Dispose(); algorithm.Region.Dispose(); } }
/// <summary> /// 小角度找圆心 /// </summary> /// <param name="ListPtIn"> </param> /// <param name="CenterX"></param> /// <param name="CenterY"></param> /// <param name="CirR"></param> /// <returns></returns> public bool FindCircleCenter(List <Point2Db> ListPtIn, out double CenterX, out double CenterY, out double CirR) { CenterX = 0; CenterY = 0; CirR = 0; int a = 0; int b = 0; List <ST_Line> line = new List <ST_Line>(); List <PointXYZ> input = new List <PointXYZ>(); HTuple phi = new HTuple(); //1.0 获取中垂线 for (int i = 0; i < ListPtIn.Count(); i++) { for (int j = i + 1; j < ListPtIn.Count(); j++) { double midX = (ListPtIn[i].Col + ListPtIn[j].Col) / 2; double midY = (ListPtIn[i].Row + ListPtIn[j].Row) / 2; HOperatorSet.LineOrientation(ListPtIn[i].Row, ListPtIn[i].Col, ListPtIn[j].Row, ListPtIn[j].Col, out phi); line.Add(new ST_Line(true));//求中垂线 ST_Line MyLine = new ST_Line(true); MyLine.startX = midX + phi.TupleSin().D * 200.0; MyLine.startY = midY + phi.TupleCos().D * 200.0; MyLine.endX = midX - phi.TupleSin().D * 200.0; MyLine.endY = midY - phi.TupleCos().D * 200.0; line[a] = MyLine; a++; } } //2.0 找交点 for (int i = 0; i < a; i++) { for (int j = i + 1; j < a; j++) { HOperatorSet.IntersectionLl(line[i].startY, line[i].startX, line[i].endY, line[i].endX, line[j].startY, line[j].startX, line[j].endY, line[j].endX, out HTuple row, out HTuple column, out HTuple isParallel); if (isParallel.I == 1) { continue; } input.Add(new PointXYZ()); input[b].X = column.D; input[b].Y = row.D; b++; } } //3.0 按权重找出圆心 PointXY xy = new PointXY(0, 0); double cx = 0.0; double cy = 0.0; //for (int i = 0; i < b; i++){ // if (input[i].X > 10000 || input[i].Y > 10000) { // input.Remove(input[i]); // i--; // b--; // } //} for (int i = 0; i < b; i++) { cx += input[i].X; cy += input[i].Y; } xy.X = cx / b; xy.Y = cy / b; InverseDistanceWeighted(input, xy); double X = 0; double Y = 0; for (int i = 0; i < b; i++) { X += input[i].X * input[i].Z; Y += input[i].Y * input[i].Z; } CenterX = X; CenterY = Y; double DistR = 0; for (int i = 0; i < ListPtIn.Count(); i++) { DistR = DistR + Math.Sqrt(Math.Pow((ListPtIn[0].Col - X), 2) + Math.Pow((ListPtIn[0].Row - Y), 2)); } DistR = DistR / ListPtIn.Count(); return(true); }
private void action() { HObject ho_Edges, ho_UnionContours; HObject ho_Region1, ho_SelectedRegions3, ho_SelectedRegions1; HObject ho_SelectedRegions, ho_Rectangle, ho_Rectangle1; HObject ho_RegionIntersection, ho_RegionIntersection1, ho_Rectangle2; HObject ho_Rectangle3, ho_RegionDifference, ho_RegionDifference1; // Local control variables HTuple hv_Row = null, hv_Column = null, hv_Phi = null; HTuple hv_Length1 = null, hv_Length2 = null, hv_Row1 = null; HTuple hv_Column1 = null, hv_Phi1 = null, hv_Length11 = null; HTuple hv_Length21 = null, hv_Row2 = null, hv_Column2 = null; HTuple hv_Phi2 = null, hv_Length12 = null, hv_Length22 = null; HTuple hv_Area = null, hv_Row3 = null, hv_Column3 = null; HTuple hv_Area1 = null, hv_Row4 = null, hv_Column4 = null; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Edges); HOperatorSet.GenEmptyObj(out ho_UnionContours); HOperatorSet.GenEmptyObj(out ho_Region1); HOperatorSet.GenEmptyObj(out ho_SelectedRegions3); HOperatorSet.GenEmptyObj(out ho_SelectedRegions1); HOperatorSet.GenEmptyObj(out ho_SelectedRegions); HOperatorSet.GenEmptyObj(out ho_Rectangle); HOperatorSet.GenEmptyObj(out ho_Rectangle1); HOperatorSet.GenEmptyObj(out ho_RegionIntersection); HOperatorSet.GenEmptyObj(out ho_RegionIntersection1); HOperatorSet.GenEmptyObj(out ho_Rectangle2); HOperatorSet.GenEmptyObj(out ho_Rectangle3); HOperatorSet.GenEmptyObj(out ho_RegionDifference); HOperatorSet.GenEmptyObj(out ho_RegionDifference1); try { ho_Edges.Dispose(); HOperatorSet.EdgesSubPix(Image, out ho_Edges, "canny", 3, 1, 100); ho_UnionContours.Dispose(); HOperatorSet.UnionAdjacentContoursXld(ho_Edges, out ho_UnionContours, 50, 1, "attr_forget"); ho_Region1.Dispose(); HOperatorSet.GenRegionContourXld(ho_UnionContours, out ho_Region1, "filled"); ho_SelectedRegions3.Dispose(); HOperatorSet.SelectShape(ho_Region1, out ho_SelectedRegions3, "area", "and", 15000, 9999900); ho_SelectedRegions1.Dispose(); HOperatorSet.Union1(ho_SelectedRegions3, out ho_SelectedRegions1); ho_SelectedRegions.Dispose(); HOperatorSet.FillUp(ho_SelectedRegions1, out ho_SelectedRegions); HOperatorSet.SmallestRectangle2(ho_SelectedRegions, out hv_Row, out hv_Column, out hv_Phi, out hv_Length1, out hv_Length2); ho_Rectangle.Dispose(); HOperatorSet.GenRectangle2(out ho_Rectangle, hv_Row - ((hv_Length1 * 0.5) * (hv_Phi.TupleSin() )), hv_Column + ((hv_Length1 * 0.5) * (hv_Phi.TupleCos())), hv_Phi, 20, hv_Length2); ho_Rectangle1.Dispose(); HOperatorSet.GenRectangle2(out ho_Rectangle1, hv_Row + ((hv_Length1 * 0.5) * (hv_Phi.TupleSin() )), hv_Column - ((hv_Length1 * 0.5) * (hv_Phi.TupleCos())), hv_Phi, 20, hv_Length2); ho_RegionIntersection.Dispose(); HOperatorSet.Intersection(ho_Rectangle, ho_SelectedRegions, out ho_RegionIntersection ); ho_RegionIntersection1.Dispose(); HOperatorSet.Intersection(ho_Rectangle1, ho_SelectedRegions, out ho_RegionIntersection1 ); HOperatorSet.SmallestRectangle2(ho_RegionIntersection, out hv_Row1, out hv_Column1, out hv_Phi1, out hv_Length11, out hv_Length21); HOperatorSet.SmallestRectangle2(ho_RegionIntersection1, out hv_Row2, out hv_Column2, out hv_Phi2, out hv_Length12, out hv_Length22); ho_Rectangle2.Dispose(); HOperatorSet.GenRectangle2(out ho_Rectangle2, hv_Row - ((hv_Length1 * 0.8) * (hv_Phi.TupleSin() )), hv_Column + ((hv_Length1 * 0.8) * (hv_Phi.TupleCos())), hv_Phi, hv_Length1 * 0.2, hv_Length11); ho_Rectangle3.Dispose(); HOperatorSet.GenRectangle2(out ho_Rectangle3, hv_Row + ((hv_Length1 * 0.8) * (hv_Phi.TupleSin() )), hv_Column - ((hv_Length1 * 0.8) * (hv_Phi.TupleCos())), hv_Phi, hv_Length1 * 0.2, hv_Length12); ho_RegionDifference.Dispose(); HOperatorSet.Difference(ho_Rectangle2, ho_SelectedRegions, out ho_RegionDifference ); ho_RegionDifference1.Dispose(); HOperatorSet.Difference(ho_Rectangle3, ho_SelectedRegions, out ho_RegionDifference1 ); HOperatorSet.AreaCenter(ho_RegionDifference, out hv_Area, out hv_Row3, out hv_Column3); HOperatorSet.AreaCenter(ho_RegionDifference1, out hv_Area1, out hv_Row4, out hv_Column4); HOperatorSet.Union1(ho_SelectedRegions, out RegionToDisp); HTuple hv_result = GetHv_result(); hv_result = hv_result.TupleConcat("长度"); hv_result = hv_result.TupleConcat(hv_Length1.D * pixeldist * 2); hv_result = hv_result.TupleConcat("直径1"); hv_result = hv_result.TupleConcat(hv_Length2.D * pixeldist * 2); hv_result = hv_result.TupleConcat("直径2"); hv_result = hv_result.TupleConcat(hv_Length11.D * pixeldist * 2); hv_result = hv_result.TupleConcat("直径3"); hv_result = hv_result.TupleConcat(hv_Length12.D * pixeldist * 2); hv_result = hv_result.TupleConcat("倒角1"); hv_result = hv_result.TupleConcat(hv_Area.D); hv_result = hv_result.TupleConcat("倒角2"); hv_result = hv_result.TupleConcat(hv_Area1.D); result = hv_result.Clone(); } catch { HTuple hv_result = GetHv_result(); hv_result = hv_result.TupleConcat("长度"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("直径1"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("直径2"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("直径3"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("倒角1"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("倒角2"); hv_result = hv_result.TupleConcat(0); result = hv_result.Clone(); } ho_Edges.Dispose(); ho_UnionContours.Dispose(); ho_Region1.Dispose(); ho_SelectedRegions3.Dispose(); ho_SelectedRegions1.Dispose(); ho_SelectedRegions.Dispose(); ho_Rectangle.Dispose(); ho_Rectangle1.Dispose(); ho_RegionIntersection.Dispose(); ho_RegionIntersection1.Dispose(); ho_Rectangle2.Dispose(); ho_Rectangle3.Dispose(); ho_RegionDifference.Dispose(); ho_RegionDifference1.Dispose(); }