You are here: Start » Program Examples » Cap (Advanced)

Cap (Advanced)

Aim

The task of this example is to check whether a seal (plastic ring under the cap) is correctly placed.

Input

An image of the top of a bottle. The position of the bottle is variable.

Output

The result of the inspection printed on the image. If a defect is detected, a rectangle is drawn around it.

Hints

The position of the object is variable along both vertical and horizontal axes. Using 1D Edge Detection technique you are able to create a local coordinate system that can be used to configure other tools to work independent of the object orientation, translation and scale.

Labeling connections is explained in this article.

Solution (AVS)

  1. In Workspace Explorer open workspace Examples and in Film strip window select CapDifficult dataset. Drag the Image channel to the ACQUIRE section.

  2. Add the ScanSingleStripe filter to detect the position of a cap along the X axis.

    • Click the "..." button at the inScanPath input to open the GUI for drawing paths of scanning. Draw a horizontal path that intersects the cap roughly at the vertical center of the cap. The path should have the same length as an image width. Click OK to close the GUI.
    • In the filter parameters set the value of the MinStripeWidth to slightly less than the expected diameter of the cap (here 300 should be enough). Make sure the StripePolarity parameter is set to Dark.
    • Click on Show/Hide Ports at the bottom of the filter and check the options Width, Point1 and Point2 in the outStripe submenu. This will allow us to see the width of the detected stripe as well as both edge points.
    • Label the outStripe.Point1 as LeftPoint
  3. Add the CreateCoordinateSystemFromPoint filter and connect the outStripe.Point1 from the previous filter to the inPoint in this filter.

    • In the Properties of the filter click on the eye icon next to the inScale. Now the input port for this parameter is visible. Connect to it the outStripe.Width parameter from the previous filter.
    • In the inScaleDivisor field enter 360. This is the diameter of the reference cap on which next steps of the program are based. Note that the position of a cap also varies along an axis perpendicular to the image, but creating the coordinate system in this way ensures that the scale will adjust to the width of the cap. In other words, if the cap is closer to the camera, the scale increases.
    • Label the outCoordinateSystem.Scale as Scale
  4. Run the program once and then add the ScanSingleEdge filter. Connect the outCoordinateSystem output from the previous filter to the inScanPathAlignment input.

    • Click the "..." button at the inScanPath. In the GUI make sure the Coordinate System is set to inScanPathAlignment. Draw a vertical scan path which intersects with the cap roughly in its highest point. Click OK to close the editor.
    • Show the outCoordinateSystem.Scale output by clicking on Show/Hide Ports and check the options Point in the outEdge submenu.
    • Label the outEdge.Point as TopPoint
  5. Create the Formula with the output outAlignment2. Note, that you can use labeled outputs instead of connecting data to the formula manually:

    outAlignment2 = CoordinateSystem2D(Point2D(LeftPoint.X, TopPoint.Y), 0.0, Scale )

    • It will create a Coordinate System inside a Formula using the constructor of the CoordinateSystem2D data type.
    • The constructor requires three inputs: the origin (Point2D), the angle (Real) and the scale (Real).
    • The scale is equal to Scale label value.
    • The angle is equal to 0.
    • The origin is equal to a point whose x coordinate is equal to the x coordinate of the LeftPoint, the y coordinate is equal to the y coordinate of the TopPoint.
  6. In the Project Explorer section click Create New Global Parameter.... Choose a name for the variable and set the type to the Segment2DArray.

    • Click the plus icon below the Initial Value. Then click Add New Item and the "..." icon in the newly created item. This will open a segment editor.
    • In the editor first choose the image from ReadFilmstrip as the reference image. Next set the Coordinate system field to the output of the previously created formula and finally draw a vertical segment intersecting with cap at roughly one fourth of its width. Click OK.
    • Now add two new items, each being a Segment2D: one at the center and one at three fourths of the width:
    • Click OK to finish creating a global parameter.
  7. Run the program once and then add another ScanSingleEdge filter.

    • Connect the previously created global parameter to the inScanPath input by dragging it from the Project Explorer section.
    • Connect the output of the formula to the inScanPathAlignment input.
    • Notice that the name of the filter is now followed by a suffix: "[3]". This indicates that the filter will run 3 times in one iteration of the program: once for each segment the array (the global parameter).
    • In the filter properties change EdgeTransition to DarkToBright. Also, lower MinMagnitude to 4 from 5.
    • Show the outEdge.IsNil output by clicking on Show/Hide Ports and check the options Point in the outEdge submenu. This will tell if the edge was detected. Label that output as DefectPresent.
  8. Add another Formula filter. Create the output outIsOK. This output will be shown in the images. The bottle will pass only if there are no gaps detected. :

    outIsOK = all(DefectPresent) ?? False

    and the outResultText. The text will read "PASS" if the input condition is True or "FAIL" if the input is False:

    outResultText = outIsOK? "PASS" : "FAIL"

  9. Add the DrawStrings_MultiColor filter.

    • Connects the outResultText to the inStrings and the outIsOK to theinColorIds.
    • In the filter properties click on the "..." icon next to the inLocations to open the editor.
    • Put a single point roughly in the upper center of the image.
    • Other properties can be modified to change the color of the text and its size. Here the inSize is set 24.
  10. Add the CreateBox filter.

  11. Add the DrawRectangles_SingleColor filter. In the Properties window the appearance of the rectangle can be changed.

  12. Select the last 3 filters in the program (from the DrawStrings_MultiColor).

    • Right click on them and select the ExtractStep... option.
    • Now you can change the name of the macrofilter (here: DrawResults)
    • By right clicking inputs and outputs of this filter and then clicking edit, you can change names and types of the ports.
  13. Enter the newly created filter and drag the outImage to the Macrofilter Outputs bar.

  14. Drag the output from the created filter to the preview.

  15. Now the final output of the whole program should be the image with the inspection result label and defects marked with rectangles.

Macrofilter Main

Macrofilter DrawResults

Used Filters

Icon Name Description
CreateCoordinateSystemFromPoint Most often used to define an object alignment from results of 1D Edge Detection or Blob Analysis.
ScanSingleEdge Very fast detection of an object (e.g. horizontal displacement of a bottle) and simple measurements (e.g. liquid level in a bottle).
CreateBox Creates a box.
DrawRectangles_SingleColor Draws rectangles on an image with a single color.
DrawStrings_MultiColor Draws strings (text) on an image with multiple colors.
ScanSingleStripe Very fast detection or measurement of an object defined by a pair of opposite edges.

Further Readings