Masks

This chapter describes the meaning of masks in TwinCAT Vision.

What is a mask?

In relation to image processing, a mask means a mask image that belongs to another image. The mask image has the same size and consists of a grayscale channel. The intensities of the mask image define which pixels of the associated image are to be taken into account for certain work steps.

What is a mask needed for?

Without further action, images are limited to a rectangular shape. If shapes other than rectangles are to be defined during image operations, this can be implemented by means of a mask. The mask is displayed by a further image with the same shape and size on which the areas (pixels) to be taken into account are marked differently.

How is a mask technically displayed?

The intensities of the mask grayscale image describe which pixels are to be considered. The interpretation of the intensities can in principle deviate from function to function. Binarily, it is frequently the case that all pixels with a value of 0 are interpreted as not to be considered and all pixels with a different value (>=1) are interpreted as to be considered. However, cases are also conceivable in which the degree of consideration can be set with a finer granularity.

A mask that restricts the observation area to a circular area in the image can look like this, for example:

Masks 1:

When applying this mask, the pixels in the corner of an associated image have no further influence on a subsequent operation.

Sample

By way of example, a mask is created for the image ipImageWork that says that only a circular area of the image is to be considered:

hr := F_VN_GetImageWidth(ipImageWork, nWidth, hr);
hr := F_VN_GetImageHeight(ipImageWork, nHeight, hr);

hr := F_VN_CreateImage(ipImageMask, nWidth, nHeight, TCVN_ET_USINT, 1, hr);
hr := F_VN_SetPixels(ipImageMask, aColorBlack, hr);
hr := F_VN_DrawCircle(nWidth/2, nHeight/2, MIN(nWidth, nHeight)/2, ipImageMask, aColorWhite, -1, hr);

The average intensity value of the image within the mask area is subsequently calculated:

hr := F_VN_ImageAverageExp(ipImageWork, aAverage, ipImageMask, hr);
fAverageIntensityInCircle := aAverage[0];

Without a mask it would not be possible without further action to restrict the average value to the circular area instead of the complete rectangular image.

The full sample can be found here: Average intensity in shapes of any kind.