[VisionPro] C#脚本介绍

一、主要功能

        和ToolBlocks工具搭配运行,得到更确定的输出结果

二、设置

[VisionPro] C#脚本介绍_第1张图片

 可以改动的地方由三个

Property:设置这个类的属性

GroupRun方法:这里是脚本运行的主要内容

ModifyLastRunRecord方法:用于脚本运行完毕最后的结果的处理。

三、脚本代码参考

#region namespace imports
using System;
using System.Collections;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Cognex.VisionPro;
using Cognex.VisionPro.ToolBlock;
using Cognex.VisionPro3D;
using Cognex.VisionPro.PMAlign;
using Cognex.VisionPro.CalibFix;
using Cognex.VisionPro.ImageProcessing;
using Cognex.VisionPro.Caliper;
using Cognex.VisionPro.Blob;
#endregion

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
  CogCircle blobRoi = new CogCircle();
  CogGraphicCollection blobs = new CogGraphicCollection();
  #endregion

  /// 
  /// Called when the parent tool is run.
  /// Add code here to customize or replace the normal run behavior.
  /// 
  /// Sets the Message in the tool's RunStatus.
  /// Sets the Result in the tool's RunStatus
  /// True if the tool should run normally,
  ///          False if GroupRun customizes run behavior
  public override bool GroupRun(ref string message, ref CogToolResultConstants result)
  {
    // To let the execution stop in this script when a debugger is attached, uncomment the following lines.
    // #if DEBUG
    // if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break();
    // #endif


    // Run each tool using the RunTool function
    CogPMAlignTool PMATool = mToolBlock.Tools["CogPMAlignTool1"] as CogPMAlignTool;
    CogFixtureTool fixture = mToolBlock.Tools["CogFixtureTool1"] as CogFixtureTool;
    CogAffineTransformTool transform = mToolBlock.Tools["CogAffineTransformTool1"] as CogAffineTransformTool;
    CogCopyRegionTool copyRegion1 = mToolBlock.Tools["CogCopyRegionTool1"] as CogCopyRegionTool;
    CogCopyRegionTool copyRegion2 = mToolBlock.Tools["CogCopyRegionTool2"] as CogCopyRegionTool;
    CogFindCircleTool findCircle = mToolBlock.Tools["CogFindCircleTool1"] as  CogFindCircleTool;
    PMATool.Run();
    fixture.Run();
    transform.Run();
    copyRegion1.Run();
    copyRegion2.Run();
    findCircle.Run();
   
    blobRoi = findCircle.Results.GetCircle();
    blobRoi.Radius = blobRoi.Radius - 50.0;
    
    CogBlobTool blobTool = mToolBlock.Tools["CogBlobTool1"] as CogBlobTool;
    blobTool.Region = blobRoi;
    blobTool.Run();
    blobs.Clear();
    var blobRes = blobTool.Results.GetBlobs();
    for (int i = 0; i < blobRes.Count; ++i)
    {
      CogPolygon poly = new CogPolygon();
      poly = blobRes[i].GetBoundary();
      poly.Color = CogColorConstants.Orange;
      blobs.Add(poly.Copy(CogCopyShapeConstants.All));
    }
    mToolBlock.Outputs["blobCounts"].Value = blobRes.Count;
    
    CogImage8Grey image = (CogImage8Grey) mToolBlock.Inputs["OutputImage"].Value;
    Bitmap bitmap = image.ToBitmap();
    CogTransform2DLinear affine = PMATool.Results[0].GetPose();
    for(int i = 0;i < blobRes.Count;i++)
    {
      CogPolygon poly = new CogPolygon();      
      poly = blobRes[i].GetBoundary();
      int pointsNumber = poly.NumVertices;
      double xMin = 5120;
      double yMin = 5120;
      double xMax = 0;
      double yMax = 0;
      for(int ii = 0;ii < pointsNumber;ii++)
      {
        double x =  poly.GetVertexX(ii);
        double y =  poly.GetVertexY(ii);
        double xMap = 0;
        double yMap = 0;
        affine.MapPoint(x,y,out xMap,out yMap);
        xMin = xMap < xMin ? xMap : xMin;
        yMin = yMap < yMin ? yMap : yMin;
        xMax = xMap > xMax ? xMap : xMax;
        yMax = yMap > yMax ? yMap : yMax;
      }
      double temp = xMin;
      while(temp <= xMax)
      {
        bitmap.SetPixel((int)temp,(int)yMin-3, Color.Red);  
        bitmap.SetPixel((int)temp,(int)yMax+3, Color.Red);
        temp=temp+1;
      }
      temp = yMin;
      while(temp < yMax)
      {
        bitmap.SetPixel((int)xMin-3,(int)temp, Color.Red);  
        bitmap.SetPixel((int)xMax+3,(int)temp, Color.Red);
        temp = temp + 1;
      }
    }
    mToolBlock.Outputs["OutputImages"].Value = new CogImage24PlanarColor(bitmap);
    
    return false;
  }

  #region When the Current Run Record is Created
  /// 
  /// Called when the current record may have changed and is being reconstructed
  /// 
  /// 
  /// The new currentRecord is available to be initialized or customized.
  public override void ModifyCurrentRunRecord(Cognex.VisionPro.ICogRecord currentRecord)
  {
  }
  #endregion

  #region When the Last Run Record is Created
  /// 
  /// Called when the last run record may have changed and is being reconstructed
  /// 
  /// 
  /// The new last run record is available to be initialized or customized.
  public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
  {
    blobRoi.Color = CogColorConstants.Green;
    mToolBlock.AddGraphicToRunRecord(blobRoi, lastRecord, "CogAffineTransformTool1.OutputImage", "");
    foreach(ICogGraphic p in blobs)
      mToolBlock.AddGraphicToRunRecord(p, lastRecord, "CogAffineTransformTool1.OutputImage", "");
  }
  #endregion

  #region When the Script is Initialized
  /// 
  /// Perform any initialization required by your script here
  /// 
  /// The host tool
  public override void Initialize(Cognex.VisionPro.ToolGroup.CogToolGroup host)
  {
    // DO NOT REMOVE - Call the base class implementation first - DO NOT REMOVE
    base.Initialize(host);


    // Store a local copy of the script host
    this.mToolBlock = ((Cognex.VisionPro.ToolBlock.CogToolBlock)(host));
  }
  #endregion

}

首先定义此 ToolBlock参数,然后在GroupRun方法中首先遍历(或者定义每个ToolBlock工具变量名)运行每个工具。之后就是自己自定义的操作。

你可能感兴趣的:(VisionPro,c#,开发语言,computer,vision,图像处理,计算机视觉)