public void ShowImage(IJFImage img) { if (null == img) { _hcWnd.ClearWindow(); return; //img.DisplayTo(pPicWnd); } else { if (null != _hoImge) { _hoImge.Dispose(); _hoImge = null; } object oImg = null; int err = img.GenHalcon(out oImg); if (0 != err) { _hcWnd.ClearWindow(); ShowTips("图像显示失败!错误信息:" + img.GetErrorInfo(err)); } _hoImge = oImg as HObject; HOperatorSet.SetPart(_hcWnd, 0, 0, img.PicHeight - 1, img.PicWidth - 1);// ch: 使图像显示适应窗口大小 || en: Make the image adapt the window size HOperatorSet.DispObj(_hoImge, _hcWnd); ShowTips("图像已显示"); } }
/// <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); } }
//打开图片 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 RefreshHalconScreen() { if (_windowHandle == null) { return; } _windowHandle.ClearWindow(); var result2D = SocketToDisplay == CavityType.Cavity1 ? LeftGraphics : RightGraphics; UpdateImageIndexList(result2D); if (result2D.Images == null || result2D.Images.Count == 0) { return; } var image = result2D.Images[ImageIndexToDisplay]; var graphics = result2D.Graphics; _windowHandle.DispImage(image); if (graphics != null) { _windowHandle.DispObj(graphics); } var errorMessage = result2D.ErrorMessage; if (errorMessage != null) { _windowHandle.DispText(errorMessage, "window", 20, 20, "red", "border_radius", 2); } }
public void repaint() { HSystem.SetSystem("flush_graphic", "false"); HWindow.ClearWindow(); try { if (image != null) { HWindow.DispObj(image); } HWindow.SetDraw("margin"); HWindow.SetColor("red"); if (region != null) { HWindow.DispObj(region); } } catch (System.Exception ex) { } finally { HSystem.SetSystem("flush_graphic", "true"); //注释了下面语句,会导致窗口无法实现缩放和拖动 HWindow.SetColor("dim gray"); HWindow.DispLine(-100.0, -100.0, -101.0, -101.0); } }
void clearDisplay() { hWindow.ClearWindow(); graphicsQueue.Clear(); hImage.GetImageSize(out width, out height); hWindow.SetPart(0, 0, height, width); hWindow.DispImage(hImage); }
/// <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; } }
/*******************************************************************/ private void mouseMoved(object sender, hvppleDotNet.HMouseEventArgs e) { //关闭缩放事件 if (isStaticWnd) { return; } double motionX = 0, motionY = 0; double posX, posY; double zoomZone; if (!mousePressed) { return; } if (roiManager != null && (roiManager.activeROIidx != -1) && (dispROI == MODE_INCLUDE_ROI)) { roiManager.mouseMoveAction(e.X, e.Y); } else if (stateView == MODE_VIEW_MOVE) { if (imageShowWidth > viewPort.Width || imageShowHeight > viewPort.Height) { motionX = ((e.X - startX)); motionY = ((e.Y - startY)); if (((int)motionX != 0) || ((int)motionY != 0)) { moveImage(motionX, motionY); startX = e.X - motionX; startY = e.Y - motionY; } } } else if (stateView == MODE_VIEW_ZOOMWINDOW) { HSystem.SetSystem("flush_graphic", "false"); ZoomWindow.ClearWindow(); posX = ((e.X - ImgCol1) / (ImgCol2 - ImgCol1)) * viewPort.Width; posY = ((e.Y - ImgRow1) / (ImgRow2 - ImgRow1)) * viewPort.Height; zoomZone = (zoomWndSize / 2) * xScale * zoomAddOn; ZoomWindow.SetWindowExtents((int)posY - (zoomWndSize / 2), (int)posX - (zoomWndSize / 2), zoomWndSize, zoomWndSize); ZoomWindow.SetPart((int)(e.Y - zoomZone), (int)(e.X - zoomZone), (int)(e.Y + zoomZone), (int)(e.X + zoomZone)); repaint(ZoomWindow); HSystem.SetSystem("flush_graphic", "true"); ZoomWindow.DispLine(-100.0, -100.0, -100.0, -100.0); } }
private void MouseMove(object sender, HMouseEventArgsWPF e) { double motionX, motionY; double posX, posY; double zoomZone; //判断是否有待生成的ROI if (RoiController.ROI != null) { RoiController.ROI.CreateROI(e.Column, e.Row); //设置ROI位置 Repaint(); //刷新显示 } if (!mousePressed) { return; } if (RoiController != null && (RoiController.ActiveROIidx != -1) && (dispROI == MODE_INCLUDE_ROI)) { RoiController.MouseMoveAction(e.Column, e.Row); } else if (stateView == MODE_VIEW_MOVE) { motionX = ((e.Column - startX)); motionY = ((e.Row - startY)); if (((int)motionX != 0) || ((int)motionY != 0)) { MoveImage(motionX, motionY); startX = e.Column - motionX; startY = e.Row - motionY; } } else if (stateView == MODE_VIEW_ZOOMWINDOW) { HSystem.SetSystem("flush_graphic", "false"); ZoomWindow.ClearWindow(); posX = ((e.Column - ImgX) / (ImgWidth - ImgX)) * viewPort.ActualWidth; posY = ((e.Row - ImgY) / (ImgHeight - ImgY)) * viewPort.ActualHeight; zoomZone = (zoomWndSize / 2) * zoomWndFactor * zoomAddOn; ZoomWindow.SetWindowExtents((int)posY - (zoomWndSize / 2), (int)posX - (zoomWndSize / 2), zoomWndSize, zoomWndSize); ZoomWindow.SetPart((int)(e.Row - zoomZone), (int)(e.Column - zoomZone), (int)(e.Row + zoomZone), (int)(e.Column + zoomZone)); Repaint(ZoomWindow); HSystem.SetSystem("flush_graphic", "true"); ZoomWindow.DispLine(-100.0, -100.0, -100.0, -100.0); } }
/*******************************************************************/ private void mouseMoved(object sender, HalconDotNet.HMouseEventArgs e) { double motionX, motionY; double posX, posY; double zoomZone; PointF point = new PointF((float)e.X, (float)e.Y); if (m_event_disCurrentPoint != null) { m_event_disCurrentPoint(point); } if (!mousePressed) { return; } if (roiManager != null && (roiManager.activeROIidx != -1) && (dispROI == MODE_INCLUDE_ROI)) { roiManager.mouseMoveAction(e.X, e.Y); } else if (stateView == MODE_VIEW_MOVE) { motionX = ((e.X - startX)); motionY = ((e.Y - startY)); if (((int)motionX != 0) || ((int)motionY != 0)) { moveImage(motionX, motionY); startX = e.X - motionX; startY = e.Y - motionY; } } else if (stateView == MODE_VIEW_ZOOMWINDOW) { HSystem.SetSystem("flush_graphic", "false"); ZoomWindow.ClearWindow(); posX = ((e.X - ImgCol1) / (ImgCol2 - ImgCol1)) * viewPort.Width; posY = ((e.Y - ImgRow1) / (ImgRow2 - ImgRow1)) * viewPort.Height; zoomZone = (zoomWndSize / 2) * zoomWndFactor * zoomAddOn; ZoomWindow.SetWindowExtents((int)posY - (zoomWndSize / 2), (int)posX - (zoomWndSize / 2), zoomWndSize, zoomWndSize); ZoomWindow.SetPart((int)(e.Y - zoomZone), (int)(e.X - zoomZone), (int)(e.Y + zoomZone), (int)(e.X + zoomZone)); repaint(ZoomWindow); HSystem.SetSystem("flush_graphic", "true"); ZoomWindow.DispLine(-100.0, -100.0, -100.0, -100.0); } }
private void ViewPort_HMouseMove(object sender, HMouseEventArgsWPF e) { if (stateView == MODE_ROI_Create) { return; } double motionX, motionY; double posX, posY; double zoomZone; int a = roiManager.mouseMoveROI(e.Column, e.Row);//鼠标经过的index if (!mousePressed) { return; } if (roiManager != null && (roiManager.activeROIidx != -1) && (dispROI == MODE_INCLUDE_ROI)) { roiManager.mouseMoveAction(e.Column, e.Row); } else if (stateView == MODE_VIEW_MOVE) { motionX = ((e.Column - startX)); motionY = ((e.Row - startY)); if (((int)motionX != 0) || ((int)motionY != 0)) { moveImage(motionX, motionY); startX = e.Column - motionX; startY = e.Row - motionY; } } else if (stateView == MODE_VIEW_ZOOMWINDOW) { HSystem.SetSystem("flush_graphic", "false"); ZoomWindow.ClearWindow(); posX = ((e.Column - ImgCol1) / (ImgCol2 - ImgCol1)) * viewPort.ActualWidth; posY = ((e.Row - ImgRow1) / (ImgRow2 - ImgRow1)) * viewPort.ActualHeight; zoomZone = (zoomWndSize / 2) * zoomWndFactor * zoomAddOn; ZoomWindow.SetWindowExtents((int)posY - (zoomWndSize / 2), (int)posX - (zoomWndSize / 2), zoomWndSize, zoomWndSize); ZoomWindow.SetPart((int)(e.Row - zoomZone), (int)(e.Column - zoomZone), (int)(e.Row + zoomZone), (int)(e.Column + zoomZone)); repaint(ZoomWindow); HSystem.SetSystem("flush_graphic", "true"); ZoomWindow.DispLine(-100.0, -100.0, -100.0, -100.0); } }
public bool LoadImage(string imagePath) { windowHandle.ClearWindow(); HObject ho_ImageRGB; HOperatorSet.ReadImage(out ho_ImageRGB, imagePath); HOperatorSet.GetImageSize(ho_ImageRGB, out hv_width, out hv_height); windowHandle.SetPart(0, 0, hv_height - 1, hv_width - 1); HTuple channelCount; HOperatorSet.CountChannels(ho_ImageRGB, out channelCount); ho_Image.Dispose(); if (channelCount >= 3) { HOperatorSet.AccessChannel(ho_ImageRGB, out ho_Image, 2); } else { HOperatorSet.AccessChannel(ho_ImageRGB, out ho_Image, 1); } windowHandle.DispObj(ho_Image); return(true); }
public void SobelFilter(HDrawingObject dobj, HWindow hwin, string type) { try { HImage image = halcon_dialog.BackgroundImage; HRegion region = new HRegion(dobj.GetDrawingObjectIconic()); hwin.SetWindowParam("flush", "false"); hwin.ClearWindow(); hwin.DispObj(image.ReduceDomain(region).SobelAmp("sum_abs", 11)); hwin.SetWindowParam("flush", "true"); hwin.FlushBuffer(); } catch (HalconException hex) { MessageBox.Show(hex.GetErrorMessage(), "HALCON error", MessageBoxButtons.OK); } }
void ShowObject(HWindow window) { if (window.IsInitialized() == false) { return; } //关闭显示刷新 HSystem.SetSystem("flush_graphic", "false"); //窗体图像清空 window.ClearWindow(); mGC.stateOfSettings.Clear(); try { //HTuple showStart; //HOperatorSet.CountSeconds(out showStart); int count1 = 0; foreach (var item in HObjList) { if (ResultShow == ResultShow.原图 && count1 > 0) { break; } if (item.HObj != null && item.HObj.IsInitialized()) { mGC.ApplyContext(window, item.gContext); window.DispObj(item.HObj); } else if (item.Message != null && isShowMessage) { //item.Message.DispMessage(window, "image", ((double)imageWidth / (double)(viewPort.Width)) / ZoomWndFactor); double sizeTmp = item.Message.CahangeDisplayFontSize(window, (1.0) / ZoomWndFactor, currentTextSize); currentTextSize = sizeTmp; item.Message.DispMessage(window, "image"); } count1++; } //HTuple showEnd; //HOperatorSet.CountSeconds(out showEnd); //double timeShow = (showEnd - showStart) * 1000.0; //Util.Notify(string.Format("内部显示图像用时{0:f2}ms", timeShow)); } catch (Exception) {; } }
/// <summary> /// 重置放大鏡視窗 /// </summary> /// <param name="e"></param> private void resetZoomWindow(HalconDotNet.HMouseEventArgs e) { HSystem.SetSystem("flush_graphic", "false"); ZoomWindow.ClearWindow(); double posX = ((e.X - ImgCol1) / (ImgCol2 - ImgCol1)) * _viewPort.Width; double posY = ((e.Y - ImgRow1) / (ImgRow2 - ImgRow1)) * _viewPort.Height; double zoomZone = (zoomWndSize / 2) * zoomWndFactor * zoomAddOn; ZoomWindow.SetWindowExtents((int)posY - (zoomWndSize / 2), (int)posX - (zoomWndSize / 2), zoomWndSize, zoomWndSize); ZoomWindow.SetPart((int)(e.Y - zoomZone), (int)(e.X - zoomZone), (int)(e.Y + zoomZone), (int)(e.X + zoomZone)); repaint(ZoomWindow); HSystem.SetSystem("flush_graphic", "true"); ZoomWindow.DispLine(-100.0, -100.0, -100.0, -100.0); }
void ShowObject(HWindow window) { if (window.IsInitialized() == false) { return; } //关闭显示刷新 //HSystem.SetSystem("flush_graphic", "false"); //窗体图像清空 window.ClearWindow(); mGC.stateOfSettings.Clear(); try { int count1 = 0; foreach (var item in HObjList) { if (ResultShow == ResultShow.原图 && count1 > 0) { break; } if (item.HObj != null && item.HObj.IsInitialized()) { mGC.ApplyContext(window, item.gContext); window.DispObj(item.HObj); } else if (item.Message != null && isShowMessage) { //item.Message.DispMessage(window, "image", ((double)imageWidth / (double)(viewPort.Width)) / ZoomWndFactor); double sizeTmp = item.Message.CahangeDisplayFontSize(window, (1.0) / ZoomWndFactor, currentTextSize); currentTextSize = sizeTmp; item.Message.DispMessage(window, "image"); } count1++; } } catch (Exception) {; } }
public void Draw(HWindow window, HObject img, bool isShow) { window.ClearWindow(); HOperatorSet.DispObj(img, window); if (isShow) { window.SetLineWidth(2); window.SetDraw("margin"); window.SetColor("green"); window.DispRectangle1(Row1, Col1, Row2, Col2); window.SetColor("red"); window.SetLineWidth(1); window.DispRectangle1(RowMark - 5, ColMark - 5, RowMark + 5, ColMark + 5); window.DispCross(RowMark, ColMark, 35, 0); window.SetDraw("fill"); window.SetColor("blue"); window.DispRectangle1(Row1 - 6, Col1 - 6, Row1 + 6, Col1 + 6); window.DispRectangle1(Row1 - 6, Col2 - 6, Row1 + 6, Col2 + 6); window.DispRectangle1(Row2 - 6, Col1 - 6, Row2 + 6, Col1 + 6); window.DispRectangle1(Row2 - 6, Col2 - 6, Row2 + 6, Col2 + 6); //HOperatorSet.OpenFramegrabber() } }
public void hWindow_rawImage_HMouseWheel(object sender, HMouseEventArgs ev) { int row, col, button; double rowShif, colShif; int row1, column1, row2, column2; int height, width; int r1, c1, r2, c2; if (_flag_act == true) { if (ev.Delta > 0) { try { _hWindowShow.GetMposition(out row, out col, out button); Console.WriteLine(ev.Delta); if (_zoom >= 32.0) { _zoom = 32.0; } else { _zoom *= 2; } _zoomTrans = _zoom / _zoomOrg; _zoomOrg = _zoom; rowShif = 0; colShif = 0; _hWindowShow.GetPart(out row1, out column1, out row2, out column2); height = row2 - row1; width = column2 - column1; r1 = Convert.ToInt32((row1 + ((1 - (1.0 / _zoomTrans)) * (row - row1)) - (rowShif / _zoomTrans))); c1 = Convert.ToInt32((column1 + ((1 - (1.0 / _zoomTrans)) * (col - column1)) - (colShif / _zoomTrans))); r2 = r1 + Convert.ToInt32(height / _zoomTrans); c2 = c1 + Convert.ToInt32(width / _zoomTrans); _hWindowShow.SetPart(r1, c1, r2, c2); _hWindowShow.ClearWindow(); _hWindowShow.DispObj(_imageShow1); _hWindowShow.DispObj(_imageShow2); } catch (Exception) { //throw; } } else if (ev.Delta < 0) { //Convert.ToInt32(ev.X); //Convert.ToInt32(ev.Y); try { _hWindowShow.GetMposition(out row, out col, out button); Console.WriteLine(ev.Delta); if (_zoom <= 1 / 16.0) { _zoom = 1 / 16.0; } else { _zoom /= 2.0; } _zoomTrans = _zoom / _zoomOrg; _zoomOrg = _zoom; rowShif = 0; colShif = 0; _hWindowShow.GetPart(out row1, out column1, out row2, out column2); height = row2 - row1; width = column2 - column1; r1 = Convert.ToInt32((row1 + ((1 - (1.0 / _zoomTrans)) * (row - row1)) - (rowShif / _zoomTrans))); c1 = Convert.ToInt32((column1 + ((1 - (1.0 / _zoomTrans)) * (col - column1)) - (colShif / _zoomTrans))); r2 = r1 + Convert.ToInt32(height / _zoomTrans); c2 = c1 + Convert.ToInt32(width / _zoomTrans); _hWindowShow.SetPart(r1, c1, r2, c2); _hWindowShow.ClearWindow(); _hWindowShow.DispObj(_imageShow1); _hWindowShow.DispObj(_imageShow2); } catch (Exception) { //throw; } } Console.WriteLine(ev.X); Console.WriteLine(ev.Y); Console.WriteLine(_zoom); } }
/*******************************************************************/ private void mouseMoved(object sender, HalconDotNet.HMouseEventArgs e) { double motionX, motionY; double posX, posY; double zoomZone; //if (viewPort.) int count = HObjList.Count; foreach (var item in HObjList) //实时显示图片的灰度 灰度显示 { HObjectEntry objNow = (HObjectEntry)item; if (objNow.HObj is HImage) { try{ HObject myImg = new HObject(); if (!objNow.HObj.IsInitialized()) { break; } //HOperatorSet.CopyObj(objNow.HObj, out myImg, 1, -1); HTuple Gray = new HTuple(); HTuple Wid, Hei; HOperatorSet.GetImageSize(objNow.HObj, out Wid, out Hei); if (0 < e.Y && e.Y < Hei && 0 < e.X && e.X < Wid) { repaint(viewPort.HalconWindow); HOperatorSet.GetGrayval(objNow.HObj, e.Y, e.X, out Gray); double scale = (ImgRow2 - ImgRow1) / Hei.D; //HOperatorSet.SetTposition(viewPort.HalconWindow, ImgRow2 - 400 * scale, ImgCol2 - 400 * scale); HOperatorSet.SetTposition(viewPort.HalconWindow, e.Y + 20 * scale, e.X + 50 * scale); HOperatorSet.SetColor(viewPort.HalconWindow, "red"); HOperatorSet.WriteString(viewPort.HalconWindow, "R:" + ((int)e.Y).ToString() + " " + "C:" + ((int)e.X).ToString() + " " + ((int)Gray[0].D).ToString()); } //myImg.Dispose(); break; } catch { break; } } } if (!mousePressed) { return; } if (roiManager != null && (roiManager.activeROIidx != -1) && (dispROI == MODE_INCLUDE_ROI)) { roiManager.mouseMoveAction(e.X, e.Y); } else if (stateView == MODE_VIEW_MOVE) //平移视图 { motionX = ((e.X - startX)); motionY = ((e.Y - startY)); if (((int)motionX != 0) || ((int)motionY != 0)) { moveImage(motionX, motionY); startX = e.X - motionX; startY = e.Y - motionY; } } else if (stateView == MODE_VIEW_ZOOMWINDOW) //缩放视图 { HSystem.SetSystem("flush_graphic", "false"); ZoomWindow.ClearWindow(); posX = ((e.X - ImgCol1) / (ImgCol2 - ImgCol1)) * viewPort.Width; posY = ((e.Y - ImgRow1) / (ImgRow2 - ImgRow1)) * viewPort.Height; zoomZone = (zoomWndSize / 2) * zoomWndFactor * zoomAddOn; ZoomWindow.SetWindowExtents((int)posY - (zoomWndSize / 2), (int)posX - (zoomWndSize / 2), zoomWndSize, zoomWndSize); ZoomWindow.SetPart((int)(e.Y - zoomZone), (int)(e.X - zoomZone), (int)(e.Y + zoomZone), (int)(e.X + zoomZone)); repaint(ZoomWindow); HSystem.SetSystem("flush_graphic", "true"); ZoomWindow.DispLine(-100.0, -100.0, -100.0, -100.0); } }
private void hWindowControl1_HMouseMove(object sender, HMouseEventArgs e) { if (Ho_Image.IsInitialized()) { try { int button_state; double positionX, positionY; string str_value; string str_position; bool _isXOut = true, _isYOut = true; HTuple channel_count; //元素 HOperatorSet.CountChannels(Ho_Image, out channel_count); //计算图片的通道 _halconWindow1.GetMpositionSubPix(out positionY, out positionX, out button_state); //得到亚像素组成 取得坐标,及鼠标的状态 str_position = String.Format("X: {0:0000.0}, Y: {1:0000.0}", positionX, positionY); //把坐标转成字符格式 HTuple width, height; HOperatorSet.GetImageSize(Ho_Image, out width, out height); _isXOut = (positionX < 0 || positionX >= (double)width); //判断是否超出范围,超出为1 _isYOut = (positionY < 0 || positionY >= (double)height); //判断是否超出范围,超出为1 if (!_isXOut && !_isYOut) { if ((int)channel_count == 1) { HTuple grayval; HOperatorSet.GetGrayval(Ho_Image, (HTuple)positionY, (HTuple)positionX, out grayval); str_value = String.Format("灰度值: {0:000.0}", (double)grayval);//显示出去 } else { if ((int)channel_count == 3) { str_value = ""; HTuple grayValRed, grayValGreen, grayValBlue; HObject _RedChannel, _GreenChannel, _BlueChannel; HOperatorSet.GenEmptyObj(out _RedChannel); HOperatorSet.GenEmptyObj(out _GreenChannel); HOperatorSet.GenEmptyObj(out _BlueChannel); HOperatorSet.AccessChannel(Ho_Image, out _RedChannel, 1); HOperatorSet.AccessChannel(Ho_Image, out _GreenChannel, 2); HOperatorSet.AccessChannel(Ho_Image, out _BlueChannel, 3); HOperatorSet.GetGrayval(_RedChannel, (HTuple)positionY, (HTuple)positionX, out grayValRed); HOperatorSet.GetGrayval(_GreenChannel, (HTuple)positionY, (HTuple)positionX, out grayValGreen); HOperatorSet.GetGrayval(_BlueChannel, (HTuple)positionY, (HTuple)positionX, out grayValBlue); _RedChannel.Dispose(); _GreenChannel.Dispose(); _BlueChannel.Dispose(); str_value = String.Format("Val: ({0:000.0}, {1:000.0}, {2:000.0})", (double)grayValRed, (double)grayValGreen, (double)grayValBlue);//显示3个通道的灰度值 } else { str_value = ""; } } StatusMessage.Text = str_position + " " + str_value; } switch (button_state) { case 0: this.Cursor = System.Windows.Forms.Cursors.Default; //显示鼠标类型为手型 break; case 1: this.Cursor = System.Windows.Forms.Cursors.Hand; //显示鼠标类型为手型 _halconWindow1.ClearWindow(); //清空窗体类容 _halconWindow1.SetPaint(new HTuple("default")); //设置显示默认 // 保持图像显示比例 zoom_beginRow -= (int)(positionY - _start_positionY); zoom_beginCol -= (int)(positionX - _start_positionX); zoom_endRow -= (int)(positionY - _start_positionY); zoom_endCol -= (int)(positionX - _start_positionX); _halconWindow1.SetPart(zoom_beginRow, zoom_beginCol, zoom_endRow, zoom_endCol); _halconWindow1.DispObj(Ho_Image); //显示图片 break; } } catch (Exception ex) { StatusMessage.Text = ex.Message; } } }
async void runcode() { if (Framgraber == null) { MessageBox.Show(" CAMERA DISCONNECTION !!"); return; } if (socketPLC == null) { MessageBox.Show(" PLC DISCONNECTION !!"); return; } double Z = 100; int _break = 0; while (true) { try { int countPointHE = 0; DataBit = clsPlc.GetBitFromPLC(3000, 1, socketPLC); // Snap DataBitTrigerHE = clsPlc.GetBitFromPLC(4000, 1, socketPLC); // HEB DataBitTrigerCaLib = clsPlc.GetBitFromPLC(5000, 1, socketPLC); Task task1; task1 = Task.Factory.StartNew(() => { try { //HOperatorSet.GrabImage(out HObject image1, _frameGrabber); } catch (Exception ex) { } }); //CALIB // if (DataBitTrigerCaLib == 1) if (DataBitTrigerCaLib == 1) { this.Invoke((MethodInvoker) delegate { btnCalibBit.BackColor = Color.Green; }); // Khoi tao Model Calibration cua Halcon HTuple CalibDataID = clsCalib.InitCalib("area_scan_division", "calibration_object", 0.008, 0, 1.85 / 1000000, 1.85 / 1000000, 4024 / 2, 3036 / 2, 4024, 3036, Application.StartupPath + "/calib/" + "caltab_30mm.descr"); int countpointCalib = 0; while (true) { DataBit = clsPlc.GetBitFromPLC(3000, 1, socketPLC); if (DataBit == 1) { snap(); HOperatorSet.WriteImage(Img, "bmp", 0, Application.StartupPath + "/hand_eye/" + "image_calib_" + countpointCalib); clsPlc.SendDataFromPLC("Word", 2, 4005, socketPLC); Thread.Sleep(100); clsPlc.SendDataFromPLC("Word", 2, 4002, socketPLC); // Send Data OK countpointCalib += 1; } if (countpointCalib == 11) { HTuple Error = clsCalib.Finally(); this.Invoke((MethodInvoker) delegate { string Errorshow = Error.ToString(); txtErrorCalib.Text = Errorshow; } ); clsPlc.SetBitFromPLC(4003, 2, 1, socketPLC); // Send Finish MessageBox.Show(" Calib Complete <3"); break; } Thread.Sleep(100); } } else { this.Invoke((MethodInvoker) delegate { btnCalibBit.BackColor = Color.Red; }); } //HE if (DataBitTrigerHE == 1) /////////-----HEB------ { HTuple lst_row_vs = new HTuple(); HTuple lst_col_vs = new HTuple(); HTuple lst_agl_vs = new HTuple(); HTuple lst_x_rb = new HTuple(); HTuple lst_y_rb = new HTuple(); HTuple lst_a_rb = new HTuple(); int countpointHE = 0; while (true) { _break = 0; try { /// Recieve signal riger Camera DataBit = clsPlc.GetBitFromPLC(3000, 1, socketPLC); if (DataBit == 1) { this.Invoke((MethodInvoker) delegate { btnHandeyeBit.BackColor = Color.Green; }); window.ClearWindow(); // Call result Shape Model result rsl = new result(); snap(); HObject newimg = new HObject(Img); rsl = clsCalib.findshapemodle(newimg, ModelID, window); ////**** Use Origin HTuple row_input = rsl.RowProj; HTuple col_input = rsl.ColProj; HTuple agl_input = rsl.Angle_; if (_useOrigin == 1) { HTuple x0 = 0.0, y0 = 0.0, agl0 = 0.0; HTuple hommat2D_findregion = new HTuple(); HOperatorSet.VectorAngleToRigid(x0, y0, agl0, rsl.lst_cordinate_shapemodel[0], rsl.lst_cordinate_shapemodel[1], rsl.lst_cordinate_shapemodel[2], out hommat2D_findregion); clsCalib.create_origin(newimg, rg_binary, rg_first, rg_second, hommat2D_findregion); row_input = rsl.RowProj; col_input = rsl.ColProj; agl_input = rsl.Angle_; } else { row_input = rsl.lst_cordinate_shapemodel[0]; col_input = rsl.lst_cordinate_shapemodel[1]; agl_input = rsl.lst_cordinate_shapemodel[2]; } //Add list vision coordinate HOperatorSet.TupleConcat(lst_row_vs, row_input, out lst_row_vs); HOperatorSet.TupleConcat(lst_col_vs, col_input, out lst_col_vs); HOperatorSet.TupleConcat(lst_agl_vs, agl_input, out lst_agl_vs); // set window window.SetColor("green"); window.SetDraw("margin"); window.SetLineWidth(3); rsl.shapemodel_affined.DispObj(window); window.SetColor("yellow"); //window.DispObj(cross); //HOperatorSet.WriteImage(Img, "bmp", 0, Application.StartupPath + "/hand_eye/" + "image_calib_" + countpointHE); clsPlc.SendDataFromPLC("Word", 2, 4005, socketPLC); // Get coordinate of Robot HTuple _x = clsPlc.RecDataFromPLC("DWord", 3100, 2, socketPLC); HTuple _y = clsPlc.RecDataFromPLC("DWord", 3102, 2, socketPLC); HTuple _angle = clsPlc.RecDataFromPLC("DWord", 3104, 2, socketPLC); HTuple division = 1000.0; HTuple _xx = _x / division; HTuple _xy = _y / division / 10; HTuple _thee = _angle / 10000.0; // Add list coordinate robot HOperatorSet.TupleConcat(lst_x_rb, _xx, out lst_x_rb); HOperatorSet.TupleConcat(lst_y_rb, _xy, out lst_y_rb); HOperatorSet.TupleConcat(lst_a_rb, _thee, out lst_a_rb); countpointHE += 1; HOperatorSet.WriteImage(Img, "bmp", 0, Application.StartupPath + "/hand_eye/" + "image_calib_" + countpointHE); this.Invoke((MethodInvoker) delegate { //timer1.Enabled = true; //MessageBox.Show($"Next count {countpointHE}..."); //timer1.Enabled = false; lbCountNumber.Text = countpointHE.ToString(); txtXRobot.Text = _x.ToString(); txtYRobot.Text = _y.ToString(); txtThetaRobot.Text = _angle.ToString(); btnTrigerBit.BackColor = Color.Green; }); Thread.Sleep(100); clsPlc.SendDataFromPLC("Word", 2, 4002, socketPLC); } if (DataBit == 0) { this.Invoke((MethodInvoker) delegate { btnTrigerBit.BackColor = Color.Red; }); } if (countpointHE == 11) { string pathsave = Application.StartupPath + "/Handeye_Hommat"; result rsl = new result(); rsl = clsCalib.handeye11point(lst_row_vs, lst_col_vs, lst_agl_vs, lst_x_rb, lst_y_rb); // Write list data point HOperatorSet.WriteTuple(lst_row_vs, pathsave + "/lst_row_vs.tup"); HOperatorSet.WriteTuple(lst_col_vs, pathsave + "/lst_col_vs.tup"); HOperatorSet.WriteTuple(lst_agl_vs, pathsave + "/lst_agl_vs.tup"); HOperatorSet.WriteTuple(lst_x_rb, pathsave + "/lst_x_rb.tup"); HOperatorSet.WriteTuple(lst_y_rb, pathsave + "/lst_y_rb.tup"); // Save Data Handeye HOperatorSet.WriteTuple(rsl.HomMat_ImgtoWorld, pathsave + "/HomMat_ImgtoWorld.tup"); HOperatorSet.WriteTuple(rsl.HomMat_WorldtoImg, pathsave + "/HomMat_WorldtoImg.tup"); HOperatorSet.WriteTuple(rsl.TCP_to_Tool, pathsave + "/TCP_to_Tool.tup"); HOperatorSet.WriteTuple(rsl.Tool_to_TCP, pathsave + "/Tool_to_TCP.tup"); clsPlc.SendDataFromPLC("Word", 4, 4003, socketPLC); // Send complete handeye to PLC. Thread.Sleep(100); MessageBox.Show($"Complete!!"); countpointHE = 0; this.Invoke((MethodInvoker) delegate { btnTrigerBit.BackColor = Color.Red; }); break; } } catch (Exception ex) { MessageBox.Show(ex.ToString()); _break = 1; break; } Thread.Sleep(100); } } else { this.Invoke((MethodInvoker) delegate { btnHandeyeBit.BackColor = Color.Red; }); } await Task.WhenAll(task1); } catch (Exception ex) { } if (_break == 1) { break; } } }