Contours

This chapter describes the use of contours in TwinCAT Vision.

What are contours?

In general, a contour (also called an outline) is the demarcation of an object from its surroundings; this is classically two-dimensional in image processing. A contour describes the shape, size and location of this object. Within the scope of image processing, this means an object in an image; hence, a contour describes the outline of the object in the image.

How are contours displayed?

A contour is displayed as a collection of 2D points. The points mostly refer to the origin of the image in which the object is mapped, provided no ROI has been set. If you connect all the points in this field with straight lines, you get a drawing of the contour.

Contours 1:

How are contours displayed in TwinCAT Vision?

A dynamic array of 2D points is handled in TwinCAT Vision as a container (ITcVnContainer) of points (TcVnPoint2_DINT/REAL). A corresponding container then has the type designation ContainerType_Vector_TcVnPoint2_DINT/REAL.

The points then frequently lie directly on the pixel grid (e.g. in case of a contour search). In this case, DINT elements are used to save the points. However, there are also cases in which the accuracy is insufficient, and REALs therefore have to be used (e.g. in case of measurements). The points then do not lie exactly at the center point of a pixel, but somewhere else.

Drawing functions can only draw contours with integer elements in an image. In case of doubt, therefore, the element type of the container must be converted.

Fields of contours

A frequent occurrence is where several contours are to be handled at the same time (e.g. several contours are usually found with F_VN_FindContours). This is technically implemented by using a two-dimensional container. Therefore, there is an array of contours or an array of arrays of 2D points. The corresponding type designation is then ContainerType_Vector_Vector_TcVnPoint2_DINT/REAL.

Contours 2:

What is contour approximation?

Generally, a very large number of points can always be used for displaying an object. However, this is frequently unnecessary, as (approximately) straight lines can be approximated through their start and end point. The points in between do not need to be saved in addition, because the start and end point suffice for the definition.

Depending on the method used and the geometry of the objects, the approximation can be more or less lossy. In any case, in the case of differing approximation, the geometrical features can also be calculated with slightly different values.

Geometrical features

On contours, i.e. from the arrays of points, a large number of individual features can be calculated that geometrically describe the respective contour. The contour and thus the respective object can be examined and, for example, classified via these features. These features include:

Contours 3:

Closed and open contours

A contour is normally closed. In this case the first and last element of the array are simply regarded as being connected. However, it is also possible to have an open contour. Open and closed contours thus do not differ in terms of their technical representation, but by the information regarding how the data are to be interpreted. Whether the respective contour is to be used open or closed will be queried accordingly at the relevant points of the API.

Samples