You are here: Start » Program Examples » Capsules

Capsules

Aim

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.

Input

An image of a single capsule.

Output

HMI containing an image with marked defects.

Hints

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.

Labeling connections is explained in this article.

Solution (AVS)

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

  2. Create Step macrofilter DetectBlackSpots and create input of type Image. Place it in the PROCESS section.

  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. Right-click on the outBlobs, select "Property Outputs" and choose IsArrayEmpty from the list. An output outBlobs.IsArrayEmpty indicates whether the size of the array equals zero.

  6. Drag the outBlobs.IsArrayEmpty and drop it on the Macrofilter Outputs block to create an output of the same data type. Name it outIsCorrect.

  7. Add RegionMassCenter filter and connect it with the outBlobs to find the center of each stain.

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

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

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

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

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

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

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

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

  16. Add MakeRegionConditional to compute region area and check whether it is in the specified range. Set the inMinimum to 100000 and inMaximum to 125000.

  17. Create an output outRegionOrNothing and connect it with MakeRegionConditional's output.

  18. Create Step macrofilter FindCapsuleShape and create inputs of types Image and Region. This macrofilter is responsible for finding the contour of the capsule.

  19. Add RegionConvexHull filter and connect its input inRegion with the inRegion input of the macrofilter.

  20. Add RegionContours filter and connect its input with the outRegion of the previous filter. It computes an array of closed paths corresponding to the contours of the input region.

  21. Add GetMaximumPath filter to get the path corresponding to the capsule outline. Connect its input with the output of RegionContours.

  22. Add ConvertToEquidistantPath filter and connect its input with the output of the previous filter. Set the input inStep to 5.

  23. Add SmoothPath_Gauss filter to smooth the output of ConvertToEquidistantPath. Set the input inStdDev to 2.

  24. Add FitPathToEdges filter and connect inImage the input inImage of the macrofilter.

  25. Right-click on the inFittingField and select "Expand Structure Fields".

  26. Connect inFittingField.Axis with the output of SmoothPath_Gauss filter.

  27. Set the following parameters of FitPathToEdges:

  28. Add InflatePath filter to enlarge a path so that it is corresponding to the outer outline of the capsule. Connect the inPath with the outPath of the FitPathToEdges.

  29. Drag the outPath of the InflatePath filter and drop it on the Macrofilter Outputs block to create an output of the same type. Name it outShapeOrNothing.

  30. 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 be computing the distance between outShapeOrNothing path and the model path. If the distance is higher than specified, the capsule is damaged.

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

  32. Right-click on the inShape input, select "Property Outputs" and choose Points from the list to get access to the points of the capsule outline.

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

  34. 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.
  35. 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.

  36. 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:

  37. Right-click on the outHigher, select "Property Outputs" and select IsArrayEmpty from the list.

  38. Create the macrofilter's output outIsCorrect and connect it with the IsArrayEmpty output.

  39. 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.

  40. 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.

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

  42. Create a Variant macrofilter PresentResultsOrInfo with forking port inResultImage of conditional type Image?.

    • In the variant Nil 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 macrofilter output outImage and connect it the output of DrawStrings_SingleColor filter.
  43. 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.
  44. 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.
  45. 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 outShapeOrNothing.IsNil.Not. To make this output visible right-click on the outShapeOrNothing and select IsNil.Not. 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 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.
DrawCircles_SingleColor Draws circles on an image with a single color.
GetMaximumPath Use this filter when you have an array of paths and you want to select one of them that best matches some criterion.
InflatePath Enlarges a shape by a given margin (points in the input path must be sorted clockwise).
MakeRegionConditional Computes a region feature, checks whether its value is in range and creates a conditional region on this basis.
CloseRegion Filling-in small gaps in a region without making it thicker.
RegionConvexHull Computes the smallest convex region containing the input region.
SplitRegionIntoBlobs Segmentation of a region into individual objects when the objects do not touch each other.
SelectByRange Selects the elements of the input that fall into the specified range.
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.
ThresholdToRegion Extraction of a region of objects that can be defined by a salient brightness.
FitPathToPath Rotates and shifts a path to minimize average distance between its points and a reference path.
FitPathToEdges Tracing of an object contour, whose rough location and shape is known beforehand.
RegionMassCenter Computes a point with coordinates equal to the average coordinates of the region's pixels.
ConvertToEquidistantPath Creates a new path whose characteristic points lie on the input path, but are equally spaced.
FillRegionHoles Adds pixels to the input region so that it contains no holes.
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.
RegionContours Computes an array of closed paths corresponding to the contours of the input region.

Further Readings

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