public override bool Execute() { this.ROI.error = false; if (this.CurrentImage == null || this.ROI == null) { return(false); } try { HImage reduceImage = this.CurrentImage.ReduceDomain(this.ROI.getRegion()); HRegion region = reduceImage.Threshold((double)this.MinThreshold, this.MaxThreshold); HRegion connectRegion = region.Connection(); HRegion selectRegionTmp, selectRegion; selectRegion = new HRegion(); selectRegion.GenEmptyRegion(); for (int i = 1; i < connectRegion.CountObj(); i++) { selectRegionTmp = connectRegion[i].SelectShape("area", "and", (double)this.MinArea, (double)this.MaxArea); if (selectRegionTmp != null) { selectRegion = selectRegion.ConcatObj(selectRegionTmp); } } HRegion fillRegion; if (this.Filled) { fillRegion = selectRegion.FillUp(); } else { fillRegion = selectRegion; } if (fillRegion.CountObj() > 0) { double areaMax = fillRegion.Area.TupleMax(); int areaIndex = fillRegion.Area.TupleFind(areaMax); this.ResultRegion = fillRegion[areaIndex + 1]; } else { this.ResultRegion = fillRegion; } if (this.ResultRegion.Area > 0) { return(true); } else { this.ROI.error = true; return(false); } } catch { this.ROI.error = true; return(false); } }
/// <summary> /// Example test method 1 for the Process call. /// </summary> private void FindBoundary() { HRegion lighter = new HRegion(); HRegion filledRgn = new HRegion(); HRegion closedRgn = new HRegion(); HRegion connectedRgn = new HRegion(); HRegion largestRegion = new HRegion(); HRegion circleRegion = new HRegion(); try { this.WaferRegion.Dispose(); lighter = this.Image.Threshold(5.0, 255.0); filledRgn = lighter.FillUp(); closedRgn = filledRgn.ClosingCircle(5.0); connectedRgn = closedRgn.Connection(); largestRegion = connectedRgn.SelectShapeStd("max_area", 0.0); // System.Threading.Thread.Sleep(2000); // Does not block. this.processedRegion = largestRegion.ShapeTrans("convex"); // Debug display test. // Uncomment and block processed display in View to see the debug output. // Comment out call to CircularizeAndShrinkRegion for a persistent display. ////this.DebugDisplay.Dispose(); ////DisplayCollection tempDC = new DisplayCollection(); ////tempDC.AddDisplayObject(lighter.CopyObj(1, -1), "red"); ////this.DebugDisplay = tempDC; } catch (HalconException) { this.ErrorMessage = "Halcon Exception in FindBoundary"; this.ErrorCode = ProcessingErrorCode.ProcessingStep1Error; } catch (Exception) { this.ErrorMessage = "System Exception in FindBoundary"; this.ErrorCode = ProcessingErrorCode.ProcessingStep1Error; } finally { lighter.Dispose(); filledRgn.Dispose(); closedRgn.Dispose(); connectedRgn.Dispose(); largestRegion.Dispose(); circleRegion.Dispose(); } }
public HRegion Process(HRegion region) { return(region.FillUp()); }
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); }
public void ProcessStep(HImage imageGrabbed) { try { HTuple startTime = null; HTuple endTime = null; HOperatorSet.CountSeconds(out startTime); int imageChannels = imageGrabbed.CountChannels(); HImage rImg = null, gImg = null, bImg = null, rgbImg = null, irImg = null; if (imageChannels == 6) { rImg = imageGrabbed.Decompose4(out gImg, out bImg, out irImg); rgbImg = rImg.Compose3(gImg, bImg); } HImage grayImage = new HImage(); if (rgbImg == null) { return; } imageGrabbed = rgbImg.CopyImage(); int presentImageNoOfChannels = imageGrabbed.CountChannels(); if (presentImageNoOfChannels == Properties.Settings.Default.NumberOfChannelsInColorImage) { lock (this.ImageToBeSaved) { this.ImageToBeSaved = imageGrabbed.CopyImage(); } if (whitebalancingStarted) { wbIterationCompletedEvent.Reset(); } imageGrabbedEvent.Set(); HImage ho_R; HImage ho_G; HImage ho_B; ho_R = imageGrabbed.Decompose3(out ho_G, out ho_B); HTuple RAOIGrayVals = Globals.GetGrayValuesOfLine(ho_R); HTuple GAOIGrayVals = Globals.GetGrayValuesOfLine(ho_G); HTuple BAOIGrayVals = Globals.GetGrayValuesOfLine(ho_B); HTuple IRAOIGrayVals = Globals.GetGrayValuesOfLine(irImg); currentWBData.Image = imageGrabbed; currentWBData.GrayValues.RPlaneVals = RAOIGrayVals.ToDArr(); currentWBData.GrayValues.GPlaneVals = GAOIGrayVals.ToDArr(); currentWBData.GrayValues.BPlaneVals = BAOIGrayVals.ToDArr(); currentWBData.GrayValues.MPlaneVals = IRAOIGrayVals.ToDArr(); HImage ho_GrayImage; ho_GrayImage = imageGrabbed.Rgb1ToGray(); HRegion whiteRegion = ho_GrayImage.Threshold( Properties.Settings.Default.MinThresholdInDeterminingGain, Properties.Settings.Default.MaxThresholdInDeterminingGain); whiteRegion = whiteRegion.FillUp(); whiteRegion = whiteRegion.ErosionRectangle1(20, 20); double rClipValue = 15.0; double gClipValue = 15.0; double bClipValue = 15.0; //CalculateOptimumClipValue(whiteRegion, ho_R, out rClipValue); //CalculateOptimumClipValue(whiteRegion, ho_G, out gClipValue); //CalculateOptimumClipValue(whiteRegion, ho_B, out bClipValue); double rMin, rMax, rRange; double gMin, gMax, gRange; double bMin, bMax, bRange; ho_R.MinMaxGray(whiteRegion, rClipValue, out rMin, out rMax, out rRange); ho_G.MinMaxGray(whiteRegion, gClipValue, out gMin, out gMax, out gRange); ho_B.MinMaxGray(whiteRegion, bClipValue, out bMin, out bMax, out bRange); double RGDiff = rMax - gMax; double GBDiff = gMax - bMax; double BRDiff = bMax - rMax; currentWBData.ErrorLevel = (Math.Max(RGDiff, Math.Max(GBDiff, BRDiff)) / this.CameraAcquisition.CurrentCameraProperties.BrightRegionReferenceGrayLevel) * 100; currentWBData.RedMax = rMax; currentWBData.GreenMax = gMax; currentWBData.BlueMax = bMax; HOperatorSet.CountSeconds(out endTime); currentWBData.TimeTaken = (endTime - startTime).D; UpdateControlUI(); wbIterationCompletedEvent.WaitOne(); } } catch (Exception ex) { string errorMessage = "Exception occurred during white balancing step. "; errorMessage = errorMessage + " Error Message: " + ex.Message; MessageBox.Show(errorMessage, "Camera Setup Tool", MessageBoxButtons.OK, MessageBoxIcon.Error); } }