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:

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

Find Contour - Approximation Method 1:

 

The result image with all points drawn according to ETcVnContourApproximationMethod.TCVN_CAM_NONE

Find Contour - Approximation Method 2:

 

The result image with all points drawn according to ETcVnContourApproximationMethod.TCVN_CAM_TC89_L1

Find Contour - Approximation Method 3:

Similar samples