Morphological filters can also be used to detect specific features in an image. In this recipe, we will learn how to detect contours and corners in a gray-level image.
The edges of an image can be detected by using the appropriate filter of the cv::morphologyEx
function. Refer to the following code:
// Get the gradient image using a 3x3 structuring element cv::Mat result; cv::morphologyEx(image,result, cv::MORPH_GRADIENT,cv::Mat()); // Apply threshold to obtain a binary image int threshold= 40; cv::threshold(result, result, threshold, 255, cv::THRESH_BINARY);
The following image is obtained as the result:
In order to detect corners using morphology, we now define a class named MorphoFeatures
as follows:
class MorphoFeatures { private: // threshold to produce binary image int threshold; // structuring...