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 Statistical image features sample 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.

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 |
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 aCenter
center point and the nRadius
radius. The image center point corrected by the user-adjustable aCenterOffset
offset 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 nCircleShrink
value.
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 ipImageWork
input image is created as the mask and painted with a white circle on a black background. The circle contains the calculated properties. The nThickness
value is selected as the -1
width 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 := TO_UDINT(aCenter[0]),
nCenterY := TO_UDINT(aCenter[1]),
nRadius := nRadius,
ipDestImage := ipImageMask,
aColor := aColorWhite,
nThickness := -1,
hrPrev := hr);
The result is that a ipImageMask
mask image is now available with the desired circular shape:

The average intensity value of the ipImageWork
input image in the defined circular area is now calculated with the help of this ipImageMask
mask. The F_VN_ImageAverageExp function returns the aAverage
array 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(TO_UDINT(aCenter[0]), TO_UDINT(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 Tc2_Utilities PLC library is required for the LREAL_TO_FMTSTR
function.