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. In Workspace Explorer open workspace Examples and in Film strip window select Capsule dataset. Drag the Image channel to the ACQUIRE section.

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

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

  4. Add the 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. The outBlobs.IsArrayEmpty output 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 the RegionMassCenter filter and connect it with the outBlobs to find the center of each stain.

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

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

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

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

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

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

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

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

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

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

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

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

  20. Add the 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 the GetMaximumPath filter to get the path corresponding to the capsule outline. Connect its input with the output of the RegionContours.

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

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

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

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

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

  27. Set the following parameters of the FitPathToEdges:

  28. Add the 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 the 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 the FitPathToPath filter and connect its inReferencePath input 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 the FitPathToPath(#1)::outAlignment coordinate system.
    • Create a closed path around the capsule.
  35. To compute the distance between paths, add the PathToPathDistanceProfile filter and connect its inPath1 with input of the macrofilter and the inPath2 with the output of the FitPathToPath filter.

  36. Add the 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 outIsCorrect output and connect it with the IsArrayEmpty output.

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

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

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

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

    • In the variant Nil add the DrawStrings_SingleColor filter. Connect its inImage input with the inInputImage input of the macrofilter.
    • Specify the string to be drawn, by setting the inString to Capsule damaged or not present.
    • Create the macrofilter's outImage output and connect it the output of the DrawStrings_SingleColor filter.
  43. Program is finished, now you can design your HMI:

    • Add the GroupBox container available in the 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 the Control GroupBox:

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

    • Add the VideoBox control from the Video Box category and connect its inImage input with output of the PresentResultsOrInfo macrofilter.
    • Add the PassFailIndicator control and connect its inValue inputs with the outShapeOrNothing.IsNil.Not output. To make this output visible right-click on the outShapeOrNothing and select IsNil.Not. This control shows whether the capsule has been detected.
    • Add the PassFailIndicator control and connect its inValue inputs with the outIsCorrect output of the DetectBlackSpots. This control shows whether there are no black spots on the capsule's surface.
    • Add the PassFailIndicator control and connect its inValue inputs with the outIsCorrect output 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)

Further Readings

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