public bool CreateLineModel() { try { HOperatorSet.ClearMetrologyModel(hv_MetrologyHandle); HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle); if (ho_Image == null) { return(false); } HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height); if (hv_Row1.Length < 1) { return(false); } HOperatorSet.AddMetrologyObjectLineMeasure(hv_MetrologyHandle, hv_Row1, hv_Column1, hv_Row2, hv_Column2, hv_MeasureLength1, hv_MeasureLength2, hv_MeasureSigma, hv_MeasureThreshold, hv_GenParamName, hv_GenParamValue, out hv_Index); errorFlag = false; return(true); } catch (Exception exc) { errorFlag = true; return(false); } }
///<summary> ///创建直线检测模板(仿射变换) ///</summary> public void CreateLineModel(HTuple hv_HomMat2D, HTuple hv_MeasureRow1, HTuple hv_MeasureColumn1, HTuple hv_MeasureRow2, HTuple hv_MeasureColumn2, HTuple hv_MeasureLength1, HTuple hv_MeasureLength2, HTuple hv_MeasureSigma, HTuple hv_MeasureThreshold, out HTuple hv_MetrologyHandle, out HTuple hv_Index) { HTuple mhv_Row1; HTuple mhv_Column1; HTuple mhv_Row2; HTuple mhv_Column2; HTuple mhv_MetrologyHandle = new HTuple(); hv_Index = new HTuple(); hv_MetrologyHandle = new HTuple(); try { HOperatorSet.ClearMetrologyModel(mhv_MetrologyHandle); HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle); HOperatorSet.AffineTransPixel(hv_HomMat2D, hv_MeasureRow1, hv_MeasureColumn1, out mhv_Row1, out mhv_Column1); HOperatorSet.AffineTransPixel(hv_HomMat2D, hv_MeasureRow2, hv_MeasureColumn2, out mhv_Row2, out mhv_Column2); HOperatorSet.DistancePp(mhv_Row1, mhv_Column1, mhv_Row2, mhv_Column2, out HTuple DistancePp); hv_MeasureLength2 = DistancePp / 2; HOperatorSet.AddMetrologyObjectLineMeasure(mhv_MetrologyHandle, mhv_Row1, mhv_Column1, mhv_Row2, mhv_Column2, hv_MeasureLength1, hv_MeasureLength2, hv_MeasureSigma, hv_MeasureThreshold, new HTuple(), new HTuple(), out hv_Index); hv_MetrologyHandle = mhv_MetrologyHandle; return; } catch (Exception) { return; } }
///<summary> ///创建直线检测模板(无仿射变换) ///</summary> public void CreateLineModel(HTuple hv_MeasureRow1, HTuple hv_MeasureColumn1, HTuple hv_MeasureRow2, HTuple hv_MeasureColumn2, HTuple hv_MeasureLength1, HTuple hv_MeasureLength2, HTuple hv_MeasureSigma, HTuple hv_MeasureThreshold, out HTuple hv_MetrologyHandle, out HTuple hv_Index) { HTuple mhv_MetrologyHandle = new HTuple(); hv_MetrologyHandle = new HTuple(); hv_Index = new HTuple(); hv_MetrologyHandle = new HTuple(); try { HOperatorSet.ClearMetrologyModel(mhv_MetrologyHandle); HOperatorSet.CreateMetrologyModel(out mhv_MetrologyHandle); //HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height); HOperatorSet.AddMetrologyObjectLineMeasure(mhv_MetrologyHandle, hv_MeasureRow1, hv_MeasureColumn1, hv_MeasureRow2, hv_MeasureColumn2, hv_MeasureLength1, hv_MeasureLength2, hv_MeasureSigma, hv_MeasureThreshold, new HTuple(), new HTuple(), out hv_Index); hv_MetrologyHandle = mhv_MetrologyHandle; return; } catch (Exception) { return; } }
private void btnSaveCornerParams_Click(object sender, EventArgs e) { try { HOperatorSet.ClearMetrologyModel(cornerParams.hv_MetrologyHandle); HOperatorSet.CreateMetrologyModel(out cornerParams.hv_MetrologyHandle); if (cornerParams.ho_Image == null) { MessageBox.Show("图像为空!"); return; } HOperatorSet.GetImageSize(cornerParams.ho_Image, out cornerParams.hv_Width, out cornerParams.hv_Height); HOperatorSet.SetMetrologyModelImageSize(cornerParams.hv_MetrologyHandle, cornerParams.hv_Width, cornerParams.hv_Height); if (cornerParams.hv_Row1_Horizon.Length < 1) { MessageBox.Show("水平直线模型为空!"); return; } //if (cornerParams.hv_Row1_Vertical.Length < 1) //{ // MessageBox.Show("竖直直线模型为空!"); // return; //} HTuple hv_Row1 = null; HTuple hv_Column1 = null; HTuple hv_Row2 = null; HTuple hv_Column2 = null; HOperatorSet.TupleInsert(cornerParams.hv_Row1_Horizon, 0, cornerParams.hv_Row1_Vertical, out hv_Row1); HOperatorSet.TupleInsert(cornerParams.hv_Column1_Horizon, 0, cornerParams.hv_Column1_Vertical, out hv_Column1); HOperatorSet.TupleInsert(cornerParams.hv_Row2_Horizon, 0, cornerParams.hv_Row2_Vertical, out hv_Row2); HOperatorSet.TupleInsert(cornerParams.hv_Column2_Horizon, 0, cornerParams.hv_Column2_Vertical, out hv_Column2); HOperatorSet.AddMetrologyObjectLineMeasure(cornerParams.hv_MetrologyHandle, hv_Row1, hv_Column1, hv_Row2, hv_Column2, cornerParams.hv_MeasureLength1, cornerParams.hv_MeasureLength2, cornerParams.hv_MeasureSigma, cornerParams.hv_MeasureThreshold, cornerParams.hv_GenParamName, cornerParams.hv_GenParamValue, out cornerParams.hv_Index); if (!FindCorner()) { MessageBox.Show("直线模型查找失败!"); return; } MessageBox.Show("保存直线模型成功!"); } catch (Exception exc) { MessageBox.Show("保存直线模型失败!" + exc.ToString()); } }
private void btnSaveLineParams_Click(object sender, EventArgs e) { try { HOperatorSet.ClearMetrologyModel(lineParams.hv_MetrologyHandle); HOperatorSet.CreateMetrologyModel(out lineParams.hv_MetrologyHandle); if (lineParams.ho_Image == null) { MessageBox.Show("图像为空!"); return; } HOperatorSet.GetImageSize(lineParams.ho_Image, out lineParams.hv_Width, out lineParams.hv_Height); HOperatorSet.SetMetrologyModelImageSize(lineParams.hv_MetrologyHandle, lineParams.hv_Width, lineParams.hv_Height); if (lineParams.hv_Row1.Length != 1) { MessageBox.Show("直线模型错误!"); return; } HOperatorSet.AddMetrologyObjectLineMeasure(lineParams.hv_MetrologyHandle, lineParams.hv_Row1, lineParams.hv_Column1, lineParams.hv_Row2, lineParams.hv_Column2, lineParams.hv_MeasureLength1, lineParams.hv_MeasureLength2, lineParams.hv_MeasureSigma, lineParams.hv_MeasureThreshold, lineParams.hv_GenParamName, lineParams.hv_GenParamValue, out lineParams.hv_Index); if (!FindCorner()) { MessageBox.Show("直线模型查找失败!"); return; } MessageBox.Show("保存直线模型成功!"); } catch (Exception exc) { MessageBox.Show("保存直线模型失败!" + exc.ToString()); } }
public static bool createLineMetrology(paramLine param, ref HTuple handle) { HTuple index; try { HOperatorSet.CreateMetrologyModel(out handle); HOperatorSet.AddMetrologyObjectLineMeasure(handle, param.lineRow1, param.lineColumn1, param.lineRow2, param.lineColumn2, param.MeasureLenght1, param.MeasureLenght2, 1, param.MeasureThreshold, "measure_transition", param.MeasureTransition, out index); HOperatorSet.SetMetrologyObjectParam(handle, "all", "num_measures", param.NumMeasures); HOperatorSet.SetMetrologyObjectParam(handle, "all", "num_instances", param.NumInstances); HOperatorSet.SetMetrologyObjectParam(handle, "all", "min_score", param.MinScore); if (0 < handle.Length) { return(true); } } catch (HalconException ex) { string msg = ex.GetErrorMessage(); } return(false); }
// Main procedure private void action() { // Local iconic variables HObject ho_Image, ho_Rectangle, ho_ImageReduced; HObject ho_Region, ho_ResultContour = null, ho_ContCircle = null; HObject ho_Contour = null, ho_Cross = null; // Local control variables HTuple hv_WindowHandle = null, hv_Width = null; HTuple hv_Height = null, hv_MetrologyHandle = null, hv_LineRow1 = null; HTuple hv_LineColumn1 = null, hv_LineRow2 = null, hv_LineColumn2 = null; HTuple hv_Tolerance = null, hv_Index1 = null, hv_Rows = null; HTuple hv_Columns = null, hv_I = null, hv_LineParameter = new HTuple(); HTuple hv_Angle = new HTuple(), hv_Row = new HTuple(); HTuple hv_Column = new HTuple(), hv_IsOverlapping1 = new HTuple(); HTuple hv_Orientation1 = new HTuple(), hv_Orientation2 = new HTuple(); HTuple hv_MRow = new HTuple(), hv_MColumn = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image); HOperatorSet.GenEmptyObj(out ho_Rectangle); HOperatorSet.GenEmptyObj(out ho_ImageReduced); HOperatorSet.GenEmptyObj(out ho_Region); HOperatorSet.GenEmptyObj(out ho_ResultContour); HOperatorSet.GenEmptyObj(out ho_ContCircle); HOperatorSet.GenEmptyObj(out ho_Contour); HOperatorSet.GenEmptyObj(out ho_Cross); try { //This program shows how to detect the edges of a diamond //with subpixel accuracy and calculate the angle between them. // //In contrast to the example measure_diamond.hdev, //this example uses a metrology model to measure the edges. // //First, the top of the diamond is roughly segmented //to align the metrology objects. //Then, the metrology model is applied and returns the //parameters of the fitted lines. //Finally, the angle between the two lines is computed. // //Display initializations dev_update_off(); if (HDevWindowStack.IsOpen()) { HOperatorSet.CloseWindow(HDevWindowStack.Pop()); } ho_Image.Dispose(); HOperatorSet.ReadImage(out ho_Image, "diamond/diamond_01"); dev_open_window_fit_image(ho_Image, 0, 0, -1, -1, out hv_WindowHandle); set_display_font(hv_WindowHandle, 16, "mono", "true", "false"); HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height); // //Create the metrology model data structure HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle); //The image size is set in advance to speed up the //first call of apply_metrology_model. HOperatorSet.SetMetrologyModelImageSize(hv_MetrologyHandle, hv_Width, hv_Height); //Define the parameters of the metrology line objects hv_LineRow1 = new HTuple(); hv_LineRow1[0] = 155; hv_LineRow1[1] = 155; hv_LineColumn1 = new HTuple(); hv_LineColumn1[0] = 400; hv_LineColumn1[1] = 400; hv_LineRow2 = new HTuple(); hv_LineRow2[0] = 290; hv_LineRow2[1] = 290; hv_LineColumn2 = new HTuple(); hv_LineColumn2[0] = 230; hv_LineColumn2[1] = 570; hv_Tolerance = 20; // //Create two metrology line objects and set parameters HOperatorSet.AddMetrologyObjectLineMeasure(hv_MetrologyHandle, hv_LineRow1, hv_LineColumn1, hv_LineRow2, hv_LineColumn2, hv_Tolerance, 10, 1, 20, new HTuple(), new HTuple(), out hv_Index1); //Create region of interest for the alignment ho_Rectangle.Dispose(); HOperatorSet.GenRectangle1(out ho_Rectangle, (hv_LineRow1.TupleSelect(0)) - 40, (hv_LineColumn1.TupleSelect(0)) - 50, (hv_LineRow1.TupleSelect(0)) + 20, (hv_LineColumn1.TupleSelect( 0)) + 50); //Change the reference coordinate system in which the //metrology model is given to be situated at the top of the diamond ho_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_Rectangle, out ho_ImageReduced); ho_Region.Dispose(); HOperatorSet.Threshold(ho_ImageReduced, out ho_Region, 128, 255); HOperatorSet.GetRegionPoints(ho_Region, out hv_Rows, out hv_Columns); HOperatorSet.SetMetrologyModelParam(hv_MetrologyHandle, "reference_system", ((((hv_Rows.TupleSelect(0))).TupleConcat(hv_Columns.TupleSelect(0)))).TupleConcat( 0)); // //Main loop // for (hv_I = 1; (int)hv_I <= 5; hv_I = (int)hv_I + 1) { ho_Image.Dispose(); HOperatorSet.ReadImage(out ho_Image, "diamond/diamond_" + (hv_I.TupleString( "02"))); //Roughly segment the diamond's position ho_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_Rectangle, out ho_ImageReduced); ho_Region.Dispose(); HOperatorSet.Threshold(ho_ImageReduced, out ho_Region, 128, 255); //Extract the top of the diamond HOperatorSet.GetRegionPoints(ho_Region, out hv_Rows, out hv_Columns); // //Use the top of the diamond to align the metrology model in //the current image // HOperatorSet.AlignMetrologyModel(hv_MetrologyHandle, hv_Rows.TupleSelect( 0), hv_Columns.TupleSelect(0), 0); // // //Perform the measurement for both lines in one call // HOperatorSet.ApplyMetrologyModel(ho_Image, hv_MetrologyHandle); // //Access results // HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "result_type", "all_param", out hv_LineParameter); HOperatorSet.AngleLl(hv_LineParameter.TupleSelect(0), hv_LineParameter.TupleSelect( 1), hv_LineParameter.TupleSelect(2), hv_LineParameter.TupleSelect(3), hv_LineParameter.TupleSelect(4), hv_LineParameter.TupleSelect(5), hv_LineParameter.TupleSelect( 6), hv_LineParameter.TupleSelect(7), out hv_Angle); hv_Angle = hv_Angle.TupleDeg(); // //Display results // //Create line contours ho_ResultContour.Dispose(); HOperatorSet.GetMetrologyObjectResultContour(out ho_ResultContour, hv_MetrologyHandle, "all", "all", 1.5); HOperatorSet.IntersectionLines(hv_LineParameter.TupleSelect(0), hv_LineParameter.TupleSelect( 1), hv_LineParameter.TupleSelect(2), hv_LineParameter.TupleSelect(3), hv_LineParameter.TupleSelect(4), hv_LineParameter.TupleSelect(5), hv_LineParameter.TupleSelect( 6), hv_LineParameter.TupleSelect(7), out hv_Row, out hv_Column, out hv_IsOverlapping1); //Calculate the orientation of the two lines HOperatorSet.LineOrientation(hv_LineParameter.TupleSelect(0), hv_LineParameter.TupleSelect( 1), hv_LineParameter.TupleSelect(2), hv_LineParameter.TupleSelect(3), out hv_Orientation1); if ((int)(new HTuple(hv_Orientation1.TupleGreater(0))) != 0) { hv_Orientation1 = hv_Orientation1 - ((new HTuple(180)).TupleRad()); } HOperatorSet.LineOrientation(hv_LineParameter.TupleSelect(4), hv_LineParameter.TupleSelect( 5), hv_LineParameter.TupleSelect(6), hv_LineParameter.TupleSelect(7), out hv_Orientation2); // //Visualize the angle between the lines ho_ContCircle.Dispose(); HOperatorSet.GenCircleContourXld(out ho_ContCircle, hv_Row, hv_Column, 100, hv_Orientation1, hv_Orientation2, "positive", 1); //Get the used measure regions and the measured points //for visualization ho_Contour.Dispose(); HOperatorSet.GetMetrologyObjectMeasures(out ho_Contour, hv_MetrologyHandle, "all", "all", out hv_MRow, out hv_MColumn); ho_Cross.Dispose(); HOperatorSet.GenCrossContourXld(out ho_Cross, hv_MRow, hv_MColumn, 6, (new HTuple(45)).TupleRad() ); //Display everything if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetLineWidth(HDevWindowStack.GetActive(), 1); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "yellow"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Contour, HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Cross, HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetLineWidth(HDevWindowStack.GetActive(), 2); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_ResultContour, HDevWindowStack.GetActive()); } if (HDevWindowStack.IsOpen()) { HOperatorSet.SetColor(HDevWindowStack.GetActive(), "blue"); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_ContCircle, HDevWindowStack.GetActive()); } disp_message(hv_WindowHandle, ("Angle = " + (hv_Angle.TupleString(".5"))) + "бу", "window", 12, 12, "black", "true"); if ((int)(new HTuple(hv_I.TupleLess(5))) != 0) { disp_continue_message(hv_WindowHandle, "black", "true"); } // stop(); only in hdevelop } //Clean up memory HOperatorSet.ClearMetrologyModel(hv_MetrologyHandle); } catch (HalconException HDevExpDefaultException) { ho_Image.Dispose(); ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_Region.Dispose(); ho_ResultContour.Dispose(); ho_ContCircle.Dispose(); ho_Contour.Dispose(); ho_Cross.Dispose(); throw HDevExpDefaultException; } ho_Image.Dispose(); ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_Region.Dispose(); ho_ResultContour.Dispose(); ho_ContCircle.Dispose(); ho_Contour.Dispose(); ho_Cross.Dispose(); }
public bool Find_halcon_line(ExecuteBuffer _executeBuffer, HWndCtrl hWndCtrl, 模板GVName_halcon Model_result, Dictionary <int, PointName> Point_temp_result, out string result_info, out HTuple Row1, out HTuple Column1, out HTuple Row2, out HTuple Column2, bool show_info) { HTuple MetrologyHandle; HTuple pic_wid, pic_height; Row1 = new HTuple(); Column1 = new HTuple(); Row2 = new HTuple(); Column2 = new HTuple(); result_info = ""; HOperatorSet.CreateMetrologyModel(out MetrologyHandle); if (!_executeBuffer.imageBuffer.ContainsKey(this.Threshold_image.SelectedItem.ToString() + ".img")) { // MessageBox.Show("查找直线: 输入图像已经不存在,请重置设置输入图像"); result_info = " 查找直线: 输入图像已经不存在,请重置设置输入图像"; return(false); } if (_executeBuffer.imageBuffer[this.Threshold_image.SelectedItem.ToString() + ".img"] == null) { // MessageBox.Show("查找直线: image参数为空或者未赋值"); result_info = " 查找直线: 输入图像已经不存在,请重置设置输入图像"; return(false); } HObject imagein = _executeBuffer.imageBuffer[this.Threshold_image.SelectedItem.ToString() + ".img"]; HOperatorSet.GetImageSize(imagein, out pic_wid, out pic_height); HOperatorSet.SetMetrologyModelImageSize(MetrologyHandle, pic_wid, pic_height); HTuple Index; HTuple trow1, trow2, tcol1, tcol2; if (checkBox_line.Checked) { if (xrow1 == null || xrow2 == null || xcol1 == null || xcol2 == null) { // MessageBox.Show("查找直线:未确认线位置"); result_info = " 查找直线: 未确认线位置"; return(false); } else { trow1 = xrow1; tcol1 = xcol1; trow2 = xrow2; tcol2 = xcol2; } } else { if (this.cbb_source1.SelectedIndex == 0) { if (Model_result == null) { // MessageBox.Show("查找直线:匹配列表为空,请设置"); result_info = " 查找直线: 匹配列表为空,请设置"; return(false); } if (Model_result.点X.TupleLength() < 1) { // MessageBox.Show("查找直线:匹配列表为空,请设置"); result_info = " 查找直线: 匹配列表为空,请设置"; return(false); } trow1 = Model_result.点Y[0]; tcol1 = Model_result.点X[0]; } else { int number1 = Convert.ToInt32(this.tb_value1.Text); if (!Point_temp_result.ContainsKey(number1)) { // MessageBox.Show("查找直线:全局列表点无此点,请设置"); result_info = " 查找直线: 全局列表无此点,请设置"; return(false); } else { trow1 = Point_temp_result[number1].点Y; tcol1 = Point_temp_result[number1].点X; } } int number2 = Convert.ToInt32(this.tb_value2.Text); if (!Point_temp_result.ContainsKey(number2)) { // MessageBox.Show("查找直线:全局列表点无此点,请设置"); result_info = " 查找直线: 全局列表无此点,请设置"; return(false); } else { trow2 = Point_temp_result[number2].点Y; tcol2 = Point_temp_result[number2].点X; } } HOperatorSet.AddMetrologyObjectLineMeasure(MetrologyHandle, trow1, tcol1, trow2, tcol2, Convert.ToInt32(this.measure_length1.Text.ToString()), Convert.ToInt32(this.measure_length2.Text.ToString()), 1, Convert.ToInt32(this.measure_threshold.Text.ToString()), new HTuple(), new HTuple(), out Index); HOperatorSet.SetMetrologyObjectParam(MetrologyHandle, "all", "measure_transition", this.cbb_transition.SelectedItem.ToString()); HOperatorSet.SetMetrologyObjectParam(MetrologyHandle, "all", "num_measures", Convert.ToInt32(this.num_measure.Text.ToString())); HOperatorSet.SetMetrologyObjectParam(MetrologyHandle, "all", "num_instances", 40); HOperatorSet.SetMetrologyObjectParam(MetrologyHandle, "all", "measure_sigma", 1); //HOperatorSet.SetMetrologyObjectParam(MetrologyHandle,"all","measure_threshold",50); HOperatorSet.SetMetrologyObjectParam(MetrologyHandle, "all", "measure_interpolation", "bicubic"); HOperatorSet.SetMetrologyObjectParam(MetrologyHandle, "all", "measure_select", this.cbb_measure_select.SelectedItem.ToString()); HOperatorSet.SetMetrologyObjectParam(MetrologyHandle, "all", "min_score", Convert.ToDouble(this.min_score.Text)); HOperatorSet.ApplyMetrologyModel(imagein, MetrologyHandle); HObject Contours, Cross; HTuple Rowx, Columnx; HOperatorSet.GetMetrologyObjectMeasures(out Contours, MetrologyHandle, "all", "all", out Rowx, out Columnx); HOperatorSet.GenCrossContourXld(out Cross, Rowx, Columnx, 6, 0.785398); HTuple hv_Parameter = null; //得到线的起点和终点坐标并显示出来 HOperatorSet.GetMetrologyObjectResult(MetrologyHandle, "all", "all", "result_type", "all_param", out hv_Parameter); if (hv_Parameter.TupleLength() == 0) { result_info = " 查找直线: 在指定位置,找直线失败"; // MessageBox.Show("未找到直线"); return(false); } int line_count = hv_Parameter.TupleLength() / 4; double[] Row_1 = new double[line_count]; double[] Column_1 = new double[line_count]; double[] Row_2 = new double[line_count]; double[] Column_2 = new double[line_count]; for (int imx = 0; imx < line_count; imx++) { Row_1[imx] = hv_Parameter[imx * 4 + 1].D; Column_1[imx] = hv_Parameter[imx * 4].D; Row_2[imx] = hv_Parameter[imx * 4 + 3].D; Column_2[imx] = hv_Parameter[imx * 4 + 2].D; } Row1 = (HTuple)Row_1; Column1 = (HTuple)Column_1; Row2 = (HTuple)Row_2; Column2 = (HTuple)Column_2; HObject ho_Contour; HOperatorSet.GenEmptyObj(out ho_Contour); ho_Contour.Dispose(); HOperatorSet.GetMetrologyObjectResultContour(out ho_Contour, MetrologyHandle, "all", "all", 1.5); HOperatorSet.ClearMetrologyModel(MetrologyHandle); if (show_info) { hWndCtrl.changeGraphicSettings(GraphicsContext.GC_COLOR, this.comboBox1.SelectedItem.ToString()); hWndCtrl.addIconicVar(Cross); hWndCtrl.addIconicVar(Contours); } hWndCtrl.changeGraphicSettings(GraphicsContext.GC_COLOR, this.comboBox1.SelectedItem.ToString()); hWndCtrl.addIconicVar(ho_Contour); hWndCtrl.repaint(); return(true); }
public bool CreateLineModel(HTuple hv_HomMat2D) { try { HOperatorSet.ClearMetrologyModel(hv_MetrologyHandle); HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle); if (ho_Image == null) { return(false); } HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height); if (hv_Row1_Horizon.Length < 1) { return(false); } if (hv_Row1_Vertical.Length < 1) { return(false); } HTuple hv_Row1 = null; HTuple hv_Column1 = null; HTuple hv_Row2 = null; HTuple hv_Column2 = null; HTuple mhv_Row1_Horizon; //水平线 HTuple mhv_Column1_Horizon; HTuple mhv_Row2_Horizon; HTuple mhv_Column2_Horizon; HTuple mhv_Row1_Vertical; //竖直线 HTuple mhv_Column1_Vertical; HTuple mhv_Row2_Vertical; HTuple mhv_Column2_Vertical; HOperatorSet.AffineTransPixel(hv_HomMat2D, hv_Row1_Horizon, hv_Column1_Horizon, out mhv_Row1_Horizon, out mhv_Column1_Horizon); HOperatorSet.AffineTransPixel(hv_HomMat2D, hv_Row2_Horizon, hv_Column2_Horizon, out mhv_Row2_Horizon, out mhv_Column2_Horizon); HOperatorSet.AffineTransPixel(hv_HomMat2D, hv_Row1_Vertical, hv_Column1_Vertical, out mhv_Row1_Vertical, out mhv_Column1_Vertical); HOperatorSet.AffineTransPixel(hv_HomMat2D, hv_Row2_Vertical, hv_Column2_Vertical, out mhv_Row2_Vertical, out mhv_Column2_Vertical); HOperatorSet.TupleInsert(mhv_Row1_Horizon, 0, mhv_Row1_Vertical, out hv_Row1); HOperatorSet.TupleInsert(mhv_Column1_Horizon, 0, mhv_Column1_Vertical, out hv_Column1); HOperatorSet.TupleInsert(mhv_Row2_Horizon, 0, mhv_Row2_Vertical, out hv_Row2); HOperatorSet.TupleInsert(mhv_Column2_Horizon, 0, mhv_Column2_Vertical, out hv_Column2); HOperatorSet.AddMetrologyObjectLineMeasure(hv_MetrologyHandle, hv_Row1, hv_Column1, hv_Row2, hv_Column2, hv_MeasureLength1, hv_MeasureLength2, hv_MeasureSigma, hv_MeasureThreshold, hv_GenParamName, hv_GenParamValue, out hv_Index); errorFlag = false; return(true); } catch (Exception exc) { errorFlag = true; return(false); } }
public void Run() { if (inputImage == null) { FormFindLine.Instance.TextBoxMessageDisp("图像为空", System.Drawing.Color.Red); toolRunStatu = ToolRunStatu.Not_Input_Image; return; } try { UpdateImage(); if (inputPose != null) { HTuple Row = inputPose.X - templatePose.X; HTuple Col = inputPose.Y - templatePose.Y; HTuple angle = inputPose.U - templatePose.U; HTuple _homMat2D; HOperatorSet.HomMat2dIdentity(out _homMat2D); HOperatorSet.HomMat2dRotate(_homMat2D, (HTuple)(angle), (HTuple)templatePose.X, (HTuple)templatePose.Y, out _homMat2D); HOperatorSet.HomMat2dTranslate(_homMat2D, (HTuple)(Row), (HTuple)(Col), out _homMat2D); //对预期线的起始点做放射变换 HOperatorSet.AffineTransPixel(_homMat2D, (HTuple)expectLineStartRow, (HTuple)expectLineStartCol, out newExpectLineStartRow, out newExpectLineStartCol); HOperatorSet.AffineTransPixel(_homMat2D, (HTuple)expectLineEndRow, (HTuple)expectLineEndCol, out newExpectLineEndRow, out newExpectLineEndCol); } else { newExpectLineStartRow = expectLineStartRow; newExpectLineStartCol = expectLineStartCol; newExpectLineEndRow = expectLineEndRow; newExpectLineEndCol = expectLineEndCol; } HTuple handleID; HOperatorSet.CreateMetrologyModel(out handleID); HTuple width, height; HOperatorSet.GetImageSize(inputImage, out width, out height); HOperatorSet.SetMetrologyModelImageSize(handleID, width[0], height[0]); HTuple index; HOperatorSet.AddMetrologyObjectLineMeasure(handleID, newExpectLineStartRow, newExpectLineStartCol, newExpectLineEndRow, newExpectLineEndCol, new HTuple(50), new HTuple(20), new HTuple(1), new HTuple(30), new HTuple(), new HTuple(), out index); //参数在这里设置 HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_transition"), new HTuple(polarity)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("num_measures"), new HTuple(cliperNum)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_length1"), new HTuple(length)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_length2"), new HTuple(weidth)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_threshold"), new HTuple(threshold)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_select"), new HTuple(edgeSelect)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_sigma"), new HTuple(sigma)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("min_score"), new HTuple(minScore)); HOperatorSet.ApplyMetrologyModel(inputImage, handleID); //显示所有卡尺 HTuple pointRow, pointCol; HOperatorSet.GetMetrologyObjectMeasures(out contours, handleID, new HTuple("all"), new HTuple("all"), out pointRow, out pointCol); HOperatorSet.SetColor(FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow, new HTuple("blue")); HOperatorSet.DispObj(contours, FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow); FormFindLine.Instance.myHwindow.DispObj(contours, "blue"); //显示指示找线方向的箭头 HTuple arrowAngle; HOperatorSet.AngleLx(newExpectLineStartRow, newExpectLineStartCol, newExpectLineEndRow, newExpectLineEndCol, out arrowAngle); #region 测试箭头 #endregion arrowAngle = arrowAngle + Math.PI / 2; arrowAngle = arrowAngle.TupleDeg(); HTuple row = (newExpectLineStartRow + newExpectLineEndRow) / 2; HTuple column = (newExpectLineStartCol + newExpectLineEndCol) / 2; double drow, dcolumn; double arrowLength = length + 100; if (0 <= arrowAngle && arrowAngle <= 90) { drow = Math.Abs(arrowLength * Math.Sin(((HTuple)arrowAngle).TupleRad())); dcolumn = Math.Abs(arrowLength * Math.Cos(((HTuple)arrowAngle).TupleRad())); HOperatorSet.DispArrow(FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow, row, column, row - drow, column + dcolumn, 5.0); } else if (arrowAngle > 90 && arrowAngle <= 180) { drow = arrowLength * Math.Sin(((HTuple)(180 - arrowAngle)).TupleRad()); dcolumn = arrowLength * Math.Cos(((HTuple)(180 - arrowAngle)).TupleRad()); HOperatorSet.DispArrow(FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow, row, column, row - drow, column - dcolumn, 5.0); } else if (arrowAngle < 0 && arrowAngle >= -90) { drow = arrowLength * Math.Sin(((HTuple)arrowAngle * -1).TupleRad()); dcolumn = arrowLength * Math.Cos(((HTuple)arrowAngle * -1).TupleRad()); HOperatorSet.DispArrow(FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow, row, column, row + drow, column + dcolumn, 5.0); } else if (arrowAngle < -90 && arrowAngle >= -180) { drow = Math.Abs(arrowLength * Math.Sin(((HTuple)arrowAngle + 180).TupleRad())); dcolumn = Math.Abs(arrowLength * Math.Cos(((HTuple)arrowAngle + 180).TupleRad())); HOperatorSet.DispArrow(FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow, row, column, row + drow, column - dcolumn, 5.0); } //把点显示出来 HObject cross; HOperatorSet.GenCrossContourXld(out cross, pointRow, pointCol, new HTuple(12), new HTuple(0)); HOperatorSet.SetColor(FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow, new HTuple("orange")); HOperatorSet.DispObj(cross, FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow); FormFindLine.Instance.myHwindow.DispObj(cross, "orange"); //得到所找到的线 HTuple parameter; HObject line; HOperatorSet.GetMetrologyObjectResult(handleID, new HTuple("all"), new HTuple("all"), new HTuple("result_type"), new HTuple("all_param"), out parameter); HOperatorSet.GetMetrologyObjectResultContour(out line, handleID, new HTuple("all"), new HTuple("all"), new HTuple(1.5)); if (parameter.Length >= 4) { ResultLineStartRow = parameter[0]; ResultLineStartCol = parameter[1]; ResultLineEndRow = parameter[2]; ResultLineEndCol = parameter[3]; Point start = new Point() { Row = ResultLineStartRow, Col = ResultLineStartCol }; Point end = new Point() { Row = ResultLineEndRow, Col = ResultLineEndCol }; resultLine = new Line() { StartPoint = start, EndPoint = end }; //显示找到的线 HOperatorSet.SetColor(FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow, new HTuple("green")); // HOperatorSet.DispObj(line, GetWindowHandle(jobName)); FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow.SetLineWidth(2); FormFindLine.Instance.myHwindow.DispObj(line, "green"); } HOperatorSet.AngleLx(ResultLineStartRow, ResultLineStartCol, ResultLineEndRow, ResultLineEndCol, out _angle); HOperatorSet.ClearMetrologyModel(handleID); FormFindLine.Instance.tbx_resultStartRow.Text = ResultLineStartRow.ToString(); FormFindLine.Instance.tbx_resultStartCol.Text = ResultLineEndCol.ToString(); FormFindLine.Instance.tbx_resultEndRow.Text = ResultLineEndRow.ToString(); FormFindLine.Instance.tbx_resultEndCol.Text = ResultLineEndCol.ToString(); FormFindLine.Instance.TextBoxMessageDisp("运行成功", System.Drawing.Color.Green); toolRunStatu = ToolRunStatu.Succeed; } catch (Exception ex) { FormFindLine.Instance.TextBoxMessageDisp("工具运行异常" + ex.Message, System.Drawing.Color.Red); } }
public void Run(SoftwareRunState softwareRunState) { Stopwatch sw = new Stopwatch(); sw.Restart(); HTuple homMat2DArrow = null; HObject arrow = null, arrowTrans = null; HObject drawLine = null, imageReducedLine; if (inputImage == null) { FormFindLine.Instance.SetToolStatus("工具输入图像为空", ToolRunStatu.Not_Input_Image); return; } try { UpdateImage(); if (inputPose != null) { HTuple Row = inputPose.X - templatePose.X; HTuple Col = inputPose.Y - templatePose.Y; HTuple angle = inputPose.U - templatePose.U; HTuple _homMat2D; HOperatorSet.HomMat2dIdentity(out _homMat2D); HOperatorSet.HomMat2dRotate(_homMat2D, (HTuple)(angle), (HTuple)templatePose.X, (HTuple)templatePose.Y, out _homMat2D); HOperatorSet.HomMat2dTranslate(_homMat2D, (HTuple)(Row), (HTuple)(Col), out _homMat2D); //对预期线的起始点做放射变换 HOperatorSet.AffineTransPixel(_homMat2D, (HTuple)expectLineStartRow, (HTuple)expectLineStartCol, out newExpectLineStartRow, out newExpectLineStartCol); HOperatorSet.AffineTransPixel(_homMat2D, (HTuple)expectLineEndRow, (HTuple)expectLineEndCol, out newExpectLineEndRow, out newExpectLineEndCol); } else { newExpectLineStartRow = expectLineStartRow; newExpectLineStartCol = expectLineStartCol; newExpectLineEndRow = expectLineEndRow; newExpectLineEndCol = expectLineEndCol; } HTuple handleID; HOperatorSet.CreateMetrologyModel(out handleID); HTuple width, height; HOperatorSet.GetImageSize(inputImage, out width, out height); HOperatorSet.SetMetrologyModelImageSize(handleID, width[0], height[0]); HTuple index; HOperatorSet.AddMetrologyObjectLineMeasure(handleID, newExpectLineStartRow, newExpectLineStartCol, newExpectLineEndRow, newExpectLineEndCol, new HTuple(50), new HTuple(20), new HTuple(1), new HTuple(30), new HTuple(), new HTuple(), out index); //参数在这里设置 HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_transition"), new HTuple(polarity)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("num_measures"), new HTuple(cliperNum)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_length1"), new HTuple(length)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_length2"), new HTuple(weidth)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_threshold"), new HTuple(threshold)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_select"), new HTuple(edgeSelect)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_sigma"), new HTuple(sigma)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("min_score"), new HTuple(minScore)); HOperatorSet.ApplyMetrologyModel(inputImage, handleID); //显示所有卡尺 HTuple pointRow, pointCol; HOperatorSet.GetMetrologyObjectMeasures(out contoursDisp, handleID, new HTuple("all"), new HTuple("all"), out pointRow, out pointCol); //显示指示找线方向的箭头 #region 测试箭头 HTuple arrowRow = null, arrowColumn = null; HOperatorSet.GenRegionLine(out drawLine, newExpectLineStartRow, newExpectLineStartCol, newExpectLineEndRow, newExpectLineEndCol); HOperatorSet.ReduceDomain(inputImage, drawLine, out imageReducedLine); HOperatorSet.GetRegionPoints(imageReducedLine, out arrowRow, out arrowColumn); if (arrowRow.Length < 200) { CommonMethods.CommonMethods.gen_arrow_contour_xld(out arrow, arrowRow[0], arrowColumn[0], arrowRow[arrowRow.Length - 1], arrowColumn[arrowRow.Length - 1], 20, 20); } else { CommonMethods.CommonMethods.gen_arrow_contour_xld(out arrow, arrowRow[0], arrowColumn[0], arrowRow[200], arrowColumn[200], 20, 20); } HOperatorSet.VectorAngleToRigid(newExpectLineStartRow, newExpectLineStartCol, 0, (newExpectLineStartRow + newExpectLineEndRow) / 2, (newExpectLineStartCol + newExpectLineEndCol) / 2, new HTuple(-90).TupleRad(), out homMat2DArrow); HOperatorSet.AffineTransContourXld(arrow, out arrowDisp, homMat2DArrow); #endregion //把点显示出来 HOperatorSet.GenCrossContourXld(out crossDisp, pointRow, pointCol, new HTuple(12), new HTuple(0)); //得到所找到的线 HTuple parameter; HOperatorSet.GetMetrologyObjectResult(handleID, new HTuple("all"), new HTuple("all"), new HTuple("result_type"), new HTuple("all_param"), out parameter); HOperatorSet.GetMetrologyObjectResultContour(out LineDisp, handleID, new HTuple("all"), new HTuple("all"), new HTuple(1.5)); if (parameter.Length >= 4) { ResultLineStartRow = parameter[0]; ResultLineStartCol = parameter[1]; ResultLineEndRow = parameter[2]; ResultLineEndCol = parameter[3]; Point start = new Point() { Row = ResultLineStartRow, Col = ResultLineStartCol }; Point end = new Point() { Row = ResultLineEndRow, Col = ResultLineEndCol }; resultLine = new Line() { StartPoint = start, EndPoint = end }; } HOperatorSet.AngleLx(ResultLineStartRow, ResultLineStartCol, ResultLineEndRow, ResultLineEndCol, out _angle); if (softwareRunState == SoftwareRunState.Debug) { DispMainWindow(FormFindLine.Instance.myHwindow); FormFindLine.Instance.tbx_resultStartRow.Text = ResultLineStartRow.ToString(); FormFindLine.Instance.tbx_resultStartCol.Text = ResultLineEndCol.ToString(); FormFindLine.Instance.tbx_resultEndRow.Text = ResultLineEndRow.ToString(); FormFindLine.Instance.tbx_resultEndCol.Text = ResultLineEndCol.ToString(); } HOperatorSet.ClearMetrologyModel(handleID); // 参数传递 ParamsTrans(); sw.Stop(); runTime = $"运行时间: {sw.ElapsedMilliseconds} ms"; FormFindLine.Instance.SetToolStatus("工具运行成功!", ToolRunStatu.Succeed); } catch (Exception ex) { FormFindLine.Instance.SetToolStatus($"工具运行异常,异常原因: {ex}", ToolRunStatu.Tool_Run_Error); } finally { //homMat2DArrow.Dispose(); //arrow.Dispose(); //arrowTrans.Dispose(); } }