You are here: Start » Program Examples » Chocolate Cookies Inspection 3D

Chocolate Cookies Inspection 3D

Aim

The task of this example is to segment cookies, count them and measure their dimensions.

Input

Surfaces of cookies grabbed by a 3D scanner.

Output

Image of segmented cookies, number of cookies, dimensions and completeness verification.

Hints

Having acquired point clouds of Surface data type, try to find a filter which would enable you to convert them into images. Then you could use Region Analysis technique to perform segmentation. After extracting blobs representing cookies it is possible to count them and measure their dimensions with MeasureObjectWidth3D.

Solution (AVS)

  1. Add EnumerateFiles and LoadObject filters to load point clouds. In the first filter set the proper directory to the point clouds. Connect outFilePath to inFile. Now you may select both filters, right-click and extract a new macrofilter step, which you may call "AcquireSurfaces". Connect outObject to macrofilter outputs and name it outSurface.

  2. Go back to Main and add CropSurface. Connect the output outSurface to inSurface. Click on the filter and in Properties window in the bottom left corner and set the values inZLimits.Min and inZLimits.Max to 12 and 23 respectively. Set inPreserveDimensions to True. That way points corresponding to the background and noise will be removed.

  3. Add the CreateImageFromSurface_AnyScales filter to create an image of z-values of the given surface. It allows us to work with 2D tools, even though input data is 3D. Connect outSurface to inSurface. Click on the filter and in the Properties window set inPixelOffset to 0.

  4. Now create a new step macrofilter which you can name e.g. "SegmentAndCountCookies". Connect outImage and outSurface to macrofilter inputs. Inside of the macrofilter, add ExtractBlobs_Intensity.

  5. Connect macrofilter input inImage to inImage. Click on the filter and set these values in the Properties window:

  6. Next, add the ClassifyRegions filter. Connect outBlobs to inRegions. Click on the filter and in the Properties window set following parameters:

    • inFeature to Rectangularity because we need to detect blobs that are rectangle-shaped,
    • inMinimum to 0.8 to make sure that incorrect blobs are not accepted.
  7. Add RegionUnion_OfArray. Connect outAccepted to inArray.

  8. Add the CropSurfaceToRegion filter to get a Surface representing only cookies, without points corresponding to the box. Connect macrofilter input inSurface to inSurface and outRegion to inRegion. Click on the filter and in the Properties window set inPreserveDimensions to True.

  9. Add an empty Formula and perform the following operations:

    • Add a new macrofilter input representing the number of cookies you are expecting to get (e.g. "inExpectedNumberOfCookies" of Integer type) and set its default value to 12. Connect it to the newly-created formula,
    • Right-click on the outAccepted output, select Property Outputs and choose the Count property. Connect it to the formula as inNumberOfCookies input,
    • Create a new output in the formula outIsCountOK like this:

      outIsCountOK = inExpectedNumberOfCookies == inNumberOfCookies

    • Connect the outIsCountOK output to the macrofilter outputs.

  10. Go back to Main and create another step macrofilter (e.g. "FindSurfaceMinimalPoint"). Connect outSurface from the SegmentAndCountCookies macrofilter to the newly-created macrofilter as an input.

  11. Connect outBlobs from the SegmentAndCountCookies macrofilter as an array connection. It will cause the macrofilter to execute in an array mode (see also: Arrays in Adaptive Vision Studio).

  12. Inside the macrofilter add FillRegionHoles to extend the region so that it contains also the pixels previously lying in its holes. Connect the macrofilter input inRegion to inRegion.

  13. Add ErodeRegion to make the region thin enough to find later a minimal point representing the cookie hight. This step is essential since the cookies have rounded edges. Connect outRegion from the previous filter to inRegion of the current one. Set inRadiusX to 30 to remove noise on edges.

  14. Add the SurfaceMinimalPoint filter to get the minimal point of the surface within the region obtained in the previous step and connect the macrofilter input inSurface to inSurface and outRegion to inRoi. Drag the outMinimalPoint output to the macrofilter outputs.

  15. Go back to Main and create a new step macrofilter to create an individual local coordinate system for each cookie (you may call it e.g. "CreateLocalCoordinateSystem3D"). Connect outSurface and outBlobs from the SegmentAndCountCookies macrofilter to it as new inputs: inSurfaceFormat and inRegion (as an array connection) respectively. The macrofilter will be executed in the array mode as well as the FindSurfaceMinimalPoint macrofilter.

  16. Inside the macrofilter, add RegionBoundingBox_OrNil. Connect macrofilter input inRegion to inRegion.

  17. Add the CreateCoordinateSystemFromRectangle filter to create an alignment for further analysis. Connect outBoundingRectangle to inRectangle.

  18. Next, add ConvertCoordinateSystem2DTo3D to convert image coordinate system into a 3D coordinate system, because they may not align unless the Surface format is given. Connect outCoordinateSystem to inCoordinateSystem. Then, connect macrofilter input inSurfaceFormat to inSurfaceFormat.

  19. Drag the outCoordinateSystem output to the macrofilter outputs.

  20. Go back to Main. Create another step macrofilter (e.g. "MeasureObjectDimensions"). Connect outSurface from the SegmentAndCountCookies macrofilter and outCoordinateSystem from the previous macrofilter to inputs of the current one as inSurface and inScanFieldAlignment respectively.

  21. Inside of the macrofilter, add two MeasureObjectWidth3D filters and connect macrofilter inputs inSurface and inScanFieldAlignment to inSurface and inScanFieldAlignment in both filters.

  22. Now click on the first filter and make the following changes in the Properties window:

  23. Click on the other MeasureObjectWidth3D filter and make the following changes in the Properties window:

    • Click on inScanField and mark the scanning path to compute the width of the cookie,
    • Set inScanCount to 10 to increase the number of scans to be performed,
    • Set inScanWidth to 10 to increase the width of scanning field,
    • Set inStripeScanParams.StripePolarity to Valid - to detect only actual points of the surface representing cookie,
    • Set inMaxProfileGapWidth to 400 which will be the maximal allowed value of profile gap,
    • Set inOutlierSuppression to Tukey which will be the best method to ignore incorrectly detected points in this case.
  24. Connect both outObjectWidth outputs to the macrofilter outputs as outVerticalDimension and outHorizontalDimension respectively.

  25. Add an empty Formula with the following inputs:

    • inMinVerticalDimension with a default value 7,
    • inMinHorizontalValue with a default value 39,
    • inMinHeight with a default value 17.
  26. Create the following outputs in the formula:

    IsVerticalDimensionOK = VerticalDimension > inMinVerticalDimension

    IsHorizontalDimensionOK = HorizontalDimension > inMinHorizontalDimension

    IsHeightOK = SurfaceMinimalPoint > inMinHeight

    InspectionResult = IsVerticalDimensionOK and IsHorizontalDimensionOK and IsHeightOK

