行远-自迩

导航

视觉定位引导算法相关总结

   1  #region ===========================两个相机=============================================
   2  public static double[] TwoPointLine(double Point1X, double Point1Y, double Point2X, double Point2Y)
   3  {
   4      double[] TwoPointPars = new double[3];
   5      try
   6      {
   7          if (Point1Y == Point2Y && Point1X == Point2X)
   8          {
   9 
  10              System.Windows.Forms.MessageBox.Show("两相同点,不能确定同一直线,请查实!"
  11                  , "错误"
  12                  , System.Windows.Forms.MessageBoxButtons.OK
  13                  , System.Windows.Forms.MessageBoxIcon.Exclamation
  14                  , System.Windows.Forms.MessageBoxDefaultButton.Button1);
  15              return null;
  16          }
  17          else if (Point1X == Point2X)
  18          {
  19              TwoPointPars[1] = 0;
  20              TwoPointPars[0] = 1;
  21              TwoPointPars[2] = -Point1X;
  22          }
  23          else
  24          {
  25              TwoPointPars[1] = 1;
  26              TwoPointPars[0] = -(Point2Y - Point1Y) / (Point2X - Point1X);
  27              TwoPointPars[2] = Point1X * (Point2Y - Point1Y) / (Point2X - Point1X) - Point1Y;
  28          }
  29 
  30      }
  31 
  32      catch (Exception ex)
  33      {
  34 
  35 
  36      }
  37      return TwoPointPars;
  38  }
  39  public static double[] PiontGradientLine(double PointX, double PointY, double[] TwoPointPars)
  40  {
  41      double[] PiontGradientPars = new double[3];
  42      if (TwoPointPars[0] == 0)
  43      {
  44          PiontGradientPars[1] = 0;
  45          PiontGradientPars[0] = 1;
  46          PiontGradientPars[2] = -PointX;
  47      }
  48      if (TwoPointPars[1] == 0)
  49      {
  50          PiontGradientPars[1] = 1;
  51          PiontGradientPars[0] = 0;
  52          PiontGradientPars[2] = -PointY;
  53      }
  54      if (TwoPointPars[0] != 0 && TwoPointPars[1] != 0)
  55      {
  56          PiontGradientPars[0] = TwoPointPars[1];
  57          PiontGradientPars[1] = -TwoPointPars[0];
  58          PiontGradientPars[2] = TwoPointPars[0] * PointY - TwoPointPars[1] * PointX;
  59      }
  60      return PiontGradientPars;
  61  }
  62  public static double[] CrossPointCordinate(double[] TwoPointPars, double[] PiontGradientPars)
  63  {
  64      double[] CrossCordinate = new double[2];
  65      if (TwoPointPars[1] == 0 && PiontGradientPars[1] == 0)
  66      {
  67          System.Windows.Forms.MessageBox.Show("两直线平行,无交点!");
  68          return null;
  69      }
  70      else if (TwoPointPars[1] == 0)
  71      {
  72          CrossCordinate[0] = -TwoPointPars[2];
  73          CrossCordinate[1] = -(PiontGradientPars[0] * CrossCordinate[0] + PiontGradientPars[2]) / PiontGradientPars[1];
  74      }
  75      else if (PiontGradientPars[1] == 0)
  76      {
  77          CrossCordinate[0] = -PiontGradientPars[2];
  78          CrossCordinate[1] = -(TwoPointPars[0] * CrossCordinate[0] + TwoPointPars[2]) / TwoPointPars[1];
  79      }
  80      else
  81      {
  82          if ((TwoPointPars[0] / TwoPointPars[1]) == (PiontGradientPars[0] / PiontGradientPars[1]))
  83          {
  84              System.Windows.Forms.MessageBox.Show("两直线平行,无交点!");
  85              return null;
  86          }
  87          else
  88          {
  89              CrossCordinate[0] = (TwoPointPars[2] - PiontGradientPars[2]) / (PiontGradientPars[0] - TwoPointPars[0]);
  90              CrossCordinate[1] = -(TwoPointPars[0] * CrossCordinate[0]) - TwoPointPars[2];
  91          }
  92      }
  93 
  94      return CrossCordinate;
  95  }
  96  public static double[] CrossPointCordinateDummy(double[] TwoPointPars, double[] PiontGradientPars)
  97  {
  98      double[] CrossCordinate = new double[2];
  99      if (TwoPointPars[1] == 0 && PiontGradientPars[1] == 0)
 100      {
 101          System.Windows.Forms.MessageBox.Show("两直线平行,无交点!");
 102          return null;
 103      }
 104      else if (TwoPointPars[1] == 0)
 105      {
 106          CrossCordinate[0] = -TwoPointPars[2];
 107          CrossCordinate[1] = -(PiontGradientPars[0] * CrossCordinate[0] + PiontGradientPars[2]) / PiontGradientPars[1];
 108      }
 109      else if (PiontGradientPars[1] == 0)
 110      {
 111          CrossCordinate[0] = -PiontGradientPars[2];
 112          CrossCordinate[1] = -(TwoPointPars[0] * CrossCordinate[0] + TwoPointPars[2]) / TwoPointPars[1];
 113      }
 114      else
 115      {
 116          if ((TwoPointPars[0] / TwoPointPars[1]) == (PiontGradientPars[0] / PiontGradientPars[1]))
 117          {
 118              System.Windows.Forms.MessageBox.Show("两直线平行,无交点!");
 119              return null;
 120          }
 121          else
 122          {
 123              CrossCordinate[0] = (TwoPointPars[1] * PiontGradientPars[2] - PiontGradientPars[1] * TwoPointPars[2]) / (TwoPointPars[0] * PiontGradientPars[1] - PiontGradientPars[0] * TwoPointPars[1]);
 124              CrossCordinate[1] = (TwoPointPars[0] * PiontGradientPars[2] - PiontGradientPars[0] * TwoPointPars[2]) / (PiontGradientPars[0] * TwoPointPars[1] - TwoPointPars[0] * PiontGradientPars[1]);
 125          }
 126      }
 127 
 128      return CrossCordinate;
 129  }
 130  public static double[] Calk(double theta, double[] PiontGradientPars)
 131  {
 132      double[] k = new double[2];
 133      if (PiontGradientPars[0] == 0)
 134      {
 135          k[0] = Math.Tan(theta);
 136          k[1] = Math.Tan(-theta);
 137      }
 138      else if (PiontGradientPars[1] == 0)
 139      {
 140          k[0] = Math.Tan((Math.PI / 2) - theta);
 141          k[1] = Math.Tan(-((Math.PI / 2) - theta));
 142      }
 143      else
 144      {
 145 
 146          if (Math.Round(1 + (Math.Tan(theta) * PiontGradientPars[0] / PiontGradientPars[1]), 5) != 0)
 147          {
 148              k[0] = (Math.Tan(theta) - (PiontGradientPars[0] / PiontGradientPars[1])) / (1 + (Math.Tan(theta) * PiontGradientPars[0] / PiontGradientPars[1]));
 149 
 150          }
 151          else
 152          {
 153              k[0] = double.PositiveInfinity;
 154          }
 155          if (Math.Round(1 - (Math.Tan(theta) * PiontGradientPars[0] / PiontGradientPars[1]), 5) != 0)
 156          {
 157              k[1] = (-(PiontGradientPars[0] / PiontGradientPars[1]) - Math.Tan(theta)) / (1 - (Math.Tan(theta) * PiontGradientPars[0] / PiontGradientPars[1]));
 158          }
 159          else
 160          {
 161              k[1] = double.PositiveInfinity;
 162          }
 163      }
 164      return k;
 165  }
 166  public static double[] Pointk(double PointX, double PointY, double k)
 167  {
 168      double[] PiontkPars = new double[3];
 169      if (double.IsInfinity(k))
 170      {
 171          PiontkPars[0] = 1;
 172          PiontkPars[1] = 0;
 173          PiontkPars[2] = PointX;
 174      }
 175      else
 176      {
 177          double b = PointY - k * PointX;
 178          PiontkPars[0] = k;
 179          PiontkPars[1] = -1;
 180          PiontkPars[2] = b;
 181      }
 182      return PiontkPars;
 183  }
 184  public static double[] ConvertXY(double X10, double Y10, double X210, double Y210)
 185  {
 186      double[] CnvtXY = new double[2];
 187 
 188      CnvtXY[0] = X10 - X210;
 189      CnvtXY[1] = Y10 - Y210;
 190      return CnvtXY;
 191 
 192  }
 193  public static double[] Calcxcy(double x10, double y10, double x11, double y11, double theta)
 194  {
 195      double[] CrossPoints = new double[2];
 196      double[] TwoPointPars = TwoPointLine(x10, y10, x11, y11);
 197 
 198      double[] PiontGradientPars = PiontGradientLine((x10 + x11) / 2, (y10 + y11) / 2, TwoPointPars);
 199      double[] kk = Calk((Math.Abs(theta / 2)) * Math.PI / 180, PiontGradientPars);
 200      double[] PiontkPars1 = Pointk(x10, y10, kk[0]);
 201      double[] PiontkPars2 = Pointk(x10, y10, kk[1]);
 202      double[] CrossCordinate1 = CrossPointCordinateDummy(PiontkPars1, PiontGradientPars);
 203      double[] CrossCordinate2 = CrossPointCordinateDummy(PiontkPars2, PiontGradientPars);
 204      if (CrossCordinate1[1] < CrossCordinate2[1])
 205      {
 206          CrossPoints = CrossCordinate1;
 207      }
 208      if (CrossCordinate2[1] < CrossCordinate1[1])
 209      {
 210          CrossPoints = CrossCordinate2;
 211      }
 212      return CrossPoints;
 213  }
 214  public static double Caltheta(double x11, double y11, double CnX, double CnY)
 215  {
 216      double theta = 0;
 217      double thetaX = CnY;
 218      double thetaY = CnX;
 219      theta = computeAngle(x11, y11, x11 + CnX, y11 + CnY);
 220      return theta;
 221  }
 222  private static double computeAngle(double x1, double y1, double x2, double y2)
 223  {
 224      double thetaX = x2 - x1;
 225      double thetaY = y2 - y1;
 226      double angleOfLine = ((Math.Atan2((y2 - y1), (x2 - x1))) * 180) / Math.PI;
 227      return angleOfLine;
 228  }
 229  public static bool oppositeSigns(int x, int y)
 230  {
 231      return ((x ^ y) < 0);
 232  }
 233 
 234  #endregion
 235 
 236  #region ===========================单个相机============================================= 
 237 
 238  #region==================旋转中心方法====================
 239  //1. 两个点加一个旋转角度
 240  // (X0,Y0)为旋转中心,(X1,Y1) (X2,Y2)为工件在视野内旋转角度a(机器人旋转(使用前先转化为弧度))的前后座标
 241  //(Xt,Yt)为两点延长线
 242 
 243  public static double[] CalcCircleCenter(double x1, double y1, double x2, double y2, double a)
 244  {
 245      double[] dCircleCenter = new double[3];
 246      //两点之间距离
 247      double distance = Math.Sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
 248      double radius = distance / 2 * (Math.Sin(a * Math.PI / (2 * 180)));
 249      double k = radius / distance;
 250      double xt = (1 - k) * x1 + k * x2;
 251      double yt = (1 - k) * y1 + k * y2;
 252 
 253      dCircleCenter[0] = Math.Cos(Math.PI / 2 - a / 2) * (xt - x1) - Math.Sin(Math.PI / 2 - a / 2) * (yt - y1) + x1;
 254      dCircleCenter[1] = Math.Cos(Math.PI / 2 - a / 2) * (yt - y1) + Math.Sin(Math.PI / 2 - a / 2) * (xt - x1) + y1;
 255      dCircleCenter[2] = radius;
 256      return dCircleCenter;
 257  }
 258 
 259 
 260  /// <summary>
 261  /// 
 262  /// </summary>
 263  /// <param name="x">旋转中心的位置</param>
 264  /// <param name="y">旋转中心的位置</param>
 265  /// <param name="a">旋转角度</param>
 266  /// <param name="x1">旋转角度a后定位到产品位置</param>
 267  /// <param name="y1">旋转角度a后定位到产品位置</param>
 268  /// <param name="x0">基准位置</param>
 269  /// <param name="y0">基准位置</param>
 270  /// <returns></returns>
 271  public static double[] CalcCdxy(double x, double y, double a, double x1, double y1, double x0, double y0)
 272  {
 273      double[] cdxytheta = new double[3];
 274      //旋转后新位置
 275      double x_new = Math.Cos(a * Math.PI / 180) * (x1 - x) - Math.Sin(a * Math.PI) * (y1 - y) + x;
 276      double y_new = Math.Cos(a * Math.PI / 180) * (y1 - y) + Math.Sin(a * Math.PI) * (x1 - x) + y;
 277      cdxytheta[2] = 0;
 278      cdxytheta[0] = x_new - x0;
 279      cdxytheta[1] = y_new - y0;
 280      return cdxytheta;
 281  }
 282  #endregion
 283 
 284 
 285 
 286         //绘制直线
 287         public void FindLineCaliperTool(HTuple hv_ExpDefaultWinHandle, HObject ho_Image, HTuple hv_LineParams, ref HTuple hv_Parameter)
 288         {
 289             HObject ho_Contours, ho_Cross, ho_Contour;
 290             HTuple hv_Width = new HTuple(), hv_Height = new HTuple();
 291             HTuple hv_MetrologyHandle = new HTuple(), hv_Index = new HTuple();
 292             HTuple hv_Row = new HTuple(), hv_Column = new HTuple();
 293             // Initialize local and output iconic variables 
 294             HOperatorSet.GenEmptyObj(out ho_Contours);
 295             HOperatorSet.GenEmptyObj(out ho_Cross);
 296             HOperatorSet.GenEmptyObj(out ho_Contour);
 297             hv_Parameter = new HTuple();
 298             try
 299             {
 300                 //******************************************卡尺工具**************************************
 301                 hv_Width.Dispose(); hv_Height.Dispose();
 302                 HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
 303                 hv_MetrologyHandle.Dispose();
 304                 HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle);
 305                 HOperatorSet.SetMetrologyModelImageSize(hv_MetrologyHandle, hv_Width, hv_Height);
 306                 hv_Index.Dispose();
 307                 HOperatorSet.AddMetrologyObjectGeneric(hv_MetrologyHandle, "line", hv_LineParams,
 308                     40, 3, 1, 30, new HTuple(), new HTuple(), out hv_Index);
 309 
 310                 //执行测量,获取边缘点集
 311                 HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "yellow");
 312                 HOperatorSet.ApplyMetrologyModel(ho_Image, hv_MetrologyHandle);
 313                 ho_Contours.Dispose(); hv_Row.Dispose(); hv_Column.Dispose();
 314                 HOperatorSet.GetMetrologyObjectMeasures(out ho_Contours, hv_MetrologyHandle,
 315                     "all", "all", out hv_Row, out hv_Column);
 316                 HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "red");
 317                 ho_Cross.Dispose();
 318                 HOperatorSet.GenCrossContourXld(out ho_Cross, hv_Row, hv_Column, 6, 0.785398);
 319 
 320                 //获取最终测量数据和轮廓线
 321                 HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "green");
 322                 HOperatorSet.SetLineWidth(hv_ExpDefaultWinHandle, 1);
 323                 hv_Parameter.Dispose();
 324                 HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "result_type",
 325                     "all_param", out hv_Parameter);
 326                 ho_Contour.Dispose();
 327                 HOperatorSet.GetMetrologyObjectResultContour(out ho_Contour, hv_MetrologyHandle,
 328                     "all", "all", 1.5);
 329                 //释放测量句柄
 330                 HOperatorSet.ClearMetrologyModel(hv_MetrologyHandle);
 331 
 332                 hv_Width.Dispose();
 333                 hv_Height.Dispose();
 334                 hv_MetrologyHandle.Dispose();
 335                 hv_Index.Dispose();
 336                 hv_Row.Dispose();
 337                 hv_Column.Dispose();
 338 
 339                 return;
 340 
 341                 hv_Width.Dispose();
 342                 hv_Height.Dispose();
 343                 hv_MetrologyHandle.Dispose();
 344                 hv_Index.Dispose();
 345                 hv_Row.Dispose();
 346                 hv_Column.Dispose();
 347 
 348                 return;
 349             }
 350             catch (HalconException HDevExpDefaultException)
 351             {
 352 
 353                 hv_Width.Dispose();
 354                 hv_Height.Dispose();
 355                 hv_MetrologyHandle.Dispose();
 356                 hv_Index.Dispose();
 357                 hv_Row.Dispose();
 358                 hv_Column.Dispose();
 359 
 360                 throw HDevExpDefaultException;
 361             }
 362         }
 363 
 364         //绘制一条直线直线
 365         public HObject DrawLine(HObject InputImage, HTuple HalconWindowHandle, ref HTuple FitLine)
 366         {
 367             HTuple htWidth, htHeight;
 368             HOperatorSet.GetImageSize(InputImage, out htWidth, out htHeight);
 369             HTuple r1, c1, r2, c2, angle;
 370             HObject hoLineRegion = new HObject();
 371             HOperatorSet.SetColor(HalconWindowHandle, "blue");
 372             HOperatorSet.GenEmptyObj(out hoLineRegion);
 373             DispMessage(HalconWindowHandle, "绘制直线", "image", 50, 50, "blue", "false");
 374             HOperatorSet.DrawLine(HalconWindowHandle, out r1, out c1, out r2, out c2);
 375             HOperatorSet.AngleLx(r1, c1, r2, c2, out angle);
 376             HTuple line = new HTuple(r1, c1, r2, c2);
 377             FindLineCaliperTool(HalconWindowHandle,InputImage,  line, ref FitLine);
 378             return hoLineRegion;
 379         }
 380 
 381         // //绘制两条直线直线
 382         public HObject[] DrawLines(HObject InputImage, HTuple HalconWindowHandle, ref HTuple FitLine1, ref HTuple FitLine2)
 383         {
 384             HObject[] LineRegions = new HObject[2];
 385             HOperatorSet.GenEmptyRegion(out LineRegions[0]);
 386             HOperatorSet.GenEmptyRegion(out LineRegions[1]);
 387             LineRegions[0] = DrawLine(InputImage, HalconWindowHandle, ref FitLine1);
 388             LineRegions[1] = DrawLine(InputImage, HalconWindowHandle, ref FitLine2);
 389             return LineRegions;
 390         }
 391 
 392         //绘制矩形框(Rectangle1)
 393         public void DrawRectangle1(HObject InputImage, HTuple HalconWindowHandle, out HObject Rectangle1)
 394         {
 395             HTuple r1, c1, r2, c2;
 396             HTuple height, width;
 397             HOperatorSet.GenEmptyObj(out Rectangle1);
 398             HOperatorSet.GetImageSize(InputImage, out width, out height);
 399             HOperatorSet.SetColor(HalconWindowHandle, "blue");
 400             HOperatorSet.DrawRectangle1Mod(HalconWindowHandle, width / 2 - 100, height / 2 - 100, width/2 + 100, height/2 + 100, out r1, out c1, out r2, out c2);
 401             HOperatorSet.GenRectangle1(out Rectangle1, r1, c1, r2, c2);
 402         }
 403         //绘制矩形框(Rectangle2)
 404         public void DrawRectangle2(HObject InputImage, HTuple HalconWindowHandle, out HObject Rectangle2)
 405         {
 406             HTuple r1, c1, ahi, length1, length2;
 407             HTuple height, width;
 408             HOperatorSet.GenEmptyObj(out Rectangle2);
 409             HOperatorSet.GetImageSize(InputImage, out width, out height);
 410             HOperatorSet.SetColor(HalconWindowHandle, "blue");
 411             HOperatorSet.DrawRectangle2Mod(HalconWindowHandle, 1 * height / 2, 2 * width / 5, 0, 1 * width / 10  , 1 * height / 10, out r1, out c1, out ahi, out length1, out length2);
 412             HOperatorSet.GenRectangle2(out Rectangle2, r1, c1, ahi, length1, length2);
 413         }
 414 
 415         //求两条直线
 416         //返回Line1直线得角度:参数Angle以弧度为单位返回角度-pi到pi
 417         public void TwoLineIntersection(HTuple Line1, HTuple Line2, ref HTuple IntersectionPoint)
 418         {
 419             //直线1到直线2夹角,与直线2到直线1夹角为相反数
 420             HTuple x, y, a, isOverLapping;
 421             //拟合出来直线后求出两个直线交点,以及一条直线的交点
 422             HOperatorSet.IntersectionLines(Line1[0], Line1[1], Line1[2], Line1[3],
 423                  Line2[0], Line2[1], Line2[2], Line2[3], out x, out y, out isOverLapping);
 424             HOperatorSet.AngleLx(Line1[0], Line1[1], Line1[2], Line1[3], out a);
 425             IntersectionPoint = new HTuple(x, y, a);
 426         }
 427         public void AffineLinePoints(HTuple Line, HTuple r1, HTuple c1, HTuple a1, HTuple r2, HTuple c2, HTuple a2, ref HTuple AffineLine)
 428         {
 429             HTuple htHomat = new HTuple();
 430             HTuple x1, y1, x2, y2;
 431             HOperatorSet.VectorAngleToRigid(r1, c1, a1, r2, c2, a2, out htHomat);
 432             HOperatorSet.AffineTransPoint2d(htHomat, Line[0], Line[1], out x1, out y1);
 433             HOperatorSet.AffineTransPoint2d(htHomat, Line[2], Line[3], out x2, out y2);
 434             AffineLine = new HTuple(x1, y1, x2, y2);
 435         }
 436 
 437         //Aniso模板      
 438         public void CreateAnisoModel(HTuple HalconWindowHandle, HObject InputImage, HObject hoTrainRegion, out HTuple hv_ModelID)
 439         {
 440             HObject ho_ModelImage = new HObject();
 441             HOperatorSet.GenEmptyObj(out ho_ModelImage);
 442             ho_ModelImage.Dispose();
 443             hv_ModelID = new HTuple();
 444             hv_ModelID.Dispose();
 445             string Message = "";
 446             try
 447             {
 448                 HOperatorSet.SetDraw(HalconWindowHandle, "margin");
 449                 if (InputImage == null)
 450                 {
 451                     Message = "图片不能为空,请输入图片";
 452                     DispMessage(HalconWindowHandle, Message, "window", 12, 12, "red", "false");
 453                     return;
 454                 }
 455 
 456                 if (hoTrainRegion == null)
 457                 {
 458                     Message = "训练区域不能为空,请输入图片";
 459                     DispMessage(HalconWindowHandle, Message, "window", 12, 12, "red", "false");
 460                     return;
 461                 }
 462                 HOperatorSet.GenEmptyObj(out ho_ModelImage);
 463                 ho_ModelImage.Dispose();
 464                 HOperatorSet.ReduceDomain(InputImage, hoTrainRegion, out ho_ModelImage);
 465                 HOperatorSet.CreateAnisoShapeModel(ho_ModelImage, "auto", -0.39, 0.79, "auto",
 466                    0.9, 1.1, "auto", 0.9, 1.1, "auto", "auto", "use_polarity", "auto", "auto",
 467                    out hv_ModelID);
 468                 if (hv_ModelID!= null)
 469                     DispMessage(HalconWindowHandle, "模板创建成功", "window", 12, 12, "green", "false");
 470                 else
 471                     DispMessage(HalconWindowHandle, Message, "window", 12, 12, "red", "false");
 472                 ho_ModelImage.Dispose();
 473             }
 474             catch (HalconException hal)
 475             {
 476                 Message = hal.Message;
 477                 DispMessage(HalconWindowHandle, Message, "window", 12, 12, "red", "false");
 478                 ho_ModelImage.Dispose();
 479             }
 480         }
 481         //匹配Aniso
 482         public void FindAnisoModel(HTuple HalconWindowHandle, HObject InputImage, HObject hoSearchRegion, HTuple htModelID,HTuple htMarkNums, ref HTuple hv_Angle, ref HTuple hv_Row, ref HTuple hv_Column, ref HTuple hv_Score)
 483         {
 484             string Message = "";
 485             if (InputImage == null)
 486             {
 487                 Message = "图片不能为空,请输入图片";
 488                 DispMessage(HalconWindowHandle, Message, "window", 12, 12, "red", "false");
 489                 return;
 490             }
 491             HObject hoReduceImage = new HObject();
 492             HTuple hv_ScaleR1 = new HTuple();
 493             HTuple hv_ScaleC1 = new HTuple();
 494             HOperatorSet.ReduceDomain(InputImage, hoSearchRegion, out hoReduceImage);
 495             HOperatorSet.FindAnisoShapeModel(hoReduceImage, htModelID, -0.39, 0.79, 0.9, 1.1,
 496         0.9, 1.1, 0.5, htMarkNums, 0.5, "least_squares", numLevels: 0, 0.9, out hv_Row, out hv_Column,
 497         out hv_Angle, out hv_ScaleR1, out hv_ScaleC1, out hv_Score);
 498             HObject ho_ModelContours = new HObject();
 499             HOperatorSet.GenEmptyObj(out ho_ModelContours);
 500             HOperatorSet.GetShapeModelContours(out ho_ModelContours, htModelID, 1);       //获取形状模型的轮廓
 501                DispShapeMatchResults(HalconWindowHandle, htModelID, hv_Score, hv_Row, hv_Column, hv_Angle, hv_ScaleR1, hv_ScaleC1, 0);
 502            // ResultShow(HalconWindowHandle,hv_Row, hv_Column, hv_Angle, hv_Score,  hoSearchRegion, ho_ModelContours);
 503         }
 504         //NCC模板
 505         public void CreateNccModel(HTuple HalconWindowHandle, HObject InputImage, HObject hoTrainRegion, out HTuple hv_ModelID)
 506         {
 507             HObject hoReduceImage = new HObject();
 508             hv_ModelID = "";
 509             string Message = "";
 510             try
 511             {
 512                 if (InputImage == null)
 513                 {
 514                     Message = "图片不能为空,请输入图片";
 515                     DispMessage(HalconWindowHandle, Message, "window", 12, 12, "red", "false");
 516                     return;
 517                 }
 518 
 519                 if (hoTrainRegion == null)
 520                 {
 521                     Message = "训练区域不能为空,请输入图片";
 522                     DispMessage(HalconWindowHandle, Message, "window", 12, 12, "red", "false");
 523                     return;
 524                 }
 525                 HOperatorSet.ReduceDomain(InputImage, hoTrainRegion, out hoReduceImage);
 526                 HOperatorSet.CreateNccModel(hoReduceImage, "auto", 0, 3.14 / 2, "auto", "use_polarity", out hv_ModelID);
 527                 if (hv_ModelID != null)
 528                     DispMessage(HalconWindowHandle, "模板创建成功", "window", 12, 12, "green", "false");
 529                 else
 530                     DispMessage(HalconWindowHandle, Message, "window", 12, 12, "red", "false");
 531 
 532             }
 533             catch (HalconException hal)
 534             {
 535                 Message = hal.Message;
 536                 DispMessage(HalconWindowHandle, Message, "window", 12, 12, "red", "false");
 537             }
 538         }
 539 
 540         //标定
 541         //N Point标定
 542         public void CalibPoints(HTuple HalconWindowHandle, HObject InputImage, HTuple hv_PixRows, HTuple hv_PixColumns, HTuple hv_MachineRows, HTuple hv_MachineColumn, ref HTuple hv_Hommat2D)
 543         {
 544             try
 545             {
 546                 if (hv_PixRows.Length > 0)
 547                 {
 548                     HOperatorSet.VectorToHomMat2d(hv_PixRows, hv_PixColumns, hv_MachineRows, hv_MachineColumn, out hv_Hommat2D);
 549                     HOperatorSet.SetColor(HalconWindowHandle, "green");
 550                     HOperatorSet.DispObj(InputImage, HalconWindowHandle);
 551                     for (int i = 0; i < hv_PixRows.Length; i++)
 552                     {
 553                         HOperatorSet.DispCross(HalconWindowHandle, hv_PixRows[i], hv_PixColumns[i], 50, 0);
 554                     }
 555                 }
 556 
 557             }
 558             catch (HalconException hal)
 559             {
 560             }
 561         }
 562 
 563         //保存标定文件
 564         public void SaveCalibFile(string sProductName, string HomatName, HTuple htHomat2D, out string msg)
 565         {
 566             //获取文件
 567             string filepath = AppDomain.CurrentDomain.BaseDirectory + @"\NinePoints\" + sProductName + @"\";
 568             // string HomatName = "CalibNPoint" + CameraNO.ToString() + ".mat";
 569             //保存Homat2D
 570             HTuple hv_SerializedItemHandle = null;
 571             HTuple hv_FileHandle = null;
 572             msg = string.Empty;
 573             if (htHomat2D != null)
 574             {
 575                 try
 576                 {
 577                     AddFileManger(HomatName, filepath);
 578                     HOperatorSet.SerializeHomMat2d(htHomat2D, out hv_SerializedItemHandle);
 579                     HOperatorSet.OpenFile(filepath + HomatName, "output_binary", out hv_FileHandle);
 580                     HOperatorSet.FwriteSerializedItem(hv_FileHandle, hv_SerializedItemHandle);
 581                     HOperatorSet.CloseFile(hv_FileHandle);
 582                     msg = "Save CalibNPointTool Completed";
 583                 }
 584                 catch (HalconException hex)
 585                 {
 586                     msg = "Save CalibNPointTool Error: " + hex.Message;
 587                 }
 588             }
 589         }
 590 
 591         public void ResultShow(HTuple hv_ExpDefaultWinHandle, HTuple hv_Row, HTuple hv_Column, HTuple hv_Angle, HTuple hv_Score, HObject RectangleSearchArea, HObject ho_ModelContours)
 592         {
 593             HTuple hv_MovementOfObject;
 594             HOperatorSet.VectorAngleToRigid(0, 0, 0, hv_Row, hv_Column, hv_Angle, out hv_MovementOfObject);
 595             HObject ho_ModelAtNewPosition;
 596             HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_ModelAtNewPosition, hv_MovementOfObject);
 597             //HOperatorSet.DispObj(ho_Image, ht_hWindowHandle_Tempalte);
 598             HOperatorSet.SetDraw(hv_ExpDefaultWinHandle, "margin");      //只显示边框
 599             HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "blue");       //设置画笔的颜色及线宽
 600             HOperatorSet.SetLineWidth(hv_ExpDefaultWinHandle, 2);
 601             HOperatorSet.DispRegion(RectangleSearchArea, hv_ExpDefaultWinHandle);
 602             HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "green");
 603             HOperatorSet.SetLineWidth(hv_ExpDefaultWinHandle, 1);        //设置线宽及颜色
 604             HOperatorSet.DispObj(ho_ModelAtNewPosition, hv_ExpDefaultWinHandle);
 605             ho_ModelAtNewPosition.Dispose();
 606             hv_MovementOfObject.UnpinTuple();
 607 
 608             HOperatorSet.DispRectangle2(hv_ExpDefaultWinHandle, hv_Row, hv_Column, hv_Angle, 50, 50);    //绘制图形框
 609             HOperatorSet.DispCross(hv_ExpDefaultWinHandle, hv_Row, hv_Column, 7, hv_Angle);  //绘制十字线
 610                                                                               //设置显示文字的样式
 611     
 612             //显示的对象只有边缘线,
 613             HOperatorSet.SetDraw(hv_ExpDefaultWinHandle, "margin");
 614             //线宽用Line Width 指定 s
 615             HOperatorSet.SetLineWidth(hv_ExpDefaultWinHandle, 2);
 616             HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "blue");
 617 
 618             if (hv_Score.D > 0.6)
 619             {
 620 
 621                 HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "green");
 622                 HOperatorSet.DispObj(ho_ModelAtNewPosition, hv_ExpDefaultWinHandle);
 623                 DispMessage(hv_ExpDefaultWinHandle, ((((("Result: " + hv_Row) + new HTuple(", ")) + hv_Column) + new HTuple(",")) + "\r\n Score:") + (hv_Score * 100).TupleString(".2f") + "%", "window", 1, 1, "green", "false");
 624             }
 625             else
 626             {
 627                 HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "red");
 628                 HOperatorSet.DispObj(ho_ModelAtNewPosition, hv_ExpDefaultWinHandle);
 629                 DispMessage(hv_ExpDefaultWinHandle, ((((("Result: " + hv_Row) + new HTuple(", ")) + hv_Column) + new HTuple(",")) + "\r\n Score:") + (hv_Score * 100).TupleString(".2f") + "%", "window", 1, 1, "red", "false");
 630             }
 631         }
 632 
 633         //特征显示
 634         private void DispShapeMatchResults(HTuple hv_ExpDefaultWinHandle, HTuple hv_ModelID, HTuple hv_Score, HTuple hv_Row, HTuple hv_Column, HTuple hv_Angle, HTuple hv_ScaleR, HTuple hv_ScaleC, HTuple hv_Model)
 635         {
 636             HObject ho_ModelContours = null, ho_ContoursAffinTrans = null;
 637             HTuple hv_NumMatches = null, hv_Index = new HTuple();
 638             HTuple hv_Match = new HTuple(), hv_HomMat2DIdentity = new HTuple();
 639             HTuple hv_HomMat2DScale = new HTuple(), hv_HomMat2DRotate = new HTuple();
 640             HTuple hv_HomMat2DTranslate = new HTuple();
 641             HTuple hv_Model_COPY_INP_TMP = hv_Model.Clone();
 642             HTuple hv_ScaleC_COPY_INP_TMP = hv_ScaleC.Clone();
 643             HTuple hv_ScaleR_COPY_INP_TMP = hv_ScaleR.Clone();
 644             HTuple hv_ContoursCenter_Row = null;
 645             HTuple hv_ContoursCenter_Col = null;
 646             HTuple hv_ContoursCenter_Area = null;
 647             HTuple hv_ContoursCenter_Pointer = null;
 648             HOperatorSet.GenEmptyObj(out ho_ModelContours);
 649             HOperatorSet.GenEmptyObj(out ho_ContoursAffinTrans);
 650             hv_NumMatches = new HTuple(hv_Row.TupleLength());
 651             if ((int)(new HTuple(hv_NumMatches.TupleGreater(0))) != 0)
 652             {
 653                 if ((int)(new HTuple((new HTuple(hv_ScaleR_COPY_INP_TMP.TupleLength())).TupleEqual(
 654                     1))) != 0)
 655                 {
 656                     HOperatorSet.TupleGenConst(hv_NumMatches, hv_ScaleR_COPY_INP_TMP, out hv_ScaleR_COPY_INP_TMP);
 657                 }
 658                 if ((int)(new HTuple((new HTuple(hv_ScaleC_COPY_INP_TMP.TupleLength())).TupleEqual(
 659                     1))) != 0)
 660                 {
 661                     HOperatorSet.TupleGenConst(hv_NumMatches, hv_ScaleC_COPY_INP_TMP, out hv_ScaleC_COPY_INP_TMP);
 662                 }
 663                 if ((int)(new HTuple((new HTuple(hv_Model_COPY_INP_TMP.TupleLength())).TupleEqual(
 664                     0))) != 0)
 665                 {
 666                     HOperatorSet.TupleGenConst(hv_NumMatches, 0, out hv_Model_COPY_INP_TMP);
 667                 }
 668                 else if ((int)(new HTuple((new HTuple(hv_Model_COPY_INP_TMP.TupleLength()
 669                     )).TupleEqual(1))) != 0)
 670                 {
 671                     HOperatorSet.TupleGenConst(hv_NumMatches, hv_Model_COPY_INP_TMP, out hv_Model_COPY_INP_TMP);
 672                 }
 673                 for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_ModelID.TupleLength())) - 1); hv_Index = (int)hv_Index + 1)
 674                 {
 675                     ho_ModelContours.Dispose();
 676                     HOperatorSet.GetShapeModelContours(out ho_ModelContours, hv_ModelID.TupleSelect(
 677                         hv_Index), 1);
 678                     HTuple end_val18 = hv_NumMatches - 1;
 679                     HTuple step_val18 = 1;
 680                     for (hv_Match = 0; hv_Match.Continue(end_val18, step_val18); hv_Match = hv_Match.TupleAdd(step_val18))
 681                     {
 682                         if ((int)(new HTuple(hv_Index.TupleEqual(hv_Model_COPY_INP_TMP.TupleSelect(
 683                             hv_Match)))) != 0)
 684                         {
 685                             HOperatorSet.HomMat2dIdentity(out hv_HomMat2DIdentity);
 686                             HOperatorSet.HomMat2dScale(hv_HomMat2DIdentity, hv_ScaleR_COPY_INP_TMP.TupleSelect(
 687                                 hv_Match), hv_ScaleC_COPY_INP_TMP.TupleSelect(hv_Match), 0, 0, out hv_HomMat2DScale);
 688                             HOperatorSet.HomMat2dRotate(hv_HomMat2DScale, hv_Angle.TupleSelect(hv_Match),
 689                                 0, 0, out hv_HomMat2DRotate);
 690                             HOperatorSet.HomMat2dTranslate(hv_HomMat2DRotate, hv_Row.TupleSelect(
 691                                 hv_Match), hv_Column.TupleSelect(hv_Match), out hv_HomMat2DTranslate);
 692                             ho_ContoursAffinTrans.Dispose();
 693                             HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_ContoursAffinTrans,
 694                                 hv_HomMat2DTranslate);
 695                             HOperatorSet.AreaCenterXld(ho_ContoursAffinTrans, out hv_ContoursCenter_Area, out hv_ContoursCenter_Row, out hv_ContoursCenter_Col, out hv_ContoursCenter_Pointer);
 696                             if (hv_Score.D > 0.6)
 697                             {
 698                                 
 699                                 HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "green");
 700                                 HOperatorSet.DispObj(ho_ContoursAffinTrans, hv_ExpDefaultWinHandle);
 701                                 DispMessage(hv_ExpDefaultWinHandle, ((((("Result: " + hv_Row) + new HTuple(", ")) + hv_Column) + new HTuple(",")) + "\r\n Score:") + (hv_Score * 100).TupleString(".2f") + "%", "window", 1, 1, "green", "false");
 702                             }
 703                             else
 704                             {
 705                                 HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "red");
 706                                 HOperatorSet.DispObj(ho_ContoursAffinTrans, hv_ExpDefaultWinHandle);
 707                                 DispMessage(hv_ExpDefaultWinHandle, ((((("Result: " + hv_Row) + new HTuple(", ")) + hv_Column) + new HTuple(",")) + "\r\n Score:") + (hv_Score * 100).TupleString(".2f") + "%", "window", 1, 1, "red", "false");
 708                             }
 709                         }
 710                     }
 711                 }
 712             }
 713         }
 714 
 715 
 716         //消息显示
 717         public void DispMessage(HTuple hv_ExpDefaultWinHandle, HTuple hv_String, HTuple hv_CoordSystem, HTuple hv_Row, HTuple hv_Column, HTuple hv_Color, HTuple hv_Box)
 718         {
 719             HTuple hv_M = null, hv_N = null, hv_Red = null;
 720             HTuple hv_Green = null, hv_Blue = null, hv_RowI1Part = null;
 721             HTuple hv_ColumnI1Part = null, hv_RowI2Part = null, hv_ColumnI2Part = null;
 722             HTuple hv_RowIWin = null, hv_ColumnIWin = null, hv_WidthWin = new HTuple();
 723             HTuple hv_HeightWin = null, hv_I = null, hv_RowI = new HTuple();
 724             HTuple hv_ColumnI = new HTuple(), hv_StringI = new HTuple();
 725             HTuple hv_MaxAscent = new HTuple(), hv_MaxDescent = new HTuple();
 726             HTuple hv_MaxWidth = new HTuple(), hv_MaxHeight = new HTuple();
 727             HTuple hv_R1 = new HTuple(), hv_C1 = new HTuple(), hv_FactorRowI = new HTuple();
 728             HTuple hv_FactorColumnI = new HTuple(), hv_UseShadow = new HTuple();
 729             HTuple hv_ShadowColor = new HTuple(), hv_Exception = new HTuple();
 730             HTuple hv_Width = new HTuple(), hv_Index = new HTuple();
 731             HTuple hv_Ascent = new HTuple(), hv_Descent = new HTuple();
 732             HTuple hv_W = new HTuple(), hv_H = new HTuple(), hv_FrameHeight = new HTuple();
 733             HTuple hv_FrameWidth = new HTuple(), hv_R2 = new HTuple();
 734             HTuple hv_C2 = new HTuple(), hv_DrawMode = new HTuple();
 735             HTuple hv_CurrentColor = new HTuple();
 736             HTuple hv_Box_COPY_INP_TMP = hv_Box.Clone();
 737             HTuple hv_Color_COPY_INP_TMP = hv_Color.Clone();
 738             HTuple hv_Column_COPY_INP_TMP = hv_Column.Clone();
 739             HTuple hv_Row_COPY_INP_TMP = hv_Row.Clone();
 740             HTuple hv_String_COPY_INP_TMP = hv_String.Clone();
 741             if ((int)(new HTuple(hv_Color_COPY_INP_TMP.TupleEqual(new HTuple()))) != 0)
 742             {
 743                 hv_Color_COPY_INP_TMP = "";
 744             }
 745             if ((int)(new HTuple(hv_Box_COPY_INP_TMP.TupleEqual(new HTuple()))) != 0)
 746             {
 747                 hv_Box_COPY_INP_TMP = "false";
 748             }
 749             //
 750             //
 751             //Check conditions
 752             //
 753             hv_M = (new HTuple(hv_Row_COPY_INP_TMP.TupleLength())) * (new HTuple(hv_Column_COPY_INP_TMP.TupleLength()
 754                 ));
 755             hv_N = new HTuple(hv_Row_COPY_INP_TMP.TupleLength());
 756             if ((int)((new HTuple(hv_M.TupleEqual(0))).TupleOr(new HTuple(hv_String_COPY_INP_TMP.TupleEqual(
 757                 new HTuple())))) != 0)
 758             {
 759 
 760                 return;
 761             }
 762             if ((int)(new HTuple(hv_M.TupleNotEqual(1))) != 0)
 763             {
 764                 //Multiple positions
 765                 //
 766                 //Expand single parameters
 767                 if ((int)(new HTuple((new HTuple(hv_Row_COPY_INP_TMP.TupleLength())).TupleEqual(
 768                     1))) != 0)
 769                 {
 770                     hv_N = new HTuple(hv_Column_COPY_INP_TMP.TupleLength());
 771                     HOperatorSet.TupleGenConst(hv_N, hv_Row_COPY_INP_TMP, out hv_Row_COPY_INP_TMP);
 772                 }
 773                 else if ((int)(new HTuple((new HTuple(hv_Column_COPY_INP_TMP.TupleLength()
 774                     )).TupleEqual(1))) != 0)
 775                 {
 776                     HOperatorSet.TupleGenConst(hv_N, hv_Column_COPY_INP_TMP, out hv_Column_COPY_INP_TMP);
 777                 }
 778                 else if ((int)(new HTuple((new HTuple(hv_Column_COPY_INP_TMP.TupleLength()
 779                     )).TupleNotEqual(new HTuple(hv_Row_COPY_INP_TMP.TupleLength())))) != 0)
 780                 {
 781                     throw new HalconException("Number of elements in Row and Column does not match.");
 782                 }
 783                 if ((int)(new HTuple((new HTuple(hv_String_COPY_INP_TMP.TupleLength())).TupleEqual(
 784                     1))) != 0)
 785                 {
 786                     HOperatorSet.TupleGenConst(hv_N, hv_String_COPY_INP_TMP, out hv_String_COPY_INP_TMP);
 787                 }
 788                 else if ((int)(new HTuple((new HTuple(hv_String_COPY_INP_TMP.TupleLength()
 789                     )).TupleNotEqual(hv_N))) != 0)
 790                 {
 791                     throw new HalconException("Number of elements in Strings does not match number of positions.");
 792                 }
 793                 //
 794             }
 795             //
 796             //Prepare window
 797             HOperatorSet.GetRgb(hv_ExpDefaultWinHandle, out hv_Red, out hv_Green, out hv_Blue);
 798             HOperatorSet.GetPart(hv_ExpDefaultWinHandle, out hv_RowI1Part, out hv_ColumnI1Part,
 799                 out hv_RowI2Part, out hv_ColumnI2Part);
 800             HOperatorSet.GetWindowExtents(hv_ExpDefaultWinHandle, out hv_RowIWin, out hv_ColumnIWin,
 801                 out hv_WidthWin, out hv_HeightWin);
 802             HOperatorSet.SetPart(hv_ExpDefaultWinHandle, 0, 0, hv_HeightWin - 1, hv_WidthWin - 1);
 803             //
 804             //Loop over all positions
 805             HTuple end_val89 = hv_N - 1;
 806             HTuple step_val89 = 1;
 807             for (hv_I = 0; hv_I.Continue(end_val89, step_val89); hv_I = hv_I.TupleAdd(step_val89))
 808             {
 809                 hv_RowI = hv_Row_COPY_INP_TMP.TupleSelect(hv_I);
 810                 hv_ColumnI = hv_Column_COPY_INP_TMP.TupleSelect(hv_I);
 811                 //Allow multiple strings for a single position.
 812                 if ((int)(new HTuple(hv_N.TupleEqual(1))) != 0)
 813                 {
 814                     hv_StringI = hv_String_COPY_INP_TMP.Clone();
 815                 }
 816                 else
 817                 {
 818                     //In case of multiple positions, only single strings
 819                     //are allowed per position.
 820                     //For line breaks, use \n in this case.
 821                     hv_StringI = hv_String_COPY_INP_TMP.TupleSelect(hv_I);
 822                 }
 823                 //Default settings
 824                 //-1 is mapped to 12.
 825                 if ((int)(new HTuple(hv_RowI.TupleEqual(-1))) != 0)
 826                 {
 827                     hv_RowI = 12;
 828                 }
 829                 if ((int)(new HTuple(hv_ColumnI.TupleEqual(-1))) != 0)
 830                 {
 831                     hv_ColumnI = 12;
 832                 }
 833                 //
 834                 //Split string into one string per line.
 835                 hv_StringI = ((("" + hv_StringI) + "")).TupleSplit("\n");
 836                 //
 837                 //Estimate extentions of text depending on font size.
 838                 HOperatorSet.GetFontExtents(hv_ExpDefaultWinHandle, out hv_MaxAscent, out hv_MaxDescent,
 839                     out hv_MaxWidth, out hv_MaxHeight);
 840                 if ((int)(new HTuple(hv_CoordSystem.TupleEqual("window"))) != 0)
 841                 {
 842                     hv_R1 = hv_RowI.Clone();
 843                     hv_C1 = hv_ColumnI.Clone();
 844                 }
 845                 else
 846                 {
 847                     //Transform image to window coordinates.
 848                     hv_FactorRowI = (1.0 * hv_HeightWin) / ((hv_RowI2Part - hv_RowI1Part) + 1);
 849                     hv_FactorColumnI = (1.0 * hv_WidthWin) / ((hv_ColumnI2Part - hv_ColumnI1Part) + 1);
 850                     hv_R1 = (((hv_RowI - hv_RowI1Part) + 0.5) * hv_FactorRowI) - 0.5;
 851                     hv_C1 = (((hv_ColumnI - hv_ColumnI1Part) + 0.5) * hv_FactorColumnI) - 0.5;
 852                 }
 853                 //
 854                 //Display text box depending on text size.
 855                 hv_UseShadow = 1;
 856                 hv_ShadowColor = "gray";
 857                 if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(0))).TupleEqual("true"))) != 0)
 858                 {
 859                     if (hv_Box_COPY_INP_TMP == null)
 860                         hv_Box_COPY_INP_TMP = new HTuple();
 861                     hv_Box_COPY_INP_TMP[0] = "#fce9d4";
 862                     hv_ShadowColor = "#f28d26";
 863                 }
 864                 if ((int)(new HTuple((new HTuple(hv_Box_COPY_INP_TMP.TupleLength())).TupleGreater(
 865                     1))) != 0)
 866                 {
 867                     if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(1))).TupleEqual("true"))) != 0)
 868                     {
 869                         //Use default ShadowColor set above
 870                     }
 871                     else if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(1))).TupleEqual(
 872                         "false"))) != 0)
 873                     {
 874                         hv_UseShadow = 0;
 875                     }
 876                     else
 877                     {
 878                         hv_ShadowColor = hv_Box_COPY_INP_TMP.TupleSelect(1);
 879                         //Valid color?
 880                         try
 881                         {
 882                             HOperatorSet.SetColor(hv_ExpDefaultWinHandle, hv_Box_COPY_INP_TMP.TupleSelect(
 883                                 1));
 884                         }
 885                         // catch (Exception) 
 886                         catch (HalconException HDevExpDefaultException1)
 887                         {
 888                             HDevExpDefaultException1.ToHTuple(out hv_Exception);
 889                             hv_Exception = new HTuple("Wrong value of control parameter Box[1] (must be a 'true', 'false', or a valid color string)");
 890                             throw new HalconException(hv_Exception);
 891                         }
 892                     }
 893                 }
 894                 if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(0))).TupleNotEqual("false"))) != 0)
 895                 {
 896                     //Valid color?
 897                     try
 898                     {
 899                         HOperatorSet.SetColor(hv_ExpDefaultWinHandle, hv_Box_COPY_INP_TMP.TupleSelect(
 900                             0));
 901                     }
 902                     // catch (Exception) 
 903                     catch (HalconException HDevExpDefaultException1)
 904                     {
 905                         HDevExpDefaultException1.ToHTuple(out hv_Exception);
 906                         hv_Exception = new HTuple("Wrong value of control parameter Box[0] (must be a 'true', 'false', or a valid color string)");
 907                         throw new HalconException(hv_Exception);
 908                     }
 909                     //Calculate box extents
 910                     hv_StringI = (" " + hv_StringI) + " ";
 911                     hv_Width = new HTuple();
 912                     for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_StringI.TupleLength()
 913                         )) - 1); hv_Index = (int)hv_Index + 1)
 914                     {
 915                         HOperatorSet.GetStringExtents(hv_ExpDefaultWinHandle, hv_StringI.TupleSelect(
 916                             hv_Index), out hv_Ascent, out hv_Descent, out hv_W, out hv_H);
 917                         hv_Width = hv_Width.TupleConcat(hv_W);
 918                     }
 919                     hv_FrameHeight = hv_MaxHeight * (new HTuple(hv_StringI.TupleLength()));
 920                     hv_FrameWidth = (((new HTuple(0)).TupleConcat(hv_Width))).TupleMax();
 921                     hv_R2 = hv_R1 + hv_FrameHeight;
 922                     hv_C2 = hv_C1 + hv_FrameWidth;
 923                     //Display rectangles
 924                     HOperatorSet.GetDraw(hv_ExpDefaultWinHandle, out hv_DrawMode);
 925                     HOperatorSet.SetDraw(hv_ExpDefaultWinHandle, "fill");
 926                     //Set shadow color
 927                     HOperatorSet.SetColor(hv_ExpDefaultWinHandle, hv_ShadowColor);
 928                     if ((int)(hv_UseShadow) != 0)
 929                     {
 930                         HOperatorSet.DispRectangle1(hv_ExpDefaultWinHandle, hv_R1 + 1, hv_C1 + 1, hv_R2 + 1,
 931                             hv_C2 + 1);
 932                     }
 933                     //Set box color
 934                     HOperatorSet.SetColor(hv_ExpDefaultWinHandle, hv_Box_COPY_INP_TMP.TupleSelect(
 935                         0));
 936                     HOperatorSet.DispRectangle1(hv_ExpDefaultWinHandle, hv_R1, hv_C1, hv_R2,
 937                         hv_C2);
 938                     HOperatorSet.SetDraw(hv_ExpDefaultWinHandle, hv_DrawMode);
 939                 }
 940                 //Write text.
 941                 for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_StringI.TupleLength())) - 1); hv_Index = (int)hv_Index + 1)
 942                 {
 943                     //Set color
 944                     if ((int)(new HTuple(hv_N.TupleEqual(1))) != 0)
 945                     {
 946                         //Wiht a single text position, each text line
 947                         //may get a different color.
 948                         hv_CurrentColor = hv_Color_COPY_INP_TMP.TupleSelect(hv_Index % (new HTuple(hv_Color_COPY_INP_TMP.TupleLength()
 949                             )));
 950                     }
 951                     else
 952                     {
 953                         //With multiple text positions, each position
 954                         //gets a single color for all text lines.
 955                         hv_CurrentColor = hv_Color_COPY_INP_TMP.TupleSelect(hv_I % (new HTuple(hv_Color_COPY_INP_TMP.TupleLength()
 956                             )));
 957                     }
 958                     if ((int)((new HTuple(hv_CurrentColor.TupleNotEqual(""))).TupleAnd(new HTuple(hv_CurrentColor.TupleNotEqual(
 959                         "auto")))) != 0)
 960                     {
 961                         //Valid color?
 962                         try
 963                         {
 964                             HOperatorSet.SetColor(hv_ExpDefaultWinHandle, hv_CurrentColor);
 965                         }
 966                         // catch (Exception) 
 967                         catch (HalconException HDevExpDefaultException1)
 968                         {
 969                             HDevExpDefaultException1.ToHTuple(out hv_Exception);
 970                             hv_Exception = ((("Wrong value of control parameter Color[" + (hv_Index % (new HTuple(hv_Color_COPY_INP_TMP.TupleLength()
 971                                 )))) + "] == '") + hv_CurrentColor) + "' (must be a valid color string)";
 972                             throw new HalconException(hv_Exception);
 973                         }
 974                     }
 975                     else
 976                     {
 977                         HOperatorSet.SetRgb(hv_ExpDefaultWinHandle, hv_Red, hv_Green, hv_Blue);
 978                     }
 979                     //Finally display text
 980                     hv_RowI = hv_R1 + (hv_MaxHeight * hv_Index);
 981                     HOperatorSet.SetTposition(hv_ExpDefaultWinHandle, hv_RowI, hv_C1);
 982                     HOperatorSet.WriteString(hv_ExpDefaultWinHandle, hv_StringI.TupleSelect(hv_Index));
 983                 }
 984             }
 985             //Reset changed window settings
 986             HOperatorSet.SetRgb(hv_ExpDefaultWinHandle, hv_Red, hv_Green, hv_Blue);
 987             HOperatorSet.SetPart(hv_ExpDefaultWinHandle, hv_RowI1Part, hv_ColumnI1Part, hv_RowI2Part,
 988                 hv_ColumnI2Part);
 989 
 990             return;
 991         }
 992 
 993         public  void AddFileManger(string fileName, string filePath)
 994         {
 995             if (!Directory.Exists(filePath))
 996             {
 997                 Directory.CreateDirectory(filePath);
 998             }
 999             if (!File.Exists(filePath + fileName))
1000             {
1001                 File.Create(filePath + fileName);
1002             }
1003         }
1004 
1005         public void CopyFile(string sourceFile, string disFile, string fileName)
1006         {
1007             if (!Directory.Exists(disFile))
1008             {
1009                 Directory.CreateDirectory(disFile);
1010             }
1011             File.Copy(sourceFile + fileName, disFile + fileName, true);
1012         }
1013 
1014         public void DelDirectory(string path)
1015         {
1016             FileAttributes attr = File.GetAttributes(path);
1017             if (attr == FileAttributes.Directory)
1018             {
1019                 if (Directory.Exists(path))
1020                 {
1021                     Directory.Delete(path, true);
1022                 }
1023             }
1024         }
1025 
1026         public void MoveFile(string sourceFile, string disFile)
1027         {
1028             Directory.Move(sourceFile, disFile);
1029         }
1030 
1031 
1032         public double[] CalMdxy(double x0, double y0, double x1, double y1)
1033         {
1034             double[] Mdxy = new double[2];
1035             Mdxy[0] = x1 - x0;
1036             Mdxy[1] = y1 - y0;
1037             return Mdxy;
1038         }
1039 
1040         //传进来的参数是角度
1041         public double[] CalCdxyθ(double stdx, double stdy, double Mdx, double Mdy, double angle)
1042         {
1043             angle = (angle * Math.PI) / 180;
1044             double[] xyθ = new double[3];
1045             xyθ[0] = (Math.Cos(angle) - 1) * stdx - Math.Sin(angle) * stdy + Mdx;
1046             xyθ[1] = (Math.Cos(angle) - 1) * stdy + Math.Sin(angle) * stdx + Mdy;
1047             xyθ[2] = angle * 180 / Math.PI;
1048             return xyθ;
1049         }
1050 
1051         //传进来的参数是角度
1052         public double[] CalStdxy(double Mdx, double Mdy, double con)
1053         {
1054             con = (con * Math.PI) / 180;
1055             double[] stdxy = new double[2];
1056             stdxy[0] = (-0.5) * (Mdx * (Math.Cos(con) - 1) + Mdy * (Math.Sin(con))) / (1 - Math.Cos(con));
1057             stdxy[1] = (0.5) * (Mdx * (Math.Sin(con)) - Mdy * (Math.Cos(con) - 1)) / (1 - Math.Cos(con));
1058             return stdxy;
1059         }
1060 
1061         public void SortHTuple(HTuple SortSelect, HTuple hv_Angle, HTuple hv_Row, HTuple hv_Column, out HTuple Angle, out HTuple Row, out HTuple Column)
1062         {
1063             Angle = new HTuple();
1064             Row = new HTuple();
1065             Column = new HTuple();
1066 
1067             HTuple midRow = new HTuple();
1068             HTuple midColumn = new HTuple();
1069             HTuple midAngle = new HTuple();
1070             HTuple MidIndices = new HTuple();
1071             if (SortSelect != "R")
1072             {
1073                 HOperatorSet.TupleSortIndex(hv_Row, out MidIndices);
1074                 for (int i = 0; i < hv_Row.Length; i++)
1075                 {
1076                     HTuple Index = MidIndices[i];
1077                     Row[i] = hv_Row[Index];
1078                     Column[i] = hv_Column[Index];
1079                     Angle[i] = hv_Angle[Index];
1080                 }
1081             }
1082             if (SortSelect != "C")
1083             {
1084                 HOperatorSet.TupleSortIndex(hv_Column, out MidIndices);
1085                 for (int i = 0; i < hv_Column.Length; i++)
1086                 {
1087                     HTuple Index = MidIndices[i];
1088                     Row[i] = hv_Row[Index];
1089                     Column[i] = hv_Column[Index];
1090                     Angle[i] = hv_Angle[Index];
1091                 }
1092             }
1093         }
1094   

 

posted on 2024-05-15 10:03  行远-自迩  阅读(15)  评论(0编辑  收藏  举报