/// <summary> /// Creates a measure object based on the model data /// defined by the ROI instance and the parameters /// describing the measure context. /// </summary> protected void UpdateMeasure() { double extent, sPhi, radius; if (mHandle != null) { mHandle.Dispose(); } mMeasAssist.exceptionText = ""; try { switch (mROIType) { case ROI.ROI_TYPE_CIRCLEARC: radius = mROICoord[2].D; sPhi = mROICoord[3].D; extent = mROICoord[4].D; mMeasROI = GenSurCircle(mROICoord, mMeasAssist.mRoiWidth); mHandle = new HMeasure(mROICoord[0].D, mROICoord[1].D, radius, sPhi, extent, mMeasAssist.mRoiWidth, mMeasAssist.mWidth, mMeasAssist.mHeight, mMeasAssist.mInterpolation); break; case ROI.ROI_TYPE_CIRCLE: //記錄 ROI 範圍 mMeasROI = GenSurCircle2(mROICoord, mMeasAssist.mRoiWidth); break; case ROI.ROI_TYPE_LINE: case ROI.ROI_TYPE_POINT: mMeasROI = GenSurRect2(mROICoord, mROICoord[4]); mHandle = new HMeasure(mMeasROI[0].D, mMeasROI[1].D, mMeasROI[2].D, mMeasROI[3].D, mMeasROI[4].D, mMeasAssist.mWidth, mMeasAssist.mHeight, mMeasAssist.mInterpolation); break; } } catch (HOperatorException e) { mEdgeXLD.Dispose(); mMeasureRegion.Dispose(); mMeasAssist.exceptionText = e.Message; ClearResultData(); return; } UpdateResults(); UpdateMeasureRegion(); }
private double FindPositionBottle(HImage Image, DataType.AlgoritmoControlloLivelloParam param, bool useIconic, ref ArrayList iconicList) { double columnCenter = 0; HMeasure MeasureHandle = null; HImage ImgEmfatize = null; try { double Rowstart, Rowend, Colstart, Colend; int Width, Height; Image.GetImageSize(out Width, out Height); Rowstart = 0.0; Rowend = Height; Colstart = Width / 2; Colend = Width / 2; if (useIconic) { iconicList.Add(new Utilities.ObjectToDisplay("Line", new HTuple(Rowstart, Colstart, Rowend, Colend), "red", 1)); } param.RectCentraggio.Column = Width / 2; if (useIconic) { // genero i due rettangoli per la misura dell'inclinazione bottiglia // solo per visualizzazione HRegion rectangleCentraggio = new HRegion(); rectangleCentraggio.GenRectangle2(param.RectCentraggio.Row, Width / 2, 0, param.RectCentraggio.Length1, param.RectCentraggio.Length2); iconicList.Add(new Utilities.ObjectToDisplay(rectangleCentraggio, "red", 1)); } // Determine all edge pairs that have a negative transition, i.e., edge pairs that enclose dark regions. const string Interpolation = "nearest_neighbor"; HTuple RowEdge_first, ColumnEdge_first, Amplitude_first, Distance_first; HTuple RowEdge_last, ColumnEdge_last, Amplitude_last, Distance_last; ImgEmfatize = Image.Emphasize(7, 7, param.EmphasizeFactorx10 / 10.0); // Misura degli edge sul primo rettangolo MeasureHandle = new HMeasure(param.RectCentraggio.Row, Width / 2, 0, param.RectCentraggio.Length1, param.RectCentraggio.Length2, Width, Height, Interpolation); MeasureHandle.MeasurePos(ImgEmfatize, param.Sigmax10 / 10.0, param.Thresholdx10 / 10.0, "all", "first", out RowEdge_first, out ColumnEdge_first, out Amplitude_first, out Distance_first); MeasureHandle.Dispose(); // Libera lo spazio associato a 'MeasureHandle' MeasureHandle = new HMeasure(param.RectCentraggio.Row, Width / 2, Math.PI, param.RectCentraggio.Length1, param.RectCentraggio.Length2, Width, Height, Interpolation); MeasureHandle.MeasurePos(ImgEmfatize, param.Sigmax10 / 10.0, param.Thresholdx10 / 10.0, "all", "first", out RowEdge_last, out ColumnEdge_last, out Amplitude_last, out Distance_last); MeasureHandle.Dispose(); // Libera lo spazio associato a 'MeasureHandle' if (RowEdge_first.Length == 1 && RowEdge_last.Length == 1 && ColumnEdge_first.Length == 1 && ColumnEdge_last.Length == 1) { columnCenter = (ColumnEdge_last.D + ColumnEdge_first.D) / 2; if (useIconic) { iconicList.Add(new Utilities.ObjectToDisplay("Cross", new HTuple(RowEdge_first.D, ColumnEdge_first.D, 15, Math.PI / 4), "magenta", 1)); iconicList.Add(new Utilities.ObjectToDisplay("Cross", new HTuple(RowEdge_last.D, ColumnEdge_last.D, 15, Math.PI / 4), "magenta", 1)); iconicList.Add(new Utilities.ObjectToDisplay("Cross", new HTuple(RowEdge_last.D, columnCenter, 15, Math.PI / 4), "green", 1)); } } else { columnCenter = Width / 2; } } catch (Exception) { columnCenter = 0; } finally { if (MeasureHandle != null) { MeasureHandle.Dispose(); } if (ImgEmfatize != null) { ImgEmfatize.Dispose(); } } return(columnCenter); }
private bool AnalisiLivello(HImage Img, DataType.AlgoritmoControlloLivelloParam param, double centerColumn, bool regolazioni, out bool errorLevelMin, out bool errorLevelMax, out bool errorEmpty, ref ArrayList iconicList) { bool ret = false; errorLevelMin = false; errorLevelMax = false; errorEmpty = false; int Width, Height; Img.GetImageSize(out Width, out Height); const string Interpolation = "nearest_neighbor"; string Transition = param.LiquidoChiaro ? "negative" : "positive"; const string Select = "first"; const double delta = 30.0; // Disegna la regione per il calcolo del Livello HRegion Rectangle = new HRegion(); Rectangle.GenRectangle2(param.RectLivello.Row, centerColumn, param.RectLivello.Angle, param.RectLivello.Length1, param.RectLivello.Length2); HMeasure MeasureHandle = new HMeasure(param.RectLivello.Row, centerColumn, param.RectLivello.Angle, param.RectLivello.Length1, param.RectLivello.Length2, Width, Height, Interpolation); HTuple RowEdge, ColumnEdge, Amplitude, Distance; Img.MeasurePos(MeasureHandle, param.SigmaLivellox10 / 10.0, param.ThresholdLivellox10 / 10.0, Transition, Select, out RowEdge, out ColumnEdge, out Amplitude, out Distance); MeasureHandle.Dispose(); if (RowEdge.Length > 0) { double altezzaSchiuma = RowEdge.D; if (altezzaSchiuma >= param.RowMaxLivello && altezzaSchiuma <= param.RowMinLivello) { if (param.LiquidoChiaro) { ret = true; } else { HRegion rectangleThreshold = new HRegion(); //double row = altezzaSchiuma + (paramLevel.rowMin - altezzaSchiuma) / 2; double row = altezzaSchiuma + (param.RectLivello.Row + param.RectLivello.Length1 - altezzaSchiuma) / 2; double column = centerColumn; //double length1 = (paramLevel.rowMin - altezzaSchiuma) / 2; double length1 = (param.RectLivello.Row + param.RectLivello.Length1 - altezzaSchiuma) / 2; double length2 = param.RectLivello.Length2; rectangleThreshold.GenRectangle2(row, column, param.RectLivello.Angle, length1, length2); HImage imgReduced = Img.ReduceDomain(rectangleThreshold); HRegion regionThreshold = imgReduced.Threshold((double)param.ThresholdMin, (double)param.ThresholdMax); imgReduced.Dispose(); double r, c; int area = rectangleThreshold.AreaCenter(out r, out c); rectangleThreshold.Dispose(); HRegion regionSelect = regionThreshold.SelectShape("area", "and", area * 0.7, double.MaxValue); regionThreshold.Dispose(); ret = regionSelect.CountObj() == 1; iconicList.Add(new Utilities.ObjectToDisplay(regionSelect, "blue", 3) { DrawMode = "fill" }); } } else if (altezzaSchiuma < param.RowMaxLivello) { errorLevelMax = true; } else if (altezzaSchiuma > param.RowMinLivello) { errorLevelMin = true; } iconicList.Add(new Utilities.ObjectToDisplay("Line", new HTuple(altezzaSchiuma, centerColumn - delta, altezzaSchiuma, centerColumn + delta), ret ? "green" : "red", 3)); } else { if (param.UseThreshold) { if (param.LiquidoChiaro) { HRegion rectangleThreshold = new HRegion(); rectangleThreshold.GenRectangle2(param.QuotaControlloVuoto, centerColumn, 0.0, param.LarghezzaControlloVuoto, 20); HImage imgReduced = Img.ReduceDomain(rectangleThreshold); rectangleThreshold.Dispose(); HRegion regionThreshold = imgReduced.Threshold((double)param.ThresholdMin, (double)param.ThresholdMax); imgReduced.Dispose(); HRegion connectedRegions = regionThreshold.Connection(); regionThreshold.Dispose(); HRegion filledCandidates = connectedRegions.FillUp(); connectedRegions.Dispose(); HRegion regionMax = filledCandidates.SelectShapeStd("max_area", 0); double row, col; int area = regionMax.AreaCenter(out row, out col); regionMax.Dispose(); if (area > param.SogliaAreaControlloVuoto) { iconicList.Add(new Utilities.ObjectToDisplay(filledCandidates, "green", 1) { DrawMode = "fill" }); ret = true; } else { iconicList.Add(new Utilities.ObjectToDisplay(filledCandidates, "red", 1) { DrawMode = "fill" }); ret = false; } iconicList.Add(new Utilities.ObjectToDisplay(area.ToString(), "blue", (int)(row - 50), (int)(col - param.LarghezzaControlloVuoto))); } else { HRegion rectangleThreshold = new HRegion(); rectangleThreshold.GenRectangle2(param.RowMaxLivello + (param.RowMinLivello - param.RowMaxLivello) / 2 , centerColumn , param.RectLivello.Angle , (param.RowMinLivello - param.RowMaxLivello) / 2 , param.RectLivello.Length2); HImage imgReduced = Img.ReduceDomain(rectangleThreshold); HRegion regionThreshold = imgReduced.Threshold((double)param.ThresholdMin, (double)param.ThresholdMax); imgReduced.Dispose(); double r, c; int area = rectangleThreshold.AreaCenter(out r, out c); HRegion regionSelect = regionThreshold.SelectShape("area", "and", area * 0.7, double.MaxValue); ret = regionSelect.CountObj() == 1; regionSelect.Dispose(); iconicList.Add(new Utilities.ObjectToDisplay(rectangleThreshold, "red", 1)); iconicList.Add(new Utilities.ObjectToDisplay(regionThreshold, "red", 1) { DrawMode = "fill" }); } if (ret == false) { errorEmpty = true; } } } iconicList.Add(new Utilities.ObjectToDisplay(Rectangle, "red", 2)); iconicList.Add(new Utilities.ObjectToDisplay("Line", new HTuple(param.RowMaxLivello, centerColumn - delta, param.RowMaxLivello, centerColumn + delta), "cyan", 3)); iconicList.Add(new Utilities.ObjectToDisplay("Max", "cyan", (int)(param.RowMaxLivello - delta), (int)(centerColumn + 2 * delta))); iconicList.Add(new Utilities.ObjectToDisplay("Line", new HTuple(param.RowMinLivello, centerColumn - delta, param.RowMinLivello, centerColumn + delta), "cyan", 3)); iconicList.Add(new Utilities.ObjectToDisplay("Min", "cyan", (int)(param.RowMinLivello), (int)(centerColumn + 2 * delta))); return(ret); }
private void Action() { double S1, S2; HTuple RowCheck, ColumnCheck, AngleCheck, Score; HHomMat2D Matrix = new HHomMat2D(); HRegion ModelRegionTrans; HTuple Rect1RowCheck, Rect1ColCheck; HTuple Rect2RowCheck, Rect2ColCheck; HRegion Rectangle1 = new HRegion(); HRegion Rectangle2 = new HRegion(); HMeasure Measure1, Measure2; HTuple RowEdgeFirst1, ColumnEdgeFirst1; HTuple AmplitudeFirst1, RowEdgeSecond1; HTuple ColumnEdgeSecond1, AmplitudeSecond1; HTuple IntraDistance1, InterDistance1; HTuple RowEdgeFirst2, ColumnEdgeFirst2; HTuple AmplitudeFirst2, RowEdgeSecond2; HTuple ColumnEdgeSecond2, AmplitudeSecond2; HTuple IntraDistance2, InterDistance2; HTuple MinDistance; int NumLeads; Img.Dispose(); Img.GrabImage(Framegrabber); Img.DispObj(Window); // Find the IC in the current image. S1 = HSystem.CountSeconds(); ShapeModel.FindShapeModel(Img, 0, new HTuple(360).TupleRad().D, 0.7, 1, 0.5, "least_squares", 4, 0.9, out RowCheck, out ColumnCheck, out AngleCheck, out Score); S2 = HSystem.CountSeconds(); MatchingTimeLabel.Content = "Time: " + String.Format("{0,4:F1}", (S2 - S1) * 1000) + "ms"; MatchingScoreLabel.Content = "Score: "; if (RowCheck.Length == 1) { MatchingScoreLabel.Content = "Score: " + String.Format("{0:F5}", Score.D); // Rotate the model for visualization purposes. Matrix.VectorAngleToRigid(new HTuple(Row), new HTuple(Column), new HTuple(0.0), RowCheck, ColumnCheck, AngleCheck); ModelRegionTrans = ModelRegion.AffineTransRegion(Matrix, "false"); Window.SetColor("green"); Window.SetDraw("fill"); ModelRegionTrans.DispObj(Window); // Compute the parameters of the measurement rectangles. Matrix.AffineTransPixel(Rect1Row, Rect1Col, out Rect1RowCheck, out Rect1ColCheck); Matrix.AffineTransPixel(Rect2Row, Rect2Col, out Rect2RowCheck, out Rect2ColCheck); // For visualization purposes, generate the two rectangles as // regions and display them. Rectangle1.GenRectangle2(Rect1RowCheck.D, Rect1ColCheck.D, RectPhi + AngleCheck.D, RectLength1, RectLength2); Rectangle2.GenRectangle2(Rect2RowCheck.D, Rect2ColCheck.D, RectPhi + AngleCheck.D, RectLength1, RectLength2); Window.SetColor("blue"); Window.SetDraw("margin"); Rectangle1.DispObj(Window); Rectangle2.DispObj(Window); // Do the actual measurements. S1 = HSystem.CountSeconds(); Measure1 = new HMeasure(Rect1RowCheck.D, Rect1ColCheck.D, RectPhi + AngleCheck.D, RectLength1, RectLength2, ImgWidth, ImgHeight, "bilinear"); Measure2 = new HMeasure(Rect2RowCheck.D, Rect2ColCheck.D, RectPhi + AngleCheck.D, RectLength1, RectLength2, ImgWidth, ImgHeight, "bilinear"); Measure1.MeasurePairs(Img, 2, 90, "positive", "all", out RowEdgeFirst1, out ColumnEdgeFirst1, out AmplitudeFirst1, out RowEdgeSecond1, out ColumnEdgeSecond1, out AmplitudeSecond1, out IntraDistance1, out InterDistance1); Measure2.MeasurePairs(Img, 2, 90, "positive", "all", out RowEdgeFirst2, out ColumnEdgeFirst2, out AmplitudeFirst2, out RowEdgeSecond2, out ColumnEdgeSecond2, out AmplitudeSecond2, out IntraDistance2, out InterDistance2); S2 = HSystem.CountSeconds(); MeasureTimeLabel.Content = "Time: " + String.Format("{0,5:F1}", (S2 - S1) * 1000) + "ms"; Window.SetColor("red"); Window.DispLine(RowEdgeFirst1 - RectLength2 * Math.Cos(AngleCheck), ColumnEdgeFirst1 - RectLength2 * Math.Sin(AngleCheck), RowEdgeFirst1 + RectLength2 * Math.Cos(AngleCheck), ColumnEdgeFirst1 + RectLength2 * Math.Sin(AngleCheck)); Window.DispLine(RowEdgeSecond1 - RectLength2 * Math.Cos(AngleCheck), ColumnEdgeSecond1 - RectLength2 * Math.Sin(AngleCheck), RowEdgeSecond1 + RectLength2 * Math.Cos(AngleCheck), ColumnEdgeSecond1 + RectLength2 * Math.Sin(AngleCheck)); Window.DispLine(RowEdgeFirst2 - RectLength2 * Math.Cos(AngleCheck), ColumnEdgeFirst2 - RectLength2 * Math.Sin(AngleCheck), RowEdgeFirst2 + RectLength2 * Math.Cos(AngleCheck), ColumnEdgeFirst2 + RectLength2 * Math.Sin(AngleCheck)); Window.DispLine(RowEdgeSecond2 - RectLength2 * Math.Cos(AngleCheck), ColumnEdgeSecond2 - RectLength2 * Math.Sin(AngleCheck), RowEdgeSecond2 + RectLength2 * Math.Cos(AngleCheck), ColumnEdgeSecond2 + RectLength2 * Math.Sin(AngleCheck)); NumLeads = IntraDistance1.Length + IntraDistance2.Length; MeasureNumLabel.Content = "Number of leads: " + String.Format("{0:D2}", NumLeads); MinDistance = InterDistance1.TupleConcat(InterDistance2).TupleMin(); MeasureDistLabel.Content = "Minimum lead distance: " + String.Format("{0:F3}", MinDistance.D); Window.FlushBuffer(); Measure1.Dispose(); Measure2.Dispose(); } }