private void DisplayPointPointDistanceGraphics(HWindow windowHandle) { windowHandle.SetColor("orange"); windowHandle.SetDraw("fill"); if (PointLineGraphics == null) { return; } HObject draw = new HObject(); draw.GenEmptyObj(); foreach (var line in PointPointGraphics) { HObject circle1, circle2, lineSeg; HOperatorSet.GenCircle(out circle1, line.YStart, line.XStart, EndPointRadius); HOperatorSet.GenCircle(out circle2, line.YEnd, line.XEnd, EndPointRadius); HOperatorSet.GenRegionLine(out lineSeg, line.YStart, line.XStart, line.YEnd, line.XEnd); draw = HalconHelper.ConcatAll(draw, circle1, circle2, lineSeg); } windowHandle.DispObj(draw); windowHandle.SetDraw("margin"); }
/// <summary> /// Get the depth or the index of instances in a displayed 3D scene. /// Instance represents: Handle of the 3D scene. /// </summary> /// <param name="windowHandle">Window handle.</param> /// <param name="row">Row coordinates.</param> /// <param name="column">Column coordinates.</param> /// <param name="information">Information. Default: "depth"</param> /// <returns>Indices or the depth of the objects at (Row,Column).</returns> public HTuple GetDisplayScene3dInfo( HWindow windowHandle, HTuple row, HTuple column, HTuple information) { IntPtr proc = HalconAPI.PreCall(1204); this.Store(proc, 1); HalconAPI.Store(proc, 0, (HTool)windowHandle); HalconAPI.Store(proc, 2, row); HalconAPI.Store(proc, 3, column); HalconAPI.Store(proc, 4, information); HalconAPI.InitOCT(proc, 0); int err = HalconAPI.CallProcedure(proc); HalconAPI.UnpinTuple(row); HalconAPI.UnpinTuple(column); HalconAPI.UnpinTuple(information); HTuple tuple; int procResult = HTuple.LoadNew(proc, 0, err, out tuple); HalconAPI.PostCall(proc, procResult); GC.KeepAlive((object)this); GC.KeepAlive((object)windowHandle); return(tuple); }
public static bool DispImageFit(this HWindow CurrentHWindow, HObject t_image) { int current_beginRow, current_beginCol, current_endRow, current_endCol; // 获取图像的当前显示部分 if (t_image != null && CurrentHWindow != null) { try { if (t_image.CountObj() == 0) { return(false); } HTuple width, height; HOperatorSet.GetImageSize(t_image, out width, out height); current_beginRow = 0; current_beginCol = 0; HOperatorSet.SetPart(CurrentHWindow, current_beginRow, current_beginCol, height, width); current_endRow = height; current_endCol = width; HOperatorSet.DispObj(t_image, CurrentHWindow);//显示图像 return(true); } catch { return(false); } } return(false); }
/// <summary> /// Paints the active handle of the ROI object into the supplied window /// </summary> /// <param name="window">HALCON window</param> public override void DisplayActive(HWindow window) { switch (ActiveHandleIdx) { case 0: window.DispRectangle2(row1, col1, 0, RoiDrawConfig.PaneWidth, RoiDrawConfig.PaneWidth); break; case 1: window.DispRectangle2(row1, col2, 0, RoiDrawConfig.PaneWidth, RoiDrawConfig.PaneWidth); break; case 2: window.DispRectangle2(row2, col2, 0, RoiDrawConfig.PaneWidth, RoiDrawConfig.PaneWidth); break; case 3: window.DispRectangle2(row2, col1, 0, RoiDrawConfig.PaneWidth, RoiDrawConfig.PaneWidth); break; case 4: window.DispRectangle2(midR, midC, 0, RoiDrawConfig.PaneWidth, RoiDrawConfig.PaneWidth); break; } }
/// <summary> /// 检测 /// </summary> /// <param name="ho_Image"></param> /// <param name="hwin"></param> /// <param name="Key"></param> /// <param name="_dictionary_resulte"></param> /// <returns></returns> public override bool analyze_show(HWindow hwin, string Key, ref Dictionary <string, object> _dictionary_resulte) { bool ok = false; if ((this._diErTiaoZhiXian != null) && (this._diYiTiaoZhiXian != null)) { if ((this._diErTiaoZhiXian.IrectShuJuPianYiOne != null) && (this._diYiTiaoZhiXian.IrectShuJuPianYiTwo != null) && (this._diErTiaoZhiXian.IrectShuJuPianYiOne.Column != null) && (this._diYiTiaoZhiXian.IrectShuJuPianYiTwo.Column != null)) { HTuple IsOverlapping; HOperatorSet.IntersectionLines(this._diYiTiaoZhiXian.IrectShuJuPianYiOne.Row, this._diYiTiaoZhiXian.IrectShuJuPianYiOne.Column, this._diYiTiaoZhiXian.IrectShuJuPianYiTwo.Row, this._diYiTiaoZhiXian.IrectShuJuPianYiTwo.Column, this._diErTiaoZhiXian.IrectShuJuPianYiOne.Row, this._diErTiaoZhiXian.IrectShuJuPianYiOne.Column, this._diErTiaoZhiXian.IrectShuJuPianYiTwo.Row, this._diErTiaoZhiXian.IrectShuJuPianYiTwo.Column, out this.row, out this.column, out IsOverlapping); HOperatorSet.AngleLl(this._diYiTiaoZhiXian.IrectShuJuPianYiOne.Row, this._diYiTiaoZhiXian.IrectShuJuPianYiOne.Column, this._diYiTiaoZhiXian.IrectShuJuPianYiTwo.Row, this._diYiTiaoZhiXian.IrectShuJuPianYiTwo.Column, this._diErTiaoZhiXian.IrectShuJuPianYiOne.Row, this._diErTiaoZhiXian.IrectShuJuPianYiOne.Column, this._diErTiaoZhiXian.IrectShuJuPianYiTwo.Row, this._diErTiaoZhiXian.IrectShuJuPianYiTwo.Column, out angle); this.JiaoDianXLD.Dispose(); HOperatorSet.GenCrossContourXld(out this._jiaoDianXLD, this.Row, this.Column, 6, this.Angle); /*************数据分析********************/ Key = "DistanceTwoLine_" + Key; _result._tolatName = Key; _result._tolatResult = true; _result.Angle = this.Angle; _result.Column = this.Column; _result.Row = this.Row; if (this._ICalibration != null) { this.Cal(this._ICalibration.HomMat2D, ref _result.Row, ref _result.Column); } _dictionary_resulte.Add(Key, this._result); show(hwin); ok = true; } } return(ok); }
public static void FindNeedleLoc(Bitmap bmp, HWindow Window, double[] data, double[] offset) //图像处理代码 { #region //声明控制变量 HTuple hv_Width = null, hv_Height = null; #endregion #region//声明需要用到的图像变量 HObject ho_src_image; #endregion #region//初始化将要用到的图像变量 HOperatorSet.GenEmptyObj(out ho_src_image); #endregion //将bitmap转换成HObject. Bitmap2HObject.Bitmap2HObj(bmp, out ho_src_image); HWindow hWindow = Window; //获取图片尺寸信息 HOperatorSet.GetImageSize(ho_src_image, out hv_Width, out hv_Height); HOperatorSet.SetPart(hWindow, 0, 0, hv_Height - 1, hv_Width - 1); HOperatorSet.DispObj(ho_src_image, hWindow); SetString(Window, "OK", "green", 100); HOperatorSet.DispText(Window, "X方向偏差:" + System.Math.Round(offset[0], 4) + " mm", "window", 12, 7, "black", new HTuple(), new HTuple()); HOperatorSet.DispText(Window, "Y方向偏差:" + System.Math.Round(offset[1], 4) + " mm", "window", 30, 7, "black", new HTuple(), new HTuple()); HOperatorSet.DispText(Window, "圆心X轴坐标:" + System.Math.Round(data[0], 4) + " pix", "window", 48, 7, "black", new HTuple(), new HTuple()); HOperatorSet.DispText(Window, "圆心Y轴坐标:" + System.Math.Round(data[1], 4) + " pix", "window", 66, 7, "black", new HTuple(), new HTuple()); #region//释放图像变量 ho_src_image.Dispose(); #endregion }
/// <summary> /// 显示 /// </summary> /// <returns></returns> public virtual bool show(HWindow hwin) { bool ok = false; ok = true; return(ok); }
/// <summary> /// 显示 分析 /// </summary> /// <param name="data"></param> /// <param name="ho_Image"></param> /// <param name="hwin"></param> /// <param name="Key"></param> /// <param name="_dictionary_resulte"></param> /// <returns></returns> public virtual bool analyze_show(ref Object data, HObject ho_Image, HWindow hwin, string Key, ref Dictionary <string, Object> _dictionary_resulte) { bool ok = false; ok = true; return(ok); }
public DrawRectangle1(HWindow window, HImage image, double row1, double col1, double row2, double col2) : base(window, image) { this.Row1 = row1; this.Col1 = col1; this.Row2 = row2; this.Col2 = col2; }
/// <summary> ///初始化控件image,和_halconWindow,及窗口设置的颜色 /// </summary> /// <returns></returns> public bool init() { bool ok = false; exit_Image = false; this._yunXingBiaoZhi = false; _halconWindow1 = hWindowControl1.HalconWindow; _hWindowControl = hWindowControl1; RepaintingROI += repaintingROI; _halconWindow1.SetColor("green"); _halconWindow1.SetDraw("margin"); HOperatorSet.GenEmptyObj(out _ho_Image); _ho_Image.Dispose(); _roiControl = new ROIControl(); _roiStatus = ROIStatus.NoChioceROI; hWindowControl1.HMouseDown += this.hWindowControl1_HMouseDown; hWindowControl1.HMouseMove += this.hWindowControl1_HMouseMove; hWindowControl1.HMouseWheel += this.hWindowControl1_HMouseWheel; ok = true; return(ok); }
private static int[] GetCorinWindow(HWindow window, HObject image, int r, int c) { int[] cor = new int[2]; double H1, W1; HTuple width, height; try { HOperatorSet.GetImageSize(image, out width, out height); int r1, c1, h1, w1; window.GetWindowExtents(out r1, out c1, out w1, out h1); int h = h1; int w = w1; if (w / h > width.D / height.D) { H1 = h; W1 = (double)h / height * width; } else { H1 = (double)w / width * height; W1 = w; } int delta_r = r - height / 2; int delat_c = c - width / 2; int Delat_R = (int)(delta_r * H1 / height.D); //delta_r * height / (w / width * height) int Delta_C = (int)(delat_c * W1 / width.D); //delat_c * width / w cor[0] = Delat_R + h / 2 >= 0 ? Delat_R + h / 2 : 0; cor[1] = Delta_C + w / 2 >= 0 ? Delta_C + w / 2 : 0; } catch { } return(cor); }
/// <summary> /// 显示 /// </summary> /// <returns></returns> public virtual bool show(ref Object data, HWindow hwin) { bool ok = false; ok = true; return(ok); }
public static void DisplayGraphics(HWindow windowHandle) { HObject lineRegions = new HObject(); lineRegions.GenEmptyObj(); lock (LineToDisplay) { foreach (var line in LineToDisplay) { HObject lineRegion; try { HalconScripts.GenLineRegion(out lineRegion, line.XStart, line.YStart, line.XEnd, line.YEnd, ImageWidth, ImageHeight); } catch (Exception e) { lineRegion = new HObject(); lineRegion.GenEmptyObj(); } HOperatorSet.ConcatObj(lineRegions, lineRegion, out lineRegions); } lineRegions.DispObj(windowHandle); ClearLineGraphics(); } }
public Form1() { InitializeComponent(); my_Algorithm = new magnet_inspect_algorithm(); my_algorithmLib = new AlgorithmLib(); HOperatorSet.GenEmptyObj(out getImage); RectModel = null; hwindow = hWindowControl1.HalconWindow;//初始化窗口变量 this.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.Image_MouseWheel);//鼠标滑轮实现缩放 this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Image_MouseDown); this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Image_MouseUp); button_Roi.Enabled = false; button_Threshold.Enabled = false; button_Input_Set.Enabled = false; button_Do.Enabled = false; buttonStopAcqGrab.Enabled = false; buttonAcqGrab.Enabled = true; trackBar_Threshold.Enabled = false; textBox_Threshold.Enabled = false; FileInfo fileInfo = new FileInfo("InputLocateParam.txt"); if (fileInfo.Exists == false)//如果当前文件不存在则创建文件。 { fileInfo.Create(); } }
//打开图片 public void button_Open_Picture_Click(object sender, EventArgs e) { OpenFileDialog openfile = new OpenFileDialog(); OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "BMP文件|*.bmp*|PNG文件|*.png*|JPEG文件|*.jpg*"; openFileDialog1.RestoreDirectory = true; openFileDialog1.FilterIndex = 1; if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { ImagePath = openFileDialog1.FileName; HObject Image; HOperatorSet.GenEmptyObj(out Image); hwindow = hWindowControl1.HalconWindow; hwindow.ClearWindow(); HOperatorSet.ReadImage(out Image, ImagePath); HOperatorSet.Rgb1ToGray(Image, out getImage); //ImagePath_state = 1; HOperatorSet.GetImageSize(getImage, out ImageWidth, out ImageHeight); HOperatorSet.SetPart(hwindow, 0, 0, ImageHeight - 1, ImageWidth - 1); HOperatorSet.DispObj(getImage, hwindow); button_Roi.Enabled = true; trackBar_Threshold.Enabled = true; button_Threshold.Enabled = true; button_Input_Set.Enabled = true; button_Do.Enabled = true; textBox_Threshold.Enabled = true; } }
/*******************************************************************/ private void activateZoomWindow(int X, int Y) { double posX, posY; int zoomZone; if (ZoomWindow != null) { ZoomWindow.Dispose(); } HOperatorSet.SetSystem("border_width", 10); ZoomWindow = new HWindow(); posX = ((X - ImgCol1) / (ImgCol2 - ImgCol1)) * _viewPort.Width; posY = ((Y - ImgRow1) / (ImgRow2 - ImgRow1)) * _viewPort.Height; zoomZone = (int)((zoomWndSize / 2) * zoomWndFactor * zoomAddOn); ZoomWindow.OpenWindow((int)posY - (zoomWndSize / 2), (int)posX - (zoomWndSize / 2), zoomWndSize, zoomWndSize, _viewPort.HalconID, "visible", ""); ZoomWindow.SetPart(Y - zoomZone, X - zoomZone, Y + zoomZone, X + zoomZone); repaint(ZoomWindow); ZoomWindow.SetColor(this.ZoomWindowColor); }
/// <summary> /// Repaints the HALCON window 'window' /// </summary> private void Repaint(HWindow window) { if (window.Handle == (IntPtr)0) { return; } //线程锁定 lock (LockDisplay) { HObject obj; HSystem.SetSystem("flush_graphic", "false"); window.ClearWindow(); for (int i = 0; i < HObjList.Count; i++) { obj = HObjList[i]; window.DispObj(obj); } //显示十字线 if (IsDisplayCross) { DisplayCross(window); } //显示Region和XLD ShowHObjectList(window); //显示所有ROI的合并区域 if (IsGatherRegionShow) { DisplayGatherRegion(window); } if (RoiController != null && (dispROI == MODE_INCLUDE_ROI)) { RoiController.PaintData(window); } //显示文本 ShowTextList(window); //显示消息 ShowMessageList(window); //显示待生成ROI的预览 if (RoiController.ROI != null && RoiController.RoiDrawConfig.IsDrawPrepare) { window.SetColor(RoiController.ActiveCol); //设置颜色 window.SetLineStyle(new HTuple(new int[] { 2, 2 })); //设置显示为虚线 RoiController.ROI.Draw(window); //显示待生成的ROI } HSystem.SetSystem("flush_graphic", "true"); window.SetColor("black"); window.DispLine(-100.0, -100.0, -101.0, -101.0); } }
private void getposistion(HDrawingObject img, HWindow hwin, string type) { HRegion region = new HRegion(img.GetDrawingObjectIconic()); HOperatorSet.RegionFeatures(region, new HTuple(new string[] { "row", "column", "rect2_len1", "rect2_len2", "phi" }), out HTuple values); arr1 = values.ToDArr(); }
public override void draw(HWindow window, int imageWidth, int imageHeight) { window.SetColor(Color); window.SetDraw(DrawMode); window.SetLineWidth(LineWidth); window.SetLineStyle(LineStyle == "dot" ? new HTuple(2, 2) : new HTuple()); double littleRecSize = getHandleWidth(imageWidth, imageHeight); if (Selected) { window.SetColor("yellow"); } window.DispRectangle1(row1, col1, row2, col2);//body midR = (row1 + row2) / 2; midC = (col1 + col2) / 2; window.SetDraw("fill"); window.DispRectangle2(midR, midC, 0, littleRecSize, littleRecSize);//0,mid if (Selected) { window.SetColor("yellow"); window.DispRectangle2(row1, col1, 0, littleRecSize, littleRecSize); //1,up left window.DispRectangle2(row1, col2, 0, littleRecSize, littleRecSize); //2,up right window.DispRectangle2(row2, col2, 0, littleRecSize, littleRecSize); //3,down right window.DispRectangle2(row2, col1, 0, littleRecSize, littleRecSize); //4,down left window.DispRectangle2(row1, (col1 + col2) / 2, 0, littleRecSize, littleRecSize); //5,up mid window.DispRectangle2((row1 + row2) / 2, col2, 0, littleRecSize, littleRecSize); //6,right mid window.DispRectangle2(row2, (col1 + col2) / 2, 0, littleRecSize, littleRecSize); //7,down mid window.DispRectangle2((row1 + row2) / 2, col1, 0, littleRecSize, littleRecSize); //8,left mid } }
/// <summary> /// 设置halcon窗口布局 /// </summary> /// <param name="hWindow">halcon窗口</param> /// <param name="imageWidth">图像宽度</param> /// <param name="imageHeiht">图像高度</param> private static void SetWindowPart(HWindow hWindow, int imageWidth, int imageHeiht) { int winRow, winCol, winWidth, winHeight, partWidth, partHeight; HOperatorSet.SetSystem("width", imageWidth); HOperatorSet.SetSystem("height", imageHeiht); if ((imageHeiht > 0) && (imageWidth > 0)) { hWindow.GetWindowExtents(out winRow, out winCol, out winWidth, out winHeight); if (winWidth < winHeight) { partWidth = imageWidth; partHeight = imageWidth * winHeight / winWidth; HOperatorSet.SetPart(hWindow, (imageHeiht - partHeight) / 2.0, 0, partHeight - 1 + ((imageHeiht - partHeight) / 2.0), partWidth - 1); } else { partWidth = imageHeiht * winWidth / winHeight; partHeight = imageHeiht; HOperatorSet.SetPart(hWindow, 0, (imageWidth - partWidth) / 2.0, partHeight - 1, partWidth - 1 + ((imageWidth - partWidth) / 2.0)); } } }
public void Display(HWindow windowHandle) { windowHandle.SetColor("magenta"); LineRegions?.DispObj(windowHandle); windowHandle.SetColor("green"); CrossedUsed?.DispObj(windowHandle); windowHandle.SetDraw("margin"); windowHandle.SetColor("orange"); FindLineRects?.DispObj(windowHandle); windowHandle.SetColor("green"); windowHandle.SetDraw("fill"); windowHandle.SetLineWidth(3); Edges?.DispObj(windowHandle); windowHandle.SetLineWidth(1); windowHandle.SetDraw("margin"); windowHandle.SetColor("red"); Line.DisplayGraphics(windowHandle); windowHandle.SetColor("firebrick"); DisplayPointMarkers(windowHandle); }
/// <summary> /// 显示控件边框,默认黄色 /// </summary> private void showBorder(HWindow window) { if (isSelected) { //获取当前显示信息 HTuple hv_Red = null, hv_Green = null, hv_Blue = null; int hv_lineWidth; window.GetRgb(out hv_Red, out hv_Green, out hv_Blue); hv_lineWidth = (int)window.GetLineWidth(); string hv_Draw = window.GetDraw(); window.SetLineWidth(3); //设置线宽 window.SetLineStyle(new HTuple()); window.SetColor("green"); //边框显示颜色 window.DispLine(ImgRow1, ImgCol1, ImgRow2, ImgCol1); window.DispLine(ImgRow1, ImgCol1, ImgRow1, ImgCol2); window.DispLine(ImgRow2, ImgCol1, ImgRow2, ImgCol2); window.DispLine(ImgRow1, ImgCol2, ImgRow2, ImgCol2); //恢复窗口显示信息 window.SetRgb(hv_Red, hv_Green, hv_Blue); window.SetLineWidth(hv_lineWidth); window.SetDraw(hv_Draw); } }
public void DrawRectangleROI(HWindow window, string fileName) { HObject region; HTuple row1 = null, column1 = null, phi = null, Length1 = null, Length2 = null; HOperatorSet.SetColor(window, "red"); HOperatorSet.SetLineWidth(window, 2); HOperatorSet.SetDraw(window, "margin"); HOperatorSet.DrawRectangle2(window, out row1, out column1, out phi, out Length1, out Length2); HOperatorSet.GenRectangle2(out region, row1, column1, phi, Length1, Length2); HOperatorSet.DispObj(region, window); DialogResult Result = MessageBox.Show("是否保存ROI!", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); if (Result == DialogResult.OK) { //if (Directory.Exists(FilePath) == false) // Directory.CreateDirectory(FilePath); HOperatorSet.WriteRegion(region, fileName); MessageBox.Show("已保存文件!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { return; } }
public static void GlueCheck_C(Bitmap bmp, HWindow window, bool ok, double[] distance) { try { HObject image, img_old; HOperatorSet.GenEmptyObj(out image); HOperatorSet.GenEmptyObj(out img_old); img_old.Dispose(); Bitmap2HObject.Bitmap2HObj(bmp, out img_old); HTuple htuple; HOperatorSet.CountChannels(img_old, out htuple); image.Dispose(); if (htuple == 3) { HOperatorSet.Rgb1ToGray(img_old, out image); } else { image = img_old.Clone(); } HTuple width, height; HOperatorSet.GetImageSize(image, out width, out height); HOperatorSet.SetPart(window, 0, 0, height - 1, width - 1); HOperatorSet.DispObj(image, window); HOperatorSet.SetLineWidth(window, 3); HOperatorSet.SetColor(window, "red"); HOperatorSet.DispText(window, "胶水外边缘最大偏差:" + System.Math.Round(distance[0], 4) + " pix", "window", 30, 7, "black", new HTuple(), new HTuple()); HOperatorSet.DispText(window, "胶水内边缘最大偏差:" + System.Math.Round(distance[1], 4) + " pix", "window", 60, 7, "black", new HTuple(), new HTuple()); HOperatorSet.DispText(window, "胶水外边缘阈值:" + System.Math.Round(Position.Instance.OutsideDistance, 4) + " pix", "window", 90, 7, "black", new HTuple(), new HTuple()); HOperatorSet.DispText(window, "胶水内边缘阈值:" + System.Math.Round(Position.Instance.insideDistance, 4) + " pix", "window", 120, 7, "black", new HTuple(), new HTuple()); HOperatorSet.DispText(window, "胶圈X轴偏移:" + System.Math.Round(distance[2], 4) + " mm", "window", 150, 7, "black", new HTuple(), new HTuple()); HOperatorSet.DispText(window, "胶圈Y轴偏移:" + System.Math.Round(distance[3], 4) + "mm", "window", 180, 7, "black", new HTuple(), new HTuple()); if (ok) { //LastCenterLocateBMP = bmp; SetString(window, "OK", "green", 100); } else { SetString(window, "NG", "red", 100); } } catch { try { SetString(window, "NG", "red", 100); } catch { } Marking.CenterLocateTestSucceed = false; } }
private void WindowControl_Load(object sender, EventArgs e) { string ImgType; Window = WindowControl.HalconWindow; Framegrabber = new HFramegrabber("File", 1, 1, 0, 0, 0, 0, "default", -1, "default", -1, "default", "board/board.seq", "default", 1, -1); Img = Framegrabber.GrabImage(); Img.GetImagePointer1(out ImgType, out ImgWidth, out ImgHeight); Window.SetPart(0, 0, ImgHeight - 1, ImgWidth - 1); Img.DispObj(Window); Window.SetDraw("margin"); Window.SetLineWidth(3); Rectangle = new HRegion(188.0, 182, 298, 412); Rectangle.AreaCenter(out Row, out Column); Rect1Row = Row - 102; Rect1Col = Column + 5; Rect2Row = Row + 107; Rect2Col = Column + 5; RectPhi = 0; RectLength1 = 170; RectLength2 = 5; this.MouseWheel += my_MouseWheel; }
public void DrawPoint(out double x, out double y) { viewPort.Focus(); #if NativeCode Wrapper.ShowUnit.ShowText(viewPort.HalconWindow, "鼠标左键点击点位置,鼠标右键完成。", 20, 20, 20, "green", "window"); #else HWndMessage message = new HWndMessage("鼠标左键点击点位置,鼠标右键完成", 20, 20, 20, "green"); message.DispMessage(viewPort.HalconWindow, "window", 1); #endif inMeasureLine = true; viewPort.ContextMenuStrip = null; //获取当前显示信息 HTuple hv_Red = null, hv_Green = null, hv_Blue = null; int hv_lineWidth; HWindow window = viewPort.HalconWindow; window.GetRgb(out hv_Red, out hv_Green, out hv_Blue); hv_lineWidth = (int)window.GetLineWidth(); string hv_Draw = window.GetDraw(); window.SetLineWidth(1); //设置线宽 window.SetLineStyle(new HTuple()); window.SetColor("green"); //画点的颜色 window.DrawPoint(out y, out x); //恢复窗口显示信息 window.SetRgb(hv_Red, hv_Green, hv_Blue); window.SetLineWidth(hv_lineWidth); window.SetDraw(hv_Draw); viewPort.ContextMenuStrip = hv_MenuStrip; inMeasureLine = false; Repaint(); }
/// <summary> /// 重绘halcon窗口 /// </summary> private void repaint(HWindow window) { if (window.IsInitialized() == false) { return; } double scale1 = (1.0) / ZoomWndFactor; #if NativeCode Wrapper.ShowUnit.Refresh(window, ResultShow == ResultShow.原图 ? true : false, isShowMessage, scale1); #else ShowObject(window); #endif int scale = (int)((double)(viewPort.Width) * ZoomWndFactor); if (roiManager != null && (showMode == ShowMode.IncludeROI)) { roiManager.PaintData(window, scale, scale1); } //显示十字架等 #if NativeCode Wrapper.ShowUnit.ShowHat(window, isShowCross); #else ShowHat(window); #endif }
/// <summary> /// Triggers the image processing thread /// </summary> private void DefineShapeBasedModel(HDrawingObject drawid, HWindow window, string type) { if (buttonRun.Enabled) { createsbm.AddMessage(dummy_msg); } }
/// <summary> /// 创建圆形模板 /// </summary> /// <param name="window">绘图窗口</param> /// <param name="image">输入图像</param> public void DrawCircle(HWindow window, HObject image) { HObject ho_Circle, ho_targetROI, ho_coutours; HTuple hv_Row2 = null, hv_Column3 = null, hv_Radius3 = null, hv_ModelID = null;; HOperatorSet.GenEmptyObj(out ho_Circle); HOperatorSet.GenEmptyObj(out ho_targetROI); HOperatorSet.GenEmptyObj(out ho_coutours); HOperatorSet.SetColor(window, "red"); HOperatorSet.SetLineWidth(window, 2); HOperatorSet.SetDraw(window, "margin"); HOperatorSet.DrawCircle(window, out hv_Row2, out hv_Column3, out hv_Radius3); ho_Circle.Dispose(); HOperatorSet.GenCircle(out ho_Circle, hv_Row2, hv_Column3, hv_Radius3); ho_targetROI.Dispose(); HOperatorSet.DispObj(ho_Circle, window); HTuple hv_Area, hv_Row4, hv_Column4; HTuple hv_HomMat2DIdentity, hv_HomMat2DTranslate; HObject ho_ContoursAffinTrans, ho_Cross; HOperatorSet.GenEmptyObj(out ho_ContoursAffinTrans); HOperatorSet.GenEmptyObj(out ho_Cross); DialogResult Result = MessageBox.Show("是否保存模板!", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); if (Result == DialogResult.OK) { HOperatorSet.ReduceDomain(image, ho_Circle, out ho_targetROI); HOperatorSet.AreaCenter(ho_targetROI, out hv_Area, out hv_Row4, out hv_Column4); HOperatorSet.SetSystem("border_shape_models", "false"); //HOperatorSet.CreateShapeModel(ho_targetROI, 4, -0.39, 0.79, "auto", "auto", "ignore_color_polarity", // "auto", "auto", out hv_ModelID); HOperatorSet.CreateShapeModel(ho_targetROI, 4, -0.39, 0.79, "auto", "point_reduction_high", "ignore_global_polarity", "auto", "auto", out hv_ModelID); //if (!Directory.Exists(_filePath)) //{ Directory.CreateDirectory(_filePath); } HOperatorSet.WriteShapeModel(hv_ModelID, /*_filePath + "//" + */ _fileName); ho_coutours.Dispose(); HOperatorSet.GetShapeModelContours(out ho_coutours, hv_ModelID, 1); HOperatorSet.HomMat2dIdentity(out hv_HomMat2DIdentity); HOperatorSet.HomMat2dTranslate(hv_HomMat2DIdentity, hv_Row4, hv_Column4, out hv_HomMat2DTranslate); ho_ContoursAffinTrans.Dispose(); HOperatorSet.AffineTransContourXld(ho_coutours, out ho_ContoursAffinTrans, hv_HomMat2DTranslate); HOperatorSet.DispObj(image, window); HOperatorSet.DispObj(ho_ContoursAffinTrans, window); HOperatorSet.SetColor(window, "green"); HOperatorSet.GenCrossContourXld(out ho_Cross, hv_Row4, hv_Column4, 180, 0.785398); HOperatorSet.DispObj(ho_Cross, window); MessageBox.Show("已保存模板", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { HOperatorSet.DispObj(image, window); return; } }
public void SobelFilter(HDrawingObject dobj, HWindow hwin, string type) { try { HObject region = null; if (dobj.GetDrawingObjectParams("type") == "line") { HTuple a = dobj.GetDrawingObjectParams("row1"); HOperatorSet.GenRegionLine(out region, dobj.GetDrawingObjectParams("row1"), dobj.GetDrawingObjectParams("column1"), dobj.GetDrawingObjectParams("row2"), dobj.GetDrawingObjectParams("column2")); } else { region = new HRegion(dobj.GetDrawingObjectIconic()); } AddToStack(region); DisplayResults(); } catch (HalconException hex) { MessageBox.Show(hex.GetErrorMessage(), "HALCON error", MessageBoxButtons.OK); } }
//相機選取檔案(Image) ----------- public void ImageFomCamera(HWindow Window) { DisposeImage(); HOperatorSet.GenEmptyObj(out Image); HOperatorSet.GrabImageStart(uEye, -1); HOperatorSet.GrabImageAsync(out Image, uEye, -1); HOperatorSet.GetImageSize(Image, out ImageWidth, out ImageHeight); if (Image != null) { HOperatorSet.SetSystem("width", ImageWidth); HOperatorSet.SetSystem("height", ImageHeight); HOperatorSet.SetPart(Window, 0, 0, ImageHeight - 1, ImageWidth - 1); HOperatorSet.ClearWindow(Window); HOperatorSet.DispObj(Image, Window); } }
//檔案位置讀取圖片(Image)--------------------------- public void ImageFomFile(HWindow Window) { OpenFileDialog OpenFileDialo_2 = new OpenFileDialog(); if (OpenFileDialo_2.ShowDialog() == System.Windows.Forms.DialogResult.OK) { DisposeImage(); HOperatorSet.GenEmptyObj(out Image); HOperatorSet.ReadImage(out Image, OpenFileDialo_2.FileName); HOperatorSet.GetImageSize(Image, out ImageWidth, out ImageHeight); } if (Image != null) { HOperatorSet.SetSystem("width", ImageWidth); HOperatorSet.SetSystem("height", ImageHeight); HOperatorSet.SetPart(Window, 0, 0, ImageHeight - 1, ImageWidth - 1); HOperatorSet.ClearWindow(Window); HOperatorSet.DispObj(Image, Window); OpenFileDialog = OpenFileDialo_2; } OpenFileDialo_2.Dispose(); }
/// <summary> /// Repaints the HALCON window 'window' using BufferWindow /// </summary> public void repaintUsingBufferWindow(HalconDotNet.HWindow window) { int count = HObjList.Count; HObjectEntry entry; // modified if ((windowWidth > 0) && (windowHeight > 0)) { if (bufferWindow == null) { bufferWindow = new HWindow(0, 0, windowWidth, windowHeight, "root", "buffer", ""); // check the boundatries of the image part if ((ImgRow2 > ImgRow1) && (ImgCol2 > ImgCol1)) bufferWindow.SetPart((int)ImgRow1, (int)ImgCol1, (int)ImgRow2, (int)ImgCol2); // set the image part to window size else bufferWindow.SetPart(0, 0, windowHeight - 1, windowWidth - 1); } else bufferWindow.ClearWindow(); mGC.stateOfSettings.Clear(); // display the graphic stack on the buffer window for (int i = 0; i < count; i++) { entry = ((HObjectEntry)HObjList[i]); if (entry != null) { mGC.applyContext(window, entry.gContext); mGC.applyContext(bufferWindow, entry.gContext); bufferWindow.DispObj(entry.HObj); } } addInfoDelegate(); // if any rois exists add this to buffer window if (roiManager != null && (dispROI == MODE_INCLUDE_ROI)) roiManager.paintData(bufferWindow); // copy the content of buffer window to HWindow bufferWindow.CopyRectangle(window, 0, 0, windowHeight - 1, windowWidth - 1, 0, 0); // dispose the buffer window bufferWindow.Dispose(); bufferWindow = null; } }
/// <summary> /// Initializes the image dimension, mouse delegation, and the /// graphical context setup of the instance. /// </summary> /// <param name="view"> HALCON window </param> public HWndCtrl(HWindowControl view) { viewPort = view; stateView = MODE_VIEW_NONE; windowWidth = viewPort.Size.Width; windowHeight = viewPort.Size.Height; // initialize the image part to window size initializeDisplayImagePart(); resetImagePart(widthImagePart, heightImagePart); if (widthImagePart > 0) zoomWndFactor = (double)widthImagePart / viewPort.Width; else zoomWndFactor = 1; zoomAddOn = Math.Pow(0.9, 5); zoomWndSize = 150; /*Set the boundaries and steps for changes for the GUI elements*/ /*default*/ CompRangeX = new int[] { 0, 100 }; CompRangeY = new int[] { 0, 100 }; prevCompX = prevCompY = 0; initValX = initValY = 0; /* Initialize the values for value range, step for * some GUI elements */ setGUICompRangeX(CompRangeX, prevCompX); setGUICompRangeY(CompRangeY, prevCompY); //displayMode = MODE_VIEW_NONE; dispROI = MODE_INCLUDE_ROI;//1; viewPort.HMouseUp += new HalconDotNet.HMouseEventHandler(this.mouseUp); viewPort.HMouseDown += new HalconDotNet.HMouseEventHandler(this.mouseDown); viewPort.HMouseMove += new HalconDotNet.HMouseEventHandler(this.mouseMoved); viewPort.HMouseWheel += new HalconDotNet.HMouseEventHandler(this.mouseWheel); addInfoDelegate = new FuncDelegate(dummyV); NotifyIconObserver = new IconicDelegate(dummy); // graphical stack HObjList = new ArrayList(20); mGC = new GraphicsContext(); mGC.gcNotification = new GCDelegate(exceptionGC); // set the variable bufferWindow to null for repaint-Method bufferWindow = null; }
/*******************************************************************/ /*******************************************************************/ private void activateZoomWindow(int X, int Y) { double posX, posY; int zoomZone; if (ZoomWindow != null) ZoomWindow.Dispose(); HOperatorSet.SetSystem("border_width", 10); ZoomWindow = new HWindow(); posX = ((X - ImgCol1) / (ImgCol2 - ImgCol1)) * viewPort.Width; posY = ((Y - ImgRow1) / (ImgRow2 - ImgRow1)) * viewPort.Height; zoomZone = (int)((zoomWndSize / 2) * zoomWndFactor * zoomAddOn); ZoomWindow.OpenWindow((int)posY - (zoomWndSize / 2), (int)posX - (zoomWndSize / 2), zoomWndSize, zoomWndSize, viewPort.HalconID, "visible", ""); ZoomWindow.SetPart(Y - zoomZone, X - zoomZone, Y + zoomZone, X + zoomZone); repaintUsingFlushGraphic(ZoomWindow); ZoomWindow.SetColor("black"); }
//將圖片顯示在指定的halcon視窗上 輸入變數:HTuple Window(視窗)--- public void ShowImage(HWindow Window) { if (Image != null) { HOperatorSet.SetSystem("width", ImageWidth); HOperatorSet.SetSystem("height", ImageHeight); HOperatorSet.SetPart(Window, 0, 0, ImageHeight - 1, ImageWidth - 1); HOperatorSet.ClearWindow(Window); HOperatorSet.DispObj(Image, Window); } }
/// <summary>Applies graphical context to the HALCON window</summary> /// <param name="window">Active HALCON window</param> /// <param name="cContext"> /// List that contains graphical modes for window /// </param> public void applyContext(HWindow window, Hashtable cContext) { string key = ""; string valS = ""; int valI = -1; HTuple valH = null; iterator = cContext.Keys.GetEnumerator(); try { while (iterator.MoveNext()) { key = (string)iterator.Current; if (stateOfSettings.Contains(key) && stateOfSettings[key] == cContext[key]) continue; switch (key) { case GC_COLOR: valS = (string)cContext[key]; window.SetColor(valS); if (stateOfSettings.Contains(GC_COLORED)) stateOfSettings.Remove(GC_COLORED); break; case GC_COLORED: valI = (int)cContext[key]; window.SetColored(valI); if (stateOfSettings.Contains(GC_COLOR)) stateOfSettings.Remove(GC_COLOR); break; case GC_DRAWMODE: valS = (string)cContext[key]; window.SetDraw(valS); break; case GC_LINEWIDTH: valI = (int)cContext[key]; window.SetLineWidth(valI); break; case GC_LUT: valS = (string)cContext[key]; window.SetLut(valS); break; case GC_PAINT: valS = (string)cContext[key]; window.SetPaint(valS); break; case GC_SHAPE: valS = (string)cContext[key]; window.SetShape(valS); break; case GC_LINESTYLE: valH = (HTuple)cContext[key]; window.SetLineStyle(valH); break; default: break; } if (valI != -1) { if (stateOfSettings.Contains(key)) stateOfSettings[key] = valI; else stateOfSettings.Add(key, valI); valI = -1; } else if (valS != "") { if (stateOfSettings.Contains(key)) stateOfSettings[key] = valI; else stateOfSettings.Add(key, valI); valS = ""; } else if (valH != null) { if (stateOfSettings.Contains(key)) stateOfSettings[key] = valI; else stateOfSettings.Add(key, valI); valH = null; } }//while } catch (HOperatorException e) { gcNotification(e.Message); return; } }