Script

Summary

With the Script Tool you can embed JavaScript code into the processing graph. In this tutorial we walk through a simple application that classifies pills according to their shapes and colors.

Detailed description

Images coming from an Image Source are passed into two separate processing branches. The leftmost one separates the bright pills according to their size and shapes. The rightmost processing branch identifies gray pills and calculates their minimum, maximum and mean areas.

Let's start from the left branch.

• Blob Detection Tool is configured to identify any blobs which have a very sharp edge and are brighter than the background. The is acquired by setting Sensitivity to a very low value (here 0.03) and Invert flag as false.

• Blob Geometry Analysis is used to calculate the areas and the ratios of Major-to-Minor principal axes lengths. This information will be needed in the script which classifies the blobs.

• Script for white pills sets limits for the size and the shape ratio and separates the blobs into four categories

1. Big and round blobs
2. Small and round blobs
4. Elongated blobs

Notice that it is fairly difficult for the human eye to separate stadium and elongated blobs. The script, however, can separate them reliably using the axes ratio. The parameter definitions and the script code are shown below.

var realStadiumRatioLimit = 1.5;
var realElongatedRatioLimit = 2.0;
var realSmallAreaLimit = 5000;

var matArea = $i.area; var matRatio =$i.ratio;

var countElongated = 0;
var countRoundBig = 0;
var countRoundSmall = 0;

for (var i = 0; i &lt; matArea.rows; ++i)
{
// Is it small and round?
if (matRatio.entry(i, 0) &lt; realStadiumRatioLimit &amp;&amp;
matArea.entry(i, 0) &lt; realSmallAreaLimit)
++countRoundSmall;
// Is it big and round?
else if (matRatio.entry(i, 0) &lt; realStadiumRatioLimit &amp;&amp;
matArea.entry(i, 0) &gt;= realSmallAreaLimit)
++countRoundBig;
else if (matRatio.entry(i, 0) &gt;= realStadiumRatioLimit &amp;&amp;
matRatio.entry(i, 0) &lt; realElongatedRatioLimit)
// Is it elongated?
else if (matRatio.entry(i, 0) &gt;= realElongatedRatioLimit)
++countElongated;
}

$o.numElongated = countElongated;$o.numStadium = countStadium;
$o.numRoundBig = countRoundBig;$o.numRoundSmall = countRoundSmall;

The right processing branch for gray pills goes like this:

• Two Level Thresholding Tool binarizes the input image so that the pixels with intensities on range Low Level...High Level are set as '1' and the others as '0'.

• Blob Detection Tool filters out noise so that only the blobs which are large enough are left.

• Blob Geometry Analysis calculates the areas of the blobs.

• Script for Gray pills use JS API functions to calculate min, max mean and standard deviation of the vector of blob areas. No manual loops are required.

The JavaScript code is as follows:

$o.minArea = Kuvio.min($i.area);
$o.maxArea = Kuvio.max($i.area);
$o.meanArea = Math.round(Kuvio.mean($i.area));
$o.deviation = Kuvio.std($i.area);