Average intensity in shapes of any kind

In this sample the average intensity of an image is calculated within a manually defined area of an image. For this purpose a circular image mask is used.

The following functions are mainly used:

Explanation

The sample Statistical image features shows how statistical image features (such as average, maximum and minimum) can be calculated. Frequently, however, these features are not calculated for the entire image, but only for certain image areas. The setting of an ROI is helpful if these areas are rectangular. You have to work with image masks in order to restrict the calculation to areas of any shape. These are essentially images whose intensity values describe which pixels in the original image are to be considered. A mask can thus assume any shape; this sample shows a circular mask.

For this sample a quasi-binary image with a collection of screws will be observed. One conceivable use case would be, for example, that the number of screws is to be approximately determined on the basis of the average intensity value.

Average intensity in shapes of any kind 1:

Application

By varying the parameters nCircleShrink and aCenterOffset, you can change the size and position of the circular mask. According to this mask, the average intensity value changes: if the mask becomes smaller and shows less of the white background, the average value decreases. If the mask shifts and shows more of the white background, the average value increases.

Result image

Mask image

Average intensity in shapes of any kind 2:

Average intensity in shapes of any kind 3:

Average intensity in shapes of any kind 4:

Average intensity in shapes of any kind 5:

Average intensity in shapes of any kind 6:

Average intensity in shapes of any kind 7:

Other images can be loaded into this sample if necessary. Note that the mask parameters may then have to be adjusted.

Code

The sample image is loaded via a FileSource state machine as ipImageWork and processed with the following code.

First of all the properties of the mask are defined. As the mask is circular, it is described by the center point aCenter and the radius nRadius. The image center point corrected by the user-adjustable offset aCenterOffset is used as the center point of the circle. The radius is calculated as the distance from the center point to the nearest border of the image, minus the value nCircleShrink.

hr := F_VN_GetImageWidth(ipImageWork, nWidth, hr);
hr := F_VN_GetImageHeight(ipImageWork, nHeight, hr);
aCenter[0] := (nWidth / 2) + aCenterOffset[0];
aCenter[1] := (nHeight / 2) + aCenterOffset[1];
nRadius := (MIN(nWidth, nHeight) / 2) - nCircleShrink;

A new image with the same size as the input image ipImageWork is created as the mask and painted with a white circle on a black background. The circle contains the calculated properties. The value nThickness is selected as the width -1 of the border of the circle so that the circle is filled out.

hr := F_VN_CreateImageAndSetPixels(ipImageMask, nWidth, nHeight, TCVN_ET_USINT, 1, aColorBlack, hr);
hr := F_VN_DrawCircle(
    nCenterX    :=  aCenter[0],
    nCenterY    :=  aCenter[1],
    nRadius     :=  nRadius,
    ipDestImage :=  ipImageMask,
    aColor      :=  aColorWhite,
    nThickness  :=  -1,
    hrPrev      :=  hr
);

The result is that a mask image ipImageMask is now available with the desired circular shape:

Average intensity in shapes of any kind 8:

The average intensity value of the input image ipImageWork in the defined circular area is now calculated with the help of this mask ipImageMask. The function F_VN_ImageAverageExp returns the array aAverage of the type TcVnVector4_LREAL in order to cover the maximum number of image channels. As the input image in this sample only has one channel, the first array element is the desired average value and is saved separately.

hr := F_VN_ImageAverageExp(
    ipSrcImage  :=  ipImageWork,
    aAverage    :=  aAverage,
    ipMask      :=  ipImageMask,
    hrPrev      :=  hr
);
fAverageInMask := aAverage[0];

Finally, the mask circle and the calculated average value are drawn in a result image.

hr := F_VN_ConvertColorSpace(ipImageWork, ipImageRes, TCVN_CST_GRAY_TO_RGB, hr);
hr := F_VN_DrawCircle(aCenter[0], aCenter[1], nRadius, ipImageRes, aColorGreen, 5, hr);
sText := CONCAT('Average: ', LREAL_TO_FMTSTR(fAverageInMask, 2, TRUE));
hr := F_VN_PutTextExp(sText, ipImageRes, 40, 60, TCVN_FT_HERSHEY_SIMPLEX, 2, aColorBlue, 3, TCVN_LT_ANTIALIASED, FALSE, hr);

The PLC library Tc2_Utilities is required for the function LREAL_TO_FMTSTR.