/// <summary> /// Test the neighborhood. /// </summary> private void NeighborhoodTest() { HImage image = new HImage("byte", 512, 512); HRegion regionLine = new HRegion(); HRegion regionComplement = new HRegion(); HRegion regionConnected = new HRegion(); HTuple hv_DefWindow = new HTuple(); HWindow HalconWindow1 = HalconWindowControl1.HalconWindow; HWindow HalconWindow2 = HalconWindowControl2.HalconWindow; try { HalconWindow1.SetColored(6); HalconWindow2.SetColored(6); int numObjects; image.GenImageGrayRamp(0, 0, 128, 256, 256, 512, 512); // Display code image.DispImage(HalconWindow1); image.DispImage(HalconWindow2); regionLine.GenRegionLine(100, -1, 150, 512); regionComplement = regionLine.Complement(); HOperatorSet.SetSystem("neighborhood", 4); regionConnected = regionComplement.Connection(); image.DispImage(HalconWindow1); regionConnected.DispRegion(HalconWindow1); numObjects = regionConnected.CountObj(); label1.Text = "Number of objects 4-connected:" + numObjects.ToString(); regionConnected.Dispose(); HOperatorSet.SetSystem("neighborhood", 8); regionConnected = regionComplement.Connection(); image.DispImage(HalconWindow2); regionConnected.DispRegion(HalconWindow2); numObjects = regionConnected.CountObj(); label2.Text = "Number of objects 8-connected:" + numObjects.ToString(); HOperatorSet.CloseWindow(HDevWindowStack.Pop()); } catch (Exception) { throw; } finally { image.Dispose(); regionLine.Dispose(); regionConnected.Dispose(); regionComplement.Dispose(); } }
/// <summary> /// Action for this instance. /// </summary> private void Action() { // Local iconic variables HObject ho_ImageGrayRamp, ho_RegionLines, ho_RegionComplement; HObject ho_ConnectedRegions; // Local control variables HTuple hv_Number = null; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_ImageGrayRamp); HOperatorSet.GenEmptyObj(out ho_RegionLines); HOperatorSet.GenEmptyObj(out ho_RegionComplement); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions); ho_ImageGrayRamp.Dispose(); HOperatorSet.GenImageGrayRamp(out ho_ImageGrayRamp, 0, 0, 128, 256, 256, 512, 512); HImage ImageGrayRamp = new HImage(ho_ImageGrayRamp); // Cast using as operator // Traditional cast ho_RegionLines.Dispose(); HOperatorSet.GenRegionLine(out ho_RegionLines, 100, -1, 150, 512); HRegion RegionLines = new HRegion(ho_RegionLines); ho_RegionComplement.Dispose(); HOperatorSet.Complement(ho_RegionLines, out ho_RegionComplement); HRegion RegionComplement = new HRegion(ho_RegionComplement); HOperatorSet.SetSystem("neighborhood", 4); ho_ConnectedRegions.Dispose(); HOperatorSet.Connection(ho_RegionComplement, out ho_ConnectedRegions); HRegion ConnectedRegions = new HRegion(ho_ConnectedRegions); ImageGrayRamp.DispImage(HalconWindow1); ConnectedRegions.DispRegion(HalconWindow1); RegionLines.DispRegion(HalconWindow1); // Should be two objects HOperatorSet.CountObj(ho_ConnectedRegions, out hv_Number); label1.Text = "Number of objects 4-connected:" + hv_Number.ToString(); HOperatorSet.SetSystem("neighborhood", 8); ConnectedRegions.Dispose(); ConnectedRegions = RegionComplement.Connection(); ImageGrayRamp.DispImage(HalconWindow2); ConnectedRegions.DispRegion(HalconWindow2); RegionLines.DispRegion(HalconWindow2); // Should be a single object hv_Number = ConnectedRegions.CountObj(); label2.Text = "Number of objects 8-connected:" + hv_Number.ToString(); ho_ImageGrayRamp.Dispose(); ho_RegionLines.Dispose(); ho_RegionComplement.Dispose(); ho_ConnectedRegions.Dispose(); }
private void Button_Click_PCBLayout(object sender, RoutedEventArgs e) { WindowControl.HDisplayCurrentObject = new HImage("pcb_layout"); HRegion region = ((HImage)WindowControl.HDisplayCurrentObject).Threshold(0, 50.0); HRegion connected_region = region.Connection(); WindowControl.HDisplayCurrentObject = connected_region; }
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(); } }
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 HRegion Process(HRegion region) { return(region.Connection()); }
// Procedure to find the coins and to read the serialized item from a // file stream private void Action() { HRegion Coin, Circle; HTuple Row, Column, Angle, Score, ResultModel; if (ImgNum == 0) { MatchingLabel.Text = "Deserialize shape models..."; MatchingLabel.Refresh(); //Reading names Stream s = File.OpenRead("serialized_shape_model"); Names = HTuple.Deserialize(s); //Reading shape models for (int i = 0; i < 4; i++) { Models[i] = HShapeModel.Deserialize(s); } s.Close(); MatchingLabel.Text = "Deserialize shape models... OK"; MatchingLabel.Refresh(); } if (ImgNum == 13) { ImgNum = 0; } HTuple Zero; if (ImgNum + 1 < 10) { Zero = "0"; } else { Zero = ""; } //Find shape based model using the read models Img.ReadImage("coins/20cent_" + Zero + (ImgNum + 1).ToString() + ".png"); ImgNum++; HRegion Region = Img.Threshold(70.0, 255.0); HRegion ConnectedRegions = Region.Connection(); HRegion SelectedRegions = ConnectedRegions.SelectShapeStd("max_area", 0); HRegion RegionTrans = SelectedRegions.ShapeTrans("convex"); Coin = new HRegion(RegionTrans.Row, RegionTrans.Column, 120); Circle = new HRegion(Coin.Row, Coin.Column, 35); HImage ImgReduced = Img.ReduceDomain(Circle); ImgReduced.FindShapeModels(Models, 0.0, new HTuple(360.0).TupleRad(), 0.6, 1, 0, "interpolation", 0, 0.9, out Row, out Column, out Angle, out Score, out ResultModel); HXLDCont ModelContours = Models[ResultModel.I].GetShapeModelContours(1); HHomMat2D HomMat2D = new HHomMat2D(); Window.SetColor("green"); HomMat2D.VectorAngleToRigid(0, 0, 0, Row, Column, Angle); HXLDCont ContoursAffineTrans = ModelContours.AffineTransContourXld(HomMat2D); HSystem.SetSystem("flush_graphic", "false"); Window.DispObj(Img); HSystem.SetSystem("flush_graphic", "true"); Window.DispObj(ContoursAffineTrans); MatchingLabel.Text = "#" + (ImgNum + 1) + ": Found: " + Names[ResultModel.I] + " coin"; }
// Training of the shape model and write the serialized item to a file // stream private void CreateBtn_Click(object sender, System.EventArgs e) { HTuple Names; HRegion Coin; HShapeModel Model; TrainBtn.Enabled = false; Window.SetColor("red"); Window.SetDraw("margin"); Window.SetLineWidth(1); Names = "german"; Names.Append("italian"); Names.Append("greek"); Names.Append("spanish"); //Write a serialized item of type tuple to a file Stream s = File.OpenWrite("serialized_shape_model"); Names.Serialize(s); // Note: The binary layout of serialized HALCON objects is determined by // the native HALCON libary. You could also package this blob together with // other .NET objects into a formatted stream, e.g. by using // // BinaryFormatter f = new BinaryFormatter(); // f.Serialize(s, Names); // // When using only HALCON objects without extra formatting, the streamed // data is interchangable with HDevelop or HALCON/C++. //Train shape models for (int i = 0; i < 4; i++) { MatchingLabel.Text = "Train coin " + (i + 1) + "/4 (" + Names[i] + ") and serialize resulting shape model to a" + " file..."; MatchingLabel.Refresh(); Img = new HImage("coins/20cent_" + Names[i]); Window.DispObj(Img); HRegion Region = Img.Threshold(70.0, 255.0); HRegion ConnectedRegions = Region.Connection(); HRegion SelectedRegions = ConnectedRegions.SelectShapeStd("max_area", 0); HRegion RegionTrans = SelectedRegions.ShapeTrans("convex"); Coin = new HRegion(RegionTrans.Row, RegionTrans.Column, 120); int Contrast = 20; HTuple HysteresisContrast; HysteresisContrast = (Contrast / 2.0); HysteresisContrast.Append(Contrast + 6.0); HysteresisContrast.Append(10.0); HImage ImgReduced = Img.ReduceDomain(Coin); //Called during the test phase to see if contrast is selected correctly Model = new HShapeModel(ImgReduced, new HTuple(0), 0.0, new HTuple(360.0).TupleRad(), new HTuple(0), new HTuple("no_pregeneration"), "ignore_local_polarity", HysteresisContrast, new HTuple(5)); //Write a serialized item of type shape based model to a file Model.Serialize(s); // Destroy shape based model and objects Coin.Dispose(); Model.Dispose(); Img.Dispose(); ImgReduced.Dispose(); Region.Dispose(); SelectedRegions.Dispose(); RegionTrans.Dispose(); ImgReduced.Dispose(); HSystem.WaitSeconds(0.4); } s.Close(); MatchingLabel.Text = "Ready to find coins"; StopBtn.Enabled = false; StartBtn.Enabled = true; }