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; }
// 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; } }
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 { } }
// 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(); }
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; } }
// 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; }
// 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(); }
// Main procedure private void action() { // Local iconic variables HObject ho_Rectangle1, ho_Rectangle2, ho_ImageReduced; HObject ho_RegionToDetect, ho_RegionOpening, ho_RegionDifference; HObject ho_Cross = null; HObject selectObject; // Local control variables HObject ho_Region; HTuple hv_maxDist = new HTuple(), hv_minDist = new HTuple(); HTuple hv_meanDist = new HTuple(), hv_Exception; HTuple area, row, col; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Region); HOperatorSet.GenEmptyObj(out ho_Rectangle1); HOperatorSet.GenEmptyObj(out ho_Rectangle2); 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); HOperatorSet.GenEmptyObj(out selectObject); try { ho_Rectangle2.Dispose(); HOperatorSet.GenRectangle1(out ho_Rectangle2, this.rr1, this.rc1, rr2, rc2); ho_Region.Dispose(); HOperatorSet.Intersection(ho_Rectangle2, this.algorithm.Region, out ho_Region); HOperatorSet.AreaCenter(ho_Region, out area, out row, out col); try { if (selectObject.IsInitialized()) { HOperatorSet.Union1(ho_Region, out RegionToDisp); } else { HOperatorSet.Union1(ho_Region, out RegionToDisp); } } // catch (Exception) catch (HalconException HDevExpDefaultException1) { HDevExpDefaultException1.ToHTuple(out hv_Exception); } finally { HTuple hv_result = new HTuple(); hv_result = hv_result.TupleConcat("面积"); hv_result = hv_result.TupleConcat(area.TupleMax()); result = hv_result.Clone(); } } catch (HalconException HDevExpDefaultException) { ho_Rectangle1.Dispose(); ho_Rectangle2.Dispose(); ho_ImageReduced.Dispose(); ho_RegionToDetect.Dispose(); ho_RegionOpening.Dispose(); ho_RegionDifference.Dispose(); ho_Cross.Dispose(); throw HDevExpDefaultException; } ho_Rectangle1.Dispose(); ho_Rectangle2.Dispose(); ho_ImageReduced.Dispose(); ho_RegionToDetect.Dispose(); ho_RegionOpening.Dispose(); ho_RegionDifference.Dispose(); ho_Cross.Dispose(); }
// 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(); }
// Chapter: Graphics / Text // Short Description: This procedure writes a text message. 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_M = null, hv_N = null, hv_Red = null; HTuple hv_Green = null, hv_Blue = null, hv_RowI1Part = null; HTuple hv_ColumnI1Part = null, hv_RowI2Part = null, hv_ColumnI2Part = null; HTuple hv_RowIWin = null, hv_ColumnIWin = null, hv_WidthWin = null; HTuple hv_HeightWin = null, hv_I = null, hv_RowI = new HTuple(); HTuple hv_ColumnI = new HTuple(), hv_StringI = new HTuple(); HTuple hv_MaxAscent = new HTuple(), hv_MaxDescent = new HTuple(); HTuple hv_MaxWidth = new HTuple(), hv_MaxHeight = new HTuple(); HTuple hv_R1 = new HTuple(), hv_C1 = new HTuple(), hv_FactorRowI = new HTuple(); HTuple hv_FactorColumnI = new HTuple(), hv_UseShadow = new HTuple(); HTuple hv_ShadowColor = new HTuple(), 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. // A tuple of values is allowed to display text at different // positions. //Column: The column coordinate of the desired text position // If set to -1, a default value of 12 is used. // A tuple of values is allowed to display text at different // positions. //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... // - if |Row| == |Column| == 1: for each new textline // = else for each text position. //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 // otherwise -> use given string as color string for the shadow color // //It is possible to display multiple text strings in a single call. //In this case, some restrictions apply: //- Multiple text positions can be defined by specifying a tuple // with multiple Row and/or Column coordinates, i.e.: // - |Row| == n, |Column| == n // - |Row| == n, |Column| == 1 // - |Row| == 1, |Column| == n //- If |Row| == |Column| == 1, // each element of String is display in a new textline. //- If multiple positions or specified, the number of Strings // must match the number of positions, i.e.: // - Either |String| == n (each string is displayed at the // corresponding position), // - or |String| == 1 (The string is displayed n times). // if ((int)(new HTuple(hv_Color_COPY_INP_TMP.TupleEqual(new HTuple()))) != 0) { hv_Color_COPY_INP_TMP = ""; } if ((int)(new HTuple(hv_Box_COPY_INP_TMP.TupleEqual(new HTuple()))) != 0) { hv_Box_COPY_INP_TMP = "false"; } // // //Check conditions // hv_M = (new HTuple(hv_Row_COPY_INP_TMP.TupleLength())) * (new HTuple(hv_Column_COPY_INP_TMP.TupleLength() )); hv_N = new HTuple(hv_Row_COPY_INP_TMP.TupleLength()); if ((int)((new HTuple(hv_M.TupleEqual(0))).TupleOr(new HTuple(hv_String_COPY_INP_TMP.TupleEqual( new HTuple())))) != 0) { return; } if ((int)(new HTuple(hv_M.TupleNotEqual(1))) != 0) { //Multiple positions // //Expand single parameters if ((int)(new HTuple((new HTuple(hv_Row_COPY_INP_TMP.TupleLength())).TupleEqual( 1))) != 0) { hv_N = new HTuple(hv_Column_COPY_INP_TMP.TupleLength()); HOperatorSet.TupleGenConst(hv_N, hv_Row_COPY_INP_TMP, out hv_Row_COPY_INP_TMP); } else if ((int)(new HTuple((new HTuple(hv_Column_COPY_INP_TMP.TupleLength() )).TupleEqual(1))) != 0) { HOperatorSet.TupleGenConst(hv_N, hv_Column_COPY_INP_TMP, out hv_Column_COPY_INP_TMP); } else if ((int)(new HTuple((new HTuple(hv_Column_COPY_INP_TMP.TupleLength() )).TupleNotEqual(new HTuple(hv_Row_COPY_INP_TMP.TupleLength())))) != 0) { throw new HalconException("Number of elements in Row and Column does not match."); } if ((int)(new HTuple((new HTuple(hv_String_COPY_INP_TMP.TupleLength())).TupleEqual( 1))) != 0) { HOperatorSet.TupleGenConst(hv_N, hv_String_COPY_INP_TMP, out hv_String_COPY_INP_TMP); } else if ((int)(new HTuple((new HTuple(hv_String_COPY_INP_TMP.TupleLength() )).TupleNotEqual(hv_N))) != 0) { throw new HalconException("Number of elements in Strings does not match number of positions."); } // } // //Prepare window HOperatorSet.GetRgb(hv_WindowHandle, out hv_Red, out hv_Green, out hv_Blue); HOperatorSet.GetPart(hv_WindowHandle, out hv_RowI1Part, out hv_ColumnI1Part, out hv_RowI2Part, out hv_ColumnI2Part); HOperatorSet.GetWindowExtents(hv_WindowHandle, out hv_RowIWin, out hv_ColumnIWin, out hv_WidthWin, out hv_HeightWin); HOperatorSet.SetPart(hv_WindowHandle, 0, 0, hv_HeightWin - 1, hv_WidthWin - 1); // //Loop over all positions HTuple end_val89 = hv_N - 1; HTuple step_val89 = 1; for (hv_I = 0; hv_I.Continue(end_val89, step_val89); hv_I = hv_I.TupleAdd(step_val89)) { hv_RowI = hv_Row_COPY_INP_TMP.TupleSelect(hv_I); hv_ColumnI = hv_Column_COPY_INP_TMP.TupleSelect(hv_I); //Allow multiple strings for a single position. if ((int)(new HTuple(hv_N.TupleEqual(1))) != 0) { hv_StringI = hv_String_COPY_INP_TMP.Clone(); } else { //In case of multiple positions, only single strings //are allowed per position. //For line breaks, use \n in this case. hv_StringI = hv_String_COPY_INP_TMP.TupleSelect(hv_I); } //Default settings //-1 is mapped to 12. if ((int)(new HTuple(hv_RowI.TupleEqual(-1))) != 0) { hv_RowI = 12; } if ((int)(new HTuple(hv_ColumnI.TupleEqual(-1))) != 0) { hv_ColumnI = 12; } // //Split string into one string per line. hv_StringI = ((("" + hv_StringI) + "")).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_RowI.Clone(); hv_C1 = hv_ColumnI.Clone(); } else { //Transform image to window coordinates. hv_FactorRowI = (1.0 * hv_HeightWin) / ((hv_RowI2Part - hv_RowI1Part) + 1); hv_FactorColumnI = (1.0 * hv_WidthWin) / ((hv_ColumnI2Part - hv_ColumnI1Part) + 1); hv_R1 = (((hv_RowI - hv_RowI1Part) + 0.5) * hv_FactorRowI) - 0.5; hv_C1 = (((hv_ColumnI - hv_ColumnI1Part) + 0.5) * hv_FactorColumnI) - 0.5; } // //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.TupleSelect(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 = new HTuple("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 = new HTuple("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_StringI = (" " + hv_StringI) + " "; hv_Width = new HTuple(); for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_StringI.TupleLength() )) - 1); hv_Index = (int)hv_Index + 1) { HOperatorSet.GetStringExtents(hv_WindowHandle, hv_StringI.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_StringI.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_StringI.TupleLength())) - 1); hv_Index = (int)hv_Index + 1) { //Set color if ((int)(new HTuple(hv_N.TupleEqual(1))) != 0) { //Wiht a single text position, each text line //may get a different color. hv_CurrentColor = hv_Color_COPY_INP_TMP.TupleSelect(hv_Index % (new HTuple(hv_Color_COPY_INP_TMP.TupleLength() ))); } else { //With multiple text positions, each position //gets a single color for all text lines. hv_CurrentColor = hv_Color_COPY_INP_TMP.TupleSelect(hv_I % (new HTuple(hv_Color_COPY_INP_TMP.TupleLength() ))); } if ((int)((new HTuple(hv_CurrentColor.TupleNotEqual(""))).TupleAnd(new HTuple(hv_CurrentColor.TupleNotEqual( "auto")))) != 0) { //Valid color? try { HOperatorSet.SetColor(hv_WindowHandle, hv_CurrentColor); } // catch (Exception) catch (HalconException HDevExpDefaultException1) { HDevExpDefaultException1.ToHTuple(out hv_Exception); hv_Exception = ((("Wrong value of control parameter Color[" + (hv_Index % (new HTuple(hv_Color_COPY_INP_TMP.TupleLength() )))) + "] == '") + hv_CurrentColor) + "' (must be a valid color string)"; throw new HalconException(hv_Exception); } } else { HOperatorSet.SetRgb(hv_WindowHandle, hv_Red, hv_Green, hv_Blue); } //Finally display text hv_RowI = hv_R1 + (hv_MaxHeight * hv_Index); HOperatorSet.SetTposition(hv_WindowHandle, hv_RowI, hv_C1); HOperatorSet.WriteString(hv_WindowHandle, hv_StringI.TupleSelect(hv_Index)); } } //Reset changed window settings HOperatorSet.SetRgb(hv_WindowHandle, hv_Red, hv_Green, hv_Blue); HOperatorSet.SetPart(hv_WindowHandle, hv_RowI1Part, hv_ColumnI1Part, hv_RowI2Part, hv_ColumnI2Part); return; }
// Main procedure private void action() { // Stack for temporary objects HObject[] OTemp = new HObject[20]; // Local iconic variables HObject ho_InfoModelContours, ho_SignModelContours; HObject ho_Image = null, ho_SymbolRegions = null, ho_ROI_OCR_01_0 = null; HObject ho_ObjectSelected = null, ho_InfoTransContours = null; HObject ho_SignTransContours = null; // Local control variables HTuple hv_BarWidth = new HTuple(), hv_BarHeight = new HTuple(); HTuple hv_BarCodeHandle = new HTuple(), hv_InfoModel = new HTuple(); HTuple hv_SignModel = new HTuple(), hv_WindowHandle = new HTuple(); //HTuple hv_AcqHandle = new HTuple(), hv_InfoRow = new HTuple(); HTuple hv_InfoRow = new HTuple(); HTuple hv_InfoColumn = new HTuple(), hv_InfoAngle = new HTuple(); HTuple hv_InfoScore = new HTuple(), hv_DecodedDataStrings = new HTuple(); HTuple hv_someitem = new HTuple(), hv_SignRow = new HTuple(); HTuple hv_SignColumn = new HTuple(), hv_SignAngle = new HTuple(); HTuple hv_SignScore = new HTuple(), hv_SymbolNames_OCR_01_0 = new HTuple(); HTuple hv_Ocr_Split = new HTuple(), hv_Area = new HTuple(); HTuple hv_IDRow = new HTuple(), hv_IDColumn = new HTuple(); HTuple hv_IDRow1 = new HTuple(), hv_IDColumn1 = new HTuple(); HTuple hv_IDRow2 = new HTuple(), hv_IDColumn2 = new HTuple(); HTuple hv_IDHeight = new HTuple(), hv_IDWidth = new HTuple(); HTuple hv_IDRatio = new HTuple(), hv_BarIndex = new HTuple(); HTuple hv_Row = new HTuple(), hv_Column = new HTuple(); HTuple hv_InfoHomMat2D = new HTuple(), hv_SignHomMat2D = new HTuple(); HTuple hv_Exception = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_InfoModelContours); HOperatorSet.GenEmptyObj(out ho_SignModelContours); HOperatorSet.GenEmptyObj(out ho_Image); HOperatorSet.GenEmptyObj(out ho_SymbolRegions); HOperatorSet.GenEmptyObj(out ho_ROI_OCR_01_0); HOperatorSet.GenEmptyObj(out ho_ObjectSelected); HOperatorSet.GenEmptyObj(out ho_InfoTransContours); HOperatorSet.GenEmptyObj(out ho_SignTransContours); try { //*** //** INIT //* INIT CONST hv_BarWidth.Dispose(); hv_BarWidth = 800; hv_BarHeight.Dispose(); hv_BarHeight = 100; //* INIT IMAGE //ImageFiles := [] //ImageFiles[0] := 'C:/Users/iwake/OneDrive - wake/Desktop/Localization/Images/Inv/CaliInvV1 (1).bmp' //ImageFiles[1] := 'C:/Users/iwake/OneDrive - wake/Desktop/Localization/Images/Inv/CaliInvV1 (2).bmp' //ImageFiles[2] := 'C:/Users/iwake/OneDrive - wake/Desktop/Localization/Images/Inv/CaliInvV1 (3).bmp' //ImageFiles[3] := 'C:/Users/iwake/OneDrive - wake/Desktop/Localization/Images/Inv/CaliInvV1 (4).bmp' //ImageFiles[4] := 'C:/Users/iwake/OneDrive - wake/Desktop/Localization/Images/Inv/CaliInvV1 (5).bmp' //ImageFiles[5] := 'C:/Users/iwake/OneDrive - wake/Desktop/Localization/Images/Inv/CaliInvV1 (6).bmp' //ImageFiles[6] := 'C:/Users/iwake/OneDrive - wake/Desktop/Localization/Images/Inv/CaliInvV1 (7).bmp' //ImageFiles[7] := 'C:/Users/iwake/OneDrive - wake/Desktop/Localization/Images/Inv/CaliInvV1 (8).bmp' //ImageFiles[8] := 'C:/Users/iwake/OneDrive - wake/Desktop/Localization/Images/Inv/CaliInvV1 (9).bmp' //ImageFiles[9] := 'C:/Users/iwake/OneDrive - wake/Desktop/Localization/Images/Inv/CaliInvV1 (10).bmp' //tuple_length (ImageFiles, Length) //* INIT BARCODE hv_BarCodeHandle.Dispose(); HOperatorSet.CreateBarCodeModel(new HTuple(), new HTuple(), out hv_BarCodeHandle); HOperatorSet.SetBarCodeParam(hv_BarCodeHandle, "quiet_zone", "true"); //* INIT LOC //* Info: hv_InfoModel.Dispose(); HOperatorSet.ReadShapeModel("C:/Users/iwake/OneDrive - wake/Desktop/Localization/InvV1CaliInfo.shm", out hv_InfoModel); ho_InfoModelContours.Dispose(); HOperatorSet.GetShapeModelContours(out ho_InfoModelContours, hv_InfoModel, 1); //* Sign hv_SignModel.Dispose(); HOperatorSet.ReadShapeModel("C:/Users/iwake/OneDrive - wake/Desktop/Localization/InvV1CaliSign.shm", out hv_SignModel); ho_SignModelContours.Dispose(); HOperatorSet.GetShapeModelContours(out ho_SignModelContours, hv_SignModel, 1); //*** //** DISPLAY //* DISPLAY INIT dev_update_off(); //dev_close_window(...); //dev_open_window(...); //*** //** LOOP //Image Acquisition 01: Code generated by Image Acquisition 01 hv_AcqHandle.Dispose(); HOperatorSet.OpenFramegrabber("GigEVision2", 0, 0, 0, 0, 0, 0, "progressive", -1, "default", -1, "false", "default", "c42f90f2b7fa_Hikvision_MVCE12010GM", 0, -1, out hv_AcqHandle); while ((int)(1) != 0) { ho_Image.Dispose(); HOperatorSet.GrabImage(out ho_Image, hv_AcqHandle); { HObject ExpTmpOutVar_0; 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 using (HDevDisposeHelper dh = new HDevDisposeHelper()) { hv_InfoRow.Dispose(); hv_InfoColumn.Dispose(); hv_InfoAngle.Dispose(); hv_InfoScore.Dispose(); HOperatorSet.FindShapeModel(ho_Image, hv_InfoModel, (new HTuple(0)).TupleRad() , (new HTuple(360)).TupleRad(), 0.3, 1, 0.5, "least_squares", (new HTuple(7)).TupleConcat( 1), 0.6, out hv_InfoRow, out hv_InfoColumn, out hv_InfoAngle, out hv_InfoScore); } using (HDevDisposeHelper dh = new HDevDisposeHelper()) { HObject ExpTmpOutVar_0; HOperatorSet.RotateImage(ho_Image, out ExpTmpOutVar_0, ((-hv_InfoAngle)).TupleDeg() , "constant"); ho_Image.Dispose(); ho_Image = ExpTmpOutVar_0; } HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle); //** RECOGNITION //* BARCODE ho_SymbolRegions.Dispose(); hv_DecodedDataStrings.Dispose(); hv_someitem.Dispose(); image_get_bar(ho_Image, out ho_SymbolRegions, hv_BarCodeHandle, out hv_DecodedDataStrings, out hv_someitem); //* LOC //* Info: using (HDevDisposeHelper dh = new HDevDisposeHelper()) { hv_InfoRow.Dispose(); hv_InfoColumn.Dispose(); hv_InfoAngle.Dispose(); hv_InfoScore.Dispose(); HOperatorSet.FindShapeModel(ho_Image, hv_InfoModel, (new HTuple(0)).TupleRad() , (new HTuple(360)).TupleRad(), 0.3, 1, 0.5, "least_squares", (new HTuple(7)).TupleConcat( 1), 0.7, out hv_InfoRow, out hv_InfoColumn, out hv_InfoAngle, out hv_InfoScore); } //* Sign using (HDevDisposeHelper dh = new HDevDisposeHelper()) { hv_SignRow.Dispose(); hv_SignColumn.Dispose(); hv_SignAngle.Dispose(); hv_SignScore.Dispose(); HOperatorSet.FindShapeModel(ho_Image, hv_SignModel, (new HTuple(0)).TupleRad() , (new HTuple(360)).TupleRad(), 0.3, 1, 0.5, "least_squares", (new HTuple(7)).TupleConcat( 1), 0.7, out hv_SignRow, out hv_SignColumn, out hv_SignAngle, out hv_SignScore); } //* Ocr using (HDevDisposeHelper dh = new HDevDisposeHelper()) { ho_ROI_OCR_01_0.Dispose(); HOperatorSet.GenRectangle2(out ho_ROI_OCR_01_0, hv_InfoRow + 70, hv_InfoColumn - 700, hv_InfoAngle, 100, 30); } hv_SymbolNames_OCR_01_0.Dispose(); hv_Ocr_Split.Dispose(); region_ocr_num_svm(ho_Image, ho_ROI_OCR_01_0, new HTuple(), new HTuple(), out hv_SymbolNames_OCR_01_0, out hv_Ocr_Split); hv_Area.Dispose(); hv_IDRow.Dispose(); hv_IDColumn.Dispose(); HOperatorSet.AreaCenter(ho_ROI_OCR_01_0, out hv_Area, out hv_IDRow, out hv_IDColumn); hv_IDRow1.Dispose(); hv_IDColumn1.Dispose(); hv_IDRow2.Dispose(); hv_IDColumn2.Dispose(); HOperatorSet.SmallestRectangle1(ho_ROI_OCR_01_0, out hv_IDRow1, out hv_IDColumn1, out hv_IDRow2, out hv_IDColumn2); hv_IDHeight.Dispose(); hv_IDWidth.Dispose(); hv_IDRatio.Dispose(); HOperatorSet.HeightWidthRatio(ho_ROI_OCR_01_0, out hv_IDHeight, out hv_IDWidth, out hv_IDRatio); //** DISPLAY //* DISPLAY BARCODE set_display_font(hv_ExpDefaultWinHandle, 14, "mono", "true", "false"); HOperatorSet.SetDraw(hv_ExpDefaultWinHandle, "margin"); HOperatorSet.SetLineWidth(hv_ExpDefaultWinHandle, 3); HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "forest green"); HOperatorSet.DispObj(ho_SymbolRegions, hv_ExpDefaultWinHandle); 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_ExpDefaultWinHandle, hv_Row - hv_BarHeight, hv_Column - (0.25 * hv_BarWidth)); } using (HDevDisposeHelper dh = new HDevDisposeHelper()) { HOperatorSet.WriteString(hv_ExpDefaultWinHandle, hv_DecodedDataStrings.TupleSelect( hv_BarIndex - 1)); } } //* DISPLAY LOC HOperatorSet.SetLineWidth(hv_ExpDefaultWinHandle, 1); //* Info: hv_InfoHomMat2D.Dispose(); HOperatorSet.HomMat2dIdentity(out hv_InfoHomMat2D); { HTuple ExpTmpOutVar_0; HOperatorSet.HomMat2dRotate(hv_InfoHomMat2D, hv_InfoAngle, 0, 0, out ExpTmpOutVar_0); hv_InfoHomMat2D.Dispose(); hv_InfoHomMat2D = ExpTmpOutVar_0; } { HTuple ExpTmpOutVar_0; HOperatorSet.HomMat2dTranslate(hv_InfoHomMat2D, hv_InfoRow, hv_InfoColumn, out ExpTmpOutVar_0); hv_InfoHomMat2D.Dispose(); hv_InfoHomMat2D = ExpTmpOutVar_0; } ho_InfoTransContours.Dispose(); HOperatorSet.AffineTransContourXld(ho_InfoModelContours, out ho_InfoTransContours, hv_InfoHomMat2D); HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "green"); HOperatorSet.DispObj(ho_InfoTransContours, hv_ExpDefaultWinHandle); //* Ocr using (HDevDisposeHelper dh = new HDevDisposeHelper()) { HOperatorSet.DispText(hv_ExpDefaultWinHandle, hv_Ocr_Split, "image", hv_IDRow1 + hv_IDHeight, hv_IDColumn1, "blue", new HTuple(), new HTuple()); } //* Sign hv_SignHomMat2D.Dispose(); HOperatorSet.HomMat2dIdentity(out hv_SignHomMat2D); { HTuple ExpTmpOutVar_0; HOperatorSet.HomMat2dRotate(hv_SignHomMat2D, hv_SignAngle, 0, 0, out ExpTmpOutVar_0); hv_SignHomMat2D.Dispose(); hv_SignHomMat2D = ExpTmpOutVar_0; } { HTuple ExpTmpOutVar_0; HOperatorSet.HomMat2dTranslate(hv_SignHomMat2D, hv_SignRow, hv_SignColumn, out ExpTmpOutVar_0); hv_SignHomMat2D.Dispose(); hv_SignHomMat2D = ExpTmpOutVar_0; } ho_SignTransContours.Dispose(); HOperatorSet.AffineTransContourXld(ho_SignModelContours, out ho_SignTransContours, hv_SignHomMat2D); HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "green"); HOperatorSet.DispObj(ho_SignTransContours, hv_ExpDefaultWinHandle); //stop () } // catch (Exception) catch (HalconException HDevExpDefaultException1) { HDevExpDefaultException1.ToHTuple(out hv_Exception); } } HOperatorSet.CloseFramegrabber(hv_AcqHandle); } catch (HalconException HDevExpDefaultException) { ho_InfoModelContours.Dispose(); ho_SignModelContours.Dispose(); ho_Image.Dispose(); ho_SymbolRegions.Dispose(); ho_ROI_OCR_01_0.Dispose(); ho_ObjectSelected.Dispose(); ho_InfoTransContours.Dispose(); ho_SignTransContours.Dispose(); hv_BarWidth.Dispose(); hv_BarHeight.Dispose(); hv_BarCodeHandle.Dispose(); hv_InfoModel.Dispose(); hv_SignModel.Dispose(); hv_WindowHandle.Dispose(); hv_AcqHandle.Dispose(); hv_InfoRow.Dispose(); hv_InfoColumn.Dispose(); hv_InfoAngle.Dispose(); hv_InfoScore.Dispose(); hv_DecodedDataStrings.Dispose(); hv_someitem.Dispose(); hv_SignRow.Dispose(); hv_SignColumn.Dispose(); hv_SignAngle.Dispose(); hv_SignScore.Dispose(); hv_SymbolNames_OCR_01_0.Dispose(); hv_Ocr_Split.Dispose(); hv_Area.Dispose(); hv_IDRow.Dispose(); hv_IDColumn.Dispose(); hv_IDRow1.Dispose(); hv_IDColumn1.Dispose(); hv_IDRow2.Dispose(); hv_IDColumn2.Dispose(); hv_IDHeight.Dispose(); hv_IDWidth.Dispose(); hv_IDRatio.Dispose(); hv_BarIndex.Dispose(); hv_Row.Dispose(); hv_Column.Dispose(); hv_InfoHomMat2D.Dispose(); hv_SignHomMat2D.Dispose(); hv_Exception.Dispose(); throw HDevExpDefaultException; } ho_InfoModelContours.Dispose(); ho_SignModelContours.Dispose(); ho_Image.Dispose(); ho_SymbolRegions.Dispose(); ho_ROI_OCR_01_0.Dispose(); ho_ObjectSelected.Dispose(); ho_InfoTransContours.Dispose(); ho_SignTransContours.Dispose(); hv_BarWidth.Dispose(); hv_BarHeight.Dispose(); hv_BarCodeHandle.Dispose(); hv_InfoModel.Dispose(); hv_SignModel.Dispose(); hv_WindowHandle.Dispose(); hv_AcqHandle.Dispose(); hv_InfoRow.Dispose(); hv_InfoColumn.Dispose(); hv_InfoAngle.Dispose(); hv_InfoScore.Dispose(); hv_DecodedDataStrings.Dispose(); hv_someitem.Dispose(); hv_SignRow.Dispose(); hv_SignColumn.Dispose(); hv_SignAngle.Dispose(); hv_SignScore.Dispose(); hv_SymbolNames_OCR_01_0.Dispose(); hv_Ocr_Split.Dispose(); hv_Area.Dispose(); hv_IDRow.Dispose(); hv_IDColumn.Dispose(); hv_IDRow1.Dispose(); hv_IDColumn1.Dispose(); hv_IDRow2.Dispose(); hv_IDColumn2.Dispose(); hv_IDHeight.Dispose(); hv_IDWidth.Dispose(); hv_IDRatio.Dispose(); hv_BarIndex.Dispose(); hv_Row.Dispose(); hv_Column.Dispose(); hv_InfoHomMat2D.Dispose(); hv_SignHomMat2D.Dispose(); hv_Exception.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; }
/// <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; }
/// <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; }
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; }
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(); }
// Main procedure private void action() { // Local iconic variables HObject ho_Region, ho_SmoothedImage, ho_ReducedImage; HObject ho_ModelImages, ho_ModelRegions, ho_ConnectedRegions; HObject ho_SelectedRegions, ho_ModelContours, ho_TestImage = null; // Local control variables HTuple hv_WindowHandle = null, hv_Colors = null; HTuple hv_MinScore = null, hv_NumMatches = null, hv_AcqHandle = null; HTuple hv_Size = null, hv_Coeffs = null, hv_ModelID = null; HTuple hv_NumLevels = null, hv_AngleStart = null, hv_AngleExtent = null; HTuple hv_AngleStep = null, hv_ScaleMin = null, hv_ScaleMax = null; HTuple hv_ScaleStep = null, hv_Metric = null, hv_MinContrast = null; HTuple hv_keepGrabbing = null, hv_Row = new HTuple(), hv_Column = new HTuple(); HTuple hv_Button = new HTuple(), hv_Exception = new HTuple(); HTuple hv_ErrorCode = new HTuple(), hv_Angle = new HTuple(); HTuple hv_Score = new HTuple(), hv_ScoreLength = new HTuple(); HTuple hv_BestMatchScore = new HTuple(), hv_location = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Region); HOperatorSet.GenEmptyObj(out ho_SmoothedImage); HOperatorSet.GenEmptyObj(out ho_ReducedImage); HOperatorSet.GenEmptyObj(out ho_ModelImages); HOperatorSet.GenEmptyObj(out ho_ModelRegions); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions); HOperatorSet.GenEmptyObj(out ho_SelectedRegions); HOperatorSet.GenEmptyObj(out ho_ModelContours); HOperatorSet.GenEmptyObj(out ho_TestImage); try { // import(...); only in hdevelop // import(...); only in hdevelop if (HDevWindowStack.IsOpen()) { HOperatorSet.CloseWindow(HDevWindowStack.Pop()); } HOperatorSet.SetWindowAttr("background_color", "white"); HOperatorSet.OpenWindow(0, 0, 1024, 768, 0, "", "", out hv_WindowHandle); HDevWindowStack.Push(hv_WindowHandle); // dev_update_time(...); only in hdevelop if (HDevWindowStack.IsOpen()) { HOperatorSet.SetLineWidth(HDevWindowStack.GetActive(), 3); } hv_Colors = new HTuple(); hv_Colors[0] = "red"; hv_Colors[1] = "green"; hv_Colors[2] = "cyan"; hv_MinScore = 0.5; hv_NumMatches = 1; //global object Image HOperatorSet.OpenFramegrabber("GigEVision", 0, 0, 0, 0, 0, 0, "default", -1, "default", -1, "false", "default", "00111cf51192_SENSORTECHNOLOGYCOLTD_STCGE83A", 0, -1, out hv_AcqHandle); HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "ExposureTimeRaw", 9000); disp_message(hv_WindowHandle, "Press F5 to begin live grab. Press Left mouse click to exit live acquisition...", "window", -1, -1, "black", "true"); // stop(...); only in hdevelop abshear_async_acquisition(hv_WindowHandle, hv_AcqHandle); disp_message(hv_WindowHandle, "Hold down Left Mouse Button to select region", "window", -1, -1, "black", "true"); ho_Region.Dispose(); HOperatorSet.DrawRegion(out ho_Region, hv_WindowHandle); HOperatorSet.InfoSmooth("deriche2", 0.3, out hv_Size, out hv_Coeffs); ho_SmoothedImage.Dispose(); HOperatorSet.SmoothImage(ExpGetGlobalVar_Image(), out ho_SmoothedImage, "deriche2", 0.3); ho_ReducedImage.Dispose(); HOperatorSet.ReduceDomain(ho_SmoothedImage, ho_Region, out ho_ReducedImage); ho_ModelImages.Dispose(); ho_ModelRegions.Dispose(); HOperatorSet.InspectShapeModel(ho_ReducedImage, out ho_ModelImages, out ho_ModelRegions, 1, 160); //Since the shape models contain a few extraneous edges, they will be //removed here to give a slightly nicer visualization. ho_ConnectedRegions.Dispose(); HOperatorSet.Connection(ho_ModelRegions, out ho_ConnectedRegions); ho_SelectedRegions.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, "area", "and", 20, 100000); ho_ModelRegions.Dispose(); HOperatorSet.Union1(ho_SelectedRegions, out ho_ModelRegions); ho_ModelContours.Dispose(); HOperatorSet.GenContoursSkeletonXld(ho_ModelRegions, out ho_ModelContours, 1, "filter"); HOperatorSet.CreateShapeModel(ho_ReducedImage, "auto", -0.39, 0.79, "auto", "auto", "use_polarity", "auto", "auto", out hv_ModelID); HOperatorSet.GetShapeModelParams(hv_ModelID, out hv_NumLevels, out hv_AngleStart, out hv_AngleExtent, out hv_AngleStep, out hv_ScaleMin, out hv_ScaleMax, out hv_ScaleStep, out hv_Metric, out hv_MinContrast); abshear_serialize_and_save(ho_SmoothedImage, hv_ModelID); hv_keepGrabbing = 1; while ((int)(hv_keepGrabbing) != 0) { 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; } //Grab and display an image for the matching ho_TestImage.Dispose(); HOperatorSet.GrabImageAsync(out ho_TestImage, hv_AcqHandle, -1); ho_SmoothedImage.Dispose(); HOperatorSet.SmoothImage(ho_TestImage, out ho_SmoothedImage, "deriche2", 0.3); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_TestImage, HDevWindowStack.GetActive()); } HOperatorSet.FindShapeModel(ho_TestImage, hv_ModelID, -0.39, 0.79, hv_MinScore, hv_NumMatches, 0.5, "least_squares", 0, 0.9, out hv_Row, out hv_Column, out hv_Angle, out hv_Score); HDevWindowStack.SetActive(hv_WindowHandle); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green"); } dev_display_shape_matching_results(hv_ModelID, "green", hv_Row, hv_Column, hv_Angle, 1.0, 1.0, 0); HOperatorSet.TupleLength(hv_Score, out hv_ScoreLength); if ((int)(new HTuple(hv_ScoreLength.TupleGreater(0))) != 0) { HOperatorSet.TupleFirstN(hv_Score, 0, out hv_BestMatchScore); disp_message(hv_WindowHandle, "BestMatch: " + hv_BestMatchScore, "window", -1, -1, "black", "true"); hv_location = (("Location: " + hv_Row) + new HTuple(", ")) + hv_Column; disp_message(hv_WindowHandle, hv_location, "window", 40, -1, "black", "true"); } else { disp_message(hv_WindowHandle, "No match found...", "window", -1, -1, "black", "true"); } } HOperatorSet.CloseFramegrabber(hv_AcqHandle); } catch (HalconException HDevExpDefaultException) { ho_Region.Dispose(); ho_SmoothedImage.Dispose(); ho_ReducedImage.Dispose(); ho_ModelImages.Dispose(); ho_ModelRegions.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions.Dispose(); ho_ModelContours.Dispose(); ho_TestImage.Dispose(); throw HDevExpDefaultException; } ho_Region.Dispose(); ho_SmoothedImage.Dispose(); ho_ReducedImage.Dispose(); ho_ModelImages.Dispose(); ho_ModelRegions.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions.Dispose(); ho_ModelContours.Dispose(); ho_TestImage.Dispose(); }
public void TestImage(ref HWindowControl hv_HWindowControl) { if (hv_Test_DlClassifierName == null) { Console.WriteLine("错误提示:无可用分类器"); return; } if (ho_Preprocessed_TestImage == null) { Console.WriteLine("图像未进行预处理"); return; } HObject ho_ImagePreprocessed = new HObject(); HTuple hv_DLClassifierResultHandle = new HTuple(); HTuple hv_Exception = new HTuple(); HTuple hv_Text = new HTuple(); hv_Test_DLClassifierHandle.Dispose(); HOperatorSet.ReadDlClassifier(hv_Test_DlClassifierName, out hv_Test_DLClassifierHandle); //If it is not possible to accumulate more than one image at a time the batch size should be set to 1. HOperatorSet.SetDlClassifierParam(hv_Test_DLClassifierHandle, "batch_size", 1); //Set the runtime to 'cpu' to perform the inference on the CPU, if this is possible on the current hardware. //(see the system requirements in the Installation Guide) try { HOperatorSet.SetDlClassifierParam(hv_Test_DLClassifierHandle, "runtime", "cpu"); hv_Runtime.Dispose(); hv_Runtime = "cpu"; } // catch (Exception) catch (HalconException HDevExpDefaultException1) { HDevExpDefaultException1.ToHTuple(out hv_Exception); //Keep the 'gpu' runtime if switching to 'cpu' failed. hv_Runtime.Dispose(); hv_Runtime = "gpu"; } //This initializes the runtime environment immediately. HOperatorSet.SetDlClassifierParam(hv_Test_DLClassifierHandle, "runtime_init", "immediately"); // //dev_disp_inference_text(hv_Runtime, hv_WindowHandle); // stop(...); only in hdevelop ho_ImagePreprocessed.Dispose(); preprocess_dl_classifier_images(this.ho_TestImage, out ho_ImagePreprocessed, new HTuple(), new HTuple(), hv_Test_DLClassifierHandle); hv_DLClassifierResultHandle.Dispose(); HOperatorSet.ApplyDlClassifier(ho_ImagePreprocessed, hv_Test_DLClassifierHandle, out hv_DLClassifierResultHandle); hv_Inference_PredictedClass.Dispose(); HOperatorSet.GetDlClassifierResult(hv_DLClassifierResultHandle, "all", "predicted_classes", out hv_Inference_PredictedClass); HOperatorSet.GetDlClassifierResult(hv_DLClassifierResultHandle, "all", "confidences", out hv_Inference_Confidences); // DispImageAdaptively(ref hv_HWindowControl, this.ho_TestImage); hv_Text.Dispose(); using (HDevDisposeHelper dh = new HDevDisposeHelper()) { hv_Text = "Predicted class: " + hv_Inference_PredictedClass + "; Confidence: " + hv_Inference_Confidences; } HOperatorSet.DispText(hv_HWindowControl.HalconWindow, hv_Text, "window", "top", "left", "red", "box", "true"); HOperatorSet.DispText(hv_HWindowControl.HalconWindow, "Click to continue", "window", "bottom", "right", "black", new HTuple(), new HTuple()); this.ho_TestImage = null; }
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(); }
static private void dilate_regions_pro(HObject ho_image, out HObject ho_ConnectedDilations, HTuple hv_Row1OfRectangles, HTuple hv_Row2OfRectangles, HTuple hv_Column1OfRectangles, HTuple hv_Column2OfRectangles, HTuple hv_RadiusOfDilation, out HTuple hv_IndexOfRectangles, out HTuple hv_NumberListOfRectangles) { // Stack for temporary objects HObject[] OTemp = new HObject[20]; // Local iconic variables HObject ho_OrgRectangles = null, ho_RegionDilation = null; HObject ho_RegionUnion = null, ho_ConnectedRegions = null, ho_RectangleDilationSelected = null; HObject ho_RectangleSelected = null; // Local control variables HTuple hv_numOfRectangles = new HTuple(); HTuple hv_Number = new HTuple(), hv_IndexOfConnectedRegions = new HTuple(); HTuple hv_NumberOfRectangles = new HTuple(), hv_SubDefectIndexList = new HTuple(); HTuple hv_IndexOfOrgRectangles = new HTuple(), hv_IsSubset = new HTuple(); HTuple hv_Exception = null; HTuple hv_Column2OfRectangles_COPY_INP_TMP = hv_Column2OfRectangles.Clone(); HTuple hv_RadiusOfDilation_COPY_INP_TMP = hv_RadiusOfDilation.Clone(); HTuple hv_Row2OfRectangles_COPY_INP_TMP = hv_Row2OfRectangles.Clone(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_ConnectedDilations); HOperatorSet.GenEmptyObj(out ho_OrgRectangles); HOperatorSet.GenEmptyObj(out ho_RegionDilation); HOperatorSet.GenEmptyObj(out ho_RegionUnion); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions); HOperatorSet.GenEmptyObj(out ho_RectangleDilationSelected); HOperatorSet.GenEmptyObj(out ho_RectangleSelected); try { dev_update_off(); HOperatorSet.SetSystem("clip_region", "true"); // ho_ConnectedDilations.Dispose(); HOperatorSet.GenEmptyObj(out ho_ConnectedDilations); // hv_IndexOfRectangles = new HTuple(); hv_NumberListOfRectangles = new HTuple(); // try { if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_image, HDevWindowStack.GetActive()); } hv_numOfRectangles = 0; HOperatorSet.TupleLength(hv_Row1OfRectangles, out hv_numOfRectangles); if ((int)(new HTuple(hv_numOfRectangles.TupleLessEqual(0))) != 0) { ho_OrgRectangles.Dispose(); ho_RegionDilation.Dispose(); ho_RegionUnion.Dispose(); ho_ConnectedRegions.Dispose(); ho_RectangleDilationSelected.Dispose(); ho_RectangleSelected.Dispose(); return; } if ((int)((new HTuple((new HTuple(hv_numOfRectangles.TupleNotEqual(new HTuple(hv_Row2OfRectangles_COPY_INP_TMP.TupleLength() )))).TupleOr(new HTuple(hv_numOfRectangles.TupleNotEqual(new HTuple(hv_Column1OfRectangles.TupleLength() )))))).TupleOr(new HTuple(hv_numOfRectangles.TupleNotEqual(new HTuple(hv_Column2OfRectangles_COPY_INP_TMP.TupleLength() ))))) != 0) { ho_OrgRectangles.Dispose(); ho_RegionDilation.Dispose(); ho_RegionUnion.Dispose(); ho_ConnectedRegions.Dispose(); ho_RectangleDilationSelected.Dispose(); ho_RectangleSelected.Dispose(); return; } HOperatorSet.TupleMax2(hv_Row1OfRectangles, hv_Row2OfRectangles_COPY_INP_TMP, out hv_Row2OfRectangles_COPY_INP_TMP); HOperatorSet.TupleMax2(hv_Column1OfRectangles, hv_Column2OfRectangles_COPY_INP_TMP, out hv_Column2OfRectangles_COPY_INP_TMP); ho_OrgRectangles.Dispose(); HOperatorSet.GenRectangle1(out ho_OrgRectangles, hv_Row1OfRectangles, hv_Column1OfRectangles, hv_Row2OfRectangles_COPY_INP_TMP, hv_Column2OfRectangles_COPY_INP_TMP); HOperatorSet.TupleMax2(1.5, hv_RadiusOfDilation_COPY_INP_TMP, out hv_RadiusOfDilation_COPY_INP_TMP); ho_RegionDilation.Dispose(); HOperatorSet.DilationCircle(ho_OrgRectangles, out ho_RegionDilation, hv_RadiusOfDilation_COPY_INP_TMP); ho_RegionUnion.Dispose(); HOperatorSet.Union1(ho_RegionDilation, out ho_RegionUnion); ho_ConnectedRegions.Dispose(); HOperatorSet.Connection(ho_RegionUnion, out ho_ConnectedRegions); { HObject ExpTmpOutVar_0; HOperatorSet.SortRegion(ho_ConnectedRegions, out ExpTmpOutVar_0, "first_point", "true", "row"); ho_ConnectedRegions.Dispose(); ho_ConnectedRegions = ExpTmpOutVar_0; } HOperatorSet.CountObj(ho_ConnectedRegions, out hv_Number); HTuple end_val27 = hv_Number; HTuple step_val27 = 1; for (hv_IndexOfConnectedRegions = 1; hv_IndexOfConnectedRegions.Continue(end_val27, step_val27); hv_IndexOfConnectedRegions = hv_IndexOfConnectedRegions.TupleAdd(step_val27)) { hv_NumberOfRectangles = 0; ho_RectangleDilationSelected.Dispose(); HOperatorSet.SelectObj(ho_ConnectedRegions, out ho_RectangleDilationSelected, hv_IndexOfConnectedRegions); hv_SubDefectIndexList = new HTuple(); HTuple end_val31 = hv_numOfRectangles; HTuple step_val31 = 1; for (hv_IndexOfOrgRectangles = 1; hv_IndexOfOrgRectangles.Continue(end_val31, step_val31); hv_IndexOfOrgRectangles = hv_IndexOfOrgRectangles.TupleAdd(step_val31)) { ho_RectangleSelected.Dispose(); HOperatorSet.SelectObj(ho_OrgRectangles, out ho_RectangleSelected, hv_IndexOfOrgRectangles); HOperatorSet.TestSubsetRegion(ho_RectangleSelected, ho_RectangleDilationSelected, out hv_IsSubset); if ((int)(hv_IsSubset) != 0) { hv_IndexOfRectangles = hv_IndexOfRectangles.TupleConcat(hv_IndexOfOrgRectangles - 1); hv_NumberOfRectangles = hv_NumberOfRectangles + 1; } } hv_NumberListOfRectangles = hv_NumberListOfRectangles.TupleConcat(hv_NumberOfRectangles); } ho_ConnectedDilations.Dispose(); ho_ConnectedDilations = ho_ConnectedRegions.CopyObj(1, -1); } // catch (Exception) catch (HalconException HDevExpDefaultException1) { HDevExpDefaultException1.ToHTuple(out hv_Exception); //do nothing. } // ho_OrgRectangles.Dispose(); ho_RegionDilation.Dispose(); ho_RegionUnion.Dispose(); ho_ConnectedRegions.Dispose(); ho_RectangleDilationSelected.Dispose(); ho_RectangleSelected.Dispose(); return; } catch (HalconException HDevExpDefaultException) { ho_OrgRectangles.Dispose(); ho_RegionDilation.Dispose(); ho_RegionUnion.Dispose(); ho_ConnectedRegions.Dispose(); ho_RectangleDilationSelected.Dispose(); ho_RectangleSelected.Dispose(); throw HDevExpDefaultException; } }
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; }
public void length1(HObject ho_RegionIntersection, HObject ho_RegionDifference, out HObject ho_ObjectsConcat, out HTuple hv_length) { // Local iconic variables HObject ho_RegionOpening = null, ho_Arrow1 = null; HObject ho_Arrow2 = null, ho_Region1 = null, ho_Region2 = null; // Local control variables HTuple hv_Rows = new HTuple(), hv_Columns = new HTuple(); HTuple hv_Rows1 = new HTuple(), hv_Columns1 = new HTuple(); HTuple hv_Exception; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_ObjectsConcat); HOperatorSet.GenEmptyObj(out ho_RegionOpening); HOperatorSet.GenEmptyObj(out ho_Arrow1); HOperatorSet.GenEmptyObj(out ho_Arrow2); HOperatorSet.GenEmptyObj(out ho_Region1); HOperatorSet.GenEmptyObj(out ho_Region2); hv_length = new HTuple(); try { try { HOperatorSet.GetRegionPoints(ho_RegionIntersection, out hv_Rows, out hv_Columns); //水平 ho_RegionOpening.Dispose(); HOperatorSet.OpeningRectangle1(ho_RegionDifference, out ho_RegionOpening, (hv_Columns.TupleMax()) - (hv_Columns.TupleMin() + 30), (hv_Rows.TupleMax()) - (hv_Rows.TupleMin() + 30 )); HOperatorSet.GetRegionPoints(ho_RegionOpening, out hv_Rows1, out hv_Columns1); if ((int)(new HTuple((new HTuple(hv_Rows1.TupleLength())).TupleGreater(0))) == 0) { hv_length = 0; return; } if ((int)(new HTuple((new HTuple(hv_Columns1.TupleLength())).TupleGreater(0))) == 0) { hv_length = 0; return; } ho_Arrow1.Dispose(); gen_arrow_contour_xld(out ho_Arrow1, hv_Rows1.TupleMax(), (hv_Columns1.TupleMax() ) + 20, hv_Rows1.TupleMax(), hv_Columns1.TupleMax(), 5, 5); ho_Arrow2.Dispose(); gen_arrow_contour_xld(out ho_Arrow2, hv_Rows1.TupleMin(), (hv_Columns1.TupleMin() ) - 20, hv_Rows1.TupleMin(), hv_Columns1.TupleMin(), 5, 5); ho_Region1.Dispose(); HOperatorSet.GenRegionContourXld(ho_Arrow1, out ho_Region1, "filled"); ho_Region2.Dispose(); HOperatorSet.GenRegionContourXld(ho_Arrow2, out ho_Region2, "filled"); ho_ObjectsConcat.Dispose(); HOperatorSet.ConcatObj(ho_Region1, ho_Region2, out ho_ObjectsConcat); if ((int)(new HTuple((new HTuple(hv_Columns1.TupleLength())).TupleGreater(0))) != 0) { hv_length = (hv_Columns1.TupleMax()) - (hv_Columns1.TupleMin()); } else { hv_length = 0; } } // catch (Exception) catch (HalconException HDevExpDefaultException1) { HDevExpDefaultException1.ToHTuple(out hv_Exception); hv_length = 0; } ho_RegionOpening.Dispose(); ho_Arrow1.Dispose(); ho_Arrow2.Dispose(); ho_Region1.Dispose(); ho_Region2.Dispose(); return; } catch (HalconException HDevExpDefaultException) { ho_RegionOpening.Dispose(); ho_Arrow1.Dispose(); ho_Arrow2.Dispose(); ho_Region1.Dispose(); ho_Region2.Dispose(); throw HDevExpDefaultException; } }
// 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(); }
// Main procedure private void action() { // Local iconic variables HObject ho_Rectangle1, ho_Rectangle2, ho_ImageReduced; HObject ho_RegionToDetect, ho_RegionOpening, ho_RegionDifference; HObject ho_Cross = null; HObject selectObject; // 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; HTuple Num; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Region); HOperatorSet.GenEmptyObj(out ho_Rectangle1); HOperatorSet.GenEmptyObj(out ho_Rectangle2); 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); HOperatorSet.GenEmptyObj(out selectObject); try { ho_Rectangle1.Dispose(); HOperatorSet.GenRectangle1(out ho_Rectangle1, this.tr1, this.tc1, this.tr2, this.tc2); ho_Region.Dispose(); HOperatorSet.Intersection(ho_Rectangle1, 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); HTuple area, row, col; HOperatorSet.Connection(ho_Region, out ho_Region); HOperatorSet.AreaCenter(ho_Region, out area, out row, out col); ho_Rectangle2.Dispose(); HOperatorSet.GenRectangle1(out ho_Rectangle2, this.rr1, this.rc1, rr2, rc2); ho_Region.Dispose(); HOperatorSet.Intersection(ho_Rectangle2, this.algorithm.Region, out ho_Region); HOperatorSet.Connection(ho_Region, out ho_Region); HOperatorSet.CountObj(ho_Region, out Num); HOperatorSet.SelectShape(ho_Region, out selectObject, "area", "and", area.TupleMax(), area.TupleMax() * 3); HOperatorSet.ShapeTrans(selectObject, out RegionToDisp, "rectangle1"); HOperatorSet.CountObj(selectObject, out Num); try { if (selectObject.IsInitialized()) { HOperatorSet.Union1(selectObject, out RegionToDisp); } else { HOperatorSet.Union1(selectObject, out RegionToDisp); } } // catch (Exception) catch (HalconException HDevExpDefaultException1) { HDevExpDefaultException1.ToHTuple(out hv_Exception); Num = 0; } finally { HTuple hv_result = new HTuple(); hv_result = hv_result.TupleConcat("数量"); hv_result = hv_result.TupleConcat(Num); result = hv_result.Clone(); } } catch (HalconException HDevExpDefaultException) { ho_Rectangle1.Dispose(); ho_Rectangle2.Dispose(); ho_ImageReduced.Dispose(); ho_RegionToDetect.Dispose(); ho_RegionOpening.Dispose(); ho_RegionDifference.Dispose(); ho_Cross.Dispose(); throw HDevExpDefaultException; } ho_Rectangle1.Dispose(); ho_Rectangle2.Dispose(); ho_ImageReduced.Dispose(); ho_RegionToDetect.Dispose(); ho_RegionOpening.Dispose(); ho_RegionDifference.Dispose(); ho_Cross.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; }
private void action(string ImgPath) { HObject ho_Image1, ho_Image = null, ho_Caltab = null; HTuple hv_ImgPath = null, hv_Pointer = null; HTuple hv_Type = null, hv_Width = null, hv_Height = null; HTuple hv_WindowHandle1 = null, hv_StartCamPar = null; HTuple hv_TmpCtrl_FindCalObjParNames = null, hv_TmpCtrl_FindCalObjParValues = null; HTuple hv_CalibDataID = null, hv_NumImages = null, hv_I = null; HTuple hv_RCoord = new HTuple(), hv_CCoord = new HTuple(); HTuple hv_Index = null, hv_StartPose = new HTuple(), hv_CaltabName = null; HTuple hv_Error = null, hv_CamParam = null, hv_WindowHandle = null; HTuple hv_Exception = null, hv_Row = null, hv_Column = null; HTuple hv_Pose = null, hv_X = null, hv_Y = null, hv_Z = null; HTuple hv_Image_X1 = null, hv_Image_Y1 = null, hv_Distance_XY = null; HTuple hv_Image_X2 = null, hv_Image_Y2 = null, hv_World_X1 = null; HTuple hv_World_Y1 = null, hv_World_X2 = null, hv_World_Y2 = null; HTuple hv_DistanceWorld = null, hv_DistanceImage = null; HTuple hv_DistanceOneMilimeter = null, hv_DistanceOnePixel = null; HTuple hv_OffSetX = null, hv_OffSetY = null, hv_FinalPose = null; HTuple hv_Erros = null, hv_X1 = null, hv_Y1 = null; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image1); HOperatorSet.GenEmptyObj(out ho_Image); HOperatorSet.GenEmptyObj(out ho_Caltab); try { //相机内部标定(需要很多位置的标定板图像) 黑三角为第三象限 //图片文件夹 hv_ImgPath = ImgPath; if (HDevWindowStack.IsOpen()) { HOperatorSet.CloseWindow(HDevWindowStack.Pop()); } ho_Image1.Dispose(); HOperatorSet.ReadImage(out ho_Image1, hv_ImgPath + "calib_01"); HOperatorSet.GetImagePointer1(ho_Image1, out hv_Pointer, out hv_Type, out hv_Width, out hv_Height); HOperatorSet.SetWindowAttr("background_color", "black"); HOperatorSet.OpenWindow(0, 0, hv_Width, hv_Height, 0, "visible", "", out hv_WindowHandle1); HDevWindowStack.Push(hv_WindowHandle1); //初始相机参数 hv_StartCamPar = new HTuple(); hv_StartCamPar[0] = 0; hv_StartCamPar[1] = 0; hv_StartCamPar[2] = 1.67e-006; hv_StartCamPar[3] = 1.67e-006; hv_StartCamPar = hv_StartCamPar.TupleConcat(hv_Width / 2); hv_StartCamPar = hv_StartCamPar.TupleConcat(hv_Height / 2); hv_StartCamPar = hv_StartCamPar.TupleConcat(hv_Width); hv_StartCamPar = hv_StartCamPar.TupleConcat(hv_Height); 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_StartCamPar); //根据标定板规格生成标定板文件 HOperatorSet.GenCaltab(7, 7, 0.000625, 0.5, "caltab.descr", "caltab.ps"); //根据标定板模型创建一个标定对象(描述校准对象) HOperatorSet.SetCalibDataCalibObject(hv_CalibDataID, 0, "caltab.descr"); hv_NumImages = 20; HTuple end_val24 = hv_NumImages; HTuple step_val24 = 1; for (hv_I = 1; hv_I.Continue(end_val24, step_val24); hv_I = hv_I.TupleAdd(step_val24)) { ho_Image.Dispose(); HOperatorSet.ReadImage(out ho_Image, (hv_ImgPath + "calib_") + (hv_I.TupleString( "02d"))); //找到Halcon标定板,并将数据存入校准数据模型中(收集数据) //即提取校准对象的mark点的图像坐标以及相机粗略估计姿态 if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } HOperatorSet.FindCalibObject(ho_Image, hv_CalibDataID, 0, 0, hv_I, hv_TmpCtrl_FindCalObjParNames, hv_TmpCtrl_FindCalObjParValues); //从校准数据模型中获取轮廓数据 ho_Caltab.Dispose(); HOperatorSet.GetCalibDataObservContours(out ho_Caltab, hv_CalibDataID, "caltab", 0, 0, hv_I); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Caltab, HDevWindowStack.GetActive()); } HOperatorSet.GetCalibDataObservPoints(hv_CalibDataID, 0, 0, hv_I, out hv_RCoord, out hv_CCoord, out hv_Index, out hv_StartPose); //find_marks_and_pose (Image, Caltab, CaltabName, StartCamPar, 128, 10, 18, 0.9, 15, 100, RCoord, CCoord, StartPose) if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "red"); } HOperatorSet.DispCircle(hv_WindowHandle1, hv_RCoord, hv_CCoord, HTuple.TupleGenConst( new HTuple(hv_RCoord.TupleLength()), 2.5)); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetPart(HDevWindowStack.GetActive(), 0, 0, hv_Height - 1, hv_Width - 1); } //set_calib_data_observ_points (CalibDataID, 0, 0, i, RCoord, CCoord, 'all', StartPose) } //执行实际的校准 //校准相机 HOperatorSet.CalibrateCameras(hv_CalibDataID, out hv_Error); //获得相机参数 HOperatorSet.GetCalibData(hv_CalibDataID, "camera", 0, "params", out hv_CamParam); //保存相机参数 HOperatorSet.WriteCamPar(hv_CamParam, "camera_parameters.dat"); HOperatorSet.ClearCalibData(hv_CalibDataID); // stop(...); only in hdevelop //外部标定(只需要一张标定板图像) //即获取相机的姿态 if (HDevWindowStack.IsOpen()) { HOperatorSet.SetDraw(HDevWindowStack.GetActive(), "margin"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetLineWidth(HDevWindowStack.GetActive(), 1); } if (HDevWindowStack.IsOpen()) { HOperatorSet.CloseWindow(HDevWindowStack.Pop()); } HOperatorSet.SetWindowAttr("background_color", "black"); HOperatorSet.OpenWindow(0, hv_Width + 5, hv_Width, hv_Height, 0, "visible", "", out hv_WindowHandle); HDevWindowStack.Push(hv_WindowHandle); //读取初始相机参数 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; // hv_X = new HTuple(); hv_X[0] = 0; hv_X[1] = 50; hv_X[2] = 100; hv_X[3] = 80; hv_Y = new HTuple(); hv_Y[0] = 5; hv_Y[1] = 0; hv_Y[2] = 5; hv_Y[3] = 0; hv_Z = new HTuple(); hv_Z[0] = 0; hv_Z[1] = 0; hv_Z[2] = 0; hv_Z[3] = 0; hv_RCoord = new HTuple(); hv_RCoord[0] = 414; hv_RCoord[1] = 227; hv_RCoord[2] = 85; hv_RCoord[3] = 128; hv_CCoord = new HTuple(); hv_CCoord[0] = 119; hv_CCoord[1] = 318; hv_CCoord[2] = 550; hv_CCoord[3] = 448; HOperatorSet.VectorToPose(hv_X, hv_Y, hv_Z, hv_RCoord, hv_CCoord, hv_CamParam, "telecentric_planar_robust", "error", out hv_FinalPose, out hv_Erros); HOperatorSet.WritePose(hv_FinalPose, "campose.dat"); HOperatorSet.ImagePointsToWorldPlane(hv_CamParam, hv_FinalPose, hv_Row, hv_Column, 1, out hv_X1, out hv_Y1); } catch (HalconException HDevExpDefaultException) { ho_Image1.Dispose(); ho_Image.Dispose(); ho_Caltab.Dispose(); throw HDevExpDefaultException; } ho_Image1.Dispose(); ho_Image.Dispose(); ho_Caltab.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 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; }
// Main procedure private void action() { // Stack for temporary objects HObject[] OTemp = new HObject[20]; // Local iconic variables HObject ho_SignModelContours, ho_Image = null; HObject ho_SymbolRegions = null, ho_ROI_OCR_01_0 = null, ho_EDGE = null; HObject ho_ObjectSelected = null, ho_SignTransContours = null; // Local control variables HTuple hv_BarWidth = new HTuple(), hv_BarHeight = new HTuple(); HTuple hv_BarCodeHandle = new HTuple(), hv_SignModel = new HTuple(); HTuple hv_AcqHandle = new HTuple(); HTuple hv_InfoRow = new HTuple(), hv_InfoColumn = new HTuple(); HTuple hv_InfoAngle = new HTuple(), hv_InfoScore = new HTuple(); HTuple hv_DecodedDataStrings = new HTuple(), hv_someitem = new HTuple(); HTuple hv_SignRow = new HTuple(), hv_SignColumn = new HTuple(); HTuple hv_SignAngle = new HTuple(), hv_SignScore = new HTuple(); HTuple hv_SymbolNames_OCR_01_0 = new HTuple(), hv_Ocr_Split = new HTuple(); HTuple hv_Area = new HTuple(), hv_IDRow = new HTuple(); HTuple hv_IDColumn = new HTuple(), hv_IDRow1 = new HTuple(); HTuple hv_IDColumn1 = new HTuple(), hv_IDRow2 = new HTuple(); HTuple hv_IDColumn2 = new HTuple(), hv_IDHeight = new HTuple(); HTuple hv_IDWidth = new HTuple(), hv_IDRatio = new HTuple(); HTuple hv_HeadSignScale = new HTuple(), hv_HeadSignRow = new HTuple(); HTuple hv_HeadSignCol = new HTuple(), hv_HeadPhi = new HTuple(); HTuple hv_sign = new HTuple(), hv_BarIndex = new HTuple(); HTuple hv_Row = new HTuple(), hv_Column = new HTuple(); HTuple hv_SignHomMat2D = new HTuple(), hv_Exception = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_SignModelContours); HOperatorSet.GenEmptyObj(out ho_Image); HOperatorSet.GenEmptyObj(out ho_SymbolRegions); HOperatorSet.GenEmptyObj(out ho_ROI_OCR_01_0); HOperatorSet.GenEmptyObj(out ho_EDGE); HOperatorSet.GenEmptyObj(out ho_ObjectSelected); HOperatorSet.GenEmptyObj(out ho_SignTransContours); try { //*** //** INIT //* INIT CONST hv_BarWidth.Dispose(); hv_BarWidth = 800; hv_BarHeight.Dispose(); hv_BarHeight = 100; //* INIT IMAGE //* 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 hv_SignModel.Dispose(); HOperatorSet.ReadShapeModel("C:/Users/zhang-sh/source/repos/repo/Model/file/InvV1CaliSign.shm", out hv_SignModel); ho_SignModelContours.Dispose(); HOperatorSet.GetShapeModelContours(out ho_SignModelContours, hv_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 using (HDevDisposeHelper dh = new HDevDisposeHelper()) { hv_InfoRow.Dispose(); hv_InfoColumn.Dispose(); hv_InfoAngle.Dispose(); hv_InfoScore.Dispose(); HOperatorSet.FindShapeModel(ho_Image, hv_SignModel, (new HTuple(0)).TupleRad() , (new HTuple(360)).TupleRad(), 0.3, 1, 0.5, "least_squares", (new HTuple(7)).TupleConcat( 1), 0.6, out hv_InfoRow, out hv_InfoColumn, out hv_InfoAngle, out hv_InfoScore); } using (HDevDisposeHelper dh = new HDevDisposeHelper()) { HObject ExpTmpOutVar_0; HOperatorSet.RotateImage(ho_Image, out ExpTmpOutVar_0, ((-hv_InfoAngle)).TupleDeg() , "constant"); ho_Image.Dispose(); ho_Image = ExpTmpOutVar_0; } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } //** RECOGNITION //* 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 using (HDevDisposeHelper dh = new HDevDisposeHelper()) { hv_SignRow.Dispose(); hv_SignColumn.Dispose(); hv_SignAngle.Dispose(); hv_SignScore.Dispose(); HOperatorSet.FindShapeModel(ho_Image, hv_SignModel, (new HTuple(0)).TupleRad() , (new HTuple(360)).TupleRad(), 0.3, 1, 0.5, "least_squares", (new HTuple(7)).TupleConcat( 1), 0.7, out hv_SignRow, out hv_SignColumn, out hv_SignAngle, out hv_SignScore); } //* Ocr using (HDevDisposeHelper dh = new HDevDisposeHelper()) { ho_ROI_OCR_01_0.Dispose(); HOperatorSet.GenRectangle2(out ho_ROI_OCR_01_0, hv_InfoRow + 70, hv_InfoColumn - 700, hv_InfoAngle, 100, 30); } hv_SymbolNames_OCR_01_0.Dispose(); hv_Ocr_Split.Dispose(); hDevelo.region_ocr_num_svm(ho_Image, ho_ROI_OCR_01_0, new HTuple(), new HTuple(), out hv_SymbolNames_OCR_01_0, out hv_Ocr_Split); hv_Area.Dispose(); hv_IDRow.Dispose(); hv_IDColumn.Dispose(); HOperatorSet.AreaCenter(ho_ROI_OCR_01_0, out hv_Area, out hv_IDRow, out hv_IDColumn); hv_IDRow1.Dispose(); hv_IDColumn1.Dispose(); hv_IDRow2.Dispose(); hv_IDColumn2.Dispose(); HOperatorSet.SmallestRectangle1(ho_ROI_OCR_01_0, out hv_IDRow1, out hv_IDColumn1, out hv_IDRow2, out hv_IDColumn2); hv_IDHeight.Dispose(); hv_IDWidth.Dispose(); hv_IDRatio.Dispose(); HOperatorSet.HeightWidthRatio(ho_ROI_OCR_01_0, out hv_IDHeight, out hv_IDWidth, out hv_IDRatio); //* Sign hv_HeadSignScale.Dispose(); hv_HeadSignScale = 1; hv_HeadSignRow.Dispose(); hv_HeadSignRow = new HTuple(hv_SignRow); hv_HeadSignCol.Dispose(); hv_HeadSignCol = new HTuple(hv_SignColumn); hv_HeadPhi.Dispose(); hv_HeadPhi = new HTuple(hv_SignAngle); ho_EDGE.Dispose(); hv_sign.Dispose(); hDevelo.region_judge_sign(ho_Image, out ho_EDGE, hv_HeadSignScale, hv_HeadSignRow, hv_HeadSignCol, hv_HeadPhi, hv_ExpDefaultWinHandle, out hv_sign); //** DISPLAY //* DISPLAY BARCODE 显示条形码 hDevelo.set_display_font(hv_ExpDefaultWinHandle, 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_ExpDefaultWinHandle, hv_Row - hv_BarHeight, hv_Column - (0.25 * hv_BarWidth)); } using (HDevDisposeHelper dh = new HDevDisposeHelper()) { HOperatorSet.WriteString(hv_ExpDefaultWinHandle, 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 hv_SignHomMat2D.Dispose(); HOperatorSet.HomMat2dIdentity(out hv_SignHomMat2D); { HTuple ExpTmpOutVar_0; HOperatorSet.HomMat2dRotate(hv_SignHomMat2D, hv_SignAngle, 0, 0, out ExpTmpOutVar_0); hv_SignHomMat2D.Dispose(); hv_SignHomMat2D = ExpTmpOutVar_0; } { HTuple ExpTmpOutVar_0; HOperatorSet.HomMat2dTranslate(hv_SignHomMat2D, hv_SignRow, hv_SignColumn, out ExpTmpOutVar_0); hv_SignHomMat2D.Dispose(); hv_SignHomMat2D = ExpTmpOutVar_0; } ho_SignTransContours.Dispose(); HOperatorSet.AffineTransContourXld(ho_SignModelContours, out ho_SignTransContours, hv_SignHomMat2D); if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_SignTransContours, HDevWindowStack.GetActive() ); } //* Sign if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColored(HDevWindowStack.GetActive(), 12); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_EDGE, HDevWindowStack.GetActive()); } //dump_window_image (ImageResult, WindowHandle) //stop () } // catch (Exception) catch (HalconException HDevExpDefaultException1) { HDevExpDefaultException1.ToHTuple(out hv_Exception); } } HOperatorSet.CloseFramegrabber(hv_AcqHandle); } catch (HalconException HDevExpDefaultException) { ho_SignModelContours.Dispose(); ho_Image.Dispose(); ho_SymbolRegions.Dispose(); ho_ROI_OCR_01_0.Dispose(); ho_EDGE.Dispose(); ho_ObjectSelected.Dispose(); ho_SignTransContours.Dispose(); hv_BarWidth.Dispose(); hv_BarHeight.Dispose(); hv_BarCodeHandle.Dispose(); hv_SignModel.Dispose(); hv_ExpImageRawWinHandle.Dispose(); hv_ExpDefaultWinHandle.Dispose(); hv_AcqHandle.Dispose(); hv_InfoRow.Dispose(); hv_InfoColumn.Dispose(); hv_InfoAngle.Dispose(); hv_InfoScore.Dispose(); hv_DecodedDataStrings.Dispose(); hv_someitem.Dispose(); hv_SignRow.Dispose(); hv_SignColumn.Dispose(); hv_SignAngle.Dispose(); hv_SignScore.Dispose(); hv_SymbolNames_OCR_01_0.Dispose(); hv_Ocr_Split.Dispose(); hv_Area.Dispose(); hv_IDRow.Dispose(); hv_IDColumn.Dispose(); hv_IDRow1.Dispose(); hv_IDColumn1.Dispose(); hv_IDRow2.Dispose(); hv_IDColumn2.Dispose(); hv_IDHeight.Dispose(); hv_IDWidth.Dispose(); hv_IDRatio.Dispose(); hv_HeadSignScale.Dispose(); hv_HeadSignRow.Dispose(); hv_HeadSignCol.Dispose(); hv_HeadPhi.Dispose(); hv_sign.Dispose(); hv_BarIndex.Dispose(); hv_Row.Dispose(); hv_Column.Dispose(); hv_SignHomMat2D.Dispose(); hv_Exception.Dispose(); throw HDevExpDefaultException; } ho_SignModelContours.Dispose(); ho_Image.Dispose(); ho_SymbolRegions.Dispose(); ho_ROI_OCR_01_0.Dispose(); ho_EDGE.Dispose(); ho_ObjectSelected.Dispose(); ho_SignTransContours.Dispose(); hv_BarWidth.Dispose(); hv_BarHeight.Dispose(); hv_BarCodeHandle.Dispose(); hv_SignModel.Dispose(); hv_ExpImageRawWinHandle.Dispose(); hv_ExpDefaultWinHandle.Dispose(); hv_AcqHandle.Dispose(); hv_InfoRow.Dispose(); hv_InfoColumn.Dispose(); hv_InfoAngle.Dispose(); hv_InfoScore.Dispose(); hv_DecodedDataStrings.Dispose(); hv_someitem.Dispose(); hv_SignRow.Dispose(); hv_SignColumn.Dispose(); hv_SignAngle.Dispose(); hv_SignScore.Dispose(); hv_SymbolNames_OCR_01_0.Dispose(); hv_Ocr_Split.Dispose(); hv_Area.Dispose(); hv_IDRow.Dispose(); hv_IDColumn.Dispose(); hv_IDRow1.Dispose(); hv_IDColumn1.Dispose(); hv_IDRow2.Dispose(); hv_IDColumn2.Dispose(); hv_IDHeight.Dispose(); hv_IDWidth.Dispose(); hv_IDRatio.Dispose(); hv_HeadSignScale.Dispose(); hv_HeadSignRow.Dispose(); hv_HeadSignCol.Dispose(); hv_HeadPhi.Dispose(); hv_sign.Dispose(); hv_BarIndex.Dispose(); hv_Row.Dispose(); hv_Column.Dispose(); hv_SignHomMat2D.Dispose(); hv_Exception.Dispose(); }