/// <summary> /// 打开相机 /// </summary> /// <returns></returns> public override bool Open() { if (m_hAcqHandle == null) { try { //HOperatorSet.OpenFramegrabber("GigEVision", 0, 0, 0, 0, 0, 0, "default", // -1, "default", -1, "false", "default", this.Name, 0, -1, out m_hAcqHandle); //HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb", // -1, "false", "default", "[0] Integrated Camera", 0, -1, out m_hAcqHandle); HOperatorSet.OpenFramegrabber("MVision", 0, 0, 0, 0, 0, 0, "default", 8, "default", -1, "false", "auto", this.Name, 0, -1, out m_hAcqHandle); if (m_hAcqHandle == null || m_hAcqHandle.Length == 0) { m_hAcqHandle = null; return(false); } else { return(true); } } catch (HalconException HDevExpDefaultException1) { System.Diagnostics.Debug.WriteLine(HDevExpDefaultException1.ToString()); m_hAcqHandle = null; return(false); } } return(true); }
// Procedures public void test_barcode(HObject ho_Image, HTuple hv_WindowHandle) { // Local iconic variables HObject ho_SymbolRegions = null; // Local control variables HTuple hv_BarCodeHandle = new HTuple(), hv_DecodedDataStrings = new HTuple(); HTuple hv_BarcodeLocation = new HTuple(), hv_Exception = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_SymbolRegions); try { //* Init Barcode这行代码放到初始化位置,函数最上边,打开相机之前 HOperatorSet.SetCurrentDir("C:/Users/iwake/OneDrive - wake/Desktop/HalconPractise/HalconProj/Barcode"); hv_BarCodeHandle.Dispose(); HOperatorSet.ReadBarCodeModel("BarCodeHandle.bcm", out hv_BarCodeHandle); //set_bar_code_param (BarCodeHandle, 'check_char', 'present') HOperatorSet.SetBarCodeParam(hv_BarCodeHandle, "quiet_zone", 1); //* Recognition Barcode这行代码什么时候想识别二维码放到那里,输入Image,BarCodeHandle, WindowHandle, //*输出SymbolRegions,DecodedDataStrings, BarcodeLocation try { ho_SymbolRegions.Dispose(); hv_DecodedDataStrings.Dispose(); hv_BarcodeLocation.Dispose(); image_get_bar(ho_Image, out ho_SymbolRegions, hv_BarCodeHandle, hv_WindowHandle, out hv_DecodedDataStrings, out hv_BarcodeLocation); } // catch (Exception) catch (HalconException HDevExpDefaultException1) { HDevExpDefaultException1.ToHTuple(out hv_Exception); } ho_SymbolRegions.Dispose(); hv_BarCodeHandle.Dispose(); hv_DecodedDataStrings.Dispose(); hv_BarcodeLocation.Dispose(); hv_Exception.Dispose(); return; } catch (HalconException HDevExpDefaultException) { ho_SymbolRegions.Dispose(); hv_BarCodeHandle.Dispose(); hv_DecodedDataStrings.Dispose(); hv_BarcodeLocation.Dispose(); hv_Exception.Dispose(); throw HDevExpDefaultException; } }
/// <summary> /// 控件缩放时自动调整halcon窗口的大小 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void VisionControl_SizeChanged(object sender, EventArgs e) { if (m_windowHandle != null) { bool bUpdate = false; LockDisplay(); try { HOperatorSet.SetWindowExtents(m_windowHandle, this.ClientRectangle.Y, this.ClientRectangle.X, this.ClientRectangle.Width, this.ClientRectangle.Height); bUpdate = true; } catch (HalconException HDevExpDefaultException1) { System.Diagnostics.Debug.WriteLine(HDevExpDefaultException1.ToString()); } catch (Exception exp) { System.Diagnostics.Debug.WriteLine(exp.ToString()); } finally { UnlockDisplay(); } if (bUpdate && m_IVisionControlUpdate != null) { m_IVisionControlUpdate.UpdateVisionControl(this); } } }
public void convert_rectangle_to_region(out HObject ho_region, HTuple hv_Left, HTuple hv_Top, HTuple hv_Right, HTuple hv_Bottom) { // Local control variables HTuple hv_Exception = null; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_region); //version: v0.0.1 try { ho_region.Dispose(); HOperatorSet.GenRectangle1(out ho_region, hv_Left.TupleReal(), hv_Top.TupleReal(), hv_Right.TupleReal(), hv_Bottom.TupleReal()); } // catch (Exception) catch (HalconException HDevExpDefaultException1) { HDevExpDefaultException1.ToHTuple(out hv_Exception); ho_region.Dispose(); HOperatorSet.GenEmptyRegion(out ho_region); } return; }
private void btn_calibrateCamParam_Click(object sender, EventArgs e) { try { HOperatorSet.CalibrateCameras(CalibDataID, out hv_Error); HOperatorSet.GetCalibData(CalibDataID, "camera", 0, "params", out tool.cameraParam); StringBuilder strb = new StringBuilder(); strb.Append(tool.cameraParam[0].S + ","); for (int i = 1; i < tool.cameraParam.Length; i++) { if (i == 3 || i == 4) { ExecuteInformation((tool.cameraParam[i].D * 1000000).ToString("F3") + ";" + Environment.NewLine); strb.Append((tool.cameraParam[i].D * 1000000).ToString("F3")); } else if (i == 7 || i == 8) { ExecuteInformation(tool.cameraParam[i].I.ToString() + ";" + Environment.NewLine); strb.Append(tool.cameraParam[i].I.ToString()); } else { if (i == 2) { ExecuteInformation(tool.cameraParam[i].D.ToString("F4") + ";" + Environment.NewLine); strb.Append(tool.cameraParam[i].D.ToString("F4")); } else { ExecuteInformation(tool.cameraParam[i].D.ToString("F4") + ";" + Environment.NewLine); strb.Append(tool.cameraParam[i].D.ToString("F3")); } } if (i < tool.cameraParam.Length - 1) { strb.Append(","); } } txt_CameraParam.Text = strb.ToString(); gb_SettingPlanPose.Enabled = true; // btn_grabCamera.Enabled = false; } catch (HalconException HDevExpDefaultException1) { HTuple hv_Exception; HDevExpDefaultException1.ToHTuple(out hv_Exception); MessageBox.Show("标定错误", hv_Exception.ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error); } ExecuteInformation("相机参数标定成功"); try { ExecuteInformation("标定误差: " + hv_Error.D.ToString("F3") + "pixel"); } catch { } }
/// <summary> /// 鼠标右键按下时返回全屏显示 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void VisionControl_MouseUp(object sender, MouseEventArgs e) { if (MouseMode == WindowMouseMode.Move && e.Button == MouseButtons.Middle) { if (isOpen()) { //Action<object> action = (object obj) => //{ bool bUpdate = false; HTuple row0, col0, row1, col1; LockDisplay(); try { HOperatorSet.GetPart(m_windowHandle, out row0, out col0, out row1, out col1); if (row0 != 0 || col0 != 0 || col1 - col0 != ImageWidth || row1 - row0 != ImageHeight) { HOperatorSet.SetPart(m_windowHandle, 0, 0, ImageHeight, ImageWidth); bUpdate = true; } } catch (HalconException HDevExpDefaultException1) { System.Diagnostics.Debug.WriteLine(HDevExpDefaultException1.ToString()); } catch (Exception exp) { System.Diagnostics.Debug.WriteLine(exp.ToString()); } finally { UnlockDisplay(); } if (bUpdate && m_IVisionControlUpdate != null) { m_IVisionControlUpdate.UpdateVisionControl(this); } //}; //Task t1 = new Task(action, ""); //t1.Start(); //t1.Wait(); } } else if (e.Button == MouseButtons.Left) { if (this.Cursor == Cursors.Hand) { this.Cursor = Cursors.Arrow; } } else if (e.Button == MouseButtons.Right) { if (this.Cursor == Cursors.Hand) { this.Cursor = Cursors.Arrow; } } }
public override void UpdateVisionControl(VisionControl ctl) { ctl.LockDisplay(); try { if (imgSrc != null && imgSrc.IsInitialized() && imgSrc.Key != IntPtr.Zero) { if (imgSrc != null) { HOperatorSet.DispObj(imgSrc, ctl.GetHalconWindow()); } while (ctl.getStack().Count > 0) { HOperatorSet.DispObj(ctl.getStack().Pop(), ctl.GetHalconWindow()); } foreach (var mea in MeasureMgr.GetInstance().MeasureList) { HOperatorSet.SetColor(ctl.GetHalconWindow(), "blue"); HOperatorSet.DispObj(mea.LineEdge, ctl.GetHalconWindow()); HOperatorSet.SetColor(ctl.GetHalconWindow(), "green"); HOperatorSet.DispObj(mea.ContourOk, ctl.GetHalconWindow()); HOperatorSet.SetColor(ctl.GetHalconWindow(), "red"); HOperatorSet.DispObj(mea.ContourAreaNG, ctl.GetHalconWindow()); HOperatorSet.SetColor(ctl.GetHalconWindow(), "magenta"); HOperatorSet.DispObj(mea.ContourPosNG, ctl.GetHalconWindow()); } HOperatorSet.SetColor(ctl.GetHalconWindow(), "blue"); //HTuple hv_Width, hv_Height; //HOperatorSet.GetImageSize(m_image, out hv_Width, out hv_Height); //HObject ht_CrossLineV, ht_CrossLineH; //HOperatorSet.SetColor(ctl.GetHalconWindow(), "red"); //HOperatorSet.GenRegionLine(out ht_CrossLineV, 0, hv_Width / 2, hv_Height, hv_Width / 2); //HOperatorSet.GenRegionLine(out ht_CrossLineH, hv_Height / 2, 0, hv_Height / 2, hv_Width); //HOperatorSet.DispObj(ht_CrossLineV, ctl.GetHalconWindow()); //HOperatorSet.DispObj(ht_CrossLineH, ctl.GetHalconWindow()); } } catch (HalconException HDevExpDefaultException1) { System.Diagnostics.Debug.WriteLine(HDevExpDefaultException1.ToString()); } catch (AccessViolationException e) { System.Diagnostics.Debug.WriteLine(e.ToString()); } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.ToString()); } finally { ctl.UnlockDisplay(); } }
// Main procedure private void action() { // Local iconic variables HObject ho_Image = null; // Local control variables HTuple hv_WindowHandle, hv_AcqHandle, hv_Index; HTuple hv_Ex = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image); try { //Code generated by Image Acquisition 01 //Attention: The initialization may fail in case parameters need to //be set in a specific order (e.g., image resolution vs. offset). HOperatorSet.SetWindowAttr("background_color", "black"); HOperatorSet.OpenWindow(0, 0, 512, 512, 0, "", "", out hv_WindowHandle); HDevWindowStack.Push(hv_WindowHandle); HOperatorSet.OpenFramegrabber("GigEVision", 0, 0, 0, 0, 0, 0, "progressive", -1, "default", -1, "false", "default", "Cam_Up", 0, -1, out hv_AcqHandle); HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "TriggerSelector", "FrameStart"); HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "grab_timeout", 10000); HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "TriggerMode", "On"); HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "TriggerSource", "Line1"); HOperatorSet.GrabImageStart(hv_AcqHandle, -1); hv_Index = 1; while ((int)(1) != 0) { try { ho_Image.Dispose(); HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1); hv_Index = hv_Index + 1; disp_message(hv_WindowHandle, hv_Index, "window", 12, 12, "black", "true"); } // catch (Ex) catch (HalconException HDevExpDefaultException1) { HDevExpDefaultException1.ToHTuple(out hv_Ex); } //Do something } HOperatorSet.CloseFramegrabber(hv_AcqHandle); } catch (HalconException HDevExpDefaultException) { ho_Image.Dispose(); throw HDevExpDefaultException; } ho_Image.Dispose(); }
/// <summary> /// 更新显示控件 /// </summary> /// <param name="ctl"></param> public override void UpdateVisionControl(VisionControl ctl) { ctl.LockDisplay(); try { if (imgSrc != null && imgSrc.IsInitialized() && imgSrc.Key != IntPtr.Zero) { HTuple num = 0; HOperatorSet.CountObj(imgSrc, out num); if (num > 0)//&& m_image.IsInitialized() && m_image.Key != IntPtr.Zero) { HOperatorSet.DispObj(imgSrc, ctl.GetHalconWindow()); } // HOperatorSet.DispObj(ModelContour, ctl.GetHalconWindow()); //if (MeasureRect != null && MeasureRect.IsInitialized()) //{ // HOperatorSet.SetColor(m_visionControl.GetHalconWindow(), "red"); // HOperatorSet.DispObj(MeasureRect, ctl.GetHalconWindow()); //} if (Lines != null && Lines.IsInitialized()) { HOperatorSet.SetColor(m_visionControl.GetHalconWindow(), "blue"); HOperatorSet.DispObj(Lines, ctl.GetHalconWindow()); } if (IntraDistance != null && IntraDistance.Length > 0) { if (IntraDistance.Type == HTupleType.STRING) { HDevelopExport.disp_message(m_visionControl.GetHalconWindow(), IntraDistance, "window", 20, -1, "green", "false"); } else { var value = SideCameraCalibData.PixelToMm(IntraDistance); HDevelopExport.disp_message(m_visionControl.GetHalconWindow(), $"{value:F2}", "window", 20, -1, "green", "false"); } } } } catch (HalconException HDevExpDefaultException1) { System.Diagnostics.Debug.WriteLine(HDevExpDefaultException1.ToString()); } catch (Exception exp) { System.Diagnostics.Debug.WriteLine(exp.ToString()); } finally { ctl.UnlockDisplay(); } }
private static void read_cam_par(string CamParFile, out HTuple CameraParam) { HTuple hv_Exception = null; try { HOperatorSet.ReadCamPar(CamParFile, out CameraParam); } catch (HalconException HDevExpDefaultException1) { CameraParam = null; HDevExpDefaultException1.ToHTuple(out hv_Exception); } }
static private void convert_single_region_to_points(HObject ho_singleRegion, out HTuple hv_posYs, out HTuple hv_posXs, out HTuple hv_pointsNum) { // Local iconic variables HObject ho_Contours = null; // Local control variables HTuple hv_Exception = null; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Contours); hv_posYs = new HTuple(); hv_posXs = new HTuple(); hv_pointsNum = new HTuple(); try { dev_update_off(); HOperatorSet.SetSystem("clip_region", "false"); try { ho_Contours.Dispose(); HOperatorSet.GenContourRegionXld(ho_singleRegion, out ho_Contours, "border"); HOperatorSet.GetContourXld(ho_Contours, out hv_posYs, out hv_posXs); hv_pointsNum = new HTuple(hv_posYs.TupleLength()); } // catch (Exception) catch (HalconException HDevExpDefaultException1) { HDevExpDefaultException1.ToHTuple(out hv_Exception); hv_posYs = new HTuple(); hv_posXs = new HTuple(); hv_pointsNum = new HTuple(); } ho_Contours.Dispose(); return; } catch (HalconException HDevExpDefaultException) { ho_Contours.Dispose(); throw HDevExpDefaultException; } }
/// <summary> /// 初始化halcon窗口,分辨率为2592 * 1944 /// </summary> /// // [DllImport("user32.dll")]static extern IntPtr GetWindowDC(IntPtr hWnd); public void InitWindow() { try { // HOperatorSet.NewExternWindow(this.Handle, 0, 0, this.Width, this.Height, out m_windowHandle); // HOperatorSet.SetWindowDc(m_windowHandle, GetWindowDC(this.Handle)); HOperatorSet.OpenWindow(0, 0, this.Width, this.Height, this.Handle, "", "", out m_windowHandle); HOperatorSet.SetWindowParam(m_windowHandle, "background_color", "#000040"); HOperatorSet.ClearWindow(m_windowHandle); HOperatorSet.SetPart(m_windowHandle, 0, 0, ImageHeight - 1, ImageWidth - 1); } catch (HalconException HDevExpDefaultException1) { System.Diagnostics.Debug.WriteLine(HDevExpDefaultException1.ToString()); } }
// Local procedures // Chapter: Example // Short Description: Async image acquisition loop to be used at the beginning of example program public void abshear_async_acquisition(HTuple hv_WindowHandle, HTuple hv_AcqHandle) { // Local iconic variables HObject ExpTmpLocalVar_Image = null; // Local control variables HTuple hv_keepGrabbing = null, hv_Row = new HTuple(); HTuple hv_Column = new HTuple(), hv_Button = new HTuple(); HTuple hv_Exception = new HTuple(), hv_ErrorCode = new HTuple(); // Initialize local and output iconic variables //global object Image hv_keepGrabbing = 1; HOperatorSet.GrabImageStart(hv_AcqHandle, -1); while ((int)(hv_keepGrabbing) != 0) { HOperatorSet.GrabImageAsync(out ExpTmpLocalVar_Image, hv_AcqHandle, -1); ExpSetGlobalVar_Image(ExpTmpLocalVar_Image); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ExpGetGlobalVar_Image(), HDevWindowStack.GetActive() ); } HOperatorSet.WriteString(hv_WindowHandle, "Press Left Mouse Button to Begin..."); try { HOperatorSet.GetMposition(hv_WindowHandle, out hv_Row, out hv_Column, out hv_Button); } // catch (Exception) catch (HalconException HDevExpDefaultException1) { HDevExpDefaultException1.ToHTuple(out hv_Exception); hv_ErrorCode = hv_Exception.TupleSelect(0); } if ((int)(new HTuple(hv_Button.TupleEqual(1))) != 0) { hv_keepGrabbing = 0; } } return; }
private void btn_grabCamera_Click(object sender, EventArgs e) { if (testImage != null && testImage.IsInitialized()) { testImage.Dispose(); } testImage = tool.GetImage(); if (testImage == null) { return; } if (testImage.IsInitialized() == false) { return; } if (calHImages == null) { calHImages = new List <HImage>(); } index = calHImages.Count + 1; try { HOperatorSet.FindCalibObject(testImage, CalibDataID, 0, 0, index, new HTuple(), new HTuple()); } // catch (Exception) catch (HalconException HDevExpDefaultException1) { HTuple hv_Exception; HDevExpDefaultException1.ToHTuple(out hv_Exception); MessageBox.Show("提取原点错误", "请注意不要遮挡或者拍摄时抖动", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } calHImages.Add(testImage.Clone()); if (index == 1) { cbb_CalibImages.Items.Clear(); } cbb_CalibImages.Items.Add(index.ToString()); cbb_CalibImages.SelectedIndex = index - 1; }
/// <summary> /// 关闭相机 /// </summary> /// <returns></returns> public override bool Close() { if (m_hAcqHandle != null) { try { HOperatorSet.CloseFramegrabber(m_hAcqHandle); } catch (HalconException HDevExpDefaultException1) { System.Diagnostics.Debug.WriteLine(HDevExpDefaultException1.ToString()); return(false); } m_hAcqHandle = null; } m_image = null; return(true); }
/// <summary> /// 软件触发一次同步采集 /// </summary> /// <returns></returns> public override int Snap() { if (m_hAcqHandle == null) { Open(); } if (m_hAcqHandle != null) { try { m_image?.Dispose(); HOperatorSet.GrabImage(out m_image, m_hAcqHandle); } catch (HalconException HDevExpDefaultException1) { System.Diagnostics.Debug.WriteLine(HDevExpDefaultException1.ToString()); return(0); } return(1); } return(0); }
public void ExternalCalibrate(out double te1, out double te2) { //读取初始相机参数 try { HOperatorSet.ReadCamPar("camera_parameters.dat", out hv_CamParam); } // catch (Exception) catch (HalconException HDevExpDefaultException1) { HDevExpDefaultException1.ToHTuple(out hv_Exception); // stop(...); only in hdevelop } //从图像点确定外部摄像机参数和世界坐标 //当校准板直接放在测量平面上时 可以从图像确定外部摄像机参数 ho_Image.Dispose(); HOperatorSet.ReadImage(out ho_Image, hv_ImgPath + "calib_01"); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } hv_CaltabName = "caltab.descr"; hv_TmpCtrl_FindCalObjParNames = new HTuple(); hv_TmpCtrl_FindCalObjParNames[0] = "gap_tolerance"; hv_TmpCtrl_FindCalObjParNames[1] = "alpha"; hv_TmpCtrl_FindCalObjParNames[2] = "skip_find_caltab"; hv_TmpCtrl_FindCalObjParValues = new HTuple(); hv_TmpCtrl_FindCalObjParValues[0] = 1; hv_TmpCtrl_FindCalObjParValues[1] = 1; hv_TmpCtrl_FindCalObjParValues[2] = "false"; //生产一个标定数据模型 HOperatorSet.CreateCalibData("calibration_object", 1, 1, out hv_CalibDataID); //设置相机类型和初始参数 HOperatorSet.SetCalibDataCamParam(hv_CalibDataID, 0, "area_scan_telecentric_division", hv_CamParam); //定义校准对象 HOperatorSet.SetCalibDataCalibObject(hv_CalibDataID, 0, hv_CaltabName); //找到Halcon标定板,并将数据写入校准数据模型中 HOperatorSet.FindCalibObject(ho_Image, hv_CalibDataID, 0, 0, 1, hv_TmpCtrl_FindCalObjParNames, hv_TmpCtrl_FindCalObjParValues); //获得标定区域轮廓 ho_Caltab.Dispose(); HOperatorSet.GetCalibDataObservContours(out ho_Caltab, hv_CalibDataID, "caltab", 0, 0, 1); //获得标定区域mark点的信息 HOperatorSet.GetCalibDataObservPoints(hv_CalibDataID, 0, 0, 1, out hv_Row, out hv_Column, out hv_Index, out hv_Pose); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Caltab, HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "red"); } //显示标定的结果 //HOperatorSet.DispCaltab(hv_WindowHandle, hv_CaltabName, hv_CamParam, hv_Pose, //1); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetLineWidth(HDevWindowStack.GetActive(), 3); } //在mark点中心画点 //HOperatorSet.DispCircle(hv_WindowHandle, hv_Row, hv_Column, HTuple.TupleGenConst( //new HTuple(hv_Row.TupleLength()), 1.5)); // stop(...); only in hdevelop //取得校准文件内的坐标数据 //caltab_points (CaltabName, X, Y, Z) //calibrate_cameras (CalibDataID, Error) //考虑标定板厚度进行姿态矫正 3.5mm厚度 HOperatorSet.SetOriginPose(hv_Pose, 0, 0, 0.0035, out hv_Pose); HOperatorSet.ClearCalibData(hv_CalibDataID); //图像坐标转世界坐标 HOperatorSet.ImagePointsToWorldPlane(hv_CamParam, hv_Pose, hv_Row, hv_Column, "mm", out hv_X, out hv_Y); hv_Image_X1 = 100; hv_Image_Y1 = 100; hv_Distance_XY = 500; hv_Image_X2 = hv_Image_X1 + hv_Distance_XY; hv_Image_Y2 = hv_Image_Y1 + hv_Distance_XY; HOperatorSet.ImagePointsToWorldPlane(hv_CamParam, hv_Pose, hv_Image_Y1, hv_Image_X1, "mm", out hv_World_X1, out hv_World_Y1); HOperatorSet.ImagePointsToWorldPlane(hv_CamParam, hv_Pose, hv_Image_Y2, hv_Image_X2, "mm", out hv_World_X2, out hv_World_Y2); //计算世界坐标距离? HOperatorSet.DistancePp(hv_World_Y1, hv_World_X1, hv_World_Y2, hv_World_X2, out hv_DistanceWorld); //计算像素坐标? HOperatorSet.DistancePp(hv_Image_X1, hv_Image_Y1, hv_Image_X2, hv_Image_Y2, out hv_DistanceImage); //每毫米世界坐标距=距离对应的像素点个数? hv_DistanceOneMilimeter = hv_DistanceImage / hv_DistanceWorld; //每个像素对应的世界坐标距离? hv_DistanceOnePixel = hv_DistanceWorld / hv_DistanceImage; //调整世界坐标至中心偏移量? hv_OffSetX = (hv_Width / 2) * hv_DistanceOnePixel; hv_OffSetY = (hv_Height / 2) * hv_DistanceOnePixel; te1 = hv_DistanceOneMilimeter; te2 = hv_DistanceOnePixel; }
// Main procedure private void action() { // Local iconic variables HObject ho_Image, ho_DerivGauss = null, ho_RegionCrossings = null; HObject ho_Region = null, ho_region_outer = null, ho_contour_outer = null; HObject ho_ContCircle = null; // Local control variables HTuple hv_AcqHandle = null, hv_side = null; HTuple hv_dia = null, hv_Width = new HTuple(), hv_Height = new HTuple(); HTuple hv_HalfH = new HTuple(), hv_HalfW = new HTuple(); HTuple hv_row_len = new HTuple(), hv_row_outer = new HTuple(); HTuple hv_col_outer = new HTuple(), hv_Rows = new HTuple(); HTuple hv_Cols = new HTuple(), hv_i = new HTuple(), hv_Indices = new HTuple(); HTuple hv_Length = new HTuple(), hv_col_min = new HTuple(); HTuple hv_indice_min = new HTuple(), hv_col_max = new HTuple(); HTuple hv_indice_max = new HTuple(), hv_Row = new HTuple(); HTuple hv_Col = new HTuple(), hv_Radius = new HTuple(); HTuple hv_StartPhi = new HTuple(), hv_EndPhi = new HTuple(); HTuple hv_PointOrder = new HTuple(), hv_TupleMax = new HTuple(); HTuple hv_IndexMax = new HTuple(), hv_colToMax0 = new HTuple(); HTuple hv_output = new HTuple(), hv_outputmm = new HTuple(); HTuple hv_TupleMin = new HTuple(), hv_IndexMin = new HTuple(); HTuple hv_colToMin0 = new HTuple(), hv_Exception = null; HTuple hv_MessageError = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image); HOperatorSet.GenEmptyObj(out ho_DerivGauss); HOperatorSet.GenEmptyObj(out ho_RegionCrossings); HOperatorSet.GenEmptyObj(out ho_Region); HOperatorSet.GenEmptyObj(out ho_region_outer); HOperatorSet.GenEmptyObj(out ho_contour_outer); HOperatorSet.GenEmptyObj(out ho_ContCircle); try { HOperatorSet.OpenFramegrabber("GigEVision", 0, 0, 0, 0, 0, 0, "default", -1, "default", -1, "false", "default", "GC3851M_CAM_4", 0, -1, out hv_AcqHandle); HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "ExposureTime", 3500.0); hv_side = 2; hv_dia = 1; HOperatorSet.GrabImageStart(hv_AcqHandle, -1); ho_Image.Dispose(); HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1); try { HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height); //* Define constants and tuples: hv_HalfH = hv_Height / 2; hv_HalfW = hv_Width / 2; hv_row_len = hv_Height.Clone(); hv_row_outer = new HTuple(); hv_col_outer = new HTuple(); //* Edge detection ho_DerivGauss.Dispose(); HOperatorSet.DerivateGauss(ho_Image, out ho_DerivGauss, 1, "x"); //* diameter 2 doesn't have a clean background => different values if ((int)((new HTuple(hv_dia.TupleEqual(2))).TupleOr(new HTuple(hv_dia.TupleEqual( 5)))) != 0) { ho_RegionCrossings.Dispose(); HOperatorSet.DualThreshold(ho_DerivGauss, out ho_RegionCrossings, 20, 2, 2); ho_Region.Dispose(); HOperatorSet.Union1(ho_RegionCrossings, out ho_Region); } else { ho_RegionCrossings.Dispose(); HOperatorSet.DualThreshold(ho_DerivGauss, out ho_RegionCrossings, 20, 12, 2); ho_Region.Dispose(); HOperatorSet.Union1(ho_RegionCrossings, out ho_Region); } //* Retrieve points from detected edges HOperatorSet.GetRegionPoints(ho_Region, out hv_Rows, out hv_Cols); //* Side 1 => upper side closer to probe if ((int)(new HTuple(hv_side.TupleEqual(1))) != 0) { //* Extract the points which define the outer or inner edge if ((int)((new HTuple((new HTuple(hv_dia.TupleEqual(3))).TupleOr(new HTuple(hv_dia.TupleEqual( 4))))).TupleOr(new HTuple(hv_dia.TupleEqual(5)))) != 0) { HTuple end_val36 = hv_row_len; HTuple step_val36 = 1; for (hv_i = 1; hv_i.Continue(end_val36, step_val36); hv_i = hv_i.TupleAdd(step_val36)) { HOperatorSet.TupleFind(hv_Rows, hv_i - 1, out hv_Indices); //* if none point exists in this row replace it with an incremental interpolation if ((int)(new HTuple(hv_Indices.TupleEqual(-1))) != 0) { if (hv_row_outer == null) { hv_row_outer = new HTuple(); } hv_row_outer[hv_i - 1] = (hv_row_outer.TupleSelect(hv_i - 2)) + 1; if (hv_col_outer == null) { hv_col_outer = new HTuple(); } hv_col_outer[hv_i - 1] = hv_col_outer.TupleSelect(hv_i - 2); continue; } HOperatorSet.TupleLength(hv_Indices, out hv_Length); HOperatorSet.TupleMin(hv_Cols.TupleSelect(hv_Indices), out hv_col_min); HOperatorSet.TupleFind(hv_Cols.TupleSelect(hv_Indices), hv_col_min, out hv_indice_min); if (hv_row_outer == null) { hv_row_outer = new HTuple(); } hv_row_outer[hv_i - 1] = hv_Rows.TupleSelect(hv_Indices.TupleSelect(hv_indice_min)); if (hv_col_outer == null) { hv_col_outer = new HTuple(); } hv_col_outer[hv_i - 1] = hv_Cols.TupleSelect(hv_Indices.TupleSelect(hv_indice_min)); } } else if ((int)((new HTuple(hv_dia.TupleEqual(1))).TupleOr(new HTuple(hv_dia.TupleEqual( 2)))) != 0) { HTuple end_val51 = hv_row_len; HTuple step_val51 = 1; for (hv_i = 1; hv_i.Continue(end_val51, step_val51); hv_i = hv_i.TupleAdd(step_val51)) { HOperatorSet.TupleFind(hv_Rows, hv_i - 1, out hv_Indices); //* if none point exists in this row replace it with an incremental interpolation if ((int)(new HTuple(hv_Indices.TupleEqual(-1))) != 0) { if (hv_row_outer == null) { hv_row_outer = new HTuple(); } hv_row_outer[hv_i - 1] = (hv_row_outer.TupleSelect(hv_i - 2)) + 1; if (hv_col_outer == null) { hv_col_outer = new HTuple(); } hv_col_outer[hv_i - 1] = hv_col_outer.TupleSelect(hv_i - 2); continue; } HOperatorSet.TupleLength(hv_Indices, out hv_Length); HOperatorSet.TupleMax(hv_Cols.TupleSelect(hv_Indices), out hv_col_max); HOperatorSet.TupleFind(hv_Cols.TupleSelect(hv_Indices), hv_col_max, out hv_indice_max); if (hv_row_outer == null) { hv_row_outer = new HTuple(); } hv_row_outer[hv_i - 1] = hv_Rows.TupleSelect(hv_Indices.TupleSelect(hv_indice_max)); if (hv_col_outer == null) { hv_col_outer = new HTuple(); } hv_col_outer[hv_i - 1] = hv_Cols.TupleSelect(hv_Indices.TupleSelect(hv_indice_max)); } } //* retrieve the outer or inner edge points + cirlce fitting ho_region_outer.Dispose(); HOperatorSet.GenRegionPoints(out ho_region_outer, hv_row_outer, hv_col_outer); ho_contour_outer.Dispose(); HOperatorSet.GenContourPolygonXld(out ho_contour_outer, hv_row_outer, hv_col_outer); HOperatorSet.FitCircleContourXld(ho_contour_outer, "geotukey", -1, 0, 0, 3, 2, out hv_Row, out hv_Col, out hv_Radius, out hv_StartPhi, out hv_EndPhi, out hv_PointOrder); ho_ContCircle.Dispose(); HOperatorSet.GenCircleContourXld(out ho_ContCircle, hv_Row, hv_Col, hv_Radius, 0, 6.28318, "positive", 1); HOperatorSet.GetContourXld(ho_ContCircle, out hv_Row, out hv_Col); //* find the maximum of the estimated circle HOperatorSet.TupleMax(hv_Col, out hv_TupleMax); HOperatorSet.TupleFindFirst(hv_Col, hv_TupleMax, out hv_IndexMax); //* calculate pixel and mm outputs hv_colToMax0 = hv_Col.TupleSelect(hv_IndexMax); hv_output = (-hv_HalfW) + hv_colToMax0; hv_outputmm = hv_output * 0.001675; } //* Side 2 => side closer to vertical moving axis if ((int)(new HTuple(hv_side.TupleEqual(2))) != 0) { //* Extract the points which define the outer or inner edge if ((int)((new HTuple((new HTuple(hv_dia.TupleEqual(3))).TupleOr(new HTuple(hv_dia.TupleEqual( 4))))).TupleOr(new HTuple(hv_dia.TupleEqual(5)))) != 0) { HTuple end_val88 = hv_row_len; HTuple step_val88 = 1; for (hv_i = 1; hv_i.Continue(end_val88, step_val88); hv_i = hv_i.TupleAdd(step_val88)) { HOperatorSet.TupleFind(hv_Rows, hv_i - 1, out hv_Indices); //* if none point exists in this row replace it with an incremental interpolation if ((int)(new HTuple(hv_Indices.TupleEqual(-1))) != 0) { if (hv_row_outer == null) { hv_row_outer = new HTuple(); } hv_row_outer[hv_i - 1] = (hv_row_outer.TupleSelect(hv_i - 2)) + 1; if (hv_col_outer == null) { hv_col_outer = new HTuple(); } hv_col_outer[hv_i - 1] = hv_col_outer.TupleSelect(hv_i - 2); continue; } HOperatorSet.TupleLength(hv_Indices, out hv_Length); HOperatorSet.TupleMax(hv_Cols.TupleSelect(hv_Indices), out hv_col_max); HOperatorSet.TupleFind(hv_Cols.TupleSelect(hv_Indices), hv_col_max, out hv_indice_max); if (hv_row_outer == null) { hv_row_outer = new HTuple(); } hv_row_outer[hv_i - 1] = hv_Rows.TupleSelect(hv_Indices.TupleSelect(hv_indice_max)); if (hv_col_outer == null) { hv_col_outer = new HTuple(); } hv_col_outer[hv_i - 1] = hv_Cols.TupleSelect(hv_Indices.TupleSelect(hv_indice_max)); } } else if ((int)((new HTuple(hv_dia.TupleEqual(1))).TupleOr(new HTuple(hv_dia.TupleEqual( 2)))) != 0) { HTuple end_val103 = hv_row_len; HTuple step_val103 = 1; for (hv_i = 1; hv_i.Continue(end_val103, step_val103); hv_i = hv_i.TupleAdd(step_val103)) { HOperatorSet.TupleFind(hv_Rows, hv_i - 1, out hv_Indices); //* if none point exists in this row replace it with an incremental interpolation if ((int)(new HTuple(hv_Indices.TupleEqual(-1))) != 0) { if (hv_row_outer == null) { hv_row_outer = new HTuple(); } hv_row_outer[hv_i - 1] = (hv_row_outer.TupleSelect(hv_i - 2)) + 1; if (hv_col_outer == null) { hv_col_outer = new HTuple(); } hv_col_outer[hv_i - 1] = hv_col_outer.TupleSelect(hv_i - 2); continue; } HOperatorSet.TupleLength(hv_Indices, out hv_Length); HOperatorSet.TupleMin(hv_Cols.TupleSelect(hv_Indices), out hv_col_min); HOperatorSet.TupleFind(hv_Cols.TupleSelect(hv_Indices), hv_col_min, out hv_indice_min); if (hv_row_outer == null) { hv_row_outer = new HTuple(); } hv_row_outer[hv_i - 1] = hv_Rows.TupleSelect(hv_Indices.TupleSelect(hv_indice_min)); if (hv_col_outer == null) { hv_col_outer = new HTuple(); } hv_col_outer[hv_i - 1] = hv_Cols.TupleSelect(hv_Indices.TupleSelect(hv_indice_min)); } } //* retrieve the outer or inner edge points + cirlce fitting ho_region_outer.Dispose(); HOperatorSet.GenRegionPoints(out ho_region_outer, hv_row_outer, hv_col_outer); ho_contour_outer.Dispose(); HOperatorSet.GenContourPolygonXld(out ho_contour_outer, hv_row_outer, hv_col_outer); HOperatorSet.FitCircleContourXld(ho_contour_outer, "geotukey", -1, 0, 0, 3, 2, out hv_Row, out hv_Col, out hv_Radius, out hv_StartPhi, out hv_EndPhi, out hv_PointOrder); ho_ContCircle.Dispose(); HOperatorSet.GenCircleContourXld(out ho_ContCircle, hv_Row, hv_Col, hv_Radius, 0, 6.28318, "positive", 1); HOperatorSet.GetContourXld(ho_ContCircle, out hv_Row, out hv_Col); //* find the minimum of the estimated circle HOperatorSet.TupleMin(hv_Col, out hv_TupleMin); HOperatorSet.TupleFindFirst(hv_Col, hv_TupleMin, out hv_IndexMin); //* calculate pixel and mm outputs hv_colToMin0 = hv_Col.TupleSelect(hv_IndexMin); hv_output = hv_HalfW - hv_colToMin0; hv_outputmm = hv_output * 0.001675; } } // catch (Exception) catch (HalconException HDevExpDefaultException1) { HDevExpDefaultException1.ToHTuple(out hv_Exception); hv_MessageError = " ERROR: Not able to analize photo, move horizontal axis"; } HOperatorSet.CloseFramegrabber(hv_AcqHandle); } catch (HalconException HDevExpDefaultException) { ho_Image.Dispose(); ho_DerivGauss.Dispose(); ho_RegionCrossings.Dispose(); ho_Region.Dispose(); ho_region_outer.Dispose(); ho_contour_outer.Dispose(); ho_ContCircle.Dispose(); throw HDevExpDefaultException; } ho_Image.Dispose(); ho_DerivGauss.Dispose(); ho_RegionCrossings.Dispose(); ho_Region.Dispose(); ho_region_outer.Dispose(); ho_contour_outer.Dispose(); ho_ContCircle.Dispose(); }
// Main procedure private void action() { // Local iconic variables HObject ho_Rectangle, ho_ImageReduced; HObject ho_RegionToDetect, ho_RegionOpening, ho_RegionDifference; HObject ho_Cross = null; // Local control variables HObject ho_Region; HTuple hv_vOrh; HTuple hv_maxDist = new HTuple(), hv_minDist = new HTuple(); HTuple hv_meanDist = new HTuple(), hv_Exception; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Region); HOperatorSet.GenEmptyObj(out ho_Rectangle); HOperatorSet.GenEmptyObj(out ho_ImageReduced); HOperatorSet.GenEmptyObj(out ho_RegionToDetect); HOperatorSet.GenEmptyObj(out ho_RegionOpening); HOperatorSet.GenEmptyObj(out ho_RegionDifference); HOperatorSet.GenEmptyObj(out ho_Cross); try { //HOperatorSet.SetDraw(this.LWindowHandle, "margin"); //disp_message(this.LWindowHandle, "选中一个牙", "window", 12, 12, "black", // "true"); //HOperatorSet.DrawRectangle1(this.LWindowHandle, out hv_Row1, out hv_Column1, // out hv_Row2, out hv_Column2); //ho_Rectangle.Dispose(); //HOperatorSet.GenRectangle1(out ho_Rectangle, hv_Row1, hv_Column1, hv_Row2, // hv_Column2); //disp_message(this.LWindowHandle, "选中检测区域", "window", 12, 12, "black", // "true"); //HOperatorSet.DrawRectangle1(this.LWindowHandle, out hv_Row11, out hv_Column11, // out hv_Row21, out hv_Column21); ho_Rectangle.Dispose(); HOperatorSet.GenRectangle1(out ho_Rectangle, this.rr1, this.rc1, this.rr2, this.rc2); ho_Region.Dispose(); HOperatorSet.Intersection(ho_Rectangle, this.algorithm.Region, out ho_Region); //ho_ImageReduced.Dispose(); //HOperatorSet.ReduceDomain(ho_Image, ho_Rectangle, out ho_ImageReduced); //ho_RegionToDetect.Dispose(); //HOperatorSet.Threshold(ho_ImageReduced, out ho_RegionToDetect, 0, 128); ho_RegionOpening.Dispose(); HOperatorSet.OpeningRectangle1(ho_Region, out ho_RegionOpening, tc2 - tc1, tr2 - tr1); ho_RegionDifference.Dispose(); HOperatorSet.Difference(ho_Region, ho_RegionOpening, out ho_RegionDifference ); hv_vOrh = "h"; try { ho_Cross.Dispose(); TeethDist(ho_RegionDifference, out ho_Cross, hv_vOrh, out hv_maxDist, out hv_minDist, out hv_meanDist); if (ho_Cross.IsInitialized()) { if (RegionToDisp.IsInitialized()) { HOperatorSet.ConcatObj(ho_Cross, RegionToDisp, out RegionToDisp); HOperatorSet.GenRegionContourXld(ho_Cross, out RegionToDisp, "filled"); HOperatorSet.Union1(RegionToDisp, out RegionToDisp); } else { HOperatorSet.GenRegionContourXld(ho_Cross, out RegionToDisp, "filled"); HOperatorSet.Union1(RegionToDisp, out RegionToDisp); } } } // catch (Exception) catch (HalconException HDevExpDefaultException1) { HDevExpDefaultException1.ToHTuple(out hv_Exception); hv_maxDist = 0; hv_minDist = 0; hv_meanDist = 0; } finally { HTuple hv_result = new HTuple(); hv_result = hv_result.TupleConcat("最大间距"); hv_result = hv_result.TupleConcat(hv_maxDist * pixeldist); hv_result = hv_result.TupleConcat("最小间距"); hv_result = hv_result.TupleConcat(hv_minDist * pixeldist); hv_result = hv_result.TupleConcat("平均间距"); hv_result = hv_result.TupleConcat(hv_meanDist * pixeldist); result = hv_result.Clone(); } } catch (HalconException HDevExpDefaultException) { ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_RegionToDetect.Dispose(); ho_RegionOpening.Dispose(); ho_RegionDifference.Dispose(); ho_Cross.Dispose(); throw HDevExpDefaultException; } ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_RegionToDetect.Dispose(); ho_RegionOpening.Dispose(); ho_RegionDifference.Dispose(); ho_Cross.Dispose(); }
/// <summary> /// 平移图像 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void VisionControl_MouseMove(object sender, MouseEventArgs e) { if (MouseMode == WindowMouseMode.Move && isOpen()) { if (this.Cursor == Cursors.Hand) { int x = e.X - ptMouse.X; int y = e.Y - ptMouse.Y; if (Math.Abs(x) > 2 || Math.Abs(y) > 2) { ptMouse.X = e.X; ptMouse.Y = e.Y; HTuple row0, col0, row1, col1; bool bUpdate = false; LockDisplay(); try { HOperatorSet.GetPart(m_windowHandle, out row0, out col0, out row1, out col1); double zoom = 1.0 * (row1 - row0) / this.Height; x = (int)(x * zoom); y = (int)(y * zoom); HOperatorSet.SetPart(m_windowHandle, row0 - y, col0 - x, row1 - y, col1 - x); //清理窗口前关闭刷新,以免闪烁 HOperatorSet.SetSystem("flush_graphic", "false"); HOperatorSet.ClearWindow(m_windowHandle); HOperatorSet.SetSystem("flush_graphic", "true"); bUpdate = true; } catch (HalconException HDevExpDefaultException1) { System.Diagnostics.Debug.WriteLine(HDevExpDefaultException1.ToString()); } catch (Exception exp) { System.Diagnostics.Debug.WriteLine(exp.ToString()); } finally { UnlockDisplay(); } if (bUpdate && m_IVisionControlUpdate != null) { Action <object> action = (object obj) => { m_IVisionControlUpdate.UpdateVisionControl(this); }; Task t1 = new Task(action, ""); t1.Start(); t1.Wait(); } } } } }
/// <summary> /// 鼠标滚动时缩放图片大小 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void VisionControl_MouseWheel(object sender, MouseEventArgs e) { if (MouseMode == WindowMouseMode.Move && isOpen()) { HTuple row, col, button; HTuple row0, col0, row1, col1; bool bUpdate = false; LockDisplay(); try { HOperatorSet.GetMposition(m_windowHandle, out row, out col, out button); HOperatorSet.GetPart(m_windowHandle, out row0, out col0, out row1, out col1); HTuple width = col1 - col0; HTuple height = row1 - row0; float k = (float)width / ImageWidth; if ((k < 50 && e.Delta < 0) || (k > 0.02 && e.Delta > 0)) { HTuple Zoom; if (e.Delta > 0) { Zoom = 1.3; } else { Zoom = 1 / 1.3; } HTuple r1 = (row0 + ((1 - (1.0 / Zoom)) * (row - row0))); HTuple c1 = (col0 + ((1 - (1.0 / Zoom)) * (col - col0))); HTuple r2 = r1 + (height / Zoom); HTuple c2 = c1 + (width / Zoom); HOperatorSet.SetPart(m_windowHandle, r1, c1, r2, c2); //清理窗口前关闭刷新,以免闪烁 HOperatorSet.SetSystem("flush_graphic", "false"); HOperatorSet.ClearWindow(m_windowHandle); HOperatorSet.SetSystem("flush_graphic", "true"); bUpdate = true; } } catch (HalconException HDevExpDefaultException1) { System.Diagnostics.Debug.WriteLine(HDevExpDefaultException1.ToString()); } catch (Exception exp) { System.Diagnostics.Debug.WriteLine(exp.ToString()); } finally { UnlockDisplay(); } if (bUpdate && m_IVisionControlUpdate != null) { Action <object> action = (object obj) => { m_IVisionControlUpdate.UpdateVisionControl(this); }; Task t1 = new Task(action, ""); t1.Start(); t1.Wait(); } } }
private void action3() { // Local iconic variables HObject ho_Image, ho_ROI_0 = null, ho_ImageReduced = null; HObject ho_Regions = null, ho_RegionFillUp1 = null, ho_Connection = null; HObject ho_SelectedRegions1 = null, ho_Contours = null, ho_SmoothedContours = null; // Local control variables HTuple hv_AcqHandle = null, hv_Width = new HTuple(); HTuple hv_Height = new HTuple(), hv_SelectNumber = new HTuple(); HTuple hv_Row = new HTuple(), hv_Col = new HTuple(), hv_HalfH = new HTuple(); HTuple hv_HalfW = new HTuple(), hv_TupleMin = new HTuple(); HTuple hv_IndexMin = new HTuple(), hv_ColumMin = new HTuple(); HTuple hv_rowToMin0 = new HTuple(), hv_colToMin0 = new HTuple(); // HTuple hv_output = new HTuple(), //public float hv_fOutput = 0.0f; HTuple hv_outputmm = new HTuple(); HTuple hv_Exception = null, hv_MessageError = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image); HOperatorSet.GenEmptyObj(out ho_ROI_0); HOperatorSet.GenEmptyObj(out ho_ImageReduced); HOperatorSet.GenEmptyObj(out ho_Regions); HOperatorSet.GenEmptyObj(out ho_RegionFillUp1); HOperatorSet.GenEmptyObj(out ho_Connection); HOperatorSet.GenEmptyObj(out ho_SelectedRegions1); HOperatorSet.GenEmptyObj(out ho_Contours); HOperatorSet.GenEmptyObj(out ho_SmoothedContours); try { //************************************************************ //ONE SHOT APP //************************************************************ //Image Acquisition: HOperatorSet.OpenFramegrabber("GigEVision", 0, 0, 0, 0, 0, 0, "default", -1, "default", -1, "false", "default", "GC3851M_CAM_4", 0, -1, out hv_AcqHandle); // HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "ExposureTime", 2000.0); HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "ExposureTime", 3500.0); HOperatorSet.GrabImageStart(hv_AcqHandle, -1); ho_Image.Dispose(); HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1); try { //Image Processing: //Find the edge conture //dev_display (Image) HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height); ho_ROI_0.Dispose(); HOperatorSet.GenRectangle1(out ho_ROI_0, 1240, 25, 1650, 3750); ho_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_ROI_0, out ho_ImageReduced); //No. 1 - dia 1 ho_Regions.Dispose(); HOperatorSet.Threshold(ho_ImageReduced, out ho_Regions, 0, 60); ho_RegionFillUp1.Dispose(); HOperatorSet.FillUp(ho_Regions, out ho_RegionFillUp1); ho_Connection.Dispose(); HOperatorSet.Connection(ho_Regions, out ho_Connection); //No. 1 - dia 1 ho_SelectedRegions1.Dispose(); HOperatorSet.SelectShape(ho_Connection, out ho_SelectedRegions1, "area", "and", 300000, 800000); HOperatorSet.CountObj(ho_SelectedRegions1, out hv_SelectNumber); ho_Contours.Dispose(); HOperatorSet.GenContourRegionXld(ho_SelectedRegions1, out ho_Contours, "border"); //Smoth edge conture ho_SmoothedContours.Dispose(); HOperatorSet.SmoothContoursXld(ho_Contours, out ho_SmoothedContours, 29); HOperatorSet.GetContourXld(ho_SmoothedContours, out hv_Row, out hv_Col); //Define constants: hv_HalfH = hv_Height / 2; hv_HalfW = hv_Width / 2; //* Define min value from tuple HOperatorSet.TupleMin(hv_Row, out hv_TupleMin); HOperatorSet.TupleFindFirst(hv_Row, hv_TupleMin, out hv_IndexMin); hv_ColumMin = hv_Col.TupleSelect(hv_IndexMin); hv_rowToMin0 = hv_Row.TupleSelect(hv_IndexMin); hv_colToMin0 = hv_Col.TupleSelect(hv_IndexMin); hv_output = 1382 - (hv_Row.TupleSelect(hv_IndexMin)); hv_outputmm = ((1382 - (hv_Row.TupleSelect(hv_IndexMin))) / 3) * 0.005; } // catch (Exception) catch (HalconException HDevExpDefaultException1) { HDevExpDefaultException1.ToHTuple(out hv_Exception); hv_MessageError = " ERROR: Not able to analize photo!"; } HOperatorSet.CloseFramegrabber(hv_AcqHandle); } catch (HalconException HDevExpDefaultException) { ho_Image.Dispose(); ho_ROI_0.Dispose(); ho_ImageReduced.Dispose(); ho_Regions.Dispose(); ho_RegionFillUp1.Dispose(); ho_Connection.Dispose(); ho_SelectedRegions1.Dispose(); ho_Contours.Dispose(); ho_SmoothedContours.Dispose(); throw HDevExpDefaultException; } ho_Image.Dispose(); ho_ROI_0.Dispose(); ho_ImageReduced.Dispose(); ho_Regions.Dispose(); ho_RegionFillUp1.Dispose(); ho_Connection.Dispose(); ho_SelectedRegions1.Dispose(); ho_Contours.Dispose(); ho_SmoothedContours.Dispose(); }
// Chapter: Graphics / Text // Short Description: Set font independent of OS public void set_display_font(HTuple hv_WindowHandle, HTuple hv_Size, HTuple hv_Font, HTuple hv_Bold, HTuple hv_Slant) { // Local iconic variables // Local control variables HTuple hv_OS = null, hv_BufferWindowHandle = new HTuple(); HTuple hv_Ascent = new HTuple(), hv_Descent = new HTuple(); HTuple hv_Width = new HTuple(), hv_Height = new HTuple(); HTuple hv_Scale = new HTuple(), hv_Exception = new HTuple(); HTuple hv_SubFamily = new HTuple(), hv_Fonts = new HTuple(); HTuple hv_SystemFonts = new HTuple(), hv_Guess = new HTuple(); HTuple hv_I = new HTuple(), hv_Index = new HTuple(), hv_AllowedFontSizes = new HTuple(); HTuple hv_Distances = new HTuple(), hv_Indices = new HTuple(); HTuple hv_FontSelRegexp = new HTuple(), hv_FontsCourier = new HTuple(); HTuple hv_Bold_COPY_INP_TMP = hv_Bold.Clone(); HTuple hv_Font_COPY_INP_TMP = hv_Font.Clone(); HTuple hv_Size_COPY_INP_TMP = hv_Size.Clone(); HTuple hv_Slant_COPY_INP_TMP = hv_Slant.Clone(); // Initialize local and output iconic variables //This procedure sets the text font of the current window with //the specified attributes. //It is assumed that following fonts are installed on the system: //Windows: Courier New, Arial Times New Roman //Mac OS X: CourierNewPS, Arial, TimesNewRomanPS //Linux: courier, helvetica, times //Because fonts are displayed smaller on Linux than on Windows, //a scaling factor of 1.25 is used the get comparable results. //For Linux, only a limited number of font sizes is supported, //to get comparable results, it is recommended to use one of the //following sizes: 9, 11, 14, 16, 20, 27 //(which will be mapped internally on Linux systems to 11, 14, 17, 20, 25, 34) // //Input parameters: //WindowHandle: The graphics window for which the font will be set //Size: The font size. If Size=-1, the default of 16 is used. //Bold: If set to 'true', a bold font is used //Slant: If set to 'true', a slanted font is used // HOperatorSet.GetSystem("operating_system", out hv_OS); // dev_get_preferences(...); only in hdevelop // dev_set_preferences(...); only in hdevelop if ((int)((new HTuple(hv_Size_COPY_INP_TMP.TupleEqual(new HTuple()))).TupleOr( new HTuple(hv_Size_COPY_INP_TMP.TupleEqual(-1)))) != 0) { hv_Size_COPY_INP_TMP = 16; } if ((int)(new HTuple(((hv_OS.TupleSubstr(0, 2))).TupleEqual("Win"))) != 0) { //Set font on Windows systems try { //Check, if font scaling is switched on //open_window(...); HOperatorSet.SetFont(hv_ExpDefaultWinHandle, "-Consolas-16-*-0-*-*-1-"); HOperatorSet.GetStringExtents(hv_ExpDefaultWinHandle, "test_string", out hv_Ascent, out hv_Descent, out hv_Width, out hv_Height); //Expected width is 110 hv_Scale = 110.0 / hv_Width; hv_Size_COPY_INP_TMP = ((hv_Size_COPY_INP_TMP * hv_Scale)).TupleInt(); //close_window(...); } // catch (Exception) catch (HalconException HDevExpDefaultException1) { HDevExpDefaultException1.ToHTuple(out hv_Exception); //throw (Exception) } if ((int)((new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("Courier"))).TupleOr( new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("courier")))) != 0) { hv_Font_COPY_INP_TMP = "Courier New"; } else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("mono"))) != 0) { hv_Font_COPY_INP_TMP = "Consolas"; } else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("sans"))) != 0) { hv_Font_COPY_INP_TMP = "Arial"; } else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("serif"))) != 0) { hv_Font_COPY_INP_TMP = "Times New Roman"; } if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("true"))) != 0) { hv_Bold_COPY_INP_TMP = 1; } else if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("false"))) != 0) { hv_Bold_COPY_INP_TMP = 0; } else { hv_Exception = "Wrong value of control parameter Bold"; throw new HalconException(hv_Exception); } if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual("true"))) != 0) { hv_Slant_COPY_INP_TMP = 1; } else if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual("false"))) != 0) { hv_Slant_COPY_INP_TMP = 0; } else { hv_Exception = "Wrong value of control parameter Slant"; throw new HalconException(hv_Exception); } try { HOperatorSet.SetFont(hv_ExpDefaultWinHandle, ((((((("-" + hv_Font_COPY_INP_TMP) + "-") + hv_Size_COPY_INP_TMP) + "-*-") + hv_Slant_COPY_INP_TMP) + "-*-*-") + hv_Bold_COPY_INP_TMP) + "-"); } // catch (Exception) catch (HalconException HDevExpDefaultException1) { HDevExpDefaultException1.ToHTuple(out hv_Exception); //throw (Exception) } } else if ((int)(new HTuple(((hv_OS.TupleSubstr(0, 2))).TupleEqual("Dar"))) != 0) { //Set font on Mac OS X systems. Since OS X does not have a strict naming //scheme for font attributes, we use tables to determine the correct font //name. hv_SubFamily = 0; if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual("true"))) != 0) { hv_SubFamily = hv_SubFamily.TupleBor(1); } else if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleNotEqual("false"))) != 0) { hv_Exception = "Wrong value of control parameter Slant"; throw new HalconException(hv_Exception); } if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("true"))) != 0) { hv_SubFamily = hv_SubFamily.TupleBor(2); } else if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleNotEqual("false"))) != 0) { hv_Exception = "Wrong value of control parameter Bold"; throw new HalconException(hv_Exception); } if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("mono"))) != 0) { hv_Fonts = new HTuple(); hv_Fonts[0] = "Menlo-Regular"; hv_Fonts[1] = "Menlo-Italic"; hv_Fonts[2] = "Menlo-Bold"; hv_Fonts[3] = "Menlo-BoldItalic"; } else if ((int)((new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("Courier"))).TupleOr( new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("courier")))) != 0) { hv_Fonts = new HTuple(); hv_Fonts[0] = "CourierNewPSMT"; hv_Fonts[1] = "CourierNewPS-ItalicMT"; hv_Fonts[2] = "CourierNewPS-BoldMT"; hv_Fonts[3] = "CourierNewPS-BoldItalicMT"; } else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("sans"))) != 0) { hv_Fonts = new HTuple(); hv_Fonts[0] = "ArialMT"; hv_Fonts[1] = "Arial-ItalicMT"; hv_Fonts[2] = "Arial-BoldMT"; hv_Fonts[3] = "Arial-BoldItalicMT"; } else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("serif"))) != 0) { hv_Fonts = new HTuple(); hv_Fonts[0] = "TimesNewRomanPSMT"; hv_Fonts[1] = "TimesNewRomanPS-ItalicMT"; hv_Fonts[2] = "TimesNewRomanPS-BoldMT"; hv_Fonts[3] = "TimesNewRomanPS-BoldItalicMT"; } else { //Attempt to figure out which of the fonts installed on the system //the user could have meant. HOperatorSet.QueryFont(hv_ExpDefaultWinHandle, out hv_SystemFonts); hv_Fonts = new HTuple(); hv_Fonts = hv_Fonts.TupleConcat(hv_Font_COPY_INP_TMP); hv_Fonts = hv_Fonts.TupleConcat(hv_Font_COPY_INP_TMP); hv_Fonts = hv_Fonts.TupleConcat(hv_Font_COPY_INP_TMP); hv_Fonts = hv_Fonts.TupleConcat(hv_Font_COPY_INP_TMP); hv_Guess = new HTuple(); hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP); hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "-Regular"); hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "MT"); for (hv_I = 0; (int)hv_I <= (int)((new HTuple(hv_Guess.TupleLength())) - 1); hv_I = (int)hv_I + 1) { HOperatorSet.TupleFind(hv_SystemFonts, hv_Guess.TupleSelect(hv_I), out hv_Index); if ((int)(new HTuple(hv_Index.TupleNotEqual(-1))) != 0) { if (hv_Fonts == null) { hv_Fonts = new HTuple(); } hv_Fonts[0] = hv_Guess.TupleSelect(hv_I); break; } } //Guess name of slanted font hv_Guess = new HTuple(); hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "-Italic"); hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "-ItalicMT"); hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "-Oblique"); for (hv_I = 0; (int)hv_I <= (int)((new HTuple(hv_Guess.TupleLength())) - 1); hv_I = (int)hv_I + 1) { HOperatorSet.TupleFind(hv_SystemFonts, hv_Guess.TupleSelect(hv_I), out hv_Index); if ((int)(new HTuple(hv_Index.TupleNotEqual(-1))) != 0) { if (hv_Fonts == null) { hv_Fonts = new HTuple(); } hv_Fonts[1] = hv_Guess.TupleSelect(hv_I); break; } } //Guess name of bold font hv_Guess = new HTuple(); hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "-Bold"); hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "-BoldMT"); for (hv_I = 0; (int)hv_I <= (int)((new HTuple(hv_Guess.TupleLength())) - 1); hv_I = (int)hv_I + 1) { HOperatorSet.TupleFind(hv_SystemFonts, hv_Guess.TupleSelect(hv_I), out hv_Index); if ((int)(new HTuple(hv_Index.TupleNotEqual(-1))) != 0) { if (hv_Fonts == null) { hv_Fonts = new HTuple(); } hv_Fonts[2] = hv_Guess.TupleSelect(hv_I); break; } } //Guess name of bold slanted font hv_Guess = new HTuple(); hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "-BoldItalic"); hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "-BoldItalicMT"); hv_Guess = hv_Guess.TupleConcat(hv_Font_COPY_INP_TMP + "-BoldOblique"); for (hv_I = 0; (int)hv_I <= (int)((new HTuple(hv_Guess.TupleLength())) - 1); hv_I = (int)hv_I + 1) { HOperatorSet.TupleFind(hv_SystemFonts, hv_Guess.TupleSelect(hv_I), out hv_Index); if ((int)(new HTuple(hv_Index.TupleNotEqual(-1))) != 0) { if (hv_Fonts == null) { hv_Fonts = new HTuple(); } hv_Fonts[3] = hv_Guess.TupleSelect(hv_I); break; } } } hv_Font_COPY_INP_TMP = hv_Fonts.TupleSelect(hv_SubFamily); try { HOperatorSet.SetFont(hv_ExpDefaultWinHandle, (hv_Font_COPY_INP_TMP + "-") + hv_Size_COPY_INP_TMP); } // catch (Exception) catch (HalconException HDevExpDefaultException1) { HDevExpDefaultException1.ToHTuple(out hv_Exception); //throw (Exception) } } else { //Set font for UNIX systems hv_Size_COPY_INP_TMP = hv_Size_COPY_INP_TMP * 1.25; hv_AllowedFontSizes = new HTuple(); hv_AllowedFontSizes[0] = 11; hv_AllowedFontSizes[1] = 14; hv_AllowedFontSizes[2] = 17; hv_AllowedFontSizes[3] = 20; hv_AllowedFontSizes[4] = 25; hv_AllowedFontSizes[5] = 34; if ((int)(new HTuple(((hv_AllowedFontSizes.TupleFind(hv_Size_COPY_INP_TMP))).TupleEqual( -1))) != 0) { hv_Distances = ((hv_AllowedFontSizes - hv_Size_COPY_INP_TMP)).TupleAbs(); HOperatorSet.TupleSortIndex(hv_Distances, out hv_Indices); hv_Size_COPY_INP_TMP = hv_AllowedFontSizes.TupleSelect(hv_Indices.TupleSelect( 0)); } if ((int)((new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("mono"))).TupleOr(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual( "Courier")))) != 0) { hv_Font_COPY_INP_TMP = "courier"; } else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("sans"))) != 0) { hv_Font_COPY_INP_TMP = "helvetica"; } else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("serif"))) != 0) { hv_Font_COPY_INP_TMP = "times"; } if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("true"))) != 0) { hv_Bold_COPY_INP_TMP = "bold"; } else if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("false"))) != 0) { hv_Bold_COPY_INP_TMP = "medium"; } else { hv_Exception = "Wrong value of control parameter Bold"; throw new HalconException(hv_Exception); } if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual("true"))) != 0) { if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("times"))) != 0) { hv_Slant_COPY_INP_TMP = "i"; } else { hv_Slant_COPY_INP_TMP = "o"; } } else if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual("false"))) != 0) { hv_Slant_COPY_INP_TMP = "r"; } else { hv_Exception = "Wrong value of control parameter Slant"; throw new HalconException(hv_Exception); } try { HOperatorSet.SetFont(hv_ExpDefaultWinHandle, ((((((("-adobe-" + hv_Font_COPY_INP_TMP) + "-") + hv_Bold_COPY_INP_TMP) + "-") + hv_Slant_COPY_INP_TMP) + "-normal-*-") + hv_Size_COPY_INP_TMP) + "-*-*-*-*-*-*-*"); } // catch (Exception) catch (HalconException HDevExpDefaultException1) { HDevExpDefaultException1.ToHTuple(out hv_Exception); if ((int)((new HTuple(((hv_OS.TupleSubstr(0, 4))).TupleEqual("Linux"))).TupleAnd( new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("courier")))) != 0) { HOperatorSet.QueryFont(hv_ExpDefaultWinHandle, out hv_Fonts); hv_FontSelRegexp = (("^-[^-]*-[^-]*[Cc]ourier[^-]*-" + hv_Bold_COPY_INP_TMP) + "-") + hv_Slant_COPY_INP_TMP; hv_FontsCourier = ((hv_Fonts.TupleRegexpSelect(hv_FontSelRegexp))).TupleRegexpMatch( hv_FontSelRegexp); if ((int)(new HTuple((new HTuple(hv_FontsCourier.TupleLength())).TupleEqual( 0))) != 0) { hv_Exception = "Wrong font name"; //throw (Exception) } else { try { HOperatorSet.SetFont(hv_ExpDefaultWinHandle, (((hv_FontsCourier.TupleSelect( 0)) + "-normal-*-") + hv_Size_COPY_INP_TMP) + "-*-*-*-*-*-*-*"); } // catch (Exception) catch (HalconException HDevExpDefaultException2) { HDevExpDefaultException2.ToHTuple(out hv_Exception); //throw (Exception) } } } //throw (Exception) } } // dev_set_preferences(...); only in hdevelop return; }
// Main procedure private void action() { // Stack for temporary objects HObject[] OTemp = new HObject[20]; // Local iconic variables HObject ho_Image = null, ho_SymbolXLDs = null; HObject ho_SymbolRegions = null, ho_ObjectSelected = null; // Local control variables HTuple hv_BarWidth = new HTuple(), hv_BarHeight = new HTuple(); HTuple hv_DataCodeHandle = new HTuple(), hv_BarCodeHandle = new HTuple(); HTuple hv_WindowHandle = new HTuple(), hv_AcqHandle = new HTuple(); HTuple hv_ResultHandles = new HTuple(), hv_DecodedDataStrings = new HTuple(); HTuple hv_someitem = new HTuple(), hv_Area = new HTuple(); HTuple hv_BarIndex = new HTuple(), hv_Row = new HTuple(); HTuple hv_Column = new HTuple(), hv_Exception = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image); HOperatorSet.GenEmptyObj(out ho_SymbolXLDs); HOperatorSet.GenEmptyObj(out ho_SymbolRegions); HOperatorSet.GenEmptyObj(out ho_ObjectSelected); try { //*** //** INIT //* INIT CONST hv_BarWidth.Dispose(); hv_BarWidth = 800; hv_BarHeight.Dispose(); hv_BarHeight = 100; //* INIT IMAGE //* INIT DATACODE hv_DataCodeHandle.Dispose(); HOperatorSet.CreateDataCode2dModel("Data Matrix ECC 200", new HTuple(), new HTuple(), out hv_DataCodeHandle); //* INIT BARCODE hv_BarCodeHandle.Dispose(); HOperatorSet.CreateBarCodeModel(new HTuple(), new HTuple(), out hv_BarCodeHandle); HOperatorSet.SetBarCodeParam(hv_BarCodeHandle, "quiet_zone", "true"); //* INIT LOC //* Info: //read_shape_model ('C:/Users/iwake/OneDrive - wake/Desktop/Localization/InvV1CaliInfo.shm', InfoModel) //get_shape_model_contours (InfoModelContours, InfoModel, 1) //* Sign //read_shape_model ('C:/Users/iwake/OneDrive - wake/Desktop/Localization/InvV1CaliSign.shm', SignModel) //get_shape_model_contours (SignModelContours, SignModel, 1) //*** //** DISPLAY //* DISPLAY INIT hDevelo.dev_update_off(); if (HDevWindowStack.IsOpen()) { HOperatorSet.CloseWindow(HDevWindowStack.Pop()); } HOperatorSet.SetWindowAttr("background_color", "black"); //HOperatorSet.OpenWindow(30, 0, 800, 1500, 0, "visible", "", out hv_WindowHandle); HDevWindowStack.Push(hv_ExpDefaultWinHandle); //*** //** LOOP //Image Acquisition 01: Code generated by Image Acquisition 01 hv_AcqHandle.Dispose(); hv_AcqHandle = acqHandle; while ((int)(1) != 0) { ho_Image.Dispose(); HOperatorSet.GrabImage(out ho_Image, hv_AcqHandle); { HObject ExpTmpOutVar_0; hDevelo.image_cali_map(ho_Image, out ExpTmpOutVar_0, new HTuple(), new HTuple()); ho_Image.Dispose(); ho_Image = ExpTmpOutVar_0; HOperatorSet.DispObj(ho_Image, hv_ExpImageRawWinHandle); } try { //** PRE //find_shape_model (Image, SignModel, rad(0), rad(360), 0.3, 1, 0.5, 'least_squares', [7,1], 0.6, InfoRow, InfoColumn, InfoAngle, InfoScore) //rotate_image (Image, Image, deg(-InfoAngle), 'constant') if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } //** RECOGNITION //* DataCode ho_SymbolXLDs.Dispose(); hv_ResultHandles.Dispose(); hv_DecodedDataStrings.Dispose(); HOperatorSet.FindDataCode2d(ho_Image, out ho_SymbolXLDs, hv_DataCodeHandle, "stop_after_result_num", 3, out hv_ResultHandles, out hv_DecodedDataStrings); hDevelo.image_display_datacode(ho_SymbolXLDs, hv_ResultHandles, hv_WindowHandle, hv_DecodedDataStrings, hv_DataCodeHandle); //* BARCODE ho_SymbolRegions.Dispose(); hv_DecodedDataStrings.Dispose(); hv_someitem.Dispose(); hDevelo.image_get_bar(ho_Image, out ho_SymbolRegions, hv_BarCodeHandle, out hv_DecodedDataStrings, out hv_someitem); //* LOC //* Info: //find_shape_model (Image, InfoModel, rad(0), rad(360), 0.3, 1, 0.5, 'least_squares', [7,1], 0.7, InfoRow, InfoColumn, InfoAngle, InfoScore) //* HaedSign //find_shape_model (Image, SignModel, rad(0), rad(360), 0.3, 1, 0.5, 'least_squares', [7,1], 0.7, SignRow, SignColumn, SignAngle, SignScore) //* Ocr //gen_rectangle2 (ROI_OCR_01_0, InfoRow + 70, InfoColumn - 700, InfoAngle, 100, 30) //region_ocr_num_svm (Image, ROI_OCR_01_0, [], [], SymbolNames_OCR_01_0, Ocr_Split) //area_center (ROI_OCR_01_0, Area, IDRow, IDColumn) //smallest_rectangle1 (ROI_OCR_01_0, IDRow1, IDColumn1, IDRow2, IDColumn2) //height_width_ratio (ROI_OCR_01_0, IDHeight, IDWidth, IDRatio) //* Sign //HeadSignScale := 1 //HeadSignRow := SignRow //HeadSignCol := SignColumn //HeadPhi := SignAngle //region_judge_sign (Image, EDGE, HeadSignScale, HeadSignRow, HeadSignCol, HeadPhi, WindowHandle, sign) //** DISPLAY //* DISPLAY BARCODE hDevelo.set_display_font(hv_WindowHandle, 14, "mono", "true", "false"); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetDraw(HDevWindowStack.GetActive(), "margin"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetLineWidth(HDevWindowStack.GetActive(), 3); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "forest green"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_SymbolRegions, HDevWindowStack.GetActive()); } for (hv_BarIndex = 1; (int)hv_BarIndex <= (int)(new HTuple(hv_DecodedDataStrings.TupleLength() )); hv_BarIndex = (int)hv_BarIndex + 1) { ho_ObjectSelected.Dispose(); HOperatorSet.SelectObj(ho_SymbolRegions, out ho_ObjectSelected, hv_BarIndex); hv_Area.Dispose(); hv_Row.Dispose(); hv_Column.Dispose(); HOperatorSet.AreaCenter(ho_ObjectSelected, out hv_Area, out hv_Row, out hv_Column); using (HDevDisposeHelper dh = new HDevDisposeHelper()) { HOperatorSet.SetTposition(hv_WindowHandle, hv_Row - hv_BarHeight, hv_Column - (0.25 * hv_BarWidth)); } using (HDevDisposeHelper dh = new HDevDisposeHelper()) { HOperatorSet.WriteString(hv_WindowHandle, hv_DecodedDataStrings.TupleSelect( hv_BarIndex - 1)); } } //* DISPLAY LOC if (HDevWindowStack.IsOpen()) { HOperatorSet.SetLineWidth(HDevWindowStack.GetActive(), 1); } //* Info: //hom_mat2d_identity (InfoHomMat2D) //hom_mat2d_rotate (InfoHomMat2D, InfoAngle, 0, 0, InfoHomMat2D) //hom_mat2d_translate (InfoHomMat2D, InfoRow, InfoColumn, InfoHomMat2D) //affine_trans_contour_xld (InfoModelContours, InfoTransContours, InfoHomMat2D) if (HDevWindowStack.IsOpen()) { //dev_set_color ('green') } if (HDevWindowStack.IsOpen()) { //dev_display (InfoTransContours) } //* Ocr if (HDevWindowStack.IsOpen()) { //dev_disp_text (Ocr_Split, 'image', IDRow1 + IDHeight, IDColumn1, 'blue', [], []) } //* HeadSign //hom_mat2d_identity (SignHomMat2D) //hom_mat2d_rotate (SignHomMat2D, SignAngle, 0, 0, SignHomMat2D) //hom_mat2d_translate (SignHomMat2D, SignRow, SignColumn, SignHomMat2D) //affine_trans_contour_xld (SignModelContours, SignTransContours, SignHomMat2D) if (HDevWindowStack.IsOpen()) { //dev_set_color ('green') } if (HDevWindowStack.IsOpen()) { //dev_display (SignTransContours) } //* Sign if (HDevWindowStack.IsOpen()) { //dev_set_colored (12) } if (HDevWindowStack.IsOpen()) { //dev_display (EDGE) } //dump_window_image (ImageResult, WindowHandle) //stop () } // catch (Exception) catch (HalconException HDevExpDefaultException1) { HDevExpDefaultException1.ToHTuple(out hv_Exception); } } HOperatorSet.CloseFramegrabber(hv_AcqHandle); } catch (HalconException HDevExpDefaultException) { ho_Image.Dispose(); ho_SymbolXLDs.Dispose(); ho_SymbolRegions.Dispose(); ho_ObjectSelected.Dispose(); hv_BarWidth.Dispose(); hv_BarHeight.Dispose(); hv_DataCodeHandle.Dispose(); hv_BarCodeHandle.Dispose(); hv_WindowHandle.Dispose(); hv_AcqHandle.Dispose(); hv_ResultHandles.Dispose(); hv_DecodedDataStrings.Dispose(); hv_someitem.Dispose(); hv_Area.Dispose(); hv_BarIndex.Dispose(); hv_Row.Dispose(); hv_Column.Dispose(); hv_Exception.Dispose(); throw HDevExpDefaultException; } ho_Image.Dispose(); ho_SymbolXLDs.Dispose(); ho_SymbolRegions.Dispose(); ho_ObjectSelected.Dispose(); hv_BarWidth.Dispose(); hv_BarHeight.Dispose(); hv_DataCodeHandle.Dispose(); hv_BarCodeHandle.Dispose(); hv_WindowHandle.Dispose(); hv_AcqHandle.Dispose(); hv_ResultHandles.Dispose(); hv_DecodedDataStrings.Dispose(); hv_someitem.Dispose(); hv_Area.Dispose(); hv_BarIndex.Dispose(); hv_Row.Dispose(); hv_Column.Dispose(); hv_Exception.Dispose(); }
private void action1() { // Local iconic variables HObject ho_Image = null, ho_Regions = null, ho_Connection = null; HObject ho_SelectedRegions1 = null, ho_Contours = null, ho_SmoothedContours = null; HObject ho_CrossMax = null, ho_CrossMin = null; // Local control variables HTuple hv_AcqHandle = new HTuple(), hv_Width = new HTuple(); HTuple hv_Height = new HTuple(), hv_SelectNumber = new HTuple(); HTuple hv_Row = new HTuple(), hv_Col = new HTuple(), hv_TupleMax = new HTuple(); HTuple hv_IndexMax = new HTuple(), hv_HalfH = new HTuple(); HTuple hv_HalfW = new HTuple(), hv_ColumMax = new HTuple(); HTuple hv_rowToMax0 = new HTuple(), hv_colToMax0 = new HTuple(); //HTuple hv_output = new HTuple() HTuple hv_outputmm = new HTuple(); HTuple hv_MessageMax = new HTuple(), hv_MessagePos = new HTuple(); HTuple hv_Messagemm = new HTuple(), hv_TupleMin = new HTuple(); HTuple hv_IndexMin = new HTuple(), hv_ColumMin = new HTuple(); HTuple hv_rowToMin0 = new HTuple(), hv_colToMin0 = new HTuple(); HTuple hv_Exception = new HTuple(), hv_MessageError = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image); HOperatorSet.GenEmptyObj(out ho_Regions); HOperatorSet.GenEmptyObj(out ho_Connection); HOperatorSet.GenEmptyObj(out ho_SelectedRegions1); HOperatorSet.GenEmptyObj(out ho_Contours); HOperatorSet.GenEmptyObj(out ho_SmoothedContours); HOperatorSet.GenEmptyObj(out ho_CrossMax); HOperatorSet.GenEmptyObj(out ho_CrossMin); try { //************************************************************ //LIVE APP //************************************************************ while ((int)(1) != 0) { HOperatorSet.OpenFramegrabber("GigEVision", 0, 0, 0, 0, 0, 0, "default", -1, "default", -1, "false", "default", "GC3851M_CAM_4", 0, -1, out hv_AcqHandle); // HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "ExposureTime", 2000.0); HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "ExposureTime", 3500.0); HOperatorSet.GrabImageStart(hv_AcqHandle, -1); ho_Image.Dispose(); HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1); try { //Find the edge conture //dev_display (Image) HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height); ho_Regions.Dispose(); // HOperatorSet.Threshold(ho_Image, out ho_Regions, 0, 30); "ExposureTime", 2000.0 HOperatorSet.Threshold(ho_Image, out ho_Regions, 0, 35); ho_Connection.Dispose(); HOperatorSet.Connection(ho_Regions, out ho_Connection); ho_SelectedRegions1.Dispose(); //HOperatorSet.SelectShape(ho_Connection, out ho_SelectedRegions1, "area", //"and", 9908.54, 1e+006); "ExposureTime", 2000.0 HOperatorSet.SelectShape(ho_Connection, out ho_SelectedRegions1, "area", "and", 380000, 1.30000e+006); HOperatorSet.CountObj(ho_SelectedRegions1, out hv_SelectNumber); ho_Contours.Dispose(); HOperatorSet.GenContourRegionXld(ho_SelectedRegions1, out ho_Contours, "border"); //Smoth edge conture //smooth_contours_xld (Contours, SmoothedContours, 109) ho_SmoothedContours.Dispose(); HOperatorSet.SmoothContoursXld(ho_Contours, out ho_SmoothedContours, 29); HOperatorSet.GetContourXld(ho_SmoothedContours, out hv_Row, out hv_Col); //* Define max value from tuple HOperatorSet.TupleMax(hv_Row, out hv_TupleMax); HOperatorSet.TupleFindFirst(hv_Row, hv_TupleMax, out hv_IndexMax); //Define constants: hv_HalfH = hv_Height / 2; hv_HalfW = hv_Width / 2; hv_ColumMax = hv_Col.TupleSelect(hv_IndexMax); lock (HDevelopExport.HalconLock) { HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle); if ((int)(new HTuple((new HTuple(750)).TupleLess(hv_ColumMax))) != 0) { if ((int)(new HTuple(hv_ColumMax.TupleLess(hv_Width - 750))) != 0) { ho_CrossMax.Dispose(); HOperatorSet.GenCrossContourXld(out ho_CrossMax, hv_Row.TupleSelect( hv_IndexMax), hv_Col.TupleSelect(hv_IndexMax), 150, 0.785398); HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "green"); HOperatorSet.DispObj(ho_CrossMax, hv_ExpDefaultWinHandle); hv_rowToMax0 = hv_Row.TupleSelect(hv_IndexMax); hv_colToMax0 = hv_Col.TupleSelect(hv_IndexMax); hv_output = 1382 - (hv_Row.TupleSelect(hv_IndexMax)); hv_outputmm = (1382 - (hv_Row.TupleSelect(hv_IndexMax))) * 0.001; hv_MessageMax = "Distance from center: " + hv_output; HOperatorSet.DispText(hv_ExpDefaultWinHandle, hv_MessageMax, "window", 10, 10, "black", new HTuple(), new HTuple()); hv_MessagePos = (("Position: Row = " + hv_rowToMax0) + " Col = ") + hv_colToMax0; HOperatorSet.DispText(hv_ExpDefaultWinHandle, hv_MessagePos, "window", 40, 10, "black", new HTuple(), new HTuple()); hv_Messagemm = "Distance from center [mm] = " + hv_outputmm; HOperatorSet.DispText(hv_ExpDefaultWinHandle, hv_Messagemm, "window", 70, 10, "black", new HTuple(), new HTuple()); } } //* Define min value from tuple HOperatorSet.TupleMin(hv_Row, out hv_TupleMin); HOperatorSet.TupleFindFirst(hv_Row, hv_TupleMin, out hv_IndexMin); hv_ColumMin = hv_Col.TupleSelect(hv_IndexMin); if ((int)(new HTuple((new HTuple(750)).TupleLess(hv_ColumMin))) != 0) { if ((int)(new HTuple(hv_ColumMin.TupleLess(hv_Width - 750))) != 0) { ho_CrossMin.Dispose(); HOperatorSet.GenCrossContourXld(out ho_CrossMin, hv_Row.TupleSelect( hv_IndexMin), hv_Col.TupleSelect(hv_IndexMin), 150, 0.785398); HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "green"); HOperatorSet.DispObj(ho_CrossMin, hv_ExpDefaultWinHandle); hv_rowToMin0 = hv_Row.TupleSelect(hv_IndexMin); hv_colToMin0 = hv_Col.TupleSelect(hv_IndexMin); hv_output = 1382 - (hv_Row.TupleSelect(hv_IndexMin)); hv_outputmm = (1382 - (hv_Row.TupleSelect(hv_IndexMin))) * 0.001; hv_MessageMax = "Distance from center: " + hv_output; HOperatorSet.DispText(hv_ExpDefaultWinHandle, hv_MessageMax, "window", 10, 10, "black", new HTuple(), new HTuple()); hv_MessagePos = (("Position: Row = " + hv_rowToMin0) + " Col = ") + hv_colToMin0; HOperatorSet.DispText(hv_ExpDefaultWinHandle, hv_MessagePos, "window", 40, 10, "black", new HTuple(), new HTuple()); hv_Messagemm = "Distance from center [mm] = " + hv_outputmm; HOperatorSet.DispText(hv_ExpDefaultWinHandle, hv_Messagemm, "window", 70, 10, "black", new HTuple(), new HTuple()); } } } //Draw middle lines HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "blue"); HOperatorSet.DispLine(hv_ExpDefaultWinHandle, hv_HalfH, 0, hv_HalfH, hv_Width); HOperatorSet.DispLine(hv_ExpDefaultWinHandle, 0, hv_HalfW, hv_Height, hv_HalfW); //Draw meassurement limit lines HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "red"); HOperatorSet.DispLine(hv_ExpDefaultWinHandle, hv_HalfH + 691, 0, hv_HalfH + 691, hv_Width); HOperatorSet.DispLine(hv_ExpDefaultWinHandle, hv_HalfH - 691, 0, hv_HalfH - 691, hv_Width); } // catch (Exception) catch (HalconException HDevExpDefaultException1) { HDevExpDefaultException1.ToHTuple(out hv_Exception); hv_MessageError = new HTuple(" ERROR: Not able to analize photo, move horizontal axis"); HOperatorSet.DispText(hv_ExpDefaultWinHandle, hv_MessageError, "window", 10, 10, "black", new HTuple(), new HTuple()); } HOperatorSet.CloseFramegrabber(hv_AcqHandle); } } catch (HalconException HDevExpDefaultException) { ho_Image.Dispose(); ho_Regions.Dispose(); ho_Connection.Dispose(); ho_SelectedRegions1.Dispose(); ho_Contours.Dispose(); ho_SmoothedContours.Dispose(); ho_CrossMax.Dispose(); ho_CrossMin.Dispose(); throw HDevExpDefaultException; } ho_Image.Dispose(); ho_Regions.Dispose(); ho_Connection.Dispose(); ho_SelectedRegions1.Dispose(); ho_Contours.Dispose(); ho_SmoothedContours.Dispose(); ho_CrossMax.Dispose(); ho_CrossMin.Dispose(); }
//} public void disp_message(HTuple hv_WindowHandle, HTuple hv_String, HTuple hv_CoordSystem, HTuple hv_Row, HTuple hv_Column, HTuple hv_Color, HTuple hv_Box) { // Local iconic variables // Local control variables HTuple hv_Red = null, hv_Green = null, hv_Blue = null; HTuple hv_Row1Part = null, hv_Column1Part = null, hv_Row2Part = null; HTuple hv_Column2Part = null, hv_RowWin = null, hv_ColumnWin = null; HTuple hv_WidthWin = null, hv_HeightWin = null, hv_MaxAscent = null; HTuple hv_MaxDescent = null, hv_MaxWidth = null, hv_MaxHeight = null; HTuple hv_R1 = new HTuple(), hv_C1 = new HTuple(), hv_FactorRow = new HTuple(); HTuple hv_FactorColumn = new HTuple(), hv_UseShadow = null; HTuple hv_ShadowColor = null, hv_Exception = new HTuple(); HTuple hv_Width = new HTuple(), hv_Index = new HTuple(); HTuple hv_Ascent = new HTuple(), hv_Descent = new HTuple(); HTuple hv_W = new HTuple(), hv_H = new HTuple(), hv_FrameHeight = new HTuple(); HTuple hv_FrameWidth = new HTuple(), hv_R2 = new HTuple(); HTuple hv_C2 = new HTuple(), hv_DrawMode = new HTuple(); HTuple hv_CurrentColor = new HTuple(); HTuple hv_Box_COPY_INP_TMP = hv_Box.Clone(); HTuple hv_Color_COPY_INP_TMP = hv_Color.Clone(); HTuple hv_Column_COPY_INP_TMP = hv_Column.Clone(); HTuple hv_Row_COPY_INP_TMP = hv_Row.Clone(); HTuple hv_String_COPY_INP_TMP = hv_String.Clone(); // Initialize local and output iconic variables //This procedure displays text in a graphics window. // //Input parameters: //WindowHandle: The WindowHandle of the graphics window, where // the message should be displayed //String: A tuple of strings containing the text message to be displayed //CoordSystem: If set to 'window', the text position is given // with respect to the window coordinate system. // If set to 'image', image coordinates are used. // (This may be useful in zoomed images.) //Row: The row coordinate of the desired text position // If set to -1, a default value of 12 is used. //Column: The column coordinate of the desired text position // If set to -1, a default value of 12 is used. //Color: defines the color of the text as string. // If set to [], '' or 'auto' the currently set color is used. // If a tuple of strings is passed, the colors are used cyclically // for each new textline. //Box: If Box[0] is set to 'true', the text is written within an orange box. // If set to' false', no box is displayed. // If set to a color string (e.g. 'white', '#FF00CC', etc.), // the text is written in a box of that color. // An optional second value for Box (Box[1]) controls if a shadow is displayed: // 'true' -> display a shadow in a default color // 'false' -> display no shadow (same as if no second value is given) // otherwise -> use given string as color string for the shadow color // //Prepare window HOperatorSet.GetRgb(hv_WindowHandle, out hv_Red, out hv_Green, out hv_Blue); HOperatorSet.GetPart(hv_WindowHandle, out hv_Row1Part, out hv_Column1Part, out hv_Row2Part, out hv_Column2Part); HOperatorSet.GetWindowExtents(hv_WindowHandle, out hv_RowWin, out hv_ColumnWin, out hv_WidthWin, out hv_HeightWin); HOperatorSet.SetPart(hv_WindowHandle, 0, 0, hv_HeightWin - 1, hv_WidthWin - 1); // //default settings if ((int)(new HTuple(hv_Row_COPY_INP_TMP.TupleEqual(-1))) != 0) { hv_Row_COPY_INP_TMP = 12; } if ((int)(new HTuple(hv_Column_COPY_INP_TMP.TupleEqual(-1))) != 0) { hv_Column_COPY_INP_TMP = 12; } if ((int)(new HTuple(hv_Color_COPY_INP_TMP.TupleEqual(new HTuple()))) != 0) { hv_Color_COPY_INP_TMP = ""; } // hv_String_COPY_INP_TMP = ((("" + hv_String_COPY_INP_TMP) + "")).TupleSplit("\n"); // //Estimate extentions of text depending on font size. HOperatorSet.GetFontExtents(hv_WindowHandle, out hv_MaxAscent, out hv_MaxDescent, out hv_MaxWidth, out hv_MaxHeight); if ((int)(new HTuple(hv_CoordSystem.TupleEqual("window"))) != 0) { hv_R1 = hv_Row_COPY_INP_TMP.Clone(); hv_C1 = hv_Column_COPY_INP_TMP.Clone(); } else { //Transform image to window coordinates hv_FactorRow = (1.0 * hv_HeightWin) / ((hv_Row2Part - hv_Row1Part) + 1); hv_FactorColumn = (1.0 * hv_WidthWin) / ((hv_Column2Part - hv_Column1Part) + 1); hv_R1 = ((hv_Row_COPY_INP_TMP - hv_Row1Part) + 0.5) * hv_FactorRow; hv_C1 = ((hv_Column_COPY_INP_TMP - hv_Column1Part) + 0.5) * hv_FactorColumn; } // //Display text box depending on text size hv_UseShadow = 1; hv_ShadowColor = "gray"; if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(0))).TupleEqual("true"))) != 0) { if (hv_Box_COPY_INP_TMP == null) { hv_Box_COPY_INP_TMP = new HTuple(); } hv_Box_COPY_INP_TMP[0] = "#fce9d4"; hv_ShadowColor = "#f28d26"; } if ((int)(new HTuple((new HTuple(hv_Box_COPY_INP_TMP.TupleLength())).TupleGreater( 1))) != 0) { if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(1))).TupleEqual("true"))) != 0) { //Use default ShadowColor set above } else if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(1))).TupleEqual( "false"))) != 0) { hv_UseShadow = 0; } else { hv_ShadowColor = hv_Box_COPY_INP_TMP[1]; //Valid color? try { HOperatorSet.SetColor(hv_WindowHandle, hv_Box_COPY_INP_TMP.TupleSelect( 1)); } // catch (Exception) catch (HalconException HDevExpDefaultException1) { HDevExpDefaultException1.ToHTuple(out hv_Exception); hv_Exception = "Wrong value of control parameter Box[1] (must be a 'true', 'false', or a valid color string)"; throw new HalconException(hv_Exception); } } } if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(0))).TupleNotEqual("false"))) != 0) { //Valid color? try { HOperatorSet.SetColor(hv_WindowHandle, hv_Box_COPY_INP_TMP.TupleSelect(0)); } // catch (Exception) catch (HalconException HDevExpDefaultException1) { HDevExpDefaultException1.ToHTuple(out hv_Exception); hv_Exception = "Wrong value of control parameter Box[0] (must be a 'true', 'false', or a valid color string)"; throw new HalconException(hv_Exception); } //Calculate box extents hv_String_COPY_INP_TMP = (" " + hv_String_COPY_INP_TMP) + " "; hv_Width = new HTuple(); for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_String_COPY_INP_TMP.TupleLength() )) - 1); hv_Index = (int)hv_Index + 1) { HOperatorSet.GetStringExtents(hv_WindowHandle, hv_String_COPY_INP_TMP.TupleSelect( hv_Index), out hv_Ascent, out hv_Descent, out hv_W, out hv_H); hv_Width = hv_Width.TupleConcat(hv_W); } hv_FrameHeight = hv_MaxHeight * (new HTuple(hv_String_COPY_INP_TMP.TupleLength() )); hv_FrameWidth = (((new HTuple(0)).TupleConcat(hv_Width))).TupleMax(); hv_R2 = hv_R1 + hv_FrameHeight; hv_C2 = hv_C1 + hv_FrameWidth; //Display rectangles HOperatorSet.GetDraw(hv_WindowHandle, out hv_DrawMode); HOperatorSet.SetDraw(hv_WindowHandle, "fill"); //Set shadow color HOperatorSet.SetColor(hv_WindowHandle, hv_ShadowColor); if ((int)(hv_UseShadow) != 0) { HOperatorSet.DispRectangle1(hv_WindowHandle, hv_R1 + 1, hv_C1 + 1, hv_R2 + 1, hv_C2 + 1); } //Set box color HOperatorSet.SetColor(hv_WindowHandle, hv_Box_COPY_INP_TMP.TupleSelect(0)); HOperatorSet.DispRectangle1(hv_WindowHandle, hv_R1, hv_C1, hv_R2, hv_C2); HOperatorSet.SetDraw(hv_WindowHandle, hv_DrawMode); } //Write text. for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_String_COPY_INP_TMP.TupleLength() )) - 1); hv_Index = (int)hv_Index + 1) { hv_CurrentColor = hv_Color_COPY_INP_TMP.TupleSelect(hv_Index % (new HTuple(hv_Color_COPY_INP_TMP.TupleLength() ))); if ((int)((new HTuple(hv_CurrentColor.TupleNotEqual(""))).TupleAnd(new HTuple(hv_CurrentColor.TupleNotEqual( "auto")))) != 0) { HOperatorSet.SetColor(hv_WindowHandle, hv_CurrentColor); } else { HOperatorSet.SetRgb(hv_WindowHandle, hv_Red, hv_Green, hv_Blue); } hv_Row_COPY_INP_TMP = hv_R1 + (hv_MaxHeight * hv_Index); HOperatorSet.SetTposition(hv_WindowHandle, hv_Row_COPY_INP_TMP, hv_C1); HOperatorSet.WriteString(hv_WindowHandle, hv_String_COPY_INP_TMP.TupleSelect( hv_Index)); } //Reset changed window settings HOperatorSet.SetRgb(hv_WindowHandle, hv_Red, hv_Green, hv_Blue); HOperatorSet.SetPart(hv_WindowHandle, hv_Row1Part, hv_Column1Part, hv_Row2Part, hv_Column2Part); return; }
/// <summary> /// 生成Die的检测区域 /// </summary> /// <param name="ho_dieMatchRegion"></param> /// <param name="hv_imgCenterX"></param> /// <param name="hv_imgCenterY"></param> /// <param name="hv_dieWidth"></param> /// <param name="hv_dieHeight"></param> /// <param name="hv_dieX"></param> /// <param name="hv_dieY"></param> /// <param name="hv_uvHxy"></param> /// <param name="hv_imgWidth"></param> /// <param name="hv_imgHeight"></param> /// <param name="hv_imgWidthFactor"></param> /// <param name="hv_imgHeightFactor"></param> /// <param name="hv_zoomFactor"></param> /// <param name="hv_dilationSize"></param> /// <param name="hv_mapRowCnt"></param> /// <param name="hv_mapColCnt"></param> /// <param name="hv_dieRow"></param> /// <param name="hv_dieCol"></param> /// <param name="hv_iFlag"></param> public void gen_die_match_region(out HObject ho_dieMatchRegion, HTuple hv_imgCenterX, HTuple hv_imgCenterY, HTuple hv_dieWidth, HTuple hv_dieHeight, HTuple hv_dieX, HTuple hv_dieY, HTuple hv_uvHxy, HTuple hv_imgWidth, HTuple hv_imgHeight, HTuple hv_imgWidthFactor, HTuple hv_imgHeightFactor, HTuple hv_zoomFactor, HTuple hv_dilationSize, HTuple hv_mapRowCnt, HTuple hv_mapColCnt, out HTuple hv_dieRow, out HTuple hv_dieCol, out HTuple hv_iFlag) { // Local iconic variables // Local control variables HTuple hv_uvHxyScaled = new HTuple(), hv_xyHuvScale = new HTuple(); HTuple hv_viewWidth = new HTuple(), hv_viewHeight = new HTuple(); HTuple hv_viewXLT = new HTuple(), hv_viewYLT = new HTuple(); HTuple hv_viewXRB = new HTuple(), hv_viewYRB = new HTuple(); HTuple hv_dieXLT = new HTuple(), hv_dieYLT = new HTuple(); HTuple hv_dieXRB = new HTuple(), hv_dieYRB = new HTuple(); HTuple hv_xLTGreater = new HTuple(), hv_yLTLess = new HTuple(); HTuple hv_xRBLess = new HTuple(), hv_yRBGreater = new HTuple(); HTuple hv_greater = new HTuple(), hv_greaterInd = new HTuple(); HTuple hv_viewDieX = new HTuple(), hv_viewDieY = new HTuple(); HTuple hv_deltaX = new HTuple(), hv_deltaY = new HTuple(); HTuple hv_xyHuv = new HTuple(), hv_deltaRow = new HTuple(); HTuple hv_deltaCol = new HTuple(), hv_dieImgRow = new HTuple(); HTuple hv_dieImgCol = new HTuple(), hv_dieImgHeight = new HTuple(); HTuple hv_dieImgWidth = new HTuple(), hv_row1 = new HTuple(); HTuple hv_col1 = new HTuple(), hv_row2 = new HTuple(); HTuple hv_col2 = new HTuple(), hv_Exception = null; HTuple hv_Less = new HTuple(), hv_Indices1 = new HTuple(), hv_Greater = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_dieMatchRegion); hv_iFlag = "Success"; ho_dieMatchRegion.Dispose(); HOperatorSet.GenEmptyObj(out ho_dieMatchRegion); hv_dieRow = new HTuple(); hv_dieCol = new HTuple(); try { HOperatorSet.HomMat2dScale(hv_uvHxy, 1 / hv_zoomFactor, 1 / hv_zoomFactor, 0, 0, out hv_uvHxyScaled); HOperatorSet.HomMat2dInvert(hv_uvHxyScaled, out hv_xyHuvScale); hv_viewWidth = (((((hv_uvHxy.TupleSelect(0)) * hv_imgHeight) * hv_imgHeightFactor) + (((hv_uvHxy.TupleSelect( 1)) * hv_imgWidth) * hv_imgWidthFactor))).TupleAbs(); hv_viewHeight = (((((hv_uvHxy.TupleSelect(3)) * hv_imgHeight) * hv_imgHeightFactor) + (((hv_uvHxy.TupleSelect( 4)) * hv_imgWidth) * hv_imgWidthFactor))).TupleAbs(); hv_viewXLT = hv_imgCenterX - (hv_viewWidth / 2.0); hv_viewYLT = hv_imgCenterY + (hv_viewHeight / 2.0); hv_viewXRB = hv_imgCenterX + (hv_viewWidth / 2.0); hv_viewYRB = hv_imgCenterY - (hv_viewHeight / 2.0); hv_dieXLT = hv_dieX - (hv_dieWidth / 2.0); hv_dieYLT = hv_dieY + (hv_dieHeight / 2.0); hv_dieXRB = hv_dieX + (hv_dieWidth / 2.0); hv_dieYRB = hv_dieY - (hv_dieHeight / 2.0); HOperatorSet.TupleGreaterEqualElem(hv_dieXLT, hv_viewXLT, out hv_xLTGreater); HOperatorSet.TupleGreaterEqualElem(hv_viewYLT, hv_dieYLT, out hv_yLTLess); HOperatorSet.TupleLessEqualElem(hv_dieXRB, hv_viewXRB, out hv_xRBLess); HOperatorSet.TupleLessEqualElem(hv_viewYRB, hv_dieYRB, out hv_yRBGreater); hv_greater = ((hv_xLTGreater * hv_yLTLess) * hv_xRBLess) * hv_yRBGreater; HOperatorSet.TupleFind(hv_greater, 1, out hv_greaterInd); if ((int)(new HTuple(hv_greaterInd.TupleEqual(-1))) != 0) { return; } hv_viewDieX = hv_dieX.TupleSelect(hv_greaterInd); hv_viewDieY = hv_dieY.TupleSelect(hv_greaterInd); hv_deltaX = hv_imgCenterX - hv_viewDieX; hv_deltaY = hv_imgCenterY - hv_viewDieY; HOperatorSet.HomMat2dInvert(hv_uvHxy, out hv_xyHuv); hv_deltaRow = ((hv_xyHuv.TupleSelect(0)) * hv_deltaX) + ((hv_xyHuv.TupleSelect( 1)) * hv_deltaY); hv_deltaCol = ((hv_xyHuv.TupleSelect(3)) * hv_deltaX) + ((hv_xyHuv.TupleSelect( 4)) * hv_deltaY); hv_dieImgRow = ((hv_imgHeight - 1) / 2.0) + hv_deltaRow; hv_dieImgCol = ((hv_imgWidth - 1) / 2.0) + hv_deltaCol; hv_dieImgHeight = ((((hv_xyHuv.TupleSelect(0)) * hv_dieWidth) + ((hv_xyHuv.TupleSelect( 1)) * (-hv_dieHeight)))).TupleAbs(); hv_dieImgWidth = ((((hv_xyHuv.TupleSelect(3)) * hv_dieWidth) + ((hv_xyHuv.TupleSelect( 4)) * (-hv_dieHeight)))).TupleAbs(); hv_row1 = (hv_dieImgRow - (hv_dieImgHeight / 2.0)) - hv_dilationSize; hv_col1 = (hv_dieImgCol - (hv_dieImgWidth / 2.0)) - hv_dilationSize; hv_row2 = (hv_dieImgRow + (hv_dieImgHeight / 2.0)) + hv_dilationSize; hv_col2 = (hv_dieImgCol + (hv_dieImgWidth / 2.0)) + hv_dilationSize; //if ((int)((new HTuple((new HTuple((new HTuple(hv_row1.TupleLess(0))).TupleOr( // new HTuple(hv_col1.TupleLess(0))))).TupleOr( // new HTuple(hv_row2.TupleGreater(hv_imgHeight))))).TupleOr( // new HTuple(hv_col2.TupleGreater(hv_imgWidth)))) != 0) //{ // return; //} //*2019/11/4 修改 解决膨胀区域超过图片像素时直接return的问题 上面注释的代码 HOperatorSet.TupleLessElem(hv_row1, 0, out hv_Less); HOperatorSet.TupleFind(hv_Less, 1, out hv_Indices1); if ((int)(new HTuple(hv_Indices1.TupleNotEqual(-1))) != 0) { if (hv_row1 == null) { hv_row1 = new HTuple(); } hv_row1[hv_Indices1] = 0; } HOperatorSet.TupleLessElem(hv_col1, 0, out hv_Less); HOperatorSet.TupleFind(hv_Less, 1, out hv_Indices1); if ((int)(new HTuple(hv_Indices1.TupleNotEqual(-1))) != 0) { if (hv_col1 == null) { hv_col1 = new HTuple(); } hv_col1[hv_Indices1] = 0; } HOperatorSet.TupleGreaterElem(hv_row2, hv_imgHeight, out hv_Greater); HOperatorSet.TupleFind(hv_Greater, 1, out hv_Indices1); if ((int)(new HTuple(hv_Indices1.TupleNotEqual(-1))) != 0) { if (hv_row2 == null) { hv_row2 = new HTuple(); } hv_row2[hv_Indices1] = hv_imgHeight; } HOperatorSet.TupleGreaterElem(hv_col2, hv_imgWidth, out hv_Greater); HOperatorSet.TupleFind(hv_Less, 1, out hv_Indices1); if ((int)(new HTuple(hv_Indices1.TupleNotEqual(-1))) != 0) { if (hv_col2 == null) { hv_col2 = new HTuple(); } hv_col2[hv_Indices1] = hv_imgWidth; } ho_dieMatchRegion.Dispose(); HOperatorSet.GenRectangle1(out ho_dieMatchRegion, hv_row1, hv_col1, hv_row2, hv_col2); hv_dieRow = hv_greaterInd / hv_mapColCnt; hv_dieCol = hv_greaterInd % hv_mapColCnt; } catch (HalconException HDevExpDefaultException1) { HDevExpDefaultException1.ToHTuple(out hv_Exception); GetErrInfo(hv_Exception, out hv_iFlag); } return; }
static public void set_display_font(HTuple hv_WindowHandle, HTuple hv_Size, HTuple hv_Font, HTuple hv_Bold, HTuple hv_Slant) { // Local control variables HTuple hv_OS, hv_Exception = new HTuple(); HTuple hv_AllowedFontSizes = new HTuple(), hv_Distances = new HTuple(); HTuple hv_Indices = new HTuple(); HTuple hv_Bold_COPY_INP_TMP = hv_Bold.Clone(); HTuple hv_Font_COPY_INP_TMP = hv_Font.Clone(); HTuple hv_Size_COPY_INP_TMP = hv_Size.Clone(); HTuple hv_Slant_COPY_INP_TMP = hv_Slant.Clone(); // Initialize local and output iconic variables //Slant: If set to 'true', a slanted font is used // HOperatorSet.GetSystem("operating_system", out hv_OS); if ((int)((new HTuple(hv_Size_COPY_INP_TMP.TupleEqual(new HTuple()))).TupleOr( new HTuple(hv_Size_COPY_INP_TMP.TupleEqual(-1)))) != 0) { hv_Size_COPY_INP_TMP = 16; } if ((int)(new HTuple((((hv_OS.TupleStrFirstN(2)).TupleStrLastN(0))).TupleEqual( "Win"))) != 0) { //set font on Windows systems if ((int)((new HTuple((new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("mono"))).TupleOr( new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("Courier"))))).TupleOr(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual( "courier")))) != 0) { hv_Font_COPY_INP_TMP = "Courier New"; } else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("sans"))) != 0) { hv_Font_COPY_INP_TMP = "Arial"; } else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("serif"))) != 0) { hv_Font_COPY_INP_TMP = "Times New Roman"; } if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("true"))) != 0) { hv_Bold_COPY_INP_TMP = 1; } else if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("false"))) != 0) { hv_Bold_COPY_INP_TMP = 0; } else { hv_Exception = "Wrong value of control parameter Bold"; throw new HalconException(hv_Exception); } if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual("true"))) != 0) { hv_Slant_COPY_INP_TMP = 1; } else if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual("false"))) != 0) { hv_Slant_COPY_INP_TMP = 0; } else { hv_Exception = "Wrong value of control parameter Slant"; throw new HalconException(hv_Exception); } try { HOperatorSet.SetFont(hv_WindowHandle, ((((((("-" + hv_Font_COPY_INP_TMP) + "-") + hv_Size_COPY_INP_TMP) + "-*-") + hv_Slant_COPY_INP_TMP) + "-*-*-") + hv_Bold_COPY_INP_TMP) + "-"); } // catch (Exception) catch (HalconException HDevExpDefaultException1) { HDevExpDefaultException1.ToHTuple(out hv_Exception); throw new HalconException(hv_Exception); } } else { //set font for UNIX systems hv_Size_COPY_INP_TMP = hv_Size_COPY_INP_TMP * 1.25; hv_AllowedFontSizes = new HTuple(); hv_AllowedFontSizes[0] = 11; hv_AllowedFontSizes[1] = 14; hv_AllowedFontSizes[2] = 17; hv_AllowedFontSizes[3] = 20; hv_AllowedFontSizes[4] = 25; hv_AllowedFontSizes[5] = 34; if ((int)(new HTuple(((hv_AllowedFontSizes.TupleFind(hv_Size_COPY_INP_TMP))).TupleEqual( -1))) != 0) { hv_Distances = ((hv_AllowedFontSizes - hv_Size_COPY_INP_TMP)).TupleAbs(); HOperatorSet.TupleSortIndex(hv_Distances, out hv_Indices); hv_Size_COPY_INP_TMP = hv_AllowedFontSizes.TupleSelect(hv_Indices.TupleSelect( 0)); } if ((int)((new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("mono"))).TupleOr(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual( "Courier")))) != 0) { hv_Font_COPY_INP_TMP = "courier"; } else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("sans"))) != 0) { hv_Font_COPY_INP_TMP = "helvetica"; } else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("serif"))) != 0) { hv_Font_COPY_INP_TMP = "times"; } if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("true"))) != 0) { hv_Bold_COPY_INP_TMP = "bold"; } else if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("false"))) != 0) { hv_Bold_COPY_INP_TMP = "medium"; } else { hv_Exception = "Wrong value of control parameter Bold"; throw new HalconException(hv_Exception); } if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual("true"))) != 0) { if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("times"))) != 0) { hv_Slant_COPY_INP_TMP = "i"; } else { hv_Slant_COPY_INP_TMP = "o"; } } else if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual("false"))) != 0) { hv_Slant_COPY_INP_TMP = "r"; } else { hv_Exception = "Wrong value of control parameter Slant"; throw new HalconException(hv_Exception); } try { HOperatorSet.SetFont(hv_WindowHandle, ((((((("-adobe-" + hv_Font_COPY_INP_TMP) + "-") + hv_Bold_COPY_INP_TMP) + "-") + hv_Slant_COPY_INP_TMP) + "-normal-*-") + hv_Size_COPY_INP_TMP) + "-*-*-*-*-*-*-*"); } // catch (Exception) catch (HalconException HDevExpDefaultException1) { HDevExpDefaultException1.ToHTuple(out hv_Exception); throw new HalconException(hv_Exception); } } return; }
// Chapter: Graphics / Text // Short Description: Set font independent of OS public void set_display_font(HTuple hv_WindowHandle, HTuple hv_Size, HTuple hv_Font, HTuple hv_Bold, HTuple hv_Slant) { // Local control variables HTuple hv_OS, hv_Exception = new HTuple(); HTuple hv_AllowedFontSizes = new HTuple(), hv_Distances = new HTuple(); HTuple hv_Indices = new HTuple(); HTuple hv_Bold_COPY_INP_TMP = hv_Bold.Clone(); HTuple hv_Font_COPY_INP_TMP = hv_Font.Clone(); HTuple hv_Size_COPY_INP_TMP = hv_Size.Clone(); HTuple hv_Slant_COPY_INP_TMP = hv_Slant.Clone(); // Initialize local and output iconic variables //This procedure sets the text font of the current window with //the specified attributes. //It is assumed that following fonts are installed on the system: //Windows: Courier New, Arial Times New Roman //Linux: courier, helvetica, times //Because fonts are displayed smaller on Linux than on Windows, //a scaling factor of 1.25 is used the get comparable results. //For Linux, only a limited number of font sizes is supported, //to get comparable results, it is recommended to use one of the //following sizes: 9, 11, 14, 16, 20, 27 //(which will be mapped internally on Linux systems to 11, 14, 17, 20, 25, 34) // //input parameters: //WindowHandle: The graphics window for which the font will be set //Size: The font size. If Size=-1, the default of 16 is used. //Bold: If set to 'true', a bold font is used //Slant: If set to 'true', a slanted font is used // HOperatorSet.GetSystem("operating_system", out hv_OS); if ((int)((new HTuple(hv_Size_COPY_INP_TMP.TupleEqual(new HTuple()))).TupleOr( new HTuple(hv_Size_COPY_INP_TMP.TupleEqual(-1)))) != 0) { hv_Size_COPY_INP_TMP = 16; } if ((int)(new HTuple((((hv_OS.TupleStrFirstN(2)).TupleStrLastN(0))).TupleEqual( "Win"))) != 0) { //set font on Windows systems if ((int)((new HTuple((new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("mono"))).TupleOr( new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("Courier"))))).TupleOr(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual( "courier")))) != 0) { hv_Font_COPY_INP_TMP = "Courier New"; } else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("sans"))) != 0) { hv_Font_COPY_INP_TMP = "Arial"; } else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("serif"))) != 0) { hv_Font_COPY_INP_TMP = "Times New Roman"; } if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("true"))) != 0) { hv_Bold_COPY_INP_TMP = 1; } else if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("false"))) != 0) { hv_Bold_COPY_INP_TMP = 0; } else { hv_Exception = "Wrong value of control parameter Bold"; throw new HalconException(hv_Exception); } if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual("true"))) != 0) { hv_Slant_COPY_INP_TMP = 1; } else if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual("false"))) != 0) { hv_Slant_COPY_INP_TMP = 0; } else { hv_Exception = "Wrong value of control parameter Slant"; throw new HalconException(hv_Exception); } try { HOperatorSet.SetFont(hv_WindowHandle, ((((((("-" + hv_Font_COPY_INP_TMP) + "-") + hv_Size_COPY_INP_TMP) + "-*-") + hv_Slant_COPY_INP_TMP) + "-*-*-") + hv_Bold_COPY_INP_TMP) + "-"); } // catch (Exception) catch (HalconException HDevExpDefaultException1) { HDevExpDefaultException1.ToHTuple(out hv_Exception); throw new HalconException(hv_Exception); } } else { //set font for UNIX systems hv_Size_COPY_INP_TMP = hv_Size_COPY_INP_TMP * 1.25; hv_AllowedFontSizes = new HTuple(); hv_AllowedFontSizes[0] = 11; hv_AllowedFontSizes[1] = 14; hv_AllowedFontSizes[2] = 17; hv_AllowedFontSizes[3] = 20; hv_AllowedFontSizes[4] = 25; hv_AllowedFontSizes[5] = 34; if ((int)(new HTuple(((hv_AllowedFontSizes.TupleFind(hv_Size_COPY_INP_TMP))).TupleEqual( -1))) != 0) { hv_Distances = ((hv_AllowedFontSizes - hv_Size_COPY_INP_TMP)).TupleAbs(); HOperatorSet.TupleSortIndex(hv_Distances, out hv_Indices); hv_Size_COPY_INP_TMP = hv_AllowedFontSizes.TupleSelect(hv_Indices.TupleSelect( 0)); } if ((int)((new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("mono"))).TupleOr(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual( "Courier")))) != 0) { hv_Font_COPY_INP_TMP = "courier"; } else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("sans"))) != 0) { hv_Font_COPY_INP_TMP = "helvetica"; } else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("serif"))) != 0) { hv_Font_COPY_INP_TMP = "times"; } if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("true"))) != 0) { hv_Bold_COPY_INP_TMP = "bold"; } else if ((int)(new HTuple(hv_Bold_COPY_INP_TMP.TupleEqual("false"))) != 0) { hv_Bold_COPY_INP_TMP = "medium"; } else { hv_Exception = "Wrong value of control parameter Bold"; throw new HalconException(hv_Exception); } if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual("true"))) != 0) { if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("times"))) != 0) { hv_Slant_COPY_INP_TMP = "i"; } else { hv_Slant_COPY_INP_TMP = "o"; } } else if ((int)(new HTuple(hv_Slant_COPY_INP_TMP.TupleEqual("false"))) != 0) { hv_Slant_COPY_INP_TMP = "r"; } else { hv_Exception = "Wrong value of control parameter Slant"; throw new HalconException(hv_Exception); } try { HOperatorSet.SetFont(hv_WindowHandle, ((((((("-adobe-" + hv_Font_COPY_INP_TMP) + "-") + hv_Bold_COPY_INP_TMP) + "-") + hv_Slant_COPY_INP_TMP) + "-normal-*-") + hv_Size_COPY_INP_TMP) + "-*-*-*-*-*-*-*"); } // catch (Exception) catch (HalconException HDevExpDefaultException1) { HDevExpDefaultException1.ToHTuple(out hv_Exception); throw new HalconException(hv_Exception); } } return; }
/// <summary> /// /// disp_message函数 /// </summary> /// <param name="hv_WindowHandle"></param> /// <param name="hv_String"></param> /// <param name="hv_CoordSystem"></param> /// <param name="hv_Row"></param> /// <param name="hv_Column"></param> /// <param name="hv_Color"></param> /// <param name="hv_Box"></param>m public static void disp_message(HTuple hv_WindowHandle, HTuple hv_String, HTuple hv_CoordSystem, HTuple hv_Row, HTuple hv_Column, HTuple hv_Color, HTuple hv_Box) { HTuple hv_Red = null, hv_Green = null, hv_Blue = null; HTuple hv_Row1Part = null, hv_Column1Part = null, hv_Row2Part = null; HTuple hv_Column2Part = null, hv_RowWin = null, hv_ColumnWin = null; HTuple hv_WidthWin = null, hv_HeightWin = null, hv_MaxAscent = null; HTuple hv_MaxDescent = null, hv_MaxWidth = null, hv_MaxHeight = null; HTuple hv_R1 = new HTuple(), hv_C1 = new HTuple(), hv_FactorRow = new HTuple(); HTuple hv_FactorColumn = new HTuple(), hv_UseShadow = null; HTuple hv_ShadowColor = null, hv_Exception = new HTuple(); HTuple hv_Width = new HTuple(), hv_Index = new HTuple(); HTuple hv_Ascent = new HTuple(), hv_Descent = new HTuple(); HTuple hv_W = new HTuple(), hv_H = new HTuple(), hv_FrameHeight = new HTuple(); HTuple hv_FrameWidth = new HTuple(), hv_R2 = new HTuple(); HTuple hv_C2 = new HTuple(), hv_DrawMode = new HTuple(); HTuple hv_CurrentColor = new HTuple(); HTuple hv_Box_COPY_INP_TMP = hv_Box.Clone(); HTuple hv_Color_COPY_INP_TMP = hv_Color.Clone(); HTuple hv_Column_COPY_INP_TMP = hv_Column.Clone(); HTuple hv_Row_COPY_INP_TMP = hv_Row.Clone(); HTuple hv_String_COPY_INP_TMP = hv_String.Clone(); // Initialize local and output iconic variables //Prepare window HOperatorSet.GetRgb(hv_WindowHandle, out hv_Red, out hv_Green, out hv_Blue); HOperatorSet.GetPart(hv_WindowHandle, out hv_Row1Part, out hv_Column1Part, out hv_Row2Part, out hv_Column2Part); HOperatorSet.GetWindowExtents(hv_WindowHandle, out hv_RowWin, out hv_ColumnWin, out hv_WidthWin, out hv_HeightWin); HOperatorSet.SetPart(hv_WindowHandle, 0, 0, hv_HeightWin - 1, hv_WidthWin - 1); // //default settings if ((int)(new HTuple(hv_Row_COPY_INP_TMP.TupleEqual(-1))) != 0) { hv_Row_COPY_INP_TMP = 12; } if ((int)(new HTuple(hv_Column_COPY_INP_TMP.TupleEqual(-1))) != 0) { hv_Column_COPY_INP_TMP = 12; } if ((int)(new HTuple(hv_Color_COPY_INP_TMP.TupleEqual(new HTuple()))) != 0) { hv_Color_COPY_INP_TMP = ""; } // hv_String_COPY_INP_TMP = ((("" + hv_String_COPY_INP_TMP) + "")).TupleSplit("\n"); // //Estimate extentions of text depending on font size. HOperatorSet.GetFontExtents(hv_WindowHandle, out hv_MaxAscent, out hv_MaxDescent, out hv_MaxWidth, out hv_MaxHeight); if ((int)(new HTuple(hv_CoordSystem.TupleEqual("window"))) != 0) { hv_R1 = hv_Row_COPY_INP_TMP.Clone(); hv_C1 = hv_Column_COPY_INP_TMP.Clone(); } else { //Transform image to window coordinates hv_FactorRow = (1.0 * hv_HeightWin) / ((hv_Row2Part - hv_Row1Part) + 1); hv_FactorColumn = (1.0 * hv_WidthWin) / ((hv_Column2Part - hv_Column1Part) + 1); hv_R1 = ((hv_Row_COPY_INP_TMP - hv_Row1Part) + 0.5) * hv_FactorRow; hv_C1 = ((hv_Column_COPY_INP_TMP - hv_Column1Part) + 0.5) * hv_FactorColumn; } // //Display text box depending on text size hv_UseShadow = 1; hv_ShadowColor = "gray"; if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(0))).TupleEqual("true"))) != 0) { if (hv_Box_COPY_INP_TMP == null) { hv_Box_COPY_INP_TMP = new HTuple(); } hv_Box_COPY_INP_TMP[0] = "#fce9d4"; hv_ShadowColor = "#f28d26"; } if ((int)(new HTuple((new HTuple(hv_Box_COPY_INP_TMP.TupleLength())).TupleGreater( 1))) != 0) { if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(1))).TupleEqual("true"))) != 0) { //Use default ShadowColor set above } else if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(1))).TupleEqual( "false"))) != 0) { hv_UseShadow = 0; } else { hv_ShadowColor = hv_Box_COPY_INP_TMP[1]; //Valid color? try { HOperatorSet.SetColor(hv_WindowHandle, hv_Box_COPY_INP_TMP.TupleSelect( 1)); } // catch (Exception) catch (HalconException HDevExpDefaultException1) { HDevExpDefaultException1.ToHTuple(out hv_Exception); hv_Exception = "Wrong value of control parameter Box[1] (must be a 'true', 'false', or a valid color string)"; throw new HalconException(hv_Exception); } } } if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(0))).TupleNotEqual("false"))) != 0) { //Valid color? try { HOperatorSet.SetColor(hv_WindowHandle, hv_Box_COPY_INP_TMP.TupleSelect(0)); } // catch (Exception) catch (HalconException HDevExpDefaultException1) { HDevExpDefaultException1.ToHTuple(out hv_Exception); hv_Exception = "Wrong value of control parameter Box[0] (must be a 'true', 'false', or a valid color string)"; throw new HalconException(hv_Exception); } //Calculate box extents hv_String_COPY_INP_TMP = (" " + hv_String_COPY_INP_TMP) + " "; hv_Width = new HTuple(); for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_String_COPY_INP_TMP.TupleLength() )) - 1); hv_Index = (int)hv_Index + 1) { HOperatorSet.GetStringExtents(hv_WindowHandle, hv_String_COPY_INP_TMP.TupleSelect( hv_Index), out hv_Ascent, out hv_Descent, out hv_W, out hv_H); hv_Width = hv_Width.TupleConcat(hv_W); } hv_FrameHeight = hv_MaxHeight * (new HTuple(hv_String_COPY_INP_TMP.TupleLength() )); hv_FrameWidth = (((new HTuple(0)).TupleConcat(hv_Width))).TupleMax(); hv_R2 = hv_R1 + hv_FrameHeight; hv_C2 = hv_C1 + hv_FrameWidth; //Display rectangles HOperatorSet.GetDraw(hv_WindowHandle, out hv_DrawMode); HOperatorSet.SetDraw(hv_WindowHandle, "fill"); //Set shadow color HOperatorSet.SetColor(hv_WindowHandle, hv_ShadowColor); if ((int)(hv_UseShadow) != 0) { HOperatorSet.DispRectangle1(hv_WindowHandle, hv_R1 + 1, hv_C1 + 1, hv_R2 + 1, hv_C2 + 1); } //Set box color HOperatorSet.SetColor(hv_WindowHandle, hv_Box_COPY_INP_TMP.TupleSelect(0)); HOperatorSet.DispRectangle1(hv_WindowHandle, hv_R1, hv_C1, hv_R2, hv_C2); HOperatorSet.SetDraw(hv_WindowHandle, hv_DrawMode); } //Write text. for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_String_COPY_INP_TMP.TupleLength() )) - 1); hv_Index = (int)hv_Index + 1) { hv_CurrentColor = hv_Color_COPY_INP_TMP.TupleSelect(hv_Index % (new HTuple(hv_Color_COPY_INP_TMP.TupleLength() ))); if ((int)((new HTuple(hv_CurrentColor.TupleNotEqual(""))).TupleAnd(new HTuple(hv_CurrentColor.TupleNotEqual( "auto")))) != 0) { HOperatorSet.SetColor(hv_WindowHandle, hv_CurrentColor); } else { HOperatorSet.SetRgb(hv_WindowHandle, hv_Red, hv_Green, hv_Blue); } hv_Row_COPY_INP_TMP = hv_R1 + (hv_MaxHeight * hv_Index); HOperatorSet.SetTposition(hv_WindowHandle, hv_Row_COPY_INP_TMP, hv_C1); HOperatorSet.WriteString(hv_WindowHandle, hv_String_COPY_INP_TMP.TupleSelect( hv_Index)); } //Reset changed window settings HOperatorSet.SetRgb(hv_WindowHandle, hv_Red, hv_Green, hv_Blue); HOperatorSet.SetPart(hv_WindowHandle, hv_Row1Part, hv_Column1Part, hv_Row2Part, hv_Column2Part); return; }