/// <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(); }
/// <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(); } }
public void CreateModel(HImage Img, HWindow window, HWindow window1, double row1, double col1, double row2, double col2, double rowMark, double colMark) { HRegion rectangle = new HRegion(row1, col1, row2, col2); rectangle.AreaCenter(out rowOrg, out colOrg); HImage ImgReduced = new HImage(); //HRegion Rectangle1 = new HRegion(); //HRegion Rectangle2 = new HRegion(); //Window.SetColor("red"); //Window.SetDraw("margin"); //Window.SetLineWidth(3); //Rectangle.GenRectangle1(188.0, 182, 298, 412); ImgReduced = Img.ReduceDomain(rectangle); ImgReduced.InspectShapeModel(out ModelRegion, 1, 30); //Rectangle1.GenRectangle2(Rect1Row, Rect1Col, RectPhi, RectLength1, RectLength2); //Rectangle2.GenRectangle2(Rect2Row, Rect2Col, RectPhi, RectLength1, RectLength2); ShapeModel = new HShapeModel(ImgReduced, 4, 0, 0, 0, "none", "use_polarity", 30, 10); ShapeModel.SetShapeModelOrigin(rowMark - rowOrg, colMark - colOrg); //window.SetColor("green"); //window.SetDraw("margin"); window.SetPart((int)row1, (int)col1, (int)row2, (int)col2); ModelRegion.DispObj(window); window1.SetPart((int)row1, (int)col1, (int)row2, (int)col2); ImgReduced.DispImage(window1); //Window.SetColor("blue"); //Window.SetDraw("margin"); //Rectangle1.DispObj(Window); //Rectangle2.DispObj(Window); }