You are here: Start » Program Examples » Cap (Easy)
Cap (Easy)
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
This is an easier variant of the same example "Caps". If you are on a more advanced level in programming in FabImage Studio, please refer to that example. If you are new to FabImage Studio, then carry on here.
The first step to perform required inspection is detection of the position of an object in an image. As the bottle moves particularly along the X axis and the background is consistent, the best way would be applying the 1D Edge Detection technique and creating a local coordinate system based on a detected edge.
Labeling connections is explained in this article.
Solution (FIS)
-
Add the EnumerateImages filter and define the inDirectory input (where your images are stored).
-
Add the ScanSingleEdge filter. Connect outImage to inImage.
-
Click on the ScanSingleEdge filter. In the Properties window in the left bottom corner, choose inScanPath and define the scanning path, so that it could find the left edge of the cap. Set the inEdgeScanParams.MinMagnitude to 10 to inure edges to possible noise.
-
Add the CreateCoordinateSystemFromPoint filter. Connect outEdge.Point to inPoint. It will create a local coordinate system at the point where the edge has been found.
-
Add another ScanSingleEdge filter. Connect outImage to inImage, and connect outCoordinateSystem to inScanPathAlignment.
-
Now go to the Project Explorer (the left side of FabImage Studio). Create a new global parameter. Name it ScanSegments which will be of Segment2DArray type. Then define three scanning paths as shown in the image below:
Connect ScanSegments to inScanPath. This filter will be now executed in an array mode.
-
Click on the ScanSingleEdge filter and in the Properties window make following changes:
- Set inScanWidth to 70 so that the width of the scanning path is wide enough,
- Set inEdgeScanParams.MinMagnitude to 4 to find even smallest deviations on the seal,
- Set inEdgeScanParams.EdgeTransition to DarkToBright.
-
Use Show/Hide ports option to add the outEdge.IsNil output. Label it as DefectPresent.
-
Add a new formula. Use label DefectPresent in the statement without connecting it to the filter:
outIsOK = all(DefectPresent) ?? False
-
Now create a new step macrofilter, name it DrawResults and connect following inputs to it:
- outImage as inImage,
- outEdge.Point (from the latter filter) as inDefectLocations,
- Formula's outIsOK output as inIsOK.
-
Step inside the newly-created macrofilter. Add ChooseByPredicate of String type. In the Properties window set following values:
- PASS in inObjectIfTrue,
- FAIL in inObjectIfFalse.
-
Connect macrofilter's input inIsOK to inCondition.
-
Add the DrawStrings_MultiColor filter:
- Connect macrofilter's input inImage to inImage,
- Connect outObject to inStrings,
- Connect macrofilter's input inIsOK to inColorIds,
- Define location of the text in inLocations somewhere over the cap,
- Set inPalette to red at index 0 and green at 1,
- Set inSize to 24.
-
Add the CreateBox filter:
- Connect macrofilter's input inDefectLocations to inLocation,
- Set inLocationAnchor to MiddleCenter,
- Set inWidth to 40,
- Set inHeight to 40.
-
Add the DrawRectangles_SingleColor filter:
- Connect outImage to inImage,
- Connect outBox to inRectangles,
- Set inColor to red,
- Set inDrawingStyle.Thickness to 3,
- Connect outImage to macrofilter outputs as outImage.
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. |
![]() |
DrawRectangles_SingleColor | Draws rectangles on an image with a single color. |
![]() |
CreateBox | Creates a box. |
![]() |
DrawStrings_MultiColor | Draws strings (text) on an image with multiple colors. |
![]() |
ChooseByPredicate | E.g. to choose GREEN color to visualize correct objects or RED to visualize defective ones. |
![]() |
ScanSingleEdge | Very fast detection of an object (e.g. horizontal displacement of a bottle) and simple measurements (e.g. liquid level in a bottle). |
Further Readings
- 1D Edge Detection - The article explaining how edge detection filters work.
- Local Coordinate Systems - This article describes basic concept of using the coordinate systems.