//创建模板 private void buttonCreateModel_Click(object sender, EventArgs e) { HObject ModelImage, ModelImages, ModelRegion, ModelRegions, BlackImage; HOperatorSet.GenEmptyObj(out ModelImage); if (RectModel != null) { this.tabControl1.SelectedIndex = 0; hWindowControl1.HalconWindow.ClearWindow(); HOperatorSet.DispObj(getImage, hwindow); Threshold = int.Parse(textBox_Threshold.Text); HOperatorSet.ReduceDomain(getImage, RectModel, out ModelImage); HOperatorSet.Threshold(ModelImage, out ModelRegion, Threshold, 255); HOperatorSet.ReduceDomain(ModelImage, ModelRegion, out ModelImage); HOperatorSet.GenImageConst(out BlackImage, "byte", ImageWidth, ImageHeight); HOperatorSet.AddImage(BlackImage, ModelImage, out ModelImage, 1, 0); HOperatorSet.ReduceDomain(ModelImage, RectModel, out ModelImage); HOperatorSet.InspectShapeModel(ModelImage, out ModelImages, out ModelRegions, 4, 20); HOperatorSet.DispObj(ModelRegions, hwindow); HOperatorSet.CreateShapeModel(ModelImage, "auto", 0, (new HTuple(360)).TupleRad(), "auto", "auto", "use_polarity", "auto", "auto", out ModelID); GC.Collect(); } else { MessageBox.Show("请选取模板区域!"); return; } }
///<summary> ///图像拼接 ///</summary> public void ImageStitching(HObject ho_ImageResult, HObject ho_Image, HObject ho_Rectangle2, HTuple hv_Phi, HTuple hv_Length1, HTuple hv_Length2, HTuple hv_CenterRow, HTuple hv_CenterCol, out HObject ho_ImageStich) { // Local iconic variables HObject ho_Rectangle2Scaled, ho_Image1, ho_Imagepaint; // Local control variables HTuple hv_Area = null, hv_PartRow = null, hv_PartCol = null; HTuple hv_ScaledRows = null, hv_ScaledColumns = null, hv_GrayvalScaled = null; HTuple hv_rows = null, hv_cols = null, hv_Width = null; HTuple hv_Height = null; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_ImageStich); HOperatorSet.GenEmptyObj(out ho_Rectangle2Scaled); HOperatorSet.GenEmptyObj(out ho_Image1); HOperatorSet.GenEmptyObj(out ho_Imagepaint); HOperatorSet.AreaCenter(ho_ImageResult, out hv_Area, out hv_PartRow, out hv_PartCol); ho_Rectangle2Scaled.Dispose(); HOperatorSet.GenRectangle2(out ho_Rectangle2Scaled, hv_PartRow, hv_PartCol, hv_Phi, hv_Length1 + 10, hv_Length2 + 10); HOperatorSet.GetRegionPoints(ho_Rectangle2Scaled, out hv_ScaledRows, out hv_ScaledColumns); HOperatorSet.GetGrayval(ho_ImageResult, hv_ScaledRows, hv_ScaledColumns, out hv_GrayvalScaled); hv_rows = (hv_ScaledRows + hv_CenterRow) - hv_PartRow; hv_cols = (hv_ScaledColumns + hv_CenterCol) - hv_PartCol; HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height); ho_Image1.Dispose(); HOperatorSet.GenImageConst(out ho_Image1, "byte", hv_Width, hv_Height); HOperatorSet.SetGrayval(ho_Image1, hv_rows, hv_cols, hv_GrayvalScaled); ho_Imagepaint.Dispose(); HOperatorSet.PaintRegion(ho_Rectangle2, ho_Image, out ho_Imagepaint, 0, "fill"); ho_ImageStich.Dispose(); HOperatorSet.AddImage(ho_Image1, ho_Imagepaint, out ho_ImageStich, 1, 0); ho_Rectangle2Scaled.Dispose(); ho_Image1.Dispose(); ho_Imagepaint.Dispose(); return; }
// Procedures public void GetRegionOfRoughWithSobelMean(HObject ho_Image, out HObject ho_FoundRegion, HTuple hv_ThresholdMinGray, HTuple hv_ThresholdMaxGray, HTuple hv_RoughAreaMinPercent, HTuple hv_MinMaxGrayPercent, HTuple hv_HatWidth, HTuple hv_HatHeight, HTuple hv_SelectAreaMin, HTuple hv_SelectAreaMax, HTuple hv_RoughAreaClosingWidth, HTuple hv_RoughAreaClosingHeight, HTuple hv_RoughAreaOpeningWidth, HTuple hv_RoughAreaOpeningHeight, HTuple hv_SobelAmpSize, HTuple hv_SobelAmpMeanMin, HTuple hv_SobelAmpThresholdMinGray) { // Local iconic variables HObject ho_Domain, ho_Region, ho_ImageScaled; HObject ho_SE, ho_ImageTopHat, ho_ImageScaled4, ho_ImageBotHat; HObject ho_ImageScaled5, ho_ImageResult, ho_ImageScaled5And6; HObject ho_Region1, ho_RegionClosing1, ho_RegionOpening; HObject ho_ConnectedRegions1, ho_SelectedRegions2, ho_RegionFillUp; HObject ho_RegionClosing10, ho_RegionOpening12, ho_ImageReduced; HObject ho_EdgeAmplitude, ho_Region2, ho_RegionClosing22; HObject ho_ConnectedRegions2, ho_RegionFillUp2, ho_SelectedRegions3; HObject ho_RegionFillUp1, ho_RegionClosing21, ho_RegionOpening22; HObject ho_RegionIntersection, ho_ConnectedRegions3; // Local control variables HTuple hv_Width = null, hv_Height = null, hv_DomainArea = null; HTuple hv_Number = null, hv_AreaValue = null, hv_RoughAreaPercent = null; HTuple hv_Min = null, hv_Max = null, hv_Range = null, hv_UsedThreshold2 = null; HTuple hv_Mean = null, hv_Deviation = null; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_FoundRegion); HOperatorSet.GenEmptyObj(out ho_Domain); HOperatorSet.GenEmptyObj(out ho_Region); HOperatorSet.GenEmptyObj(out ho_ImageScaled); HOperatorSet.GenEmptyObj(out ho_SE); HOperatorSet.GenEmptyObj(out ho_ImageTopHat); HOperatorSet.GenEmptyObj(out ho_ImageScaled4); HOperatorSet.GenEmptyObj(out ho_ImageBotHat); HOperatorSet.GenEmptyObj(out ho_ImageScaled5); HOperatorSet.GenEmptyObj(out ho_ImageResult); HOperatorSet.GenEmptyObj(out ho_ImageScaled5And6); HOperatorSet.GenEmptyObj(out ho_Region1); HOperatorSet.GenEmptyObj(out ho_RegionClosing1); HOperatorSet.GenEmptyObj(out ho_RegionOpening); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1); HOperatorSet.GenEmptyObj(out ho_SelectedRegions2); HOperatorSet.GenEmptyObj(out ho_RegionFillUp); HOperatorSet.GenEmptyObj(out ho_RegionClosing10); HOperatorSet.GenEmptyObj(out ho_RegionOpening12); HOperatorSet.GenEmptyObj(out ho_ImageReduced); HOperatorSet.GenEmptyObj(out ho_EdgeAmplitude); HOperatorSet.GenEmptyObj(out ho_Region2); HOperatorSet.GenEmptyObj(out ho_RegionClosing22); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions2); HOperatorSet.GenEmptyObj(out ho_RegionFillUp2); HOperatorSet.GenEmptyObj(out ho_SelectedRegions3); HOperatorSet.GenEmptyObj(out ho_RegionFillUp1); HOperatorSet.GenEmptyObj(out ho_RegionClosing21); HOperatorSet.GenEmptyObj(out ho_RegionOpening22); HOperatorSet.GenEmptyObj(out ho_RegionIntersection); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions3); ho_Domain.Dispose(); HOperatorSet.GetDomain(ho_Image, out ho_Domain); HOperatorSet.RegionFeatures(ho_Domain, "width", out hv_Width); HOperatorSet.RegionFeatures(ho_Domain, "height", out hv_Height); HOperatorSet.RegionFeatures(ho_Domain, "area", out hv_DomainArea); ho_Region.Dispose(); HOperatorSet.Threshold(ho_Image, out ho_Region, hv_ThresholdMinGray, hv_ThresholdMaxGray); HOperatorSet.CountObj(ho_Region, out hv_Number); if ((int)(new HTuple(hv_Number.TupleEqual(0))) != 0) { ho_FoundRegion.Dispose(); HOperatorSet.GenEmptyRegion(out ho_FoundRegion); ho_Domain.Dispose(); ho_Region.Dispose(); ho_ImageScaled.Dispose(); ho_SE.Dispose(); ho_ImageTopHat.Dispose(); ho_ImageScaled4.Dispose(); ho_ImageBotHat.Dispose(); ho_ImageScaled5.Dispose(); ho_ImageResult.Dispose(); ho_ImageScaled5And6.Dispose(); ho_Region1.Dispose(); ho_RegionClosing1.Dispose(); ho_RegionOpening.Dispose(); ho_ConnectedRegions1.Dispose(); ho_SelectedRegions2.Dispose(); ho_RegionFillUp.Dispose(); ho_RegionClosing10.Dispose(); ho_RegionOpening12.Dispose(); ho_ImageReduced.Dispose(); ho_EdgeAmplitude.Dispose(); ho_Region2.Dispose(); ho_RegionClosing22.Dispose(); ho_ConnectedRegions2.Dispose(); ho_RegionFillUp2.Dispose(); ho_SelectedRegions3.Dispose(); ho_RegionFillUp1.Dispose(); ho_RegionClosing21.Dispose(); ho_RegionOpening22.Dispose(); ho_RegionIntersection.Dispose(); ho_ConnectedRegions3.Dispose(); return; } HOperatorSet.RegionFeatures(ho_Region, "area", out hv_AreaValue); if ((int)(new HTuple(hv_AreaValue.TupleEqual(0))) != 0) { ho_FoundRegion.Dispose(); HOperatorSet.GenEmptyRegion(out ho_FoundRegion); ho_Domain.Dispose(); ho_Region.Dispose(); ho_ImageScaled.Dispose(); ho_SE.Dispose(); ho_ImageTopHat.Dispose(); ho_ImageScaled4.Dispose(); ho_ImageBotHat.Dispose(); ho_ImageScaled5.Dispose(); ho_ImageResult.Dispose(); ho_ImageScaled5And6.Dispose(); ho_Region1.Dispose(); ho_RegionClosing1.Dispose(); ho_RegionOpening.Dispose(); ho_ConnectedRegions1.Dispose(); ho_SelectedRegions2.Dispose(); ho_RegionFillUp.Dispose(); ho_RegionClosing10.Dispose(); ho_RegionOpening12.Dispose(); ho_ImageReduced.Dispose(); ho_EdgeAmplitude.Dispose(); ho_Region2.Dispose(); ho_RegionClosing22.Dispose(); ho_ConnectedRegions2.Dispose(); ho_RegionFillUp2.Dispose(); ho_SelectedRegions3.Dispose(); ho_RegionFillUp1.Dispose(); ho_RegionClosing21.Dispose(); ho_RegionOpening22.Dispose(); ho_RegionIntersection.Dispose(); ho_ConnectedRegions3.Dispose(); return; } hv_RoughAreaPercent = (hv_AreaValue / hv_DomainArea) * 100; if ((int)(new HTuple(hv_RoughAreaPercent.TupleLess(hv_RoughAreaMinPercent))) != 0) { ho_FoundRegion.Dispose(); HOperatorSet.GenEmptyRegion(out ho_FoundRegion); ho_Domain.Dispose(); ho_Region.Dispose(); ho_ImageScaled.Dispose(); ho_SE.Dispose(); ho_ImageTopHat.Dispose(); ho_ImageScaled4.Dispose(); ho_ImageBotHat.Dispose(); ho_ImageScaled5.Dispose(); ho_ImageResult.Dispose(); ho_ImageScaled5And6.Dispose(); ho_Region1.Dispose(); ho_RegionClosing1.Dispose(); ho_RegionOpening.Dispose(); ho_ConnectedRegions1.Dispose(); ho_SelectedRegions2.Dispose(); ho_RegionFillUp.Dispose(); ho_RegionClosing10.Dispose(); ho_RegionOpening12.Dispose(); ho_ImageReduced.Dispose(); ho_EdgeAmplitude.Dispose(); ho_Region2.Dispose(); ho_RegionClosing22.Dispose(); ho_ConnectedRegions2.Dispose(); ho_RegionFillUp2.Dispose(); ho_SelectedRegions3.Dispose(); ho_RegionFillUp1.Dispose(); ho_RegionClosing21.Dispose(); ho_RegionOpening22.Dispose(); ho_RegionIntersection.Dispose(); ho_ConnectedRegions3.Dispose(); return; } HOperatorSet.MinMaxGray(ho_Region, ho_Image, hv_MinMaxGrayPercent, out hv_Min, out hv_Max, out hv_Range); ho_ImageScaled.Dispose(); HOperatorSet.ScaleImage(ho_Image, out ho_ImageScaled, 255 / (hv_Max - hv_Min), (-(255 / (hv_Max - hv_Min))) * hv_Min); // ho_SE.Dispose(); HOperatorSet.GenDiscSe(out ho_SE, "byte", hv_HatWidth, hv_HatHeight, 0); // ho_ImageTopHat.Dispose(); HOperatorSet.GrayTophat(ho_ImageScaled, ho_SE, out ho_ImageTopHat); HOperatorSet.MinMaxGray(ho_Region, ho_ImageTopHat, hv_MinMaxGrayPercent, out hv_Min, out hv_Max, out hv_Range); ho_ImageScaled4.Dispose(); HOperatorSet.ScaleImage(ho_ImageTopHat, out ho_ImageScaled4, 255 / (hv_Max - hv_Min), (-(255 / (hv_Max - hv_Min))) * hv_Min); ho_ImageBotHat.Dispose(); HOperatorSet.GrayBothat(ho_ImageScaled, ho_SE, out ho_ImageBotHat); HOperatorSet.MinMaxGray(ho_Region, ho_ImageBotHat, hv_MinMaxGrayPercent, out hv_Min, out hv_Max, out hv_Range); ho_ImageScaled5.Dispose(); HOperatorSet.ScaleImage(ho_ImageBotHat, out ho_ImageScaled5, 255 / (hv_Max - hv_Min), (-(255 / (hv_Max - hv_Min))) * hv_Min); ho_ImageResult.Dispose(); HOperatorSet.AddImage(ho_ImageScaled4, ho_ImageScaled5, out ho_ImageResult, 0.5, 0); HOperatorSet.MinMaxGray(ho_Region, ho_ImageResult, hv_MinMaxGrayPercent, out hv_Min, out hv_Max, out hv_Range); ho_ImageScaled5And6.Dispose(); HOperatorSet.ScaleImage(ho_ImageResult, out ho_ImageScaled5And6, 255 / (hv_Max - hv_Min), (-(255 / (hv_Max - hv_Min))) * hv_Min); // ho_Region1.Dispose(); HOperatorSet.BinaryThreshold(ho_ImageScaled5And6, out ho_Region1, "max_separability", "light", out hv_UsedThreshold2); ho_RegionClosing1.Dispose(); HOperatorSet.ClosingRectangle1(ho_Region1, out ho_RegionClosing1, hv_HatWidth, hv_HatHeight); ho_RegionOpening.Dispose(); HOperatorSet.OpeningRectangle1(ho_RegionClosing1, out ho_RegionOpening, hv_HatWidth * 2, hv_HatHeight * 2); ho_ConnectedRegions1.Dispose(); HOperatorSet.Connection(ho_RegionOpening, out ho_ConnectedRegions1); ho_SelectedRegions2.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegions1, out ho_SelectedRegions2, "area", "and", hv_SelectAreaMin, hv_SelectAreaMax); ho_RegionFillUp.Dispose(); HOperatorSet.FillUp(ho_SelectedRegions2, out ho_RegionFillUp); ho_RegionClosing10.Dispose(); HOperatorSet.ClosingRectangle1(ho_RegionFillUp, out ho_RegionClosing10, hv_RoughAreaClosingWidth, hv_RoughAreaClosingHeight); ho_RegionOpening12.Dispose(); HOperatorSet.OpeningRectangle1(ho_RegionClosing10, out ho_RegionOpening12, hv_RoughAreaOpeningWidth, hv_RoughAreaOpeningHeight); //sobel ho_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_Region, out ho_ImageReduced); ho_EdgeAmplitude.Dispose(); HOperatorSet.SobelAmp(ho_Image, out ho_EdgeAmplitude, "sum_abs", hv_SobelAmpSize); HOperatorSet.Intensity(ho_Region, ho_EdgeAmplitude, out hv_Mean, out hv_Deviation); if ((int)(new HTuple(hv_Mean.TupleLess(hv_SobelAmpMeanMin))) != 0) { ho_FoundRegion.Dispose(); HOperatorSet.GenEmptyRegion(out ho_FoundRegion); ho_Domain.Dispose(); ho_Region.Dispose(); ho_ImageScaled.Dispose(); ho_SE.Dispose(); ho_ImageTopHat.Dispose(); ho_ImageScaled4.Dispose(); ho_ImageBotHat.Dispose(); ho_ImageScaled5.Dispose(); ho_ImageResult.Dispose(); ho_ImageScaled5And6.Dispose(); ho_Region1.Dispose(); ho_RegionClosing1.Dispose(); ho_RegionOpening.Dispose(); ho_ConnectedRegions1.Dispose(); ho_SelectedRegions2.Dispose(); ho_RegionFillUp.Dispose(); ho_RegionClosing10.Dispose(); ho_RegionOpening12.Dispose(); ho_ImageReduced.Dispose(); ho_EdgeAmplitude.Dispose(); ho_Region2.Dispose(); ho_RegionClosing22.Dispose(); ho_ConnectedRegions2.Dispose(); ho_RegionFillUp2.Dispose(); ho_SelectedRegions3.Dispose(); ho_RegionFillUp1.Dispose(); ho_RegionClosing21.Dispose(); ho_RegionOpening22.Dispose(); ho_RegionIntersection.Dispose(); ho_ConnectedRegions3.Dispose(); return; } ho_Region2.Dispose(); HOperatorSet.Threshold(ho_EdgeAmplitude, out ho_Region2, hv_SobelAmpThresholdMinGray, 255); ho_RegionClosing22.Dispose(); HOperatorSet.ClosingRectangle1(ho_Region2, out ho_RegionClosing22, hv_HatWidth, hv_HatHeight); ho_ConnectedRegions2.Dispose(); HOperatorSet.Connection(ho_RegionClosing22, out ho_ConnectedRegions2); ho_RegionFillUp2.Dispose(); HOperatorSet.FillUp(ho_ConnectedRegions2, out ho_RegionFillUp2); ho_SelectedRegions3.Dispose(); HOperatorSet.SelectShape(ho_RegionFillUp2, out ho_SelectedRegions3, "area", "and", hv_SelectAreaMin, 99999999); ho_RegionFillUp1.Dispose(); HOperatorSet.FillUp(ho_SelectedRegions3, out ho_RegionFillUp1); ho_RegionClosing21.Dispose(); HOperatorSet.ClosingRectangle1(ho_RegionFillUp1, out ho_RegionClosing21, hv_RoughAreaClosingWidth, hv_RoughAreaClosingHeight); //opening_rectangle1 (RegionClosing21, RegionOpening22, RoughAreaOpeningWidth, RoughAreaOpeningHeight) ho_RegionOpening22.Dispose(); HOperatorSet.OpeningRectangle1(ho_RegionClosing21, out ho_RegionOpening22, hv_RoughAreaClosingWidth, hv_RoughAreaClosingHeight); //shape_trans (RegionOpening22, RegionTrans, 'rectangle2') //intersection ho_RegionIntersection.Dispose(); HOperatorSet.Intersection(ho_RegionOpening22, ho_RegionOpening12, out ho_RegionIntersection ); ho_ConnectedRegions3.Dispose(); HOperatorSet.Connection(ho_RegionIntersection, out ho_ConnectedRegions3); ho_SelectedRegions3.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegions3, out ho_SelectedRegions3, "area", "and", hv_SelectAreaMin, hv_SelectAreaMax); ho_FoundRegion.Dispose(); HOperatorSet.MoveRegion(ho_SelectedRegions3, out ho_FoundRegion, 0, 0); ho_Domain.Dispose(); ho_Region.Dispose(); ho_ImageScaled.Dispose(); ho_SE.Dispose(); ho_ImageTopHat.Dispose(); ho_ImageScaled4.Dispose(); ho_ImageBotHat.Dispose(); ho_ImageScaled5.Dispose(); ho_ImageResult.Dispose(); ho_ImageScaled5And6.Dispose(); ho_Region1.Dispose(); ho_RegionClosing1.Dispose(); ho_RegionOpening.Dispose(); ho_ConnectedRegions1.Dispose(); ho_SelectedRegions2.Dispose(); ho_RegionFillUp.Dispose(); ho_RegionClosing10.Dispose(); ho_RegionOpening12.Dispose(); ho_ImageReduced.Dispose(); ho_EdgeAmplitude.Dispose(); ho_Region2.Dispose(); ho_RegionClosing22.Dispose(); ho_ConnectedRegions2.Dispose(); ho_RegionFillUp2.Dispose(); ho_SelectedRegions3.Dispose(); ho_RegionFillUp1.Dispose(); ho_RegionClosing21.Dispose(); ho_RegionOpening22.Dispose(); ho_RegionIntersection.Dispose(); ho_ConnectedRegions3.Dispose(); return; }
///<summary> ///图像锐化(时域) ///</summary> public void ImageSharpening(HObject ho_ImagePart, HTuple hv_MaskWidth, HTuple hv_MaskHeight, HTuple hv_MaskType, HTuple hv_Radius, HTuple hv_MaskSize, HTuple hv_FilterMask, HTuple hv_MultImage_Mult, HTuple hv_AddImage_Mult, HTuple hv_ScaleImage_Mult, HTuple hv_ScaleImage_Add, out HObject ho_ImageResult) { // Local iconic variables HObject ho_ImageSmooth, ho_ImageLaplace, ho_ImageScaled; HObject ho_ImageConverted, ho_ImageEdgeAmp, ho_ImageMean; HObject ho_ImageAdd1, ho_ImageMult, ho_ImageAdd2; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_ImageResult); HOperatorSet.GenEmptyObj(out ho_ImageSmooth); HOperatorSet.GenEmptyObj(out ho_ImageLaplace); HOperatorSet.GenEmptyObj(out ho_ImageScaled); HOperatorSet.GenEmptyObj(out ho_ImageConverted); HOperatorSet.GenEmptyObj(out ho_ImageEdgeAmp); HOperatorSet.GenEmptyObj(out ho_ImageMean); HOperatorSet.GenEmptyObj(out ho_ImageAdd1); HOperatorSet.GenEmptyObj(out ho_ImageMult); HOperatorSet.GenEmptyObj(out ho_ImageAdd2); //中值滤波 ho_ImageSmooth.Dispose(); HOperatorSet.MedianImage(ho_ImagePart, out ho_ImageSmooth, hv_MaskType, hv_Radius, "mirrored"); //拉普拉斯锐化 ho_ImageLaplace.Dispose(); HOperatorSet.Laplace(ho_ImageSmooth, out ho_ImageLaplace, "signed", hv_MaskSize, hv_FilterMask); ho_ImageScaled.Dispose(); HOperatorSet.ScaleImage(ho_ImageLaplace, out ho_ImageScaled, 7, 50); ho_ImageConverted.Dispose(); HOperatorSet.ConvertImageType(ho_ImageScaled, out ho_ImageConverted, "byte"); //prewitt锐化 ho_ImageEdgeAmp.Dispose(); HOperatorSet.PrewittAmp(ho_ImagePart, out ho_ImageEdgeAmp); ho_ImageMean.Dispose(); HOperatorSet.MeanImage(ho_ImageEdgeAmp, out ho_ImageMean, hv_MaskWidth, hv_MaskHeight); ho_ImageAdd1.Dispose(); HOperatorSet.AddImage(ho_ImageEdgeAmp, ho_ImageMean, out ho_ImageAdd1, 1, 0); //图像锐化处理 ho_ImageMult.Dispose(); HOperatorSet.MultImage(ho_ImageConverted, ho_ImageAdd1, out ho_ImageMult, hv_MultImage_Mult, 0); ho_ImageMean.Dispose(); HOperatorSet.MeanImage(ho_ImageMult, out ho_ImageMean, hv_MaskWidth, hv_MaskHeight); ho_ImageAdd2.Dispose(); HOperatorSet.AddImage(ho_ImageSmooth, ho_ImageMean, out ho_ImageAdd2, hv_AddImage_Mult, 0); //灰度拉伸 ho_ImageResult.Dispose(); HOperatorSet.ScaleImage(ho_ImageAdd2, out ho_ImageResult, hv_ScaleImage_Mult, hv_ScaleImage_Add); ho_ImageSmooth.Dispose(); ho_ImageLaplace.Dispose(); ho_ImageScaled.Dispose(); ho_ImageConverted.Dispose(); ho_ImageEdgeAmp.Dispose(); ho_ImageMean.Dispose(); ho_ImageAdd1.Dispose(); ho_ImageMult.Dispose(); ho_ImageAdd2.Dispose(); return; }
//DateTime t1, t2, t3, t4,t5,t6,t7; private void action() { HObject ho_Image1 = null, ho_Image2 = null, ho_ImageMean = null, ho_d = null; HObject ho_Image3 = null, ho_ImageResult = null, ho_ImageResult1 = null; HObject ho_Region = null, ho_ConnectedRegions = null, ho_SelectedRegions = null; HObject ho_RegionFillUp = null, ho_RegionDifference = null; HObject ho_ConnectedRegions1 = null, ho_SelectedRegions1 = null; HObject ho_RegionDifference1 = null, ho_ConnectedRegions2 = null; HObject ho_SelectedRegions2 = null, ho_RegionDilation = null; HObject ho_ImageReduced = null, ho_Region1 = null, ho_ConnectedRegions3 = null; HObject ho_SelectedRegions3 = null, ho_RegionFillUp1 = null; HObject ho_Circle = null, ho_Circle1 = null, ho_RegionDifference2 = null; HObject ho_RegionUnion = null; HTuple hv_Area = new HTuple(), hv_Row = new HTuple(), hv_Column = new HTuple(), rx = new HTuple(), rc = new HTuple(), re = new HTuple(); HTuple hv_Area1 = new HTuple(), hv_Row1 = new HTuple(), hmp = new HTuple(), dmp = new HTuple(); HTuple hv_Column1 = new HTuple(), hv_Row2 = new HTuple(); HTuple hv_Column2 = new HTuple(), hv_Radius = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image1); HOperatorSet.GenEmptyObj(out ho_Image2); HOperatorSet.GenEmptyObj(out ho_Image3); HOperatorSet.GenEmptyObj(out ho_ImageResult); HOperatorSet.GenEmptyObj(out ho_ImageResult1); HOperatorSet.GenEmptyObj(out ho_Region); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions); HOperatorSet.GenEmptyObj(out ho_SelectedRegions); HOperatorSet.GenEmptyObj(out ho_RegionFillUp); HOperatorSet.GenEmptyObj(out ho_RegionDifference); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1); HOperatorSet.GenEmptyObj(out ho_SelectedRegions1); HOperatorSet.GenEmptyObj(out ho_RegionDifference1); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions2); HOperatorSet.GenEmptyObj(out ho_SelectedRegions2); HOperatorSet.GenEmptyObj(out ho_RegionDilation); HOperatorSet.GenEmptyObj(out ho_ImageReduced); HOperatorSet.GenEmptyObj(out ho_Region1); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions3); HOperatorSet.GenEmptyObj(out ho_SelectedRegions3); HOperatorSet.GenEmptyObj(out ho_RegionFillUp1); HOperatorSet.GenEmptyObj(out ho_Circle); HOperatorSet.GenEmptyObj(out ho_Circle1); HOperatorSet.GenEmptyObj(out ho_RegionDifference2); HOperatorSet.GenEmptyObj(out ho_RegionUnion); HOperatorSet.GenEmptyObj(out ho_ImageMean); HOperatorSet.GenEmptyObj(out ho_d); HOperatorSet.Union1(algorithm.Region, out RegionToDisp); //t3 = DateTime.Now; try { ho_Image1.Dispose(); ho_Image2.Dispose(); ho_Image3.Dispose(); HOperatorSet.Decompose3(Image, out ho_Image1, out ho_Image2, out ho_Image3 ); ho_ImageResult.Dispose(); HOperatorSet.AddImage(ho_Image2, ho_Image3, out ho_ImageResult, 0.5, 0); ho_ImageResult1.Dispose(); HOperatorSet.DivImage(ho_Image1, ho_ImageResult, out ho_ImageResult1, 128, 0); ho_ImageMean.Dispose(); HOperatorSet.MeanImage(ho_ImageResult1, out ho_ImageMean, 500, 500); //ho_RegionDynThresh.Dispose(); ho_Region.Dispose(); HOperatorSet.DynThreshold(ho_ImageResult1, ho_ImageMean, out ho_Region, 3, "light"); //ho_Region.Dispose(); //HOperatorSet.Threshold(ho_ImageResult1, out ho_Region, 130, 255); ho_ConnectedRegions.Dispose(); HOperatorSet.Connection(ho_Region, out ho_ConnectedRegions); ho_SelectedRegions.Dispose(); HOperatorSet.SelectShapeStd(ho_ConnectedRegions, out ho_SelectedRegions, "max_area", 70); ho_RegionFillUp.Dispose(); HOperatorSet.FillUp(ho_SelectedRegions, out ho_RegionFillUp); ho_RegionDifference.Dispose(); HOperatorSet.Difference(ho_RegionFillUp, ho_SelectedRegions, out ho_RegionDifference ); ho_ConnectedRegions1.Dispose(); HOperatorSet.Connection(ho_RegionDifference, out ho_ConnectedRegions1); ho_SelectedRegions1.Dispose(); HOperatorSet.SelectShapeStd(ho_ConnectedRegions1, out ho_SelectedRegions1, "max_area", 70); HOperatorSet.AreaCenter(ho_SelectedRegions1, out hv_Area, out hv_Row, out hv_Column); HOperatorSet.SmallestCircle(ho_SelectedRegions1, out rx, out rc, out re); HOperatorSet.Intensity(ho_SelectedRegions1, Image, out hmp, out dmp); ho_RegionDifference1.Dispose(); HOperatorSet.Difference(ho_RegionDifference, ho_SelectedRegions1, out ho_RegionDifference1 ); ho_ConnectedRegions2.Dispose(); HOperatorSet.Connection(ho_RegionDifference1, out ho_ConnectedRegions2); ho_SelectedRegions2.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegions2, out ho_SelectedRegions2, "area", "and", 1500, 9999900); HOperatorSet.AreaCenter(ho_SelectedRegions2, out hv_Area1, out hv_Row1, out hv_Column1); ho_RegionDilation.Dispose(); HOperatorSet.DilationCircle(ho_RegionFillUp, out ho_RegionDilation, 200); ho_d.Dispose(); HOperatorSet.Difference(ho_RegionDilation, ho_RegionFillUp, out ho_d); ho_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(Image, ho_d, out ho_ImageReduced); ho_Region1.Dispose(); HOperatorSet.Threshold(ho_ImageReduced, out ho_Region1, thv, 255); ho_ConnectedRegions3.Dispose(); HOperatorSet.Connection(ho_Region1, out ho_ConnectedRegions3); ho_SelectedRegions3.Dispose(); HOperatorSet.SelectShapeStd(ho_ConnectedRegions3, out ho_SelectedRegions3, "max_area", 70); ho_RegionFillUp1.Dispose(); HOperatorSet.FillUp(ho_SelectedRegions3, out ho_RegionFillUp1); HOperatorSet.InnerCircle(ho_RegionFillUp1, out hv_Row2, out hv_Column2, out hv_Radius); if ((int)(new HTuple((new HTuple(hv_Row2.TupleLength())).TupleEqual(1))) != 0) { ho_Circle.Dispose(); HOperatorSet.GenCircle(out ho_Circle, hv_Row2, hv_Column2, hv_Radius); ho_Circle1.Dispose(); HOperatorSet.GenCircle(out ho_Circle1, hv_Row2, hv_Column2, hv_Radius - 2); ho_RegionDifference2.Dispose(); HOperatorSet.Difference(ho_Circle, ho_Circle1, out ho_RegionDifference2); } ho_RegionUnion.Dispose(); HOperatorSet.Union2(ho_RegionDifference2, ho_SelectedRegions2, out ho_RegionUnion ); HOperatorSet.Union2(ho_RegionUnion, ho_SelectedRegions1, out RegionToDisp ); if (hv_Area1.TupleLength() == 0) { hv_Area1 = 0; } HTuple hv_result = GetHv_result(); hv_result = hv_result.TupleConcat("垫片内孔面积"); hv_result = hv_result.TupleConcat(hv_Area.D); hv_result = hv_result.TupleConcat("内圆直径"); hv_result = hv_result.TupleConcat(hv_Radius.D * 2 * pixeldist); hv_result = hv_result.TupleConcat("垫片灰尘"); hv_result = hv_result.TupleConcat(hv_Area1.D); hv_result = hv_result.TupleConcat("内孔灰度"); hv_result = hv_result.TupleConcat(hmp.D); hv_result = hv_result.TupleConcat("内孔直径"); hv_result = hv_result.TupleConcat(re.D * pixeldist * 2); result = hv_result.Clone(); //t4 = DateTime.Now; } 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); result = hv_result.Clone(); } finally { ho_Image1.Dispose(); ho_Image2.Dispose(); ho_Image3.Dispose(); ho_ImageResult.Dispose(); ho_ImageResult1.Dispose(); ho_Region.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions.Dispose(); ho_RegionFillUp.Dispose(); ho_RegionDifference.Dispose(); ho_ConnectedRegions1.Dispose(); ho_SelectedRegions1.Dispose(); ho_RegionDifference1.Dispose(); ho_ConnectedRegions2.Dispose(); ho_SelectedRegions2.Dispose(); ho_RegionDilation.Dispose(); ho_ImageReduced.Dispose(); ho_Region1.Dispose(); ho_ConnectedRegions3.Dispose(); ho_SelectedRegions3.Dispose(); ho_RegionFillUp1.Dispose(); ho_Circle.Dispose(); ho_Circle1.Dispose(); ho_RegionDifference2.Dispose(); ho_RegionUnion.Dispose(); ho_ImageMean.Dispose(); ho_d.Dispose(); algorithm.Region.Dispose(); } }