Find Contour - Approximation Method
This sample deals with the possible approximation modes (eApproximationMethod of type ETcVnContourApproximationMethod) for contour search.
Explanation
The functions F_VN_FindContours, F_VN_FindContoursExp and F_VN_FindContourByHierarchy are used to detect contours in the image. The contours are described by points. Depending on the selected approximation mode (eApproximationMethod of type ETcVnContourApproximationMethod) all points of each contour or a simplified point set are returned:
- TCVN_CAM_NONE
All points of the contour are returned - TCVN_CAM_SIMPLE
Loss-free compression is applied - TCVN_CAM_TC89_L1 oder TCVN_CAM_TC89_KCOS
Both functions apply one of the variants of the Teh-Chin chain approximation algorithm
Variables
hr : HRESULT;
ipImageIn : ITcVnImage;
ipImageInDisp : ITcVnDisplayableImage;
ipImageRes : ITcVnImage;
ipImageResDisp : ITcVnDisplayableImage;
// Sample Specific Variables
ipContourList : ITcVnContainer;
ipContour : ITcVnContainer;
ipIterator : ITcVnForwardIterator;
eRetrievalMode : ETcVnContourRetrievalMode := TCVN_CRM_LIST;
eApproximationMethod : ETcVnContourApproximationMethod := TCVN_CAM_SIMPLE;
aOffset : TcVnPoint;
// Colors
aColorRed : TcVnVector4_LREAL := [255, 0, 0];
Code
// Create Result Image
hr := F_VN_ConvertColorSpace(ipImageIn, ipImageRes, TCVN_CST_Gray_TO_RGB, hr);
// Find Contours and their Hierarchy
// -----------------------------------
hr := F_VN_FindContoursExp(
ipSrcImage := ipImageIn,
ipContours := ipContourList,
eRetrievalMode := eRetrievalMode,
eApproximationMethod := eApproximationMethod,
aOffset := aOffset,
hrPrev := hr);
// Draw the points of the contour
hr := F_VN_GetForwardIterator(ipContourList, ipIterator, hr);
WHILE SUCCEEDED(hr) AND_THEN ipIterator.CheckIfEnd() <> S_OK DO
hr := F_VN_GetContainer(ipIterator, ipContour, hr);
hr := F_VN_IncrementIterator(ipIterator, hr);
hr := F_VN_DrawPoints(ipContour, ipImageRes, ETcVnDrawShape.TCVN_DS_CIRCLE, aColorRed, hr);
END_WHILE
Results
The input image
The result image with all points drawn according to ETcVnContourApproximationMethod.TCVN_CAM_NONE
The result image with all points drawn according to ETcVnContourApproximationMethod.TCVN_CAM_TC89_L1