# Calculate Cumulative Blob Count Using a Script

You've acquired, then cropped and processed your image with VisionPro tools in order to detect blobs (defects) on the Jell-O box labels.

Now, utilize C# to add a counter that tallies all blobs as you run your application repeatedly and until you choose to reset the counter.

Before beginning this module, ensure the following are true:

- You have verified your Task window appears similar to what is depicted in the last step of the topic Process Image for Defects with CogBlobTool.
- You have acquired an image and run your application at least a few times. If an Select an Image Source and Acquire Your Image, select the Task window and Run All tasks several times to accumulate data.

## Code Execution and Script Blocks

The predefined C# code that is included with the Single Camera Project Type contains a Script Block named CalculateResult, which consists of one line of C# code:

(return $TotalInspections % 2 == 0;)

This code returns a boolean value of true when TotalInspections is divisible by 2. In other words, true is returned each second execution.

Note that Result links to the connector (pin) protruding from the right-side of CalculateResult in the figure below. Result links to the boolean output (either True or False) produced by the block by CalculateResult. Since execution in the Cognex Designer Task window is left to right by default, the right-sided connector symbolizes an output from the CalculateResult block.

## Creating and Validating a New Script Block: CalculateCumulativeBlobs

You next create a script block similar to CalculateResult, which calculates the cumulative defects (blobs) detected by this application as it runs multiple times.

1. Before creating a script block that calculates the cumulative number of blobs over multiple executions of your application, you need to create a tag that points to the link Process Image for Defects with CogBlobTool so you can the pass the number of blobs counted to your script block after each singular execution of your application.
   1. Select the right connector protruding from the right-side of CogBlobInspect.
   2. Right-click and select Assign to New Tag.
   3. In the Create New Tag dialog box, create a name for the tag that describes the value it represents. In this example, enter the tag name Blobs_Counted.
   4. Click Accept.
   5. Hover your mouse over the right connector and observe the variable value (Results_GetBlobs_Count) associated with the tag name you created Blobs_Counted, shown in the box next to the connector. Note how the link is assigned to a type 32-bit Integer. As also shown, the current integer value of Blobs_Counted is 61. Since the application you are running is selecting and processing images at random, your results will vary when you perform this tutorial. In other words, Results_GetBlobs_Count may contain another value.

2. In the Explorer pane, double-click System > Tag Manager to open the Tag Manager.
3. Using the Tag Manager, add a tag that will represent the cumulative defect (blob) count value as your application runs multiple times.

1. Double-click in the first empty Name cell in the table in the Tag Manager tab. A red exclamation point appears to the left of the cell, prompting your input

2. Type the following tag name: Blob_Count_Cumulative in the empty cell. This tag will represent the cumulative blob count to your Cognex Designer application.
3. In the Data Type column of the same row, double-click Boolean. Using the drop-down box, assign Integer as the Data Type.
4. Ensure that 0 is the Default Value of Blob_Count_Cumulative, to initialize the cumulative blob count.

5. Select anywhere in the Tag Manager grid to complete the tag addition. The Tag Manager list should now look similar to the following, with the tags you've recently added highlighted in yellow.

4. Select the Task window.
5. Create a new script block that will calculate the cumulative number of defects that your application finds as i runs multiple times. This number is represented by the tag Blob_Count_Cumulative that you defined.

1. In the Toolbox pane, select Scripts > Script Block.
2. Drag-and-drop a Scriptblock into the Task window. Place it directly above the existing script block, CalculateResult. Since the two blocks are parallel to each other in the sequence grid of the Task window, they execute simultaneously when you run your Task.
3. Rename the new script block to describe its function.

1. Select Scriptblock and right-click.
2. Click Rename.
3. Type CalculateCumulativeBlobs.
4. Click anywhere in the Task window to complete the renaming operation.
5. Verify that your Task window looks like the following graphic.

6. Add C# code to the CalculateCumulativeBlobs script to tally the number of defects (blobs) your application finds as it runs multiple times.

1. Select the CalculateCumulativeBlobs script block.
2. Right-click and select Edit. The Script Definition Editor opens.
3. In the Script Definition Editor, click the green plus sign to add arguments.

4. When defining script arguments, consider the inputs and outputs of the CalculateCumulativeBlobs script.

Since the purpose of this script block is only to maintain a running tally of blobs for later use, the script requires no output to be passed to another task block for processing. Instead, you can reference that value from your application using the tag Blob_Count_Cumulative. Therefore, the return type for the CalculateCumulativeBlobs script block signature is void.

On the other hand, you are passing an integer (Results_GetBlobs_Count) to the CalculateCumulativeBlobs script. Therefore, you must define Results_GetBlobs_Count as input in the script's method signature as follows:

public void Execute(int CogBlobInspect_Results_GetBlobs_Count)

1. Select Integer from the drop-down box that currently contains String, by default.
2. In the field immediately to the left, select the text argument, and over-type with the following string: CogBlobInspect_Results_GetBlobs_Count. Notice that you prepend the name of the block (CogBlobInspect) that generates Results_GetBlobs_Count to the argument name giving the complete argument string CogBlobInspect_Results_GetBlobs_Count.
3. As there is no return type (void), leave the Return Type option unchecked.
4. Click Accept. A script opens in a window titled CalculateCumlativeBlobs.Execute. Note that the method signature you created with Script Definition Editor is in the script's heading:

   public void Execute(int CogBlobInspect_Results_GetBlobs_Count)

5. In this script, paste a single line of code:

   $Blob_count_cumulative += CogBlobInspect_Results_GetBlobs_Count;

   This code adds the input CogBlobInspect_Results_GetBlobs_Count to $Blob_count_cumulative, which you tagged earlier, with the C# operator +=.

   Note that, as with all C# statements, the line of code terminates with a semi-colon (;).

6. Validate your C# code to ensure you have no errors. Click the green check mark (). If any errors exist, correct them. Otherwise, exit the script by closing the CalculateCumlativeBlobs.Execute window.

7. At this point in the tutorial, your Task window should look like the following figure. Note the input connector (left-hand side) protruding from your new CalculateCumlativeBlobs script block. Hover your mouse over the connector, indicating the input integer (CogBlobInspect_Results_GetBlobs_Count) you defined.

In the next step in this tutorial, you'll modify your predefined HMI to display Total Blobs Counted (from Blob_Count_Cumulative) and run the application from start to finish.