private void action() { HObject ho_Rectangle1, ho_ImageReduced1, ho_Border, ho_ContoursSplit, ho_RegionLines; HOperatorSet.GenEmptyObj(out ho_Rectangle1); HOperatorSet.GenEmptyObj(out ho_ImageReduced1); HOperatorSet.GenEmptyObj(out ho_Border); HOperatorSet.GenEmptyObj(out ho_ContoursSplit); HOperatorSet.GenEmptyObj(out ho_RegionLines); HTuple hv_RowBeginOut = null, hv_ColBeginOut = null, hv_RowEndOut = null, hv_ColEndOut = null; if (hv_ModelID == null) { HOperatorSet.ReadNccModel(PathHelper.currentProductPath + @"\jdmx.ncm", out hv_ModelID); } HTuple hv_ColBegin = null, hv_RowEnd = null, hv_ColEnd = null, hv_Angle1 = null, hv_jiaodu = null; HTuple hv_Nr = null, hv_Nc = null, hv_Dist = null, hv_RowBegin = null; HTuple hv_Angle = null, hv_Score = null, hv_Row1 = null, hv_Column1 = null; HOperatorSet.FindNccModel(Image, hv_ModelID, -0.2, 0.39, 0.8, 1, 0.5, "true", 0, out hv_Row1, out hv_Column1, out hv_Angle, out hv_Score); ho_Rectangle1.Dispose(); HOperatorSet.GenRectangle2(out ho_Rectangle1, hv_Row1, hv_Column1, hv_Phi + hv_Angle, hv_Length1, hv_Length2); ho_ImageReduced1.Dispose(); HOperatorSet.ReduceDomain(Image, ho_Rectangle1, out ho_ImageReduced1); ho_Border.Dispose(); HOperatorSet.ThresholdSubPix(ho_ImageReduced1, out ho_Border, thv); ho_ContoursSplit.Dispose(); HOperatorSet.SegmentContoursXld(ho_Border, out ho_ContoursSplit, "lines_circles", 5, 10, 10); HOperatorSet.FitLineContourXld(ho_ContoursSplit, "tukey", -1, 0, 5, 2, out hv_RowBegin, out hv_ColBegin, out hv_RowEnd, out hv_ColEnd, out hv_Nr, out hv_Nc, out hv_Dist); HOperatorSet.SelectLinesLongest(hv_RowBegin, hv_ColBegin, hv_RowEnd, hv_ColEnd, 2, out hv_RowBeginOut, out hv_ColBeginOut, out hv_RowEndOut, out hv_ColEndOut); HOperatorSet.AngleLl(hv_RowBeginOut.TupleSelect(0), hv_ColBeginOut.TupleSelect(0), hv_RowEndOut.TupleSelect(0), hv_ColEndOut.TupleSelect(0), hv_RowBeginOut.TupleSelect( 1), hv_ColBeginOut.TupleSelect(1), hv_RowEndOut.TupleSelect(1), hv_ColEndOut.TupleSelect( 1), out hv_Angle1); ho_RegionLines.Dispose(); HOperatorSet.GenRegionLine(out ho_RegionLines, hv_RowBeginOut, hv_ColBeginOut, hv_RowEndOut, hv_ColEndOut); HOperatorSet.Union2(ho_RegionLines, ho_RegionLines, out RegionToDisp); hv_jiaodu = ((hv_Angle1.TupleDeg())).TupleAbs(); HTuple hv_result = GetHv_result(); hv_result = hv_result.TupleConcat("夹角"); hv_result = hv_result.TupleConcat(hv_jiaodu.D); result = hv_result.Clone(); ho_Rectangle1.Dispose(); ho_ImageReduced1.Dispose(); ho_Border.Dispose(); ho_ContoursSplit.Dispose(); ho_RegionLines.Dispose(); }
//向外传递匹配结果 public void SendMatchResult() { //到这里我就完全得到了静态图像的特征区块的中心点坐标和偏转角度(更新这个数组) MatchDataArray[0] = hv_Column; MatchDataArray[1] = hv_Row[0]; //角度转换为角度值(为了方便,角度和弧度都传递出去,要用哪个就用哪个) MatchDataArray[2] = hv_Angle.TupleDeg(); MatchDataArray[3] = hv_Angle; MatchDataArray[4] = hv_Error; OnGetMatchPatternSuccess(MatchDataArray); }
private void button1_Click(object sender, EventArgs e) { this.hWindows.HalconWindow.SetColor("red"); this.hWindows.HalconWindow.SetLineWidth(1); this.hWindows.HalconWindow.SetDraw("margin"); double beginX = 0, beginY = 0, endX = 0, endY = 0; this.hWindows.HalconWindow.DrawLine(out beginX, out beginY, out endX, out endY); HTuple anglePI = 0; HOperatorSet.LineOrientation(beginX, beginY, endX, endY, out anglePI); this.hWindows.HalconWindow.DispLine(beginX, beginY, endX, endY); this.lAngle.Text = $"角度:{anglePI.TupleDeg().D.ToString("f3")}"; }
/// <summary> /// 玻璃创建模板 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button7_Click(object sender, EventArgs e) { if (!System.IO.Directory.Exists(_Path)) { System.IO.Directory.CreateDirectory(_Path); //创建该文件夹 } if (_Image != null && _Image.IsInitialized()) { frmMatch2 = new FrmMatchModel(_Image, _Path); frmMatch2.ShowDialog(); } else { frmMatch2 = new FrmMatchModel(_Path); frmMatch2.ShowDialog(); } switch (Name) { case "左边膜": { attri.LeftRow = frmMatch2.ModelPose[0].D; attri.LeftCol = frmMatch2.ModelPose[1].D; HTuple ang = frmMatch2.ModelPose[2]; attri.LeftAngle = ang.TupleDeg(); } break; case "右边膜": { attri.RightRow = frmMatch2.ModelPose[0].D; attri.RightCol = frmMatch2.ModelPose[1].D; HTuple ang = frmMatch2.ModelPose[2]; attri.RightAngle = ang.TupleDeg(); } break; case "玻璃": { attri.GlassRow = frmMatch2.ModelPose[0].D; attri.GlassCol = frmMatch2.ModelPose[1].D; HTuple ang = frmMatch2.ModelPose[2]; attri.GlassAngle = ang.TupleDeg(); } break; default: break; } }
public void Matching(HObject Image) { List <coordinate> Lctest = new List <coordinate>(); HOperatorSet.GenEmptyObj(out ho_ModelContours); HOperatorSet.GenEmptyObj(out ho_TransContours); HOperatorSet.CreateShapeModel(Image, 2, (new HTuple(0)).TupleRad(), (new HTuple(360)).TupleRad(), (new HTuple(6.2177)).TupleRad(), (new HTuple("none")).TupleConcat( "no_pregeneration"), "use_polarity", ((new HTuple(10)).TupleConcat(13)).TupleConcat(4), 10, out hv_ModelId); ho_ModelContours.Dispose(); HOperatorSet.GetShapeModelContours(out ho_ModelContours, hv_ModelId, 1); HOperatorSet.FindShapeModel(ho_Dog, hv_ModelId, (new HTuple(0)).TupleRad(), (new HTuple(360)).TupleRad(), MatchingParameters.Score, 0, 0.5, "least_squares", (new HTuple(2)).TupleConcat( 1), 0.9, out hv_ModelRow, out hv_ModelColumn, out hv_ModelAngle, out hv_ModelScore); hv_Deg = hv_ModelAngle.TupleDeg(); HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "red"); HOperatorSet.SetLineWidth(hv_ExpDefaultWinHandle, 3); int totalcount = hv_ModelScore.TupleLength(); HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "green"); HOperatorSet.SetLineWidth(hv_ExpDefaultWinHandle, 1); for (hv_MatchingObjIdx = 0; (int)hv_MatchingObjIdx <= totalcount - 1; hv_MatchingObjIdx = (int)hv_MatchingObjIdx + 1) { MainWindow.cd.Add(new coordinate { Number = hv_MatchingObjIdx + 1, X = hv_ModelRow.TupleSelect(hv_MatchingObjIdx), Y = hv_ModelColumn.TupleSelect(hv_MatchingObjIdx), Angle = hv_Deg.TupleSelect(hv_MatchingObjIdx), }); Lctest.Add(new coordinate { Number = hv_MatchingObjIdx + 1, X = hv_ModelRow.TupleSelect(hv_MatchingObjIdx), Y = hv_ModelColumn.TupleSelect(hv_MatchingObjIdx), Angle = hv_Deg.TupleSelect(hv_MatchingObjIdx), }); } foreach (coordinate hs in Lctest) { } for (int i = 0; i < totalcount; i++) { // HOperatorSet.DispCircle(hv_ExpDefaultWinHandle, hv_ModelRow.TupleSelect(i), hv_ModelColumn.TupleSelect(i), 10); // HOperatorSet.GenCircle(hv_ExpDefaultWinHandle, hv_ModelRow.TupleSelect(i), hv_ModelColumn.TupleSelect(i), 10); } for (hv_MatchingObjIdx = 0; (int)hv_MatchingObjIdx <= totalcount - 1; hv_MatchingObjIdx = (int)hv_MatchingObjIdx + 1) { HOperatorSet.DispCross(hv_ExpDefaultWinHandle, hv_ModelRow.TupleSelect(hv_MatchingObjIdx), hv_ModelColumn.TupleSelect(hv_MatchingObjIdx), 10, hv_ModelAngle.TupleSelect(hv_MatchingObjIdx)); HOperatorSet.HomMat2dIdentity(out hv_HomMat); HOperatorSet.HomMat2dRotate(hv_HomMat, hv_ModelAngle.TupleSelect(hv_MatchingObjIdx), 0, 0, out hv_HomMat); HOperatorSet.HomMat2dTranslate(hv_HomMat, hv_ModelRow.TupleSelect(hv_MatchingObjIdx), hv_ModelColumn.TupleSelect(hv_MatchingObjIdx), out hv_HomMat); ho_TransContours.Dispose(); HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_TransContours, hv_HomMat); //HOperatorSet.ClearWindow(hv_ExpDefaultWinHandle); //HOperatorSet.DispObj(ho_Dog, hv_ExpDefaultWinHandle); HOperatorSet.SetDraw(hv_ExpDefaultWinHandle, "fill"); HOperatorSet.DispObj(ho_TransContours, hv_ExpDefaultWinHandle); HOperatorSet.SetWindowParam(hv_ExpDefaultWinHandle, "flush", "true"); HOperatorSet.FlushBuffer(hv_ExpDefaultWinHandle); } //Matching 01: Clear model when done HOperatorSet.ClearShapeModel(hv_ModelId); //Matching 01: END of generated code for model application }
// Main procedure private void action() { // Local iconic variables HObject ho_Image, ho_ImageGray, ho_ImageMean; HObject ho_Region, ho_ImageRotate1, ho_Rectangle, ho_ImageReduced; HObject ho_ImagePart; // Local control variables HTuple hv_WindowHandle = new HTuple(), hv_AbsoluteHisto = new HTuple(); HTuple hv_RelativeHisto = new HTuple(), hv_MinThresh = new HTuple(); HTuple hv_MaxThresh = new HTuple(), hv_Area = new HTuple(); HTuple hv_Row = new HTuple(), hv_Column = new HTuple(); HTuple hv_Phi3 = new HTuple(), hv_Row1 = new HTuple(); HTuple hv_Column1 = new HTuple(), hv_Row2 = new HTuple(); HTuple hv_Column2 = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image); HOperatorSet.GenEmptyObj(out ho_ImageGray); HOperatorSet.GenEmptyObj(out ho_ImageMean); HOperatorSet.GenEmptyObj(out ho_Region); HOperatorSet.GenEmptyObj(out ho_ImageRotate1); HOperatorSet.GenEmptyObj(out ho_Rectangle); HOperatorSet.GenEmptyObj(out ho_ImageReduced); HOperatorSet.GenEmptyObj(out ho_ImagePart); dev_update_off(); if (HDevWindowStack.IsOpen()) { HOperatorSet.CloseWindow(HDevWindowStack.Pop()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetDraw(HDevWindowStack.GetActive(), "margin"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "blue"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetLineWidth(HDevWindowStack.GetActive(), 3); } //read_image (Image, 'D:/Work/LCD/LCD Pictures/昆山OK相片/1S20JES0YF07R90PMAL8_white.jpg') ho_Image.Dispose(); HOperatorSet.ReadImage(out ho_Image, "D:/Work/LCD/LCD Pictures/昆山OK相片/lineNG_white.jpg"); hv_WindowHandle.Dispose(); dev_open_window_fit_image(ho_Image, 0, 0, -1, -1, out hv_WindowHandle); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } ho_ImageGray.Dispose(); HOperatorSet.Rgb3ToGray(ho_Image, ho_Image, ho_Image, out ho_ImageGray); ho_ImageMean.Dispose(); HOperatorSet.MeanImage(ho_ImageGray, out ho_ImageMean, 9, 9); hv_AbsoluteHisto.Dispose(); hv_RelativeHisto.Dispose(); HOperatorSet.GrayHisto(ho_ImageMean, ho_ImageMean, out hv_AbsoluteHisto, out hv_RelativeHisto); hv_MinThresh.Dispose(); hv_MaxThresh.Dispose(); HOperatorSet.HistoToThresh(hv_RelativeHisto, 20, out hv_MinThresh, out hv_MaxThresh); using (HDevDisposeHelper dh = new HDevDisposeHelper()) { ho_Region.Dispose(); HOperatorSet.Threshold(ho_ImageMean, out ho_Region, hv_MaxThresh.TupleSelect( 0), hv_MaxThresh.TupleSelect(1)); } hv_Area.Dispose(); hv_Row.Dispose(); hv_Column.Dispose(); HOperatorSet.AreaCenter(ho_Region, out hv_Area, out hv_Row, out hv_Column); hv_Phi3.Dispose(); HOperatorSet.OrientationRegion(ho_Region, out hv_Phi3); using (HDevDisposeHelper dh = new HDevDisposeHelper()) { ho_ImageRotate1.Dispose(); HOperatorSet.RotateImage(ho_ImageMean, out ho_ImageRotate1, -(hv_Phi3.TupleDeg() ), "constant"); } //count_obj (Region, Number) if (HDevWindowStack.IsOpen()) { HOperatorSet.ClearWindow(HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { //dev_display (Region) } //dev_clear_obj (Region) using (HDevDisposeHelper dh = new HDevDisposeHelper()) { ho_Region.Dispose(); HOperatorSet.Threshold(ho_ImageRotate1, out ho_Region, hv_MaxThresh.TupleSelect( 0), hv_MaxThresh.TupleSelect(1)); } hv_Row1.Dispose(); hv_Column1.Dispose(); hv_Row2.Dispose(); hv_Column2.Dispose(); HOperatorSet.InnerRectangle1(ho_Region, out hv_Row1, out hv_Column1, out hv_Row2, out hv_Column2); ho_Rectangle.Dispose(); HOperatorSet.GenRectangle1(out ho_Rectangle, hv_Row1, hv_Column1, hv_Row2, hv_Column2); ho_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_ImageRotate1, ho_Rectangle, out ho_ImageReduced); ho_ImagePart.Dispose(); HOperatorSet.CropDomain(ho_ImageReduced, out ho_ImagePart); HOperatorSet.WriteImage(ho_ImagePart, "bmp", 0, "C:/Users/D19040019/Desktop/test.bmp"); ho_Image.Dispose(); ho_ImageGray.Dispose(); ho_ImageMean.Dispose(); ho_Region.Dispose(); ho_ImageRotate1.Dispose(); ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_ImagePart.Dispose(); hv_WindowHandle.Dispose(); hv_AbsoluteHisto.Dispose(); hv_RelativeHisto.Dispose(); hv_MinThresh.Dispose(); hv_MaxThresh.Dispose(); hv_Area.Dispose(); hv_Row.Dispose(); hv_Column.Dispose(); hv_Phi3.Dispose(); hv_Row1.Dispose(); hv_Column1.Dispose(); hv_Row2.Dispose(); hv_Column2.Dispose(); }
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); }
private void action() { // Stack for temporary objects HObject[] OTemp = new HObject[20]; // Local iconic variables HObject ho_Rectangle, ho_ImageReduced; HObject ho_Region, ho_ConnectedRegions, ho_SelectedRegions; HObject ho_Rectangle1, ho_ImageRotate, ho_Region1, ho_RegionAffineTrans; HObject ho_Region2, ho_RegionUnionu, ho_RegionUniond, ho_Contours; HObject ho_SelectedContours, ho_SmoothedContours1, ho_ClippedContoursu; HObject ho_ClippedContoursd, ho_Rectangle2, ho_ClippedContoursuObjectSelected = null; HObject ho_ClippedContoursdObjectSelected = null, ho_Circle = null; HObject ho_Circle1 = null, ho_ConnectedRegions1, ho_SelectedRegions1; HObject ho_SortedRegionssdj, ho_Region3, ho_ConnectedRegions2; HObject ho_SelectedRegions2, ho_SortedRegionsxdj, ho_RegionLines = null; HObject ho_RegionAffineTrans1; // Local control variables HTuple hv_Width = null, hv_Height = null; HTuple hv_jd = null, hv_Row = null, hv_Column = null, hv_Phi = null; HTuple hv_Length1 = null, hv_Length2 = null, hv_HomMat2DIdentity = null; HTuple hv_HomMat2DRotate = null, hv_Row1 = null, hv_Column1 = null; HTuple hv_Row2 = null, hv_Column2 = null, hv_Rown = null; HTuple hv_Rowu = null, hv_Columnu = null, hv_Phiu = null; HTuple hv_Length1u = null, hv_Length2u = null, hv_MeasureHandleu = null; HTuple hv_GrayValuesu = null, hv_Sigmau = null, hv_Functionu = null; HTuple hv_SmoothedFunctionu = null, hv_FirstDerivativeu = null; HTuple hv_ZeroCrossingsu = null, hv_RowStartu = null, hv_ColStartu = null; HTuple hv_RowEndu = null, hv_ColEndu = null, hv_iu = null; HTuple hv_Rowd = null, hv_Columnd = null, hv_Phid = null; HTuple hv_Length1d = null, hv_Length2d = null, hv_MeasureHandled = null; HTuple hv_GrayValuesd = null, hv_Sigmad = null, hv_Functiond = null; HTuple hv_SmoothedFunctiond = null, hv_FirstDerivatived = null; HTuple hv_ZeroCrossingsd = null, hv_RowStartd = null, hv_ColStartd = null; HTuple hv_RowEndd = null, hv_ColEndd = null, hv_id = null; HTuple hv_RowIntuAll = null, hv_ColumnIntuAll = null, hv_u1 = null; HTuple hv_i = null, hv_Length = new HTuple(), hv_Indices1 = new HTuple(); HTuple hv_RowIntu = new HTuple(), hv_ColumnIntu = new HTuple(); HTuple hv_IsOverlappingu = new HTuple(), hv_RowIntdAll = null; HTuple hv_ColumnIntdAll = null, hv_j = null, hv_RowIntd = new HTuple(); HTuple hv_ColumnIntd = new HTuple(), hv_IsOverlappingd = new HTuple(); HTuple hv_Sortedu = null, hv_Sortedd = null, hv_RowIntuu = null; HTuple hv_ColumnIntuu = null, hv_RowIntud = null, hv_ColumnIntud = null; HTuple hv_uu = null, hv_ud = null, hv_ub1 = null, hv_RowIntdu = null; HTuple hv_ColumnIntdu = null, hv_RowIntdd = null, hv_ColumnIntdd = null; HTuple hv_du = null, hv_dd = null, hv_db1 = null, hv_Areasdj = null; HTuple hv_Rowsdj = null, hv_Columnsdj = null, hv_sdj = null; HTuple hv_Areaxdj = null, hv_Rowxdj = null, hv_Columnxdj = null; HTuple hv_xdj = null, hv_djsl1 = null, hv_djsl = null; HTuple hv_Mean1 = null, hv_Mean2 = null, hv_Mean3 = null; HTuple hv_Mean4 = null, hv_Columnuu = null, hv_Columndd = null; HTuple hv_AngleAll = null, hv_l = null, hv_Angle = new HTuple(); HTuple hv_DegAll = null, hv_Mean5 = null, hv_Mean8 = null; HTuple hv_Columnuum = null, hv_uum1 = null, hv_uus = null; HTuple hv_Columnddm = null, hv_ddm = null, hv_dds = null; HTuple hv_ColumnmArr = null, hv_Mean9 = null, hv_Mean10 = null; HTuple hv_Sorteda = null, hv_Ai = null, hv_Ax = null, hv_pixeldist = null; HTuple hv_L = null, hv_D = null, hv_Dmax = null, hv_Dmin = null; HTuple hv_d = null, hv_dmax = null, hv_dmin = null, hv_LS = null; HTuple hv_HomMat2DIdentity1 = null, hv_HomMat2DRotate1 = null; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Rectangle); HOperatorSet.GenEmptyObj(out ho_ImageReduced); HOperatorSet.GenEmptyObj(out ho_Region); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions); HOperatorSet.GenEmptyObj(out ho_SelectedRegions); HOperatorSet.GenEmptyObj(out ho_Rectangle1); HOperatorSet.GenEmptyObj(out ho_ImageRotate); HOperatorSet.GenEmptyObj(out ho_Region1); HOperatorSet.GenEmptyObj(out ho_RegionAffineTrans); HOperatorSet.GenEmptyObj(out ho_Region2); HOperatorSet.GenEmptyObj(out ho_RegionUnionu); HOperatorSet.GenEmptyObj(out ho_RegionUniond); HOperatorSet.GenEmptyObj(out ho_Contours); HOperatorSet.GenEmptyObj(out ho_SelectedContours); HOperatorSet.GenEmptyObj(out ho_SmoothedContours1); HOperatorSet.GenEmptyObj(out ho_ClippedContoursu); HOperatorSet.GenEmptyObj(out ho_ClippedContoursd); HOperatorSet.GenEmptyObj(out ho_Rectangle2); HOperatorSet.GenEmptyObj(out ho_ClippedContoursuObjectSelected); HOperatorSet.GenEmptyObj(out ho_ClippedContoursdObjectSelected); HOperatorSet.GenEmptyObj(out ho_Circle); HOperatorSet.GenEmptyObj(out ho_Circle1); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1); HOperatorSet.GenEmptyObj(out ho_SelectedRegions1); HOperatorSet.GenEmptyObj(out ho_SortedRegionssdj); HOperatorSet.GenEmptyObj(out ho_Region3); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions2); HOperatorSet.GenEmptyObj(out ho_SelectedRegions2); HOperatorSet.GenEmptyObj(out ho_SortedRegionsxdj); HOperatorSet.GenEmptyObj(out ho_RegionLines); HOperatorSet.GenEmptyObj(out ho_RegionAffineTrans1); HOperatorSet.Union1(algorithm.Region, out RegionToDisp); try { HOperatorSet.GetImageSize(Image, out hv_Width, out hv_Height); ho_Rectangle.Dispose(); HOperatorSet.GenRectangle1(out ho_Rectangle, DRow1m, DCol1m, DRow2m, DCol2m); ho_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(Image, ho_Rectangle, out ho_ImageReduced); //* ho_Region.Dispose(); HOperatorSet.Threshold(ho_ImageReduced, out ho_Region, 0, 128); ho_ConnectedRegions.Dispose(); HOperatorSet.Connection(ho_Region, out ho_ConnectedRegions); ho_SelectedRegions.Dispose(); HOperatorSet.SelectShapeStd(ho_ConnectedRegions, out ho_SelectedRegions, "max_area", 70); //hv_jd = 0; HOperatorSet.SmallestRectangle2(ho_SelectedRegions, out hv_Row, out hv_Column, out hv_Phi, out hv_Length1, out hv_Length2); ho_Rectangle1.Dispose(); HOperatorSet.GenRectangle2(out ho_Rectangle1, hv_Row, hv_Column, hv_Phi, hv_Length1, hv_Length2); ho_ImageRotate.Dispose(); HOperatorSet.RotateImage(Image, out ho_ImageRotate, -(hv_Phi.TupleDeg()), "constant"); //* ho_Region1.Dispose(); HOperatorSet.Threshold(ho_ImageRotate, out ho_Region1, 0, 128); //* HOperatorSet.HomMat2dIdentity(out hv_HomMat2DIdentity); HOperatorSet.HomMat2dRotate(hv_HomMat2DIdentity, -hv_Phi, hv_Height / 2, hv_Width / 2, out hv_HomMat2DRotate); ho_RegionAffineTrans.Dispose(); HOperatorSet.AffineTransRegion(ho_Rectangle1, out ho_RegionAffineTrans, hv_HomMat2DRotate, "nearest_neighbor"); HOperatorSet.SmallestRectangle1(ho_RegionAffineTrans, out hv_Row1, out hv_Column1, out hv_Row2, out hv_Column2); //*** ho_Region2.Dispose(); HOperatorSet.GenRegionPoints(out ho_Region2, hv_Row1, hv_Column1); ho_RegionUnionu.Dispose(); HOperatorSet.Union1(ho_Region2, out ho_RegionUnionu); ho_RegionUniond.Dispose(); HOperatorSet.Union1(ho_Region2, out ho_RegionUniond); ho_Contours.Dispose(); HOperatorSet.GenContourRegionXld(ho_Region1, out ho_Contours, "border"); ho_SelectedContours.Dispose(); HOperatorSet.SelectContoursXld(ho_Contours, out ho_SelectedContours, "contour_length", 20, 2000000, -1, 1); ho_SmoothedContours1.Dispose(); HOperatorSet.SmoothContoursXld(ho_SelectedContours, out ho_SmoothedContours1, 13); //* hv_Rown = (hv_Row1 + hv_Row2) / 2; ho_ClippedContoursu.Dispose(); HOperatorSet.ClipContoursXld(ho_SmoothedContours1, out ho_ClippedContoursu, hv_Row1, hv_Column1, hv_Rown, hv_Column2); ho_ClippedContoursd.Dispose(); HOperatorSet.ClipContoursXld(ho_SmoothedContours1, out ho_ClippedContoursd, hv_Rown, hv_Column1, hv_Row2, hv_Column2); hv_Rowu = (hv_Row1 + hv_Rown) / 2; hv_Columnu = (hv_Column1 + hv_Column2) / 2; hv_Phiu = 0; hv_Length1u = (hv_Column2 - hv_Column1) / 2; hv_Length2u = (hv_Rown - hv_Row1) / 2; //dev_set_draw ('margin') HOperatorSet.GenMeasureRectangle2(hv_Rowu, hv_Columnu, hv_Phiu, hv_Length1u, hv_Length2u, hv_Width, hv_Height, "bilinear", out hv_MeasureHandleu); ho_Rectangle2.Dispose(); HOperatorSet.GenRectangle2(out ho_Rectangle2, hv_Rowu, hv_Columnu, hv_Phiu, hv_Length1u, hv_Length2u); HOperatorSet.MeasureProjection(ho_ImageRotate, hv_MeasureHandleu, out hv_GrayValuesu); hv_Sigmau = 5; HOperatorSet.CreateFunct1dArray(hv_GrayValuesu, out hv_Functionu); HOperatorSet.SmoothFunct1dGauss(hv_Functionu, hv_Sigmau, out hv_SmoothedFunctionu); HOperatorSet.DerivateFunct1d(hv_SmoothedFunctionu, "first", out hv_FirstDerivativeu); HOperatorSet.ZeroCrossingsFunct1d(hv_FirstDerivativeu, out hv_ZeroCrossingsu); hv_RowStartu = hv_Rowu - (hv_Length2u / 2); hv_ColStartu = (hv_Columnu - hv_Length1u) + hv_ZeroCrossingsu; hv_RowEndu = hv_Rowu + (hv_Length2u / 2); hv_ColEndu = (hv_Columnu - hv_Length1u) + hv_ZeroCrossingsu; hv_iu = new HTuple(hv_ZeroCrossingsu.TupleLength()); hv_Rowd = (hv_Rown + hv_Row2) / 2; hv_Columnd = (hv_Column1 + hv_Column2) / 2; hv_Phid = 0; hv_Length1d = (hv_Column2 - hv_Column1) / 2; hv_Length2d = (hv_Row2 - hv_Rown) / 2; HOperatorSet.GenMeasureRectangle2(hv_Rowd, hv_Columnd, hv_Phid, hv_Length1d, hv_Length2d, hv_Width, hv_Height, "bilinear", out hv_MeasureHandled); HOperatorSet.MeasureProjection(ho_ImageRotate, hv_MeasureHandled, out hv_GrayValuesd); // hv_Sigmad = 5; HOperatorSet.CreateFunct1dArray(hv_GrayValuesd, out hv_Functiond); HOperatorSet.SmoothFunct1dGauss(hv_Functiond, hv_Sigmad, out hv_SmoothedFunctiond); HOperatorSet.DerivateFunct1d(hv_SmoothedFunctiond, "first", out hv_FirstDerivatived); HOperatorSet.ZeroCrossingsFunct1d(hv_FirstDerivatived, out hv_ZeroCrossingsd); HOperatorSet.CloseMeasure(hv_MeasureHandleu); HOperatorSet.CloseMeasure(hv_MeasureHandled); hv_RowStartd = hv_Rowd - (hv_Length2d / 2); hv_ColStartd = (hv_Columnd - hv_Length1d) + hv_ZeroCrossingsd; hv_RowEndd = hv_Rowd + (hv_Length2d / 2); hv_ColEndd = (hv_Columnd - hv_Length1d) + hv_ZeroCrossingsd; hv_id = new HTuple(hv_ZeroCrossingsd.TupleLength()); hv_RowIntuAll = new HTuple(); hv_ColumnIntuAll = new HTuple(); hv_u1 = 0; HTuple end_val96 = hv_iu - 1; HTuple step_val96 = 1; for (hv_i = 0; hv_i.Continue(end_val96, step_val96); hv_i = hv_i.TupleAdd(step_val96)) { HOperatorSet.LengthXld(ho_ClippedContoursu, out hv_Length); HOperatorSet.TupleFind(hv_Length, hv_Length.TupleMax(), out hv_Indices1); ho_ClippedContoursuObjectSelected.Dispose(); HOperatorSet.SelectObj(ho_ClippedContoursu, out ho_ClippedContoursuObjectSelected, hv_Indices1 + 1); HOperatorSet.IntersectionLineContourXld(ho_ClippedContoursuObjectSelected, hv_RowStartu, hv_ColStartu.TupleSelect(hv_i), hv_RowEndu, hv_ColEndu.TupleSelect( hv_i), out hv_RowIntu, out hv_ColumnIntu, out hv_IsOverlappingu); //*gen_cross_contour_xld (Cross1u, RowIntu, ColumnIntu, 12, 0)//根据每个输入点交叉的形状创键一个XLD轮廓(contour) hv_RowIntuAll = hv_RowIntuAll.TupleConcat(hv_RowIntu); hv_ColumnIntuAll = hv_ColumnIntuAll.TupleConcat(hv_ColumnIntu); hv_u1 = hv_u1 + 1; } hv_RowIntdAll = new HTuple(); hv_ColumnIntdAll = new HTuple(); HTuple end_val109 = hv_id - 1; HTuple step_val109 = 1; for (hv_j = 0; hv_j.Continue(end_val109, step_val109); hv_j = hv_j.TupleAdd(step_val109)) { HOperatorSet.LengthXld(ho_ClippedContoursd, out hv_Length); HOperatorSet.TupleFind(hv_Length, hv_Length.TupleMax(), out hv_Indices1); ho_ClippedContoursdObjectSelected.Dispose(); HOperatorSet.SelectObj(ho_ClippedContoursd, out ho_ClippedContoursdObjectSelected, hv_Indices1 + 1); HOperatorSet.IntersectionLineContourXld(ho_ClippedContoursdObjectSelected, hv_RowStartd, hv_ColStartd.TupleSelect(hv_j), hv_RowEndd, hv_ColEndd.TupleSelect( hv_j), out hv_RowIntd, out hv_ColumnIntd, out hv_IsOverlappingd); //*gen_cross_contour_xld (Cross1d, RowIntd, ColumnIntd, 12, 0)//根据每个输入点交叉的形状创键一个XLD轮廓(contour) hv_RowIntdAll = hv_RowIntdAll.TupleConcat(hv_RowIntd); hv_ColumnIntdAll = hv_ColumnIntdAll.TupleConcat(hv_ColumnIntd); } //stop () HOperatorSet.TupleSort(hv_RowIntuAll, out hv_Sortedu); HOperatorSet.TupleSort(hv_RowIntdAll, out hv_Sortedd); hv_RowIntuu = new HTuple(); hv_ColumnIntuu = new HTuple(); hv_RowIntud = new HTuple(); hv_ColumnIntud = new HTuple(); hv_uu = 0; hv_ud = 0; for (hv_ub1 = 0; (int)hv_ub1 <= (int)((new HTuple(hv_Sortedu.TupleLength())) - 1); hv_ub1 = (int)hv_ub1 + 1) { if ((int)(new HTuple(((hv_RowIntuAll.TupleSelect(hv_ub1))).TupleLess(((((hv_Sortedu.TupleSelect( 0)) * 2) + (hv_Sortedu.TupleSelect((new HTuple(hv_Sortedu.TupleLength())) - 1))) / 3) + 3))) != 0) { hv_RowIntuu = hv_RowIntuu.TupleConcat(hv_RowIntuAll.TupleSelect(hv_ub1)); hv_ColumnIntuu = hv_ColumnIntuu.TupleConcat(hv_ColumnIntuAll.TupleSelect( hv_ub1)); ho_Circle.Dispose(); HOperatorSet.GenCircle(out ho_Circle, hv_RowIntuAll.TupleSelect(hv_ub1), hv_ColumnIntuAll.TupleSelect(hv_ub1), 1); { HObject ExpTmpOutVar_0; HOperatorSet.Union2(ho_RegionUnionu, ho_Circle, out ExpTmpOutVar_0); ho_RegionUnionu.Dispose(); ho_RegionUnionu = ExpTmpOutVar_0; } hv_uu = hv_uu + 1; } if ((int)(new HTuple(((hv_RowIntuAll.TupleSelect(hv_ub1))).TupleGreater((((hv_Sortedu.TupleSelect( 0)) + ((hv_Sortedu.TupleSelect((new HTuple(hv_Sortedu.TupleLength())) - 1)) * 2)) / 3) - 3))) != 0) { hv_RowIntud = hv_RowIntud.TupleConcat(hv_RowIntuAll.TupleSelect(hv_ub1)); hv_ColumnIntud = hv_ColumnIntud.TupleConcat(hv_ColumnIntuAll.TupleSelect( hv_ub1)); hv_ud = hv_ud + 1; } } //stop () hv_RowIntdu = new HTuple(); hv_ColumnIntdu = new HTuple(); hv_RowIntdd = new HTuple(); hv_ColumnIntdd = new HTuple(); hv_du = 0; hv_dd = 0; for (hv_db1 = 0; (int)hv_db1 <= (int)((new HTuple(hv_Sortedd.TupleLength())) - 1); hv_db1 = (int)hv_db1 + 1) { if ((int)(new HTuple(((hv_RowIntdAll.TupleSelect(hv_db1))).TupleLess(((((hv_Sortedd.TupleSelect( 0)) * 2) + (hv_Sortedd.TupleSelect((new HTuple(hv_Sortedd.TupleLength())) - 1))) / 3) + 3))) != 0) { hv_RowIntdu = hv_RowIntdu.TupleConcat(hv_RowIntdAll.TupleSelect(hv_db1)); hv_ColumnIntdu = hv_ColumnIntdu.TupleConcat(hv_ColumnIntdAll.TupleSelect( hv_db1)); hv_du = hv_du + 1; } if ((int)(new HTuple(((hv_RowIntdAll.TupleSelect(hv_db1))).TupleGreater((((hv_Sortedd.TupleSelect( 0)) + ((hv_Sortedd.TupleSelect((new HTuple(hv_Sortedd.TupleLength())) - 1)) * 2)) / 3) - 3))) != 0) { hv_RowIntdd = hv_RowIntdd.TupleConcat(hv_RowIntdAll.TupleSelect(hv_db1)); hv_ColumnIntdd = hv_ColumnIntdd.TupleConcat(hv_ColumnIntdAll.TupleSelect( hv_db1)); ho_Circle1.Dispose(); HOperatorSet.GenCircle(out ho_Circle1, hv_RowIntdAll.TupleSelect(hv_db1), hv_ColumnIntdAll.TupleSelect(hv_db1), 1); { HObject ExpTmpOutVar_0; HOperatorSet.Union2(ho_RegionUniond, ho_Circle1, out ExpTmpOutVar_0); ho_RegionUniond.Dispose(); ho_RegionUniond = ExpTmpOutVar_0; } hv_dd = hv_dd + 1; } } ho_ConnectedRegions1.Dispose(); HOperatorSet.Connection(ho_RegionUnionu, out ho_ConnectedRegions1); ho_SelectedRegions1.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegions1, out ho_SelectedRegions1, "area", "and", 3, 99999); ho_SortedRegionssdj.Dispose(); HOperatorSet.SortRegion(ho_SelectedRegions1, out ho_SortedRegionssdj, "upper_left", "true", "column"); HOperatorSet.AreaCenter(ho_SortedRegionssdj, out hv_Areasdj, out hv_Rowsdj, out hv_Columnsdj); ho_Region3.Dispose(); HOperatorSet.GenRegionPoints(out ho_Region3, hv_Rowsdj.TupleSelect(0), hv_Columnsdj.TupleSelect( 0)); hv_sdj = new HTuple(hv_Columnsdj.TupleLength()); ho_ConnectedRegions2.Dispose(); HOperatorSet.Connection(ho_RegionUniond, out ho_ConnectedRegions2); ho_SelectedRegions2.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegions2, out ho_SelectedRegions2, "area", "and", 3, 99999); ho_SortedRegionsxdj.Dispose(); HOperatorSet.SortRegion(ho_SelectedRegions2, out ho_SortedRegionsxdj, "upper_left", "true", "column"); HOperatorSet.AreaCenter(ho_SortedRegionsxdj, out hv_Areaxdj, out hv_Rowxdj, out hv_Columnxdj); hv_xdj = new HTuple(hv_Columnxdj.TupleLength()); hv_djsl1 = new HTuple(); hv_djsl1 = hv_djsl1.TupleConcat(hv_sdj); hv_djsl1 = hv_djsl1.TupleConcat(hv_xdj); HOperatorSet.TupleMin(hv_djsl1, out hv_djsl); HOperatorSet.TupleMean(hv_RowIntuu, out hv_Mean1); HOperatorSet.TupleMean(hv_RowIntud, out hv_Mean2); HOperatorSet.TupleMean(hv_RowIntdu, out hv_Mean3); HOperatorSet.TupleMean(hv_RowIntdd, out hv_Mean4); hv_Columnuu = new HTuple(); hv_Columndd = new HTuple(); hv_AngleAll = new HTuple(); HTuple end_val191 = hv_djsl - 1; HTuple step_val191 = 1; for (hv_l = 0; hv_l.Continue(end_val191, step_val191); hv_l = hv_l.TupleAdd(step_val191)) { ho_RegionLines.Dispose(); HOperatorSet.GenRegionLine(out ho_RegionLines, hv_Rowsdj.TupleSelect(hv_l), hv_Columnsdj.TupleSelect(hv_l), hv_Rowxdj.TupleSelect(hv_l), hv_Columnxdj.TupleSelect( hv_l)); { HObject ExpTmpOutVar_0; HOperatorSet.Union2(ho_Region3, ho_RegionLines, out ExpTmpOutVar_0); ho_Region3.Dispose(); ho_Region3 = ExpTmpOutVar_0; } HOperatorSet.AngleLx(hv_Rowsdj.TupleSelect(hv_l), hv_Columnsdj.TupleSelect( hv_l), hv_Rowxdj.TupleSelect(hv_l), hv_Columnxdj.TupleSelect(hv_l), out hv_Angle); hv_AngleAll = hv_AngleAll.TupleConcat(0 - hv_Angle); if ((int)(new HTuple(hv_l.TupleGreater(0))) != 0) { hv_Columnuu = hv_Columnuu.TupleConcat((hv_ColumnIntuu.TupleSelect(hv_l)) - (hv_ColumnIntuu.TupleSelect( hv_l - 1))); hv_Columndd = hv_Columndd.TupleConcat((hv_ColumnIntdd.TupleSelect(hv_l)) - (hv_ColumnIntdd.TupleSelect( hv_l - 1))); } //stop () } HOperatorSet.TupleDeg(hv_AngleAll, out hv_DegAll); HOperatorSet.TupleMean(hv_Columnuu, out hv_Mean5); HOperatorSet.TupleMean(hv_Columndd, out hv_Mean8); //**过滤 hv_Columnuum = new HTuple(); hv_uum1 = 0; for (hv_uus = 0; (int)hv_uus <= (int)((new HTuple(hv_Columnuu.TupleLength())) - 1); hv_uus = (int)hv_uus + 1) { if ((int)((new HTuple(hv_Columnuu.TupleLessEqual(hv_Mean5 * 1.1))).TupleAnd(new HTuple(hv_Columnuu.TupleGreaterEqual( hv_Mean5 * 0.9)))) != 0) { hv_Columnuum = hv_Columnuum.TupleConcat(hv_Columnuu.TupleSelect(hv_uus)); hv_uum1 = hv_uum1 + 1; } } hv_Columnddm = new HTuple(); hv_ddm = 0; for (hv_dds = 0; (int)hv_dds <= (int)((new HTuple(hv_Columndd.TupleLength())) - 1); hv_dds = (int)hv_dds + 1) { if ((int)((new HTuple(hv_Columndd.TupleLessEqual(hv_Mean8 * 1.1))).TupleAnd(new HTuple(hv_Columndd.TupleGreaterEqual( hv_Mean8 * 0.9)))) != 0) { hv_Columnddm = hv_Columnddm.TupleConcat(hv_Columndd.TupleSelect(hv_dds)); hv_ddm = hv_ddm + 1; } } hv_ColumnmArr = new HTuple(); hv_ColumnmArr = hv_ColumnmArr.TupleConcat(hv_Columnuum); hv_ColumnmArr = hv_ColumnmArr.TupleConcat(hv_Columnddm); HOperatorSet.TupleMean(hv_ColumnmArr, out hv_Mean9); HOperatorSet.TupleMean(hv_DegAll, out hv_Mean10); HOperatorSet.TupleSort(hv_DegAll, out hv_Sorteda); hv_Ai = hv_Sorteda[0]; hv_Ax = hv_Sorteda[(new HTuple(hv_Sorteda.TupleLength())) - 1]; hv_pixeldist = 1; hv_L = ((hv_Columnsdj.TupleSelect(hv_djsl - 1)) - (hv_Columnsdj.TupleSelect(0))) * hv_pixeldist; hv_D = (hv_Mean4 - hv_Mean1) * hv_pixeldist; hv_Dmax = ((hv_RowIntdd.TupleMax()) - (hv_RowIntuu.TupleMin())) * hv_pixeldist; hv_Dmin = ((hv_RowIntdd.TupleMin()) - (hv_RowIntuu.TupleMax())) * hv_pixeldist; hv_d = (hv_Mean3 - hv_Mean2) * hv_pixeldist; hv_dmax = ((hv_RowIntdu.TupleMax()) - (hv_RowIntud.TupleMin())) * hv_pixeldist; hv_dmin = ((hv_RowIntdu.TupleMin()) - (hv_RowIntud.TupleMax())) * hv_pixeldist; hv_LS = hv_Mean9 * hv_pixeldist; //*区域旋转 HOperatorSet.HomMat2dIdentity(out hv_HomMat2DIdentity1); HOperatorSet.HomMat2dRotate(hv_HomMat2DIdentity1, hv_Phi, hv_Height / 2, hv_Width / 2, out hv_HomMat2DRotate1); ho_RegionAffineTrans1.Dispose(); HOperatorSet.AffineTransRegion(ho_Region3, out ho_RegionAffineTrans1, hv_HomMat2DRotate1, "nearest_neighbor"); HOperatorSet.Union1(ho_RegionAffineTrans1, out RegionToDisp); HTuple hv_result = GetHv_result(); hv_result = hv_result.TupleConcat("齿数"); hv_result = hv_result.TupleConcat(hv_djsl.D); hv_result = hv_result.TupleConcat("螺纹长度"); hv_result = hv_result.TupleConcat(hv_L.D * pixeldist); hv_result = hv_result.TupleConcat("螺纹大径平均值"); hv_result = hv_result.TupleConcat(hv_D.D * pixeldist); hv_result = hv_result.TupleConcat("螺纹大径最小值"); hv_result = hv_result.TupleConcat(hv_Dmin.D * pixeldist); hv_result = hv_result.TupleConcat("螺纹大径最大值"); hv_result = hv_result.TupleConcat(hv_Dmax.D * pixeldist); hv_result = hv_result.TupleConcat("螺纹小径平均值"); hv_result = hv_result.TupleConcat(hv_d.D * pixeldist); hv_result = hv_result.TupleConcat("螺纹小径最小值"); hv_result = hv_result.TupleConcat(hv_dmin.D * pixeldist); hv_result = hv_result.TupleConcat("螺纹小径最大值"); hv_result = hv_result.TupleConcat(hv_dmax.D * pixeldist); hv_result = hv_result.TupleConcat("牙距"); hv_result = hv_result.TupleConcat(hv_LS.D * pixeldist); hv_result = hv_result.TupleConcat("平均牙倾角"); hv_result = hv_result.TupleConcat(hv_Mean10.D); hv_result = hv_result.TupleConcat("最小牙倾角"); hv_result = hv_result.TupleConcat(hv_Ai.D); hv_result = hv_result.TupleConcat("最大牙倾角"); hv_result = hv_result.TupleConcat(hv_Ax.D); result = hv_result.Clone(); ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_Region.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions.Dispose(); ho_Rectangle1.Dispose(); ho_ImageRotate.Dispose(); ho_Region1.Dispose(); ho_RegionAffineTrans.Dispose(); ho_Region2.Dispose(); ho_RegionUnionu.Dispose(); ho_RegionUniond.Dispose(); ho_Contours.Dispose(); ho_SelectedContours.Dispose(); ho_SmoothedContours1.Dispose(); ho_ClippedContoursu.Dispose(); ho_ClippedContoursd.Dispose(); ho_Rectangle2.Dispose(); ho_ClippedContoursuObjectSelected.Dispose(); ho_ClippedContoursdObjectSelected.Dispose(); ho_Circle.Dispose(); ho_Circle1.Dispose(); ho_ConnectedRegions1.Dispose(); ho_SelectedRegions1.Dispose(); ho_SortedRegionssdj.Dispose(); ho_Region3.Dispose(); ho_ConnectedRegions2.Dispose(); ho_SelectedRegions2.Dispose(); ho_SortedRegionsxdj.Dispose(); ho_RegionLines.Dispose(); ho_RegionAffineTrans1.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("螺纹大径最小值"); 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_Region.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions.Dispose(); ho_Rectangle1.Dispose(); ho_ImageRotate.Dispose(); ho_Region1.Dispose(); ho_RegionAffineTrans.Dispose(); ho_Region2.Dispose(); ho_RegionUnionu.Dispose(); ho_RegionUniond.Dispose(); ho_Contours.Dispose(); ho_SelectedContours.Dispose(); ho_SmoothedContours1.Dispose(); ho_ClippedContoursu.Dispose(); ho_ClippedContoursd.Dispose(); ho_Rectangle2.Dispose(); ho_ClippedContoursuObjectSelected.Dispose(); ho_ClippedContoursdObjectSelected.Dispose(); ho_Circle.Dispose(); ho_Circle1.Dispose(); ho_ConnectedRegions1.Dispose(); ho_SelectedRegions1.Dispose(); ho_SortedRegionssdj.Dispose(); ho_Region3.Dispose(); ho_ConnectedRegions2.Dispose(); ho_SelectedRegions2.Dispose(); ho_SortedRegionsxdj.Dispose(); ho_RegionLines.Dispose(); ho_RegionAffineTrans1.Dispose(); algorithm.Region.Dispose(); } finally { ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_Region.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions.Dispose(); ho_Rectangle1.Dispose(); ho_ImageRotate.Dispose(); ho_Region1.Dispose(); ho_RegionAffineTrans.Dispose(); ho_Region2.Dispose(); ho_RegionUnionu.Dispose(); ho_RegionUniond.Dispose(); ho_Contours.Dispose(); ho_SelectedContours.Dispose(); ho_SmoothedContours1.Dispose(); ho_ClippedContoursu.Dispose(); ho_ClippedContoursd.Dispose(); ho_Rectangle2.Dispose(); ho_ClippedContoursuObjectSelected.Dispose(); ho_ClippedContoursdObjectSelected.Dispose(); ho_Circle.Dispose(); ho_Circle1.Dispose(); ho_ConnectedRegions1.Dispose(); ho_SelectedRegions1.Dispose(); ho_SortedRegionssdj.Dispose(); ho_Region3.Dispose(); ho_ConnectedRegions2.Dispose(); ho_SelectedRegions2.Dispose(); ho_SortedRegionsxdj.Dispose(); ho_RegionLines.Dispose(); ho_RegionAffineTrans1.Dispose(); algorithm.Region.Dispose(); } }
// Main procedure private HTuple action() { // Local iconic variables HObject ho_Image, ho_ImageRotate, ho_GrayImage; HObject ho_ImageMedian, ho_Region, ho_ConnectedRegions; HObject ho_SelectedRegions, ho_SelectedRegions2, ho_SelectedRegions1; HObject ho_SelectedRegions3, ho_SortedRegions, ho_RegionUnion; HObject ho_Rectangle, ho_ImageReduced, ho_GrayImageA, ho_GragImageA; HObject ho_RegionsA_temp, ho_ConnectedRegionsA_temp, ho_SelectedRegionsA_temp; HObject ho_ImageAffineTrans, ho_RegionsA, ho_RegionDilationA; HObject ho_ConnectedRegionsA, ho_SelectedRegionsA, ho_RegionsA1; HObject ho_RegionOpeningA, ho_SelectedRegionsA1, ho_RegionUnionA; HObject ho_SelectedRegionsA2, ho_RegionsB, ho_ConnectedRegionsB; HObject ho_SelectedRegionsB, ho_RectangleB1, ho_RectangleB2; HObject ho_RegionDifferenceB, ho_ImageReducedB1, ho_RegionB1; HObject ho_ConnectedRegionsB2, ho_SelectedRegionsB2, ho_outContours; HObject ho_SelectedContours, ho_SortedContours, ho_Region1; HObject ho_ConnectedRegions1, ho_SelectedRegions4, ho_SelectedRegions5; HObject ho_SelectedRegions6, ho_Contours, ho_SortedContours3; HObject ho_obj = null, ho_objj = null, ho_ContoursSplit = null; HObject ho_SelectedXLD = null, ho_SortedContours1 = null, ho_M = null; HObject ho_Contour = null, ho_SelectedXLD1 = null, ho_SortedContours2 = null; HObject ho_ObjectSelectedB = null; // Local control variables HTuple hv_NumberB = null, hv_Row1 = null, hv_Column1 = null; HTuple hv_Phi = null, hv_Length1 = null, hv_Length2 = null; HTuple hv_Phi_temp = null, hv_Area_temp = null, hv_Row_temp = null; HTuple hv_Column_temp = null, hv_HomMat2D = null, hv_RowA1 = null; HTuple hv_ColumnA1 = null, hv_RowA2 = null, hv_ColumnA2 = null; HTuple hv_RowA3 = null, hv_ColumnA3 = null, hv_RowA4 = null; HTuple hv_ColumnA4 = null, hv_RowB16 = null, hv_ColumnB16 = null; HTuple hv_PhiB3 = null, hv_LengthB1 = null, hv_LengthB2 = null; HTuple hv_AreaB = null, hv_RowB2 = null, hv_ColumnB2 = null; HTuple hv_Number = null, hv_Number3 = null, hv_A1 = null; HTuple hv_A2 = null, hv_B1 = null, hv_B2 = null, hv_C1 = null; HTuple hv_C2 = null, hv_C3 = null, hv_C4 = null, hv_i = null; HTuple hv_Ro1 = new HTuple(), hv_Co1 = new HTuple(), hv_MinBR = new HTuple(); HTuple hv_MaxBR = new HTuple(), hv_MinBC = new HTuple(); HTuple hv_MaxBC = new HTuple(), hv_BR = new HTuple(), hv_BC = new HTuple(); HTuple hv_j = new HTuple(), hv_Number1 = new HTuple(); HTuple hv_a = new HTuple(), hv_Ro2 = new HTuple(), hv_Co2 = new HTuple(); HTuple hv_LUC = new HTuple(), hv_RUC = new HTuple(), hv_Ro3 = new HTuple(); HTuple hv_Co3 = new HTuple(), hv_LDC = new HTuple(), hv_RDC = new HTuple(); HTuple hv_RowBegin = new HTuple(), hv_ColBegin = new HTuple(); HTuple hv_RowEnd = new HTuple(), hv_ColEnd = new HTuple(); HTuple hv_Nr = new HTuple(), hv_Nc = new HTuple(), hv_Dist = new HTuple(); HTuple hv_Rowc = new HTuple(), hv_Colc = new HTuple(); HTuple hv_MeanR = new HTuple(), hv_H1 = new HTuple(), hv_H3 = new HTuple(); HTuple hv_Number2 = new HTuple(), hv_b = new HTuple(); HTuple hv_Ro4 = new HTuple(), hv_Co4 = new HTuple(), hv_LUR = new HTuple(); HTuple hv_LDR = new HTuple(), hv_Ro5 = new HTuple(), hv_Co5 = new HTuple(); HTuple hv_RUR = new HTuple(), hv_RDR = new HTuple(), hv_MeanC = new HTuple(); HTuple hv_H4 = new HTuple(), hv_H2 = new HTuple(), hv_Distance1 = new HTuple(); HTuple hv_Distance2 = new HTuple(), hv_AreaA = null, hv_RowA = null; HTuple hv_ColumnA = null, hv_phi = null, hv_paozhengwei = null; HTuple hv_k = null, hv_Indices = null, hv_liebo = null; HTuple hv_NumB = null, hv_count = null, hv_I = new HTuple(); HTuple hv_AreaB1 = new HTuple(), hv_RowB3 = new HTuple(); HTuple hv_ColumnB3 = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image); HOperatorSet.GenEmptyObj(out ho_ImageRotate); HOperatorSet.GenEmptyObj(out ho_GrayImage); HOperatorSet.GenEmptyObj(out ho_ImageMedian); HOperatorSet.GenEmptyObj(out ho_Region); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions); HOperatorSet.GenEmptyObj(out ho_SelectedRegions); HOperatorSet.GenEmptyObj(out ho_SelectedRegions2); HOperatorSet.GenEmptyObj(out ho_SelectedRegions1); HOperatorSet.GenEmptyObj(out ho_SelectedRegions3); HOperatorSet.GenEmptyObj(out ho_SortedRegions); HOperatorSet.GenEmptyObj(out ho_RegionUnion); HOperatorSet.GenEmptyObj(out ho_Rectangle); HOperatorSet.GenEmptyObj(out ho_ImageReduced); HOperatorSet.GenEmptyObj(out ho_GrayImageA); HOperatorSet.GenEmptyObj(out ho_GragImageA); HOperatorSet.GenEmptyObj(out ho_RegionsA_temp); HOperatorSet.GenEmptyObj(out ho_ConnectedRegionsA_temp); HOperatorSet.GenEmptyObj(out ho_SelectedRegionsA_temp); HOperatorSet.GenEmptyObj(out ho_ImageAffineTrans); HOperatorSet.GenEmptyObj(out ho_RegionsA); HOperatorSet.GenEmptyObj(out ho_RegionDilationA); HOperatorSet.GenEmptyObj(out ho_ConnectedRegionsA); HOperatorSet.GenEmptyObj(out ho_SelectedRegionsA); HOperatorSet.GenEmptyObj(out ho_RegionsA1); HOperatorSet.GenEmptyObj(out ho_RegionOpeningA); HOperatorSet.GenEmptyObj(out ho_SelectedRegionsA1); HOperatorSet.GenEmptyObj(out ho_RegionUnionA); HOperatorSet.GenEmptyObj(out ho_SelectedRegionsA2); HOperatorSet.GenEmptyObj(out ho_RegionsB); HOperatorSet.GenEmptyObj(out ho_ConnectedRegionsB); HOperatorSet.GenEmptyObj(out ho_SelectedRegionsB); HOperatorSet.GenEmptyObj(out ho_RectangleB1); HOperatorSet.GenEmptyObj(out ho_RectangleB2); HOperatorSet.GenEmptyObj(out ho_RegionDifferenceB); HOperatorSet.GenEmptyObj(out ho_ImageReducedB1); HOperatorSet.GenEmptyObj(out ho_RegionB1); HOperatorSet.GenEmptyObj(out ho_ConnectedRegionsB2); HOperatorSet.GenEmptyObj(out ho_SelectedRegionsB2); HOperatorSet.GenEmptyObj(out ho_outContours); HOperatorSet.GenEmptyObj(out ho_SelectedContours); HOperatorSet.GenEmptyObj(out ho_SortedContours); HOperatorSet.GenEmptyObj(out ho_Region1); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1); HOperatorSet.GenEmptyObj(out ho_SelectedRegions4); HOperatorSet.GenEmptyObj(out ho_SelectedRegions5); HOperatorSet.GenEmptyObj(out ho_SelectedRegions6); HOperatorSet.GenEmptyObj(out ho_Contours); HOperatorSet.GenEmptyObj(out ho_SortedContours3); HOperatorSet.GenEmptyObj(out ho_obj); HOperatorSet.GenEmptyObj(out ho_objj); HOperatorSet.GenEmptyObj(out ho_ContoursSplit); HOperatorSet.GenEmptyObj(out ho_SelectedXLD); HOperatorSet.GenEmptyObj(out ho_SortedContours1); HOperatorSet.GenEmptyObj(out ho_M); HOperatorSet.GenEmptyObj(out ho_Contour); HOperatorSet.GenEmptyObj(out ho_SelectedXLD1); HOperatorSet.GenEmptyObj(out ho_SortedContours2); HOperatorSet.GenEmptyObj(out ho_ObjectSelectedB); ho_Image.Dispose(); if (!this.index) { HOperatorSet.ReadImage(out ho_Image, "test.bmp"); } else { HOperatorSet.ReadImage(out ho_Image, "PersonImg\\xiaosy.png"); } //*******************************************第一组预处理*********************************************** ho_ImageRotate.Dispose(); HOperatorSet.RotateImage(ho_Image, out ho_ImageRotate, 45, "constant"); ho_GrayImage.Dispose(); HOperatorSet.Rgb1ToGray(ho_ImageRotate, out ho_GrayImage); ho_ImageMedian.Dispose(); HOperatorSet.MedianImage(ho_GrayImage, out ho_ImageMedian, "circle", 3, "mirrored"); ho_Region.Dispose(); HOperatorSet.Threshold(ho_GrayImage, out ho_Region, 200, 255); ho_ConnectedRegions.Dispose(); HOperatorSet.Connection(ho_Region, out ho_ConnectedRegions); ho_SelectedRegions.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, "area", "and", 150, 99999); ho_SelectedRegions2.Dispose(); HOperatorSet.SelectShape(ho_SelectedRegions, out ho_SelectedRegions2, "circularity", "and", 0.37, 0.7); ho_SelectedRegions1.Dispose(); HOperatorSet.ClosingRectangle1(ho_SelectedRegions2, out ho_SelectedRegions1, 5, 5); ho_SelectedRegions3.Dispose(); HOperatorSet.SelectShape(ho_SelectedRegions1, out ho_SelectedRegions3, "holes_num", "and", 1, 99); ho_SortedRegions.Dispose(); HOperatorSet.SortRegion(ho_SelectedRegions3, out ho_SortedRegions, "first_point", "true", "column"); HOperatorSet.CountObj(ho_SelectedRegions3, out hv_NumberB); ho_RegionUnion.Dispose(); HOperatorSet.Union1(ho_SelectedRegions3, out ho_RegionUnion); HOperatorSet.SmallestRectangle2(ho_RegionUnion, out hv_Row1, out hv_Column1, out hv_Phi, out hv_Length1, out hv_Length2); ho_Rectangle.Dispose(); HOperatorSet.GenRectangle2(out ho_Rectangle, hv_Row1, hv_Column1, hv_Phi, hv_Length1, hv_Length2); ho_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_GrayImage, ho_Rectangle, out ho_ImageReduced); //*******************************************第二组预处理*********************************************** ho_GrayImageA.Dispose(); HOperatorSet.Rgb1ToGray(ho_Image, out ho_GrayImageA); ho_GragImageA.Dispose(); HOperatorSet.GrayDilationRect(ho_GrayImageA, out ho_GragImageA, 4, 4); ho_RegionsA_temp.Dispose(); HOperatorSet.Threshold(ho_GrayImageA, out ho_RegionsA_temp, 116, 218); ho_ConnectedRegionsA_temp.Dispose(); HOperatorSet.Connection(ho_RegionsA_temp, out ho_ConnectedRegionsA_temp); ho_SelectedRegionsA_temp.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegionsA_temp, out ho_SelectedRegionsA_temp, "area", "and", 108357, 495042); HOperatorSet.OrientationRegion(ho_SelectedRegionsA_temp, out hv_Phi_temp); HOperatorSet.AreaCenter(ho_SelectedRegionsA_temp, out hv_Area_temp, out hv_Row_temp, out hv_Column_temp); HOperatorSet.VectorAngleToRigid(hv_Row_temp, hv_Column_temp, hv_Phi_temp, hv_Row_temp, hv_Column_temp, (new HTuple(0)).TupleRad(), out hv_HomMat2D); ho_ImageAffineTrans.Dispose(); HOperatorSet.AffineTransImage(ho_GrayImageA, out ho_ImageAffineTrans, hv_HomMat2D, "constant", "false"); ho_RegionsA.Dispose(); HOperatorSet.Threshold(ho_ImageAffineTrans, out ho_RegionsA, 116, 218); ho_RegionDilationA.Dispose(); HOperatorSet.DilationRectangle1(ho_RegionsA, out ho_RegionDilationA, 2, 2); ho_ConnectedRegionsA.Dispose(); HOperatorSet.Connection(ho_RegionDilationA, out ho_ConnectedRegionsA); ho_SelectedRegionsA.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegionsA, out ho_SelectedRegionsA, "circularity", "and", 0.001, 0.03); ho_RegionsA1.Dispose(); HOperatorSet.Threshold(ho_GrayImageA, out ho_RegionsA1, 227, 255); ho_RegionOpeningA.Dispose(); HOperatorSet.OpeningCircle(ho_RegionsA1, out ho_RegionOpeningA, 5.5); ho_ConnectedRegionsA.Dispose(); HOperatorSet.Connection(ho_RegionOpeningA, out ho_ConnectedRegionsA); ho_SelectedRegionsA1.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegionsA, out ho_SelectedRegionsA1, "row", "and", 513.2, 601.23); ho_RegionUnionA.Dispose(); HOperatorSet.Union1(ho_SelectedRegionsA1, out ho_RegionUnionA); HOperatorSet.SmallestRectangle1(ho_RegionUnionA, out hv_RowA1, out hv_ColumnA1, out hv_RowA2, out hv_ColumnA2); ho_SelectedRegionsA2.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegionsA, out ho_SelectedRegionsA2, "width", "and", 92.694, 100); HOperatorSet.InnerRectangle1(ho_SelectedRegionsA2, out hv_RowA3, out hv_ColumnA3, out hv_RowA4, out hv_ColumnA4); ho_ConnectedRegionsA.Dispose(); HOperatorSet.Connection(ho_RegionsA1, out ho_ConnectedRegionsA); //*******************************************第三组预处理********************************************* ho_RegionsB.Dispose(); HOperatorSet.Threshold(ho_Image, out ho_RegionsB, 131, 153); ho_ConnectedRegionsB.Dispose(); HOperatorSet.Connection(ho_RegionsB, out ho_ConnectedRegionsB); ho_SelectedRegionsB.Dispose(); HOperatorSet.SelectShapeStd(ho_ConnectedRegionsB, out ho_SelectedRegionsB, "max_area", 70); HOperatorSet.SmallestRectangle2(ho_SelectedRegionsB, out hv_RowB16, out hv_ColumnB16, out hv_PhiB3, out hv_LengthB1, out hv_LengthB2); ho_RectangleB1.Dispose(); HOperatorSet.GenRectangle2(out ho_RectangleB1, hv_RowB16 - 5, hv_ColumnB16, hv_PhiB3, hv_LengthB1 - 5, hv_LengthB2 - 5); ho_RectangleB2.Dispose(); HOperatorSet.GenRectangle2(out ho_RectangleB2, hv_RowB16 - 3, hv_ColumnB16, hv_PhiB3, hv_LengthB1 - 5, hv_LengthB2 - 18); ho_RegionDifferenceB.Dispose(); HOperatorSet.Difference(ho_RectangleB1, ho_RectangleB2, out ho_RegionDifferenceB ); ho_ImageReducedB1.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_RegionDifferenceB, out ho_ImageReducedB1 ); ho_RegionB1.Dispose(); HOperatorSet.Threshold(ho_ImageReducedB1, out ho_RegionB1, 200, 255); ho_ConnectedRegionsB2.Dispose(); HOperatorSet.Connection(ho_RegionB1, out ho_ConnectedRegionsB2); ho_SelectedRegionsB2.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegionsB2, out ho_SelectedRegionsB2, "area", "and", 26.01, 200); HOperatorSet.AreaCenter(ho_SelectedRegionsB2, out hv_AreaB, out hv_RowB2, out hv_ColumnB2); //*****************************************花瓣ABC************************************************** //边缘轮廓检测 ho_outContours.Dispose(); HOperatorSet.GenContourRegionXld(ho_SelectedRegions3, out ho_outContours, "border"); ho_SelectedContours.Dispose(); HOperatorSet.SelectContoursXld(ho_outContours, out ho_SelectedContours, "contour_length", 60, 600, -0.5, 0.5); ho_SortedContours.Dispose(); HOperatorSet.SortContoursXld(ho_SelectedContours, out ho_SortedContours, "upper_left", "true", "column"); HOperatorSet.CountObj(ho_SortedContours, out hv_Number); //中心轮廓检测 ho_Region1.Dispose(); HOperatorSet.Threshold(ho_ImageReduced, out ho_Region1, 0, 135); ho_ConnectedRegions1.Dispose(); HOperatorSet.Connection(ho_Region1, out ho_ConnectedRegions1); ho_SelectedRegions4.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegions1, out ho_SelectedRegions4, "area", "and", 230, 600); ho_SelectedRegions5.Dispose(); HOperatorSet.SelectShape(ho_SelectedRegions4, out ho_SelectedRegions5, "ratio", "and", 0.7, 1.352); ho_SelectedRegions6.Dispose(); HOperatorSet.SelectShape(ho_SelectedRegions5, out ho_SelectedRegions6, "circularity", "and", 0.2, 1); ho_Contours.Dispose(); HOperatorSet.GenContourRegionXld(ho_SelectedRegions6, out ho_Contours, "border"); ho_SortedContours3.Dispose(); HOperatorSet.SortContoursXld(ho_Contours, out ho_SortedContours3, "upper_left", "true", "row"); HOperatorSet.CountObj(ho_SortedContours3, out hv_Number3); if ((int)(new HTuple((((hv_Number + hv_Number3) % 2)).TupleEqual(1))) != 0) { //这里需要在C#输出无法检测的信息 HDevelopStop(); } //里面的轮廓 A1左上到右下 A2左下到右上 hv_A1 = new HTuple(); hv_A2 = new HTuple(); //外面的轮廓 B1垂直方向 B2水平方向 hv_B1 = new HTuple(); hv_B2 = new HTuple(); //c1到c4顺时针顺序的四个花瓣长度 hv_C1 = new HTuple(); hv_C2 = new HTuple(); hv_C3 = new HTuple(); hv_C4 = new HTuple(); //B的检测 HTuple end_val103 = hv_Number; HTuple step_val103 = 1; for (hv_i = 1; hv_i.Continue(end_val103, step_val103); hv_i = hv_i.TupleAdd(step_val103)) { ho_obj.Dispose(); HOperatorSet.SelectObj(ho_SortedContours, out ho_obj, hv_i); HOperatorSet.GetContourXld(ho_obj, out hv_Ro1, out hv_Co1); HOperatorSet.TupleMin(hv_Ro1, out hv_MinBR); HOperatorSet.TupleMax(hv_Ro1, out hv_MaxBR); HOperatorSet.TupleMin(hv_Co1, out hv_MinBC); HOperatorSet.TupleMax(hv_Co1, out hv_MaxBC); hv_BR = hv_MaxBR - hv_MinBR; hv_BC = hv_MaxBC - hv_MinBC; hv_B1 = hv_B1.TupleConcat(hv_BR); hv_B2 = hv_B2.TupleConcat(hv_BC); //A的检测 HTuple end_val118 = hv_Number3; HTuple step_val118 = 1; for (hv_j = 1; hv_j.Continue(end_val118, step_val118); hv_j = hv_j.TupleAdd(step_val118)) { if ((int)(new HTuple(hv_i.TupleEqual(hv_j))) != 0) { ho_objj.Dispose(); HOperatorSet.SelectObj(ho_SortedContours3, out ho_objj, hv_j); //C的检测 ho_ContoursSplit.Dispose(); HOperatorSet.SegmentContoursXld(ho_objj, out ho_ContoursSplit, "lines", 5, 4, 2); ho_SelectedXLD.Dispose(); HOperatorSet.SelectShapeXld(ho_ContoursSplit, out ho_SelectedXLD, "width", "and", 13, 99); ho_SortedContours1.Dispose(); HOperatorSet.SortContoursXld(ho_SelectedXLD, out ho_SortedContours1, "upper_left", "true", "row"); HOperatorSet.CountObj(ho_SortedContours1, out hv_Number1); HTuple end_val130 = hv_Number1; HTuple step_val130 = 1; for (hv_a = 1; hv_a.Continue(end_val130, step_val130); hv_a = hv_a.TupleAdd(step_val130)) { ho_M.Dispose(); HOperatorSet.SelectObj(ho_SortedContours1, out ho_M, hv_a); if ((int)(new HTuple(hv_a.TupleEqual(1))) != 0) { HOperatorSet.GetContourXld(ho_M, out hv_Ro2, out hv_Co2); HOperatorSet.TupleMin(hv_Co2, out hv_LUC); HOperatorSet.TupleMax(hv_Co2, out hv_RUC); } else { HOperatorSet.GetContourXld(ho_M, out hv_Ro3, out hv_Co3); HOperatorSet.TupleMin(hv_Co3, out hv_LDC); HOperatorSet.TupleMax(hv_Co3, out hv_RDC); } HOperatorSet.FitLineContourXld(ho_M, "huber", -1, 0, 5, 2, out hv_RowBegin, out hv_ColBegin, out hv_RowEnd, out hv_ColEnd, out hv_Nr, out hv_Nc, out hv_Dist); ho_Contour.Dispose(); HOperatorSet.GenContourPolygonXld(out ho_Contour, hv_RowBegin.TupleConcat( hv_RowEnd), hv_ColBegin.TupleConcat(hv_ColEnd)); HOperatorSet.GetContourXld(ho_Contour, out hv_Rowc, out hv_Colc); HOperatorSet.TupleMean(hv_Rowc, out hv_MeanR); if ((int)(new HTuple(hv_a.TupleEqual(1))) != 0) { hv_H1 = hv_MeanR - hv_MinBR; hv_C1 = hv_C1.TupleConcat(hv_H1); } else { hv_H3 = hv_MaxBR - hv_MeanR; hv_C3 = hv_C3.TupleConcat(hv_H3); } } ho_SelectedXLD1.Dispose(); HOperatorSet.SelectShapeXld(ho_ContoursSplit, out ho_SelectedXLD1, "height", "and", 13, 99); ho_SortedContours2.Dispose(); HOperatorSet.SortContoursXld(ho_SelectedXLD1, out ho_SortedContours2, "upper_left", "true", "column"); HOperatorSet.CountObj(ho_SortedContours2, out hv_Number2); HTuple end_val157 = hv_Number2; HTuple step_val157 = 1; for (hv_b = 1; hv_b.Continue(end_val157, step_val157); hv_b = hv_b.TupleAdd(step_val157)) { ho_M.Dispose(); HOperatorSet.SelectObj(ho_SortedContours2, out ho_M, hv_b); if ((int)(new HTuple(hv_b.TupleEqual(1))) != 0) { HOperatorSet.GetContourXld(ho_M, out hv_Ro4, out hv_Co4); HOperatorSet.TupleMin(hv_Ro4, out hv_LUR); HOperatorSet.TupleMax(hv_Ro4, out hv_LDR); } else { HOperatorSet.GetContourXld(ho_M, out hv_Ro5, out hv_Co5); HOperatorSet.TupleMin(hv_Ro5, out hv_RUR); HOperatorSet.TupleMax(hv_Ro5, out hv_RDR); } HOperatorSet.FitLineContourXld(ho_M, "huber", -1, 0, 5, 2, out hv_RowBegin, out hv_ColBegin, out hv_RowEnd, out hv_ColEnd, out hv_Nr, out hv_Nc, out hv_Dist); ho_Contour.Dispose(); HOperatorSet.GenContourPolygonXld(out ho_Contour, hv_RowBegin.TupleConcat( hv_RowEnd), hv_ColBegin.TupleConcat(hv_ColEnd)); HOperatorSet.GetContourXld(ho_Contour, out hv_Rowc, out hv_Colc); HOperatorSet.TupleMean(hv_Colc, out hv_MeanC); if ((int)(new HTuple(hv_b.TupleEqual(1))) != 0) { hv_H4 = hv_MeanC - hv_MinBC; hv_C4 = hv_C4.TupleConcat(hv_H4); } else { hv_H2 = hv_MaxBC - hv_MeanC; hv_C2 = hv_C2.TupleConcat(hv_H2); } } HOperatorSet.DistancePp(hv_LUC, hv_LUR, hv_RDC, hv_RDR, out hv_Distance1); HOperatorSet.DistancePp(hv_RUC, hv_RUR, hv_LDC, hv_LDR, out hv_Distance2); hv_A1 = hv_A1.TupleConcat(hv_Distance1); hv_A2 = hv_A2.TupleConcat(hv_Distance2); } } } //*********************************倾斜角度 跑针位 k******************************************* HOperatorSet.AreaCenter(ho_SelectedRegionsA, out hv_AreaA, out hv_RowA, out hv_ColumnA); HOperatorSet.OrientationRegion(ho_SelectedRegionsA, out hv_Phi); hv_Phi = hv_Phi.TupleDeg(); hv_phi = ((90 - (hv_Phi.TupleAbs()))).TupleAbs(); hv_paozhengwei = (hv_RowA3 - ((hv_RowA2 / 2) + (hv_RowA1 / 2))) - 4; hv_k = (hv_RowA2 - hv_RowA1) + 4; //***************************************裂箔**************************************************** HOperatorSet.TupleSortIndex(hv_AreaB, out hv_Indices); hv_liebo = new HTuple(); hv_NumB = new HTuple(hv_Indices.TupleLength()); hv_count = 0; if ((int)(new HTuple(hv_NumB.TupleGreater(0))) != 0) { HTuple end_val208 = hv_NumB; HTuple step_val208 = 1; for (hv_I = 1; hv_I.Continue(end_val208, step_val208); hv_I = hv_I.TupleAdd(step_val208)) { ho_ObjectSelectedB.Dispose(); HOperatorSet.SelectObj(ho_SelectedRegionsB2, out ho_ObjectSelectedB, hv_I); HOperatorSet.AreaCenter(ho_ObjectSelectedB, out hv_AreaB1, out hv_RowB3, out hv_ColumnB3); if ((int)(new HTuple(hv_AreaB1.TupleGreater(30))) != 0) { hv_count = hv_count + 1; } else { } } } else { } if (hv_liebo == null) { hv_liebo = new HTuple(); } hv_liebo[0] = hv_count; HTuple result = new HTuple(); result.Append(hv_A1[0]); result.Append(hv_A2[0]); result.Append(hv_B1[0]); result.Append(hv_B2[0]); result.Append(hv_C1[0]); result.Append(hv_C2[0]); result.Append(hv_C3[0]); result.Append(hv_C4[0]); result.Append(hv_phi[0]); result.Append(hv_paozhengwei[0]); result.Append(hv_liebo[0]); //************************************************************************************************** ho_Image.Dispose(); ho_ImageRotate.Dispose(); ho_GrayImage.Dispose(); ho_ImageMedian.Dispose(); ho_Region.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions.Dispose(); ho_SelectedRegions2.Dispose(); ho_SelectedRegions1.Dispose(); ho_SelectedRegions3.Dispose(); ho_SortedRegions.Dispose(); ho_RegionUnion.Dispose(); ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_GrayImageA.Dispose(); ho_GragImageA.Dispose(); ho_RegionsA_temp.Dispose(); ho_ConnectedRegionsA_temp.Dispose(); ho_SelectedRegionsA_temp.Dispose(); ho_ImageAffineTrans.Dispose(); ho_RegionsA.Dispose(); ho_RegionDilationA.Dispose(); ho_ConnectedRegionsA.Dispose(); ho_SelectedRegionsA.Dispose(); ho_RegionsA1.Dispose(); ho_RegionOpeningA.Dispose(); ho_SelectedRegionsA1.Dispose(); ho_RegionUnionA.Dispose(); ho_SelectedRegionsA2.Dispose(); ho_RegionsB.Dispose(); ho_ConnectedRegionsB.Dispose(); ho_SelectedRegionsB.Dispose(); ho_RectangleB1.Dispose(); ho_RectangleB2.Dispose(); ho_RegionDifferenceB.Dispose(); ho_ImageReducedB1.Dispose(); ho_RegionB1.Dispose(); ho_ConnectedRegionsB2.Dispose(); ho_SelectedRegionsB2.Dispose(); ho_outContours.Dispose(); ho_SelectedContours.Dispose(); ho_SortedContours.Dispose(); ho_Region1.Dispose(); ho_ConnectedRegions1.Dispose(); ho_SelectedRegions4.Dispose(); ho_SelectedRegions5.Dispose(); ho_SelectedRegions6.Dispose(); ho_Contours.Dispose(); ho_SortedContours3.Dispose(); ho_obj.Dispose(); ho_objj.Dispose(); ho_ContoursSplit.Dispose(); ho_SelectedXLD.Dispose(); ho_SortedContours1.Dispose(); ho_M.Dispose(); ho_Contour.Dispose(); ho_SelectedXLD1.Dispose(); ho_SortedContours2.Dispose(); ho_ObjectSelectedB.Dispose(); return(result); }
// Main procedure private void action() { // Local iconic variables HObject ho_Image, ho_Rectangle, ho_ImageReduced; HObject ho_Region, ho_ResultContour = null, ho_ContCircle = null; HObject ho_Contour = null, ho_Cross = null; // Local control variables HTuple hv_WindowHandle = null, hv_Width = null; HTuple hv_Height = null, hv_MetrologyHandle = null, hv_LineRow1 = null; HTuple hv_LineColumn1 = null, hv_LineRow2 = null, hv_LineColumn2 = null; HTuple hv_Tolerance = null, hv_Index1 = null, hv_Rows = null; HTuple hv_Columns = null, hv_I = null, hv_LineParameter = new HTuple(); HTuple hv_Angle = new HTuple(), hv_Row = new HTuple(); HTuple hv_Column = new HTuple(), hv_IsOverlapping1 = new HTuple(); HTuple hv_Orientation1 = new HTuple(), hv_Orientation2 = new HTuple(); HTuple hv_MRow = new HTuple(), hv_MColumn = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image); HOperatorSet.GenEmptyObj(out ho_Rectangle); HOperatorSet.GenEmptyObj(out ho_ImageReduced); HOperatorSet.GenEmptyObj(out ho_Region); HOperatorSet.GenEmptyObj(out ho_ResultContour); HOperatorSet.GenEmptyObj(out ho_ContCircle); HOperatorSet.GenEmptyObj(out ho_Contour); HOperatorSet.GenEmptyObj(out ho_Cross); try { //This program shows how to detect the edges of a diamond //with subpixel accuracy and calculate the angle between them. // //In contrast to the example measure_diamond.hdev, //this example uses a metrology model to measure the edges. // //First, the top of the diamond is roughly segmented //to align the metrology objects. //Then, the metrology model is applied and returns the //parameters of the fitted lines. //Finally, the angle between the two lines is computed. // //Display initializations dev_update_off(); if (HDevWindowStack.IsOpen()) { HOperatorSet.CloseWindow(HDevWindowStack.Pop()); } ho_Image.Dispose(); HOperatorSet.ReadImage(out ho_Image, "diamond/diamond_01"); dev_open_window_fit_image(ho_Image, 0, 0, -1, -1, out hv_WindowHandle); set_display_font(hv_WindowHandle, 16, "mono", "true", "false"); HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height); // //Create the metrology model data structure HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle); //The image size is set in advance to speed up the //first call of apply_metrology_model. HOperatorSet.SetMetrologyModelImageSize(hv_MetrologyHandle, hv_Width, hv_Height); //Define the parameters of the metrology line objects hv_LineRow1 = new HTuple(); hv_LineRow1[0] = 155; hv_LineRow1[1] = 155; hv_LineColumn1 = new HTuple(); hv_LineColumn1[0] = 400; hv_LineColumn1[1] = 400; hv_LineRow2 = new HTuple(); hv_LineRow2[0] = 290; hv_LineRow2[1] = 290; hv_LineColumn2 = new HTuple(); hv_LineColumn2[0] = 230; hv_LineColumn2[1] = 570; hv_Tolerance = 20; // //Create two metrology line objects and set parameters HOperatorSet.AddMetrologyObjectLineMeasure(hv_MetrologyHandle, hv_LineRow1, hv_LineColumn1, hv_LineRow2, hv_LineColumn2, hv_Tolerance, 10, 1, 20, new HTuple(), new HTuple(), out hv_Index1); //Create region of interest for the alignment ho_Rectangle.Dispose(); HOperatorSet.GenRectangle1(out ho_Rectangle, (hv_LineRow1.TupleSelect(0)) - 40, (hv_LineColumn1.TupleSelect(0)) - 50, (hv_LineRow1.TupleSelect(0)) + 20, (hv_LineColumn1.TupleSelect( 0)) + 50); //Change the reference coordinate system in which the //metrology model is given to be situated at the top of the diamond ho_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_Rectangle, out ho_ImageReduced); ho_Region.Dispose(); HOperatorSet.Threshold(ho_ImageReduced, out ho_Region, 128, 255); HOperatorSet.GetRegionPoints(ho_Region, out hv_Rows, out hv_Columns); HOperatorSet.SetMetrologyModelParam(hv_MetrologyHandle, "reference_system", ((((hv_Rows.TupleSelect(0))).TupleConcat(hv_Columns.TupleSelect(0)))).TupleConcat( 0)); // //Main loop // for (hv_I = 1; (int)hv_I <= 5; hv_I = (int)hv_I + 1) { ho_Image.Dispose(); HOperatorSet.ReadImage(out ho_Image, "diamond/diamond_" + (hv_I.TupleString( "02"))); //Roughly segment the diamond's position ho_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_Rectangle, out ho_ImageReduced); ho_Region.Dispose(); HOperatorSet.Threshold(ho_ImageReduced, out ho_Region, 128, 255); //Extract the top of the diamond HOperatorSet.GetRegionPoints(ho_Region, out hv_Rows, out hv_Columns); // //Use the top of the diamond to align the metrology model in //the current image // HOperatorSet.AlignMetrologyModel(hv_MetrologyHandle, hv_Rows.TupleSelect( 0), hv_Columns.TupleSelect(0), 0); // // //Perform the measurement for both lines in one call // HOperatorSet.ApplyMetrologyModel(ho_Image, hv_MetrologyHandle); // //Access results // HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "result_type", "all_param", out hv_LineParameter); HOperatorSet.AngleLl(hv_LineParameter.TupleSelect(0), hv_LineParameter.TupleSelect( 1), hv_LineParameter.TupleSelect(2), hv_LineParameter.TupleSelect(3), hv_LineParameter.TupleSelect(4), hv_LineParameter.TupleSelect(5), hv_LineParameter.TupleSelect( 6), hv_LineParameter.TupleSelect(7), out hv_Angle); hv_Angle = hv_Angle.TupleDeg(); // //Display results // //Create line contours ho_ResultContour.Dispose(); HOperatorSet.GetMetrologyObjectResultContour(out ho_ResultContour, hv_MetrologyHandle, "all", "all", 1.5); HOperatorSet.IntersectionLines(hv_LineParameter.TupleSelect(0), hv_LineParameter.TupleSelect( 1), hv_LineParameter.TupleSelect(2), hv_LineParameter.TupleSelect(3), hv_LineParameter.TupleSelect(4), hv_LineParameter.TupleSelect(5), hv_LineParameter.TupleSelect( 6), hv_LineParameter.TupleSelect(7), out hv_Row, out hv_Column, out hv_IsOverlapping1); //Calculate the orientation of the two lines HOperatorSet.LineOrientation(hv_LineParameter.TupleSelect(0), hv_LineParameter.TupleSelect( 1), hv_LineParameter.TupleSelect(2), hv_LineParameter.TupleSelect(3), out hv_Orientation1); if ((int)(new HTuple(hv_Orientation1.TupleGreater(0))) != 0) { hv_Orientation1 = hv_Orientation1 - ((new HTuple(180)).TupleRad()); } HOperatorSet.LineOrientation(hv_LineParameter.TupleSelect(4), hv_LineParameter.TupleSelect( 5), hv_LineParameter.TupleSelect(6), hv_LineParameter.TupleSelect(7), out hv_Orientation2); // //Visualize the angle between the lines ho_ContCircle.Dispose(); HOperatorSet.GenCircleContourXld(out ho_ContCircle, hv_Row, hv_Column, 100, hv_Orientation1, hv_Orientation2, "positive", 1); //Get the used measure regions and the measured points //for visualization ho_Contour.Dispose(); HOperatorSet.GetMetrologyObjectMeasures(out ho_Contour, hv_MetrologyHandle, "all", "all", out hv_MRow, out hv_MColumn); ho_Cross.Dispose(); HOperatorSet.GenCrossContourXld(out ho_Cross, hv_MRow, hv_MColumn, 6, (new HTuple(45)).TupleRad() ); //Display everything if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetLineWidth(HDevWindowStack.GetActive(), 1); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "yellow"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Contour, HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Cross, HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetLineWidth(HDevWindowStack.GetActive(), 2); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_ResultContour, HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "blue"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_ContCircle, HDevWindowStack.GetActive()); } disp_message(hv_WindowHandle, ("Angle = " + (hv_Angle.TupleString(".5"))) + "бу", "window", 12, 12, "black", "true"); if ((int)(new HTuple(hv_I.TupleLess(5))) != 0) { disp_continue_message(hv_WindowHandle, "black", "true"); } // stop(); only in hdevelop } //Clean up memory HOperatorSet.ClearMetrologyModel(hv_MetrologyHandle); } catch (HalconException HDevExpDefaultException) { ho_Image.Dispose(); ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_Region.Dispose(); ho_ResultContour.Dispose(); ho_ContCircle.Dispose(); ho_Contour.Dispose(); ho_Cross.Dispose(); throw HDevExpDefaultException; } ho_Image.Dispose(); ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_Region.Dispose(); ho_ResultContour.Dispose(); ho_ContCircle.Dispose(); ho_Contour.Dispose(); ho_Cross.Dispose(); }
/// <summary> /// 执行 /// </summary> /// <param name="image">图像</param> /// <param name="outputs">输出结果</param> /// <returns>执行结果</returns> public void Execute(object image, out ItemCollection outputs) { HObject hImage = image as HObject; outputs = new ItemCollection(); stopwatch.Restart(); HObject ho_ImageReduced = null; HObject ho_R = null, ho_G = null, ho_B = null, ho_Hue = null, ho_Saturation = null; HObject ho_Intensity = null, ho_Regions = null, ho_RegionOpening = null; HObject ho_RegionClosing = null, ho_ImageReduced2 = null, ho_Regions1 = null; HObject ho_RegionOpening1 = null, ho_RegionClosing1 = null; HObject ho_ImageReduced3 = null, ho_Regions2 = null, ho_RegionOpening2 = null; HObject ho_RegionClosing2 = null, ho_RegionDifference = null; HObject ho_RegionOpening3 = null, ho_ConnectedRegions = null; HObject ho_RegionFillUp = null, ho_SelectedRegions = null, ho_Skeleton = null, ho_EndPoints = null; HObject ho_JuncPoints = null, ho_RegionDifference1 = null, ho_ConnectedRegions1 = null; HObject ho_SelectedRegions1 = null, ho_Rectangle = null, ho_Contours = null; HObject ho_SelectedXLD = null, ho_UnionContours = null, ho_SelectedXLD1 = null; HObject ho_Cross = null, ho_ObjectSelected = null; HTuple hv_Number = new HTuple(); HTuple hv_Row = new HTuple(), hv_Column = new HTuple(); HTuple hv_Phi = new HTuple(), hv_Length1 = new HTuple(); HTuple hv_Length2 = new HTuple(), hv_Row1 = new HTuple(); HTuple hv_Col1 = new HTuple(), hv_RowMedian = new HTuple(); HTuple hv_ColMedian = new HTuple(), hv_Exception = new HTuple(); HTuple hv_Length = new HTuple(); HOperatorSet.GenEmptyObj(out ho_ImageReduced); HOperatorSet.GenEmptyObj(out ho_R); HOperatorSet.GenEmptyObj(out ho_G); HOperatorSet.GenEmptyObj(out ho_B); HOperatorSet.GenEmptyObj(out ho_Hue); HOperatorSet.GenEmptyObj(out ho_Saturation); HOperatorSet.GenEmptyObj(out ho_Intensity); HOperatorSet.GenEmptyObj(out ho_Regions); HOperatorSet.GenEmptyObj(out ho_RegionOpening); HOperatorSet.GenEmptyObj(out ho_RegionClosing); HOperatorSet.GenEmptyObj(out ho_ImageReduced2); HOperatorSet.GenEmptyObj(out ho_Regions1); HOperatorSet.GenEmptyObj(out ho_RegionOpening1); HOperatorSet.GenEmptyObj(out ho_RegionClosing1); HOperatorSet.GenEmptyObj(out ho_ImageReduced3); HOperatorSet.GenEmptyObj(out ho_Regions2); HOperatorSet.GenEmptyObj(out ho_RegionOpening2); HOperatorSet.GenEmptyObj(out ho_RegionClosing2); HOperatorSet.GenEmptyObj(out ho_RegionDifference); HOperatorSet.GenEmptyObj(out ho_RegionOpening3); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions); HOperatorSet.GenEmptyObj(out ho_RegionFillUp); HOperatorSet.GenEmptyObj(out ho_SelectedRegions); HOperatorSet.GenEmptyObj(out ho_Skeleton); HOperatorSet.GenEmptyObj(out ho_EndPoints); HOperatorSet.GenEmptyObj(out ho_JuncPoints); HOperatorSet.GenEmptyObj(out ho_RegionDifference1); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1); HOperatorSet.GenEmptyObj(out ho_SelectedRegions1); HOperatorSet.GenEmptyObj(out ho_Rectangle); HOperatorSet.GenEmptyObj(out ho_Contours); HOperatorSet.GenEmptyObj(out ho_SelectedXLD); HOperatorSet.GenEmptyObj(out ho_UnionContours); HOperatorSet.GenEmptyObj(out ho_SelectedXLD1); HOperatorSet.GenEmptyObj(out ho_Cross); HOperatorSet.GenEmptyObj(out ho_ObjectSelected); try { HTuple width, height; HOperatorSet.GetImageSize(hImage, out width, out height); //若未初始化,则进行初始化 if (!isInit) { isInit = true; ho_ROI_0?.Dispose(); HOperatorSet.GenRectangle1(out ho_ROI_0, 0, 0, height, width); } if (runningWindow == null) { try { runningWindow = (RunningWindow as HSmartWindowControlWPF).HalconWindow; } catch (Exception) { } } if (configWindow == null) { try { configWindow = (ConfigWindow as HSmartWindowControlWPF).HalconWindow; } catch (Exception) { } } if (runningWindow != null) { SetWindowPart(runningWindow, width, height); HOperatorSet.ClearWindow(runningWindow); HOperatorSet.DispObj(hImage, runningWindow); } if (configWindow != null) { SetWindowPart(configWindow, width, height); HOperatorSet.ClearWindow(configWindow); HOperatorSet.DispObj(hImage, configWindow); } //执行主任务 HOperatorSet.ReduceDomain(hImage, ho_ROI_0, out ho_ImageReduced); //转成HSV格式 ho_R.Dispose(); ho_G.Dispose(); ho_B.Dispose(); HOperatorSet.Decompose3(ho_ImageReduced, out ho_R, out ho_G, out ho_B); ho_Hue.Dispose(); ho_Saturation.Dispose(); ho_Intensity.Dispose(); HOperatorSet.TransFromRgb(ho_R, ho_G, ho_B, out ho_Hue, out ho_Saturation, out ho_Intensity, "hsv"); //提取背景区域 ho_Regions.Dispose(); HOperatorSet.Threshold(ho_Hue, out ho_Regions, new HTuple(Inputs["HMinThreshold"].Value), new HTuple(Inputs["HMaxThreshold"].Value)); ho_RegionOpening.Dispose(); HOperatorSet.OpeningCircle(ho_Regions, out ho_RegionOpening, new HTuple(Inputs["OpenRadius"].Value)); ho_RegionClosing.Dispose(); HOperatorSet.ClosingCircle(ho_RegionOpening, out ho_RegionClosing, new HTuple(Inputs["CloseRadius"].Value)); ho_ImageReduced2.Dispose(); HOperatorSet.ReduceDomain(ho_Saturation, ho_RegionClosing, out ho_ImageReduced2 ); ho_Regions1.Dispose(); HOperatorSet.Threshold(ho_ImageReduced2, out ho_Regions1, new HTuple(Inputs["SMinThreshold"].Value), new HTuple(Inputs["SMaxThreshold"].Value)); ho_RegionOpening1.Dispose(); HOperatorSet.OpeningCircle(ho_Regions1, out ho_RegionOpening1, new HTuple(Inputs["OpenRadius"].Value)); ho_RegionClosing1.Dispose(); HOperatorSet.ClosingCircle(ho_RegionOpening1, out ho_RegionClosing1, new HTuple(Inputs["CloseRadius"].Value)); ho_ImageReduced3.Dispose(); HOperatorSet.ReduceDomain(ho_Intensity, ho_RegionClosing1, out ho_ImageReduced3 ); ho_Regions2.Dispose(); HOperatorSet.Threshold(ho_ImageReduced3, out ho_Regions2, new HTuple(Inputs["IMinThreshold"].Value), new HTuple(Inputs["IMaxThreshold"].Value)); ho_RegionOpening2.Dispose(); HOperatorSet.OpeningCircle(ho_Regions2, out ho_RegionOpening2, new HTuple(Inputs["OpenRadius"].Value)); ho_RegionClosing2.Dispose(); HOperatorSet.ClosingCircle(ho_RegionOpening2, out ho_RegionClosing2, new HTuple(Inputs["CloseRadius"].Value)); //求出眼镜腿区域 ho_RegionDifference.Dispose(); HOperatorSet.Difference(ho_ROI_0, ho_RegionClosing2, out ho_RegionDifference ); ho_RegionOpening3.Dispose(); HOperatorSet.OpeningCircle(ho_RegionDifference, out ho_RegionOpening3, 8); ho_ConnectedRegions.Dispose(); HOperatorSet.Connection(ho_RegionOpening3, out ho_ConnectedRegions); ho_RegionFillUp.Dispose(); HOperatorSet.FillUp(ho_ConnectedRegions, out ho_RegionFillUp); ho_SelectedRegions.Dispose(); HOperatorSet.SelectShape(ho_RegionFillUp, out ho_SelectedRegions, ( (new HTuple("area")).TupleConcat("rect2_len1")).TupleConcat("rect2_len2"), "and", ((new HTuple(Inputs["MinArea"].Value)).TupleConcat(new HTuple(Inputs["MinLen1"].Value))).TupleConcat(new HTuple(Inputs["MinLen2"].Value)), ((new HTuple(Inputs["MaxArea"].Value)).TupleConcat( new HTuple(Inputs["MaxLen1"].Value))).TupleConcat(new HTuple(Inputs["MaxLen2"].Value))); //校验模板结果 HOperatorSet.CountObj(ho_SelectedRegions, out hv_Number); if (hv_Number.I > 0) { Location[] locations = new Location[hv_Number.I]; for (int i = 0; i < hv_Number.I; i++) { ho_ObjectSelected.Dispose(); HOperatorSet.SelectObj(ho_SelectedRegions, out ho_ObjectSelected, i + 1); //产生骨骼 ho_Skeleton.Dispose(); HOperatorSet.Skeleton(ho_ObjectSelected, out ho_Skeleton); //计算骨骼端点和关节点 ho_EndPoints.Dispose(); ho_JuncPoints.Dispose(); HOperatorSet.JunctionsSkeleton(ho_Skeleton, out ho_EndPoints, out ho_JuncPoints ); //从骨骼中去除关节点 ho_RegionDifference1.Dispose(); HOperatorSet.Difference(ho_Skeleton, ho_JuncPoints, out ho_RegionDifference1 ); //提取最长的骨骼 ho_ConnectedRegions1.Dispose(); HOperatorSet.Connection(ho_RegionDifference1, out ho_ConnectedRegions1 ); ho_SelectedRegions1.Dispose(); HOperatorSet.SelectShapeStd(ho_ConnectedRegions1, out ho_SelectedRegions1, "max_area", 70); HOperatorSet.SmallestRectangle2(ho_SelectedRegions1, 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_Column, hv_Phi, hv_Length1, hv_Length2); //转成xld ho_Contours.Dispose(); HOperatorSet.GenContoursSkeletonXld(ho_Skeleton, out ho_Contours, 1, "filter"); ho_SelectedXLD.Dispose(); HOperatorSet.SelectShapeXld(ho_Contours, out ho_SelectedXLD, (new HTuple("rect2_phi")).TupleConcat( "rect2_phi"), "or", (((((hv_Phi.TupleDeg()) - new HTuple(Inputs["XLDPhi"].Value))).TupleRad())).TupleConcat( ((((hv_Phi.TupleDeg()) + 180) - new HTuple(Inputs["XLDPhi"].Value))).TupleRad()), (((((hv_Phi.TupleDeg() ) + new HTuple(Inputs["XLDPhi"].Value))).TupleRad())).TupleConcat(((((hv_Phi.TupleDeg()) + 180) + new HTuple(Inputs["XLDPhi"].Value))).TupleRad() )); ho_UnionContours.Dispose(); HOperatorSet.UnionAdjacentContoursXld(ho_SelectedXLD, out ho_UnionContours, 30, 1, "attr_keep"); //选择最长的xlds HTuple hv_SortIndex = new HTuple(); HOperatorSet.LengthXld(ho_UnionContours, out hv_Length); HOperatorSet.TupleSortIndex(hv_Length, out hv_SortIndex); ho_SelectedXLD1.Dispose(); HOperatorSet.SelectObj(ho_UnionContours, out ho_SelectedXLD1, hv_SortIndex[hv_SortIndex.Length - 1].I + 1); HOperatorSet.GetContourXld(ho_SelectedXLD1, out hv_Row1, out hv_Col1); ho_Cross.Dispose(); HOperatorSet.GenCrossContourXld(out ho_Cross, hv_Row1[hv_Row1.Length / 2], hv_Col1[hv_Col1.Length / 2], 100, ((new HTuple(45)).TupleRad()) + hv_Phi); if (runningWindow != null) { HOperatorSet.DispObj(ho_SelectedXLD1, runningWindow); HOperatorSet.DispObj(ho_Cross, runningWindow); } if (configWindow != null) { HOperatorSet.DispObj(ho_SelectedXLD1, configWindow); HOperatorSet.DispObj(ho_Cross, configWindow); } locations[i] = new Location(hv_Col1[hv_Col1.Length / 2].D, hv_Row1[hv_Row1.Length / 2].D, hv_Phi[0].D); } if ((bool)Inputs["AutoSave"].Value == true) { //保存文件到本地 HOperatorSet.WriteImage(hImage, "bmp", 0, new HTuple(Inputs["ImagePath"].Value)); } //封装有效结果 Outputs["ItemLocation"].Value = locations; stopwatch.Stop(); RunStatus = new RunStatus(stopwatch.Elapsed.TotalMilliseconds); } else { //没有有效的结果 Outputs["ItemLocation"].Value = new Location[0]; stopwatch.Stop(); RunStatus = new RunStatus(stopwatch.Elapsed.TotalMilliseconds, EResult.Warning, "没找到有效目标"); } outputs = new ItemCollection(Outputs); } catch (Exception ex) { stopwatch.Stop(); RunStatus = new RunStatus(stopwatch.Elapsed.TotalMilliseconds, EResult.Error, ex.Message, ex); throw; } finally { hImage.Dispose(); ho_ImageReduced.Dispose(); ho_R.Dispose(); ho_G.Dispose(); ho_B.Dispose(); ho_Hue.Dispose(); ho_Saturation.Dispose(); ho_Intensity.Dispose(); ho_Regions.Dispose(); ho_RegionOpening.Dispose(); ho_RegionClosing.Dispose(); ho_ImageReduced2.Dispose(); ho_Regions1.Dispose(); ho_RegionOpening1.Dispose(); ho_RegionClosing1.Dispose(); ho_ImageReduced3.Dispose(); ho_Regions2.Dispose(); ho_RegionOpening2.Dispose(); ho_RegionClosing2.Dispose(); ho_RegionDifference.Dispose(); ho_RegionOpening3.Dispose(); ho_ConnectedRegions.Dispose(); ho_RegionFillUp.Dispose(); ho_SelectedRegions.Dispose(); ho_Skeleton.Dispose(); ho_EndPoints.Dispose(); ho_JuncPoints.Dispose(); ho_RegionDifference1.Dispose(); ho_ConnectedRegions1.Dispose(); ho_SelectedRegions1.Dispose(); ho_Rectangle.Dispose(); ho_Contours.Dispose(); ho_SelectedXLD.Dispose(); ho_UnionContours.Dispose(); ho_SelectedXLD1.Dispose(); ho_Cross.Dispose(); ho_ObjectSelected.Dispose(); } }