You are here: Start » Program Examples » Capsules



The task is to inspect a quality of capsules and create a simple HMI. The following defects should be detected: black spots on the surface of capsule and deformations of its shape.


An image of a single capsule.


HMI containing an image with marked defects.


To detect black spots on the capsule surface, use Blob Analysis technique, which is the fastest and the simplest way to accomplish this task. To detect shape deformations you can create a path around the undamaged capsule, which is path model, and compare it with paths bounding consecutive capsules.

Solution (AVS)

  1. Add EnumerateImages filter to get load consecutive images of some disk directory.

  2. Create Step macrofilter DetectBlackSpots and create input of type Image.

  3. As spots are much darker than the background, add ThresholdToRegion filter. Set inMinValue to 0 and inMaxValue to 50.

  4. Add SplitRegionIntoBlobs filter to split the detected region into array of regions corresponding to each stain.

  5. Add TestArrayEmpty filter to test whether the size of the array equals zero. An output outIsEmpty is a result of inspection. Create output of type Bool and connect it with the outIsEmpty.

  6. Add RegionMassCenter filter to find the center of each stain.

  7. To create circles around stains, add CreateCircle filter and connect its input inPoint with the outMassCenter. Set its radius inRadius to 20.

  8. To draw the result on the image, add DrawCircles_SingleColor filter. Set inColor to red and inDrawingStyle.Thickness to 2.5.

  9. Create an output outBlackSpotsImage and connect it with output of the DrawCircles_SingleColor filter.

  10. Create Step macrofilter FindCapsuleRegion which is responsible for finding the region of capsule. Create its input of type Image.

  11. Add ThresholdToRegion_Dynamic filter to create region containing dark edges. Set both inRadiusX and inRadiusY to 8. Set also inMaxRelativeValue to -4.

  12. Add CloseRegion filter and set inKernel to Cross, also set both inRadiusX and inRadiusY to 2.

  13. Add FillRegionHoles filter to obtain the extended region, which contains all the pixels previously lying in its holes.

  14. Add OpenRegion filter to perform a morphological opening, hence noises are removed.

  15. To test whether the found region is correct, add the following filters:

  16. Create an output outRegionOrNothing and connect it with MakeConditional's output.

  17. Create Step macrofilter FindCapsuleShape and create inputs of types Image and Region. Connect the input of type Region with outRegionOrNothing. This macrofilter is responsible for finding the contour of the capsule.

  18. Add DilateRegion filter, connect its input inRegion with input of the macrofilter. Set its inputs: inKernel to Ellipse, and both inRadiusX and inRadiusY to 15.

  19. Add ErodeRegion filter to reduce the size of region. Set its inputs: inKernel to Ellipse, and both inRadiusX and inRadiusY to 30.

  20. Add RegionDifference filter. Then connect its inputs: inRegion1 with output of the DilateRegion filter and inRegion2 with output of the ErodeRegion. In this way you obtain a region containing outer edges of the capsule.

  21. To find these edges, add DetectEdges_AsPaths filter and connect its input inRoi with output of the RegionDifference filter. Then set the following properties:

  22. The next step is to classify edges, found using DetectEdges_AsPaths filter, by their length. To accomplish this, add ClassifyPaths filter, connect its input to output of the DetectEdges_AsPaths filter. Set inFeature to Length and inMinimum to 55.

  23. Add RegionMassCenter filter and connect its input to input of the macrofilter. It will be used in the next steps of the program.

  24. So far, you have an array of edges represented by paths. To obtain a contour of the capsule, a few operations must be performed. This is a subtask, therefore create a macrofilter CombineEdgesIntoContour.

  25. Create the following inputs: inEdges of type PathArray and inCapsuleMasCenter of type Point2D.

  26. Add SelectOuterPaths filter and connect its inputs with inputs of the macrofilter.

  27. Add JoinAdjacentPaths filter to join those paths which endpoints are linear enough. Set its inputs: inMaxJoiningDistance to 100 and inMaxJoiningAngle to 180.

  28. Add ClosePath filter to close the paths by adding the segment connecting last point to the first one.

  29. Add ConvertToEquidistantPath filter and SmoothPath_Gauss filter to improve the shape of the path.

  30. Add ClassifyPaths filter and set its property inFeature to ShapeArea. Specify the range of area by setting inMinimum to 85000.

  31. The last filter you need to add in this macrofilter is GetArrayElement_OrNil, to get the computed contour or return Nil if it was not found.

  32. Create an output outContourOrNothing and connect it with the GetArrayElement_OrNil's output.

  33. Go back to the parent macrofilter (FindCapsuleRegion) and connect its output with output of the CombineEdgesIntoContour macrofilter.

  34. The task of macrofilters FindCapsuleRegion and FindCapsuleShape was to extract the contour of the capsule. The next step is to create Step macrofilter DetectDeformations. In this filter you will computing the distance between outShapeOrNothing path and the model path. If the distance is higher than specified, the capsule is damaged.

  35. Create the following inputs: inImage of type Image, inShape of type Path and inBendErrorThreshold of type Real.

  36. Inside of the DetectDeformations macrofilter add AccessPath filter to obtain array of points of this path.

  37. Add FitPathToPath filter and connect its input inReferencePath with input of the macrofilter. Set the following parameters:

  38. Click the "..." button at the inPath input to open the GUI for creating path model.

    • Select FitPathToPath(#1)::outAlignment coordinate system.
    • Create a closed path around the capsule.
  39. To compute the distance between paths, add PathToPathDistanceProfile filter and connect its inputs: inPath1 with input of the macrofilter and inPath2 with the output of the FitPathToPath filter.

  40. Add ClassifyByRange filter to divide the input array into three output arrays, depending on the distance between points of the paths. You should be interested in the array of points which distance to a model path is higher than specified maximum distance. Connect the following ports:

  41. Add Formula to test whether the shape is correct.

    • Create the input inHigherCount and connect it with outHigher.Count.
    • Create the output IsCorrect and type the formula outIsCorrect = inHigherCount == 0.
  42. Create the macrofilter's output outIsCorrect and connect it with the formula's output.

  43. To draw the model path on the image, add DrawPaths_SingleColor filter and connect its input inPaths with the outPath. Set the following properties: inColor to white, inDrawingStyle.PointShape to Circle, inDrawingStyle.PointSize to 3.

  44. To draw the parts of the contour, where capsule is deformed, add CreateCircle filter and connect its input inPoint with outHigher. Then add DrawCircles_SingleColor filter and connect its input with outCircle.

  45. Create the macrofilter's output DeformationsImage and connect it with the output image.

  46. Go to the Main macrofilter and add TestObjectNotNil filter. Connect its input with outDeformationsImage. This filter returns true if capsule has been detected.

  47. Create a Variant macrofilter PresentResultsOrInfo with forking port inResultImageExists of type Bool.

    • Create the inputs inInputImage and inResultImage, both of type Image.
    • In the variant True add MergeDefault filter. Connect an inConditionalObject with the inResultImage and an inDefaultObject.
    • In the variant False add DrawStrings_SingleColor filter. Connect its input inImage with input inInputImage of the macrofilter.
    • Specify the string to be drawn, by setting the inString to Capsule damaged or not present.
    • Create the output outImage and connect it with outputs of filters in the both variants.
  48. Program is finished, now you can design your HMI:

    • Add GroupBox container available in Containers category of the HMI Controls and set its Text property to Control.
    • Add another GroupBox container and set its Text property to Results.
  49. In Control GroupBox:

    • Add ProgramControlBox control available in Controls category to allow an user to control program execution.
    • To set the maximal shape deviation, add Knob control and connect its outValue output with input inBendErrorThreshold of the DetectDeformations macrofilter.
  50. In Results GroupBox:

    • Add VideoBox control from Video Box category and connect its input inImage with output of the PresentResultsOrInfo macrofilter.
    • Add PassFailIndicator control and connect its inputs inValue with output outIsNotNil of the TestObjectNotNil. This control shows whether the capsule has been detected.
    • Add PassFailIndicator control and connect its inputs inValue with output outIsCorrect of the DetectBlackSpots. This control shows whether there are no black spots on the capsule's surface.
    • Add PassFailIndicator control and connect its inputs inValue with output outIsCorrect of the DetectDeformations. This control shows whether shape of the capsule is correct.

Macrofilter Main

Macrofilter FindCapsuleRegion

Macrofilter DetectBlackSpots

Macrofilter CombineEdgesIntoContour

Macrofilter FindCapsuleShape

Macrofilter DetectDeformations

Macrofilter PresentResultsOrInfo(default)

Macrofilter PresentResultsOrInfo(Nil)

Used Filters

Icon Name Description
EnumerateImages Emulates image acquisition with images stored on disk.
SmoothPath_Gauss Smooths a path by averaging its characteristic points within a kernel using gaussian-weighted average.
DetectEdges_AsPaths Consistent detection of contours of variable or unpredictable shape, e.g. screw thread outline or a custom piece of textile.
ClassifyPaths Use this filter when to you have an array of paths and you want to select some of them for further processing.
DrawCircles_SingleColor Draws circles on an image with a single color.
DilateRegion Making the region thicker or filling-in small holes within it.
ClosePath Adds the segment connecting last point to the first one to a path.
MakeRegionConditional Computes region feature and checks whether its value is in range.
CloseRegion Filling-in small gaps in a region without making it thicker.
RegionDifference Computes a region containing pixels from the first input region, but not from the second input region.
AccessPath Returns individual fields of a path.
SplitRegionIntoBlobs Segmentation of a region into individual objects when the objects do not touch each other.
JoinAdjacentPaths Joins those paths of an array which endpoints lie near enough.
OpenRegion Removing small parts from a region without making it thinner.
DrawStrings_SingleColor Draws strings (text) on an image with a single color.
PathToPathDistanceProfile Computes the profile of distances between two paths.
GetArrayElement_OrNil Extracts a single element from an array at the specified index; returns NIL if the index is out of range.
ClassifyByRange E.g. selection of the objects (e.g. blobs) whose associated values (e.g. area) meet the specified minimum and maximum requirements.
FitPathToPath Rotates and shifts a path to minimize average distance between its points and a reference path.
RegionMassCenter Computes a point with coordinates equal to the average coordinates of the region's pixels.
ThresholdToRegion Extraction of a region of objects that can be defined by a salient brightness.
ConvertToEquidistantPath Creates a new path whose characteristic points lie on the input path, but are equally spaced.
FillRegionHoles Extends the input region so that it contains also all the pixels previously lying in its holes.
SelectOuterPaths Selects paths which do not obstruct visibility of other paths from a point.
ErodeRegion Making the region thinner or removing small parts.
DrawPaths_SingleColor Draws paths on an image with a single color.
CreateCircle Creates a circle from an aligned point and radius.
ThresholdToRegion_Dynamic Useful in case of uneven illumination.

Further Readings

  • Blob Analysis - Article presents detailed information about the Blob Analysis technique.
  • Shape Fitting - This article presents usage of the Shape Fitting technique.