Record and write c# advanced script of visionPro

1. Script import namespace

come on!  You are the best

The page displayed after clicking is like this, then import what you want!

import

After clicking OK, don't forget to click the arrow (write script to tool)

to write

2. Start writing code

  • Import the tools you use so that you can Run() them later.
    insert image description here
CogPMAlignTool PM = mToolBlock.Tools["CogPMAlignTool1"] as CogPMAlignTool;
CogFixtureTool fix = mToolBlock.Tools["CogFixtureTool1"] as CogFixtureTool; 
CogFixtureTool fix1 = mToolBlock.Tools["CogFixtureTool2"] as CogFixtureTool;
CogIPOneImageTool ipo = mToolBlock.Tools["CogIPOneImageTool1"] as CogIPOneImageTool;
  • Import variables: import the variables you need, easy to use (variables can facilitate us to adjust the data in order to find the most suitable value range!)

insert image description here

bool bTrue = (bool) mToolBlock.Inputs["bTrue"].Value;
double dMean = (double) mToolBlock.Inputs["dMean"].Value;
double dVariance = (double) mToolBlock.Inputs["dVariance"].Value;  
  • Regarding the use of arrays to store tools, it is convenient for later cycle use
CogHistogramTool[] His = new CogHistogramTool[]{
    
    his1, his2, his3, his4};
CogBlobTool[] Blob = new CogBlobTool[]{
    
    blob1, blob2, blob3, blob4};
  • Reasonable use of return can optimize the running time
 if(PM.Results.Count != 8)
    {
    
    
      mToolBlock.Outputs["FlowResult"].Value = bTrue ? true : false;
      return false;
    }
对于当符合某一个条件就能判断结果时,使用return,可以直接不运行后面的代码,节省很多时间。
  • define global properties
 #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;

Here you can write some constants or variables to be used globally. The following is the collection that defines the labels used to save them. The code used will be posted below! (both are fine)

System.Collections.ArrayList LabelList = new System.Collections.ArrayList();
// CogGraphicCollection LabelList = new CogGraphicCollection();

Code snippet one explanation

 for(int i = 0; (i < 8 && i < (int) mToolBlock.Inputs["test"].Value); i++)
    {
    
    
      fix.RunParams.UnfixturedFromFixturedTransform = PM.Results[i].GetPose();
      fix.Run();
      his1.Run();
      his2.Run();
  1. Inputs["test"] here is the imported variable we mentioned above for easy debugging, because a loop is used here. If we need to debug, we can choose to input the value of test and stop at which loop to see where it is. Something went wrong.
  2. **.Run()** allows us to make a certain tool run.
  3. Another line of code is equivalent to linking PM.Results[0].GetPose() with fix.RunParams.UnfixturedFromFixturedTransform in the tool . However, there are 8 results of the template tool here, so use the code to realize the coordinate system position space (X, Y axis and angle space coordinate system) of the CogFixtureTool tool.
    tool

Code snippet 2 explanation

接上面的第一个for 循环
	  double dx = 0; 
      double dy = 0; 
      for(int j = 0 ; j < His.Length ; j++){
    
    
        CogGraphicLabel Mylabe = new CogGraphicLabel();  
        CogTransform2DLinear newTrans = PM.Results[i].GetPose();
        
        CogRectangleAffine rect = His[j].Region as CogRectangleAffine;
        rect1 = rect.MapLinear(newTrans, CogCopyShapeConstants.GeometryOnly); 
        //rect1 = rect.MapLinear((CogTransform2DLinear)fix1.OutputImage.PixelFromRootTransform, CogCopyShapeConstants.All);

        rect1.Color = CogColorConstants.Orange;
        LabelList.Add(rect1);
        
        newTrans.MapPoint(rect.CenterX, rect.CenterY, out dx, out dy);
  1. The for loop is also used here. Since the algorithm uses four CogHistogramTool tools, His[j] just uses an array to apply it to the loop.
  2. new CogGraphicLabel() , you can customize the image label and add it to the previous global variable LabelList --> LabelList.Add(rect1)
  3. newTrans is the 2D line conversion area; newTrans.MapPoint –> generally uses the beginning of the Map to copy or map the output. Here the coordinates of rect.CenterX are copied to dx .
  4. rect.MapLinear refers to copying the things in the newTrans area (angle, side length, center, etc.), and there are many modes!
 注:第一个参数 CogTransform2DLinear类型 要和工具所选空间名称对比一下,一不一致都无所谓但要注意转换成自己想要的效果。
 比如:
   不一致时,可以将第二个参数设置为 CogCopyShapeConstants.GeometryOnly(只复制几何形状,而不复制坐标)
   一致时,可以设置为 CogCopyShapeConstants.All(全部复制,包含坐标点,角度等)。

code hints

  1. His[j].Region refers to the region shape used by the CogHistogramTool tool
    insert image description here

Code snippet three explanation

接上面的第二个for循环,相信这段很多都能看懂,都是一些基本的判断语句。
 	    if(His[j].Result.Mean > dMean){
    
    
          Mylabe.SetXYText(dx, dy, "OK");
          Mylabe.Color = CogColorConstants.Green;
          LabelList.Add(Mylabe);
        }else{
    
      
          rect1.Color = CogColorConstants.Blue;
          fix1.InputImage = PM.InputImage;
          fix1.RunParams.UnfixturedFromFixturedTransform = PM.Results[i].GetPose();
          fix1.Run();

          Blob[j].InputImage = fix1.OutputImage;
          
          blobAffine = rect.MapLinear(newTrans, CogCopyShapeConstants.GeometryOnly); 
          blobAffine.Color = CogColorConstants.Blue;
          blobAffine.SetOriginCornerXCornerY(rect.CornerOriginX, rect.CornerOriginY, rect.CornerXX, rect.CornerXY, rect.CornerYX, rect.CornerYY);
          blobAffine.SideXLength *= 10;
        
          Blob[j].Region = blobAffine;
          Caliper[j].Region = blobAffine;
          Blob[j].Run();
          Caliper[j].Run();

		    if(Caliper[j].Results[0].Width > 50 && Caliper[j].Results[0].Width < 75){
    
    
              Mylabe.SetXYText(dx, dy, "OK");
              Mylabe.Color = CogColorConstants.Green;
              LabelList.Add(Mylabe);
            }else{
    
    
              mToolBlock.Outputs["FlowResult"].Value = bTrue ? true : false;
              Mylabe.SetXYText(dx, dy, "NG");
              Mylabe.Color = CogColorConstants.Red; 
              LabelList.Add(Mylabe);
            }
 		  Blob[j].InputImage = null;  //后面会注释掉
          Caliper[j].InputImage = null;  //后面会注释掉
          Blob[j].Run();   //后面会注释掉
          Caliper[j].Run();  //后面会注释掉
        }//大if   
      }//for循环
    }//大for循环
  1. fix1.InputImage = PM.InputImage and Blob[j].InputImage = fix1.OutputImage The reason for these paragraphs is to optimize the code and save running time, because this is an if...else statement. And under normal circumstances ( His[j].Result.Mean > dMean ) this condition is basically established, so most of the time you don’t need to use tools and don’t need to run processing, but the situation is that as long as you click the run of CogToolBlock , all The tools will run (after the verification conclusion below, all the tools that execute .Run() in the code will run) , and the tools will keep the input picture of the last run and the last input picture. It has the same meaning as
    the code fix1.RunParams.UnfixturedFromFixturedTransform = PM.Results[i].GetPose( ) , and you can roughly understand what I am talking about by combining the pictures below.

This is the normal state of the tool link

insert image description here

This is the tool link state achieved with code ( fix1.InputImage = PM.InputImage )

insert image description here

The reason why the Blob tool became popular here is that Blob[j].InputImage = null is executed after the loop, and the input image of the Blob tool is set to null . Of course, it is still not enough to simply use InputImage , because there is still the last InputImage , namely LastRun.InputImage , there will still be a picture here, which will cause the Blob tool to execute . There is still a difference between executing the Blob tool without a picture and with a picture . Although the difference is not big, the important thing is the code optimization idea, and you must always have a heart that wants to optimize.
So when setting the Blob to null ( Blob[j].InputImage = null ), you need to run Blob[j].Run() once , why Run() ? Because when Blob[j].InputImage = null , you run Blobtool will become popular (you can comment out Blob[j].Run(), then run CogToolBlock, then go into the Blob tool and run it manually, and then come out to see if the Blob tool has become popular), which is equivalent to When the Blob tool is not used, it becomes popular and discarded, and when it is used, run Blob[j].InputImage = fix1.OutputImage . (The underlined ones are redundant)
Note: Blob[j].Run() here is the last sentence instead of the previous one.

However, just when I thought I was thinking right, the execution of code verification was beyond my expectations. In fact, some of my conclusions were proved to be wrong. Blob 's LastRun.InputImage will not run by itself, only you in the code It will only be executed when the .Run() method is executed on the above or when the Blob[j].InputImage has a picture and is not null .

(This is the running time result of the Blob and Caliper tools when an NG product is executed first)

insert image description here

(This is the running time result of the Blob and Caliper tools when the NG product is executed immediately after the OK product is executed. The result is exactly the same as the above. What does it prove? It proves that the tool is not running, and even the same picture will have some time difference , because the algorithm cannot be stable enough to run two identical images with the same calculation time, if you don’t believe me, you can read the verification below!)

insert image description here

(Here is an NG wrong product, don’t ask me why. Because if my above conclusion is correct, if you take an OK product that does not use the else code, the blob tool cannot verify that the calculation time used by the same image algorithm is inconsistent , since it doesn't run the blob tool at all, i.e. blob.Run())

insert image description here
Verification steps: select an NG product image --> then run it in CogToolBlock

First run result:
insert image description here

The result of the second run:
insert image description here

Conclusion 1: The two results are different, which means that when the algorithm operates two identical graphs, the calculation time will also be different.

Conclusion 2: Blob 's LastRun.InputImage will not run by itself, it will only be executed when you execute the .Run() method on the code or when Blob[j].InputImage has an image and is not null . Therefore: it is unnecessary to comment out the last Blob[j].Run() and Caliper[j].Run() directly.

  1. The variable blobAffine , the variable blobAffine is defined to set the region for the Blob tool Blob[j].Region and Caliper[j].Region , in order not to let it run too far (because there may be some deviations in the template matching), give it Set the origin, side length, etc. Moreover, the origin attribute in the CogRectangleAffine class cannot be directly assigned. The method SetOriginCornerXCornerY()
    insert image description here is used to set the origin here, because when I started writing, I didn’t know much about the positioning space. . . However, since this method exists, it should have its meaning, and there is no actual situation where it can only be implemented by code.

  2. Although the above Blob[j].Run() is redundant, then Blob[j].InputImage = null is redundant. The verification is as follows:

(This is the running time of the OK product operation when an NG error product is executed first, and then an OK product is executed, and the code of Blob[j].InputImage = null should not be commented)

insert image description here

Then run the same OK product again and find that the running time of the Blob tool has not changed, but if you manually enter and run it, the time does change, which proves that the tool is not running

(When an NG product is executed first, but this time the code of Blob[j].InputImage = null is commented, the running time of the NG product Blob operation is as shown in the figure below)

insert image description here

Then switch and run an OK product, and find that the running time of the OK product Blob calculation is consistent with the above, and has not changed

Conclusion: The execution time of the code with or without Blob[j].InputImage = null is the same. It proves that in the tool, no matter whether you have xxx.InputImage or not, as long as you do not execute xxx.Run() in the code or you manually click the tool xxx Neither will run by itself, affecting code execution time.

3. Introduction of CogFixtureTool

CogFixtureTool is a positioning tool to redefine one's own coordinate system, redefine the origin, etc. tool, the interface is as follows:

insert image description here

First of all, you need to understand that the original origin of the initial image is the upper left corner, which is also the initial coordinate system space, as shown in the following figure:

insert image description here

Generally speaking, the CogFixtureTool tool is used when using the template matching tool CogPMAlignTool . And generally, the center origin of the CogPMAlignTool result is assigned to CogFixtureTool as the origin of the new coordinate system. The figure below is not linked by lines because it is implemented using the code fix.RunParams.UnfixturedFromFixturedTransform = PM.Results[i].GetPose() .

insert image description here

After linking, the center origin of CogPMAlignTool is the origin of the new coordinate system in the figure below.

insert image description here

At this time, the tools used later can use the new space coordinate system, for example, "@\Fixture" is used here

insert image description here
insert image description here

Maybe many people are still very vague, what's the use of this? Draw a schematic diagram below to demonstrate.

  1. The input image is like this, the purpose is to find whether the product has more or less stains.
  2. The first step is to find the product through template matching, and mark the center origin as somewhere in the template, so that the center origin of the product matched by the template is fixed at the upper right of the product, and the x and y directions are relative to the found template direction relatively unchanged.

insert image description here

  1. Use the CogFixtureTool tool to establish a new coordinate space with template matching center origin, x, y, and rotation angle.

insert image description here

  1. Use the CogHistogramTool tool to detect stains, and set the space name to be the space name of the new coordinate space. For example, the origin position of the CogHistogramTool tool shown in the figure is x=-113.69; y=70; the rotation is 90 degrees relative to the origin of the new coordinate space.
    insert image description here

insert image description here

  1. In this way, no matter how the product moves, the origin of the CogHistogramTool tool relative to the origin of the new coordinate space is x=-113.69; y=70; the rotation is 90 degrees. Follow the origin of the new coordinate space (that is, the center origin of the template matching result).
    insert image description here

Pay attention to the difference between UnfixturedSpace and fixturedSpace .

insert image description here

When using fixturedSpace , the outputImage tool of the linked CogFixtureTool uses the same space name "input image space" and "@\Fixture" .

insert image description here
When using UnfixturedSpace , the outputImage tool of the linked CogFixtureTool uses space names "input image space" and "@\Fixture" inconsistently. The **"input image space"** here is the space of the incoming CogFixtureTool image.

Guess you like

Origin blog.csdn.net/GDIUOOO/article/details/130210957