Macrofilter Main

Macrofilter AcquireSurface

Macrofilter CreateLocalCoordinateSystem3D

Macrofilter FindSurfaceMinimalPoint

Macrofilter MeasureObjectDimensions

Macrofilter SegmentAndCountCookies

Used Filters

Icon Name Description
ConvertCoordinateSystem2DTo3D Converts a coordinate system connected with the surface image to a coordinate system connected with the surface.
ClassifyRegions Use this filter when you have an array of regions and you want to select some of them for further processing.
CropSurfaceToRegion Removes points that are not present in a given region.
CreateCoordinateSystemFromRectangle Most often used to define an object alignment from a filter like RegionBoundingRectangle.
SurfaceMinimalPoint Finds the surface point with minimal Z coordinate.
FillRegionHoles Adds pixels to the input region so that it contains no holes.
ErodeRegion Making the region thinner or removing small parts.
MeasureObjectWidth3D Measures the width of an object using stripe detection.
RegionUnion_OfArray Computes a region containing all the pixels that any of the input regions contains.
CreateImageFromSurface_AnyScales Allows for performing 2D operations on 3D data.
LoadObject Loads an object from a file.
CropSurface Removes from the surface points that are not contained in a given rectangular box.
RegionBoundingRectangle_OrNil Computes the smallest rectangle containing a region; returns NIL if the region is empty.
EnumerateFiles Enumerates the files present in a disk directory.
ExtractBlobs_Intensity Segments an image into blobs by thresholding.

Further Readings

  • Blob Analysis - Article presents detailed information about the Blob Analysis technique.
  • Formulas - Detailed information about using formulas.
  • Geometry 3D - List of filters useful in 3D geometry.
  • Surface - Filters performing operations on surfaces in Adaptive Vision Studio.