Script

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.

Downloads

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.

Dark pills have been analyzed.

Dark pills have been analyzed.

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
    3. Stadium-shaped 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.

Inputs and outputs of the white pills script.

Inputs and outputs of the white pills script.

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

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

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

for (var i = 0; i < matArea.rows; ++i)
  {
    // Is it small and round?
    if (matRatio.entry(i, 0) < realStadiumRatioLimit &&
        matArea.entry(i, 0) < realSmallAreaLimit)
      ++countRoundSmall;
    // Is it big and round?
    else if (matRatio.entry(i, 0) < realStadiumRatioLimit &&
             matArea.entry(i, 0) >= realSmallAreaLimit)
      ++countRoundBig;
    // Is it stadium-shaped?
    else if (matRatio.entry(i, 0) >= realStadiumRatioLimit &&
        matRatio.entry(i, 0) < realElongatedRatioLimit)
      ++countStadium;
    // Is it elongated?
    else if (matRatio.entry(i, 0) >= 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.

Inputs and outputs of the gray pills script.

Inputs and outputs of the gray pills script.

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);