Color similarity with RGB reference color
In this sample, the function F_VN_ReferenceColorSimilarityExp_TcVnVector3_LREAL calculates the similarity of each pixel to 3 reference colors and returns them as grayscale images. The objects in the reference colors are then detected on the basis of these grayscale images.
Variables
hr : HRESULT;
// Images
ipImageIn : ITcVnImage;
ipImageInDisp : ITcVnDisplayableImage;
ipImageThres : ITcVnImage;
ipImageThresDisp : ARRAY [0..2] OF ITcVnDisplayableImage;
ipImageWorkCol : ITcVnImage;
ipImageWorkColDisp : ARRAY [0..2] OF ITcVnDisplayableImage;
ipImageRes : ITcVnImage;
ipImageResDisp : ITcVnDisplayableImage;
// Color
iColor : INT;
aColorTxt : ARRAY[0..2] OF STRING := [ 'RED', 'GREEN', 'BLUE' ];
aColor : ARRAY[0..2] OF TcVnVector4_LREAL := [ [150, 0, 0], [0, 255, 0], [0, 0, 255] ];
aColorRef : ARRAY[0..2] OF TcVnVector3_LREAL := [ [255, 75, 60], [40, 140, 95], [40, 140, 190] ];
// Contours
ipContourList : ITcVnContainer;
ipIterator : ITcVnForwardIterator;
aOffset : TcVnPoint;
ipContour : ITcVnContainer;
fArea : LREAL;
aCenter : TcVnPoint2_LREAL;
Code
// Attention: With other images another color space transformation could be necessary
hr := F_VN_ConvertColorSpace(ipImageIn, ipImageRes, TCVN_CST_Bayer_RG_TO_RGB, hr);
FOR iColor := 0 TO 2 DO
// Compute the Color Similarity to a Reference Color
hr := F_VN_ReferenceColorSimilarityExp_TcVnVector3_LREAL(
ipSrcImage := ipImageRes,
ipDestImage := ipImageWorkCol,
aRefColor := aColorRef[iColor],
fVariance := 0.1,
fLuminanceWeight := 0.2,
hrPrev := hr);
hr := F_VN_Threshold(ipImageWorkCol, ipImageThres, 200, 255, TCVN_TT_Binary, hr);
// Find all objects / contours in the black and white image
hr := F_VN_FindContours(ipImageThres, ipContourList, hr);
hr := F_VN_GetForwardIterator(ipContourList, ipIterator, hr);
// Filter the objects by size and draw the contours
WHILE SUCCEEDED(hr) AND_THEN ipIterator.CheckIfEnd() <> S_OK DO
hr := F_VN_GetContainer(ipIterator, ipContour, hr);
hr := F_VN_IncrementIterator(ipIterator, hr);
// Filter contours by size
hr := F_VN_ContourArea(ipContour, fArea, hr);
IF fArea > 5000 THEN
// Draw Results into an Image
hr := F_VN_DrawContours(ipContour, -1, ipImageRes, aColor[iColor], 3, hr);
hr := F_VN_ContourCenterOfMass(ipContour, aCenter, hr);
hr := F_VN_PutText(aColorTxt[iColor], ipImageRes, LREAL_TO_UDINT(aCenter[0])-30, LREAL_TO_UDINT(aCenter[1])+10, TCVN_FT_HERSHEY_PLAIN, 2, aColor[iColor],hr);
END_IF
END_WHILE
// Display effect of the Color-Similarity-Function
hr := F_VN_TransformIntoDisplayableImage(ipImageThres, ipImageThresDisp[iColor], hr);
hr := F_VN_TransformIntoDisplayableImage(ipImageWorkCol, ipImageWorkColDisp[iColor], hr);
END_FOR
Result
The input image in Bayer RG format
After converting the input image to an RGB image with the function F_VN_ConvertColorSpace, the F_VN_ReferenceColorSimilarityExp_TcVnVector3_LREAL can be used to determine the color similarity of each pixel compared to the reference color. The following applies in the returned result image:
- The pixel value 255 (white) corresponds to a 100% match with the reference color
- The pixel value 0 (black) corresponds to a 0% match with the reference color
- and the pixel values between 0 and 255 a partial correlation to the reference color.
// Compute the Color Similarity to a Reference Color
hr := F_VN_ReferenceColorSimilarityExp_TcVnVector3_LREAL(
ipSrcImage := ipImageRes,
ipDestImage := ipImageWorkCol,
aRefColor := aColorRef[iColor],
fVariance := 0.1,
fLuminanceWeight := 0.2,
hrPrev := hr);
A threshold value (F_VN_Threshold) is used to define how close the match to the reference color should be in order to be assigned to it.
hr := F_VN_Threshold(ipImageWorkCol, ipImageThres, 200, 255, TCVN_TT_Binary, hr);
The result image, after subsequent detection.