形态学处理
处理类型 操作 作用 开运算 先腐蚀再膨胀 可在纤细点出分离物体。有助于消除噪音 闭运算 先膨胀后腐蚀 用于排除前景对象中的小孔或对象上的小黑点 形态学梯度 膨胀图与腐蚀图之差 用于保留目标物体的边缘轮廓 顶帽 原图与开运算图之差 分离比邻近点亮的斑块,用于突出原图像中比周围亮的区域 黑帽 闭运算图与原图差 分离比邻近点暗的斑块,突出原图像中比周围暗的区域
操作函数
void morphologyEx ( InputArray src, OutputArray dst, int op, InputArray kernel, Point anchor = Point ( - 1 , - 1 ) , int iterations = 1 , int borderType = BORDER_CONSTANT, const Scalar& borderValue = morphologyDefaultBorderValue ( ) ) ;
enum MorphTypes {
MORPH_ERODE = 0 ,
MORPH_DILATE = 1 ,
MORPH_OPEN = 2 ,
MORPH_CLOSE = 3 ,
MORPH_GRADIENT = 4 ,
MORPH_TOPHAT = 5 ,
MORPH_BLACKHAT = 6 ,
MORPH_HITMISS = 7
}
核操作
Mat getStructuringElement ( int shape, Size ksize, Point anchor = Point ( - 1 , - 1 ) ) ;
enum MorphShapes {
MORPH_RECT = 0 ,
MORPH_CROSS = 1 ,
MORPH_ELLIPSE = 2
} ;
综合案例
# include <iostream>
# include <map>
# include <string>
# include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
class Morphology
{
public:
Morphology ( ) : img ( imread ( "20.jpg" ) )
{
result[ "原图" ] = img;
}
void Show ( )
{
for ( auto & v : result)
{
imshow ( v. first, v. second) ;
}
waitKey ( 0 ) ;
}
void TestMorphology ( )
{
Mat kernel = getStructuringElement ( MORPH_RECT, Size ( 5 , 5 ) ) ;
morphologyEx ( img, result[ "腐蚀" ] , MORPH_ERODE, kernel) ;
morphologyEx ( img, result[ "膨胀" ] , MORPH_DILATE, kernel) ;
morphologyEx ( img, result[ "开" ] , MORPH_OPEN, kernel) ;
morphologyEx ( img, result[ "闭" ] , MORPH_CLOSE, kernel) ;
morphologyEx ( img, result[ "梯度" ] , MORPH_GRADIENT, kernel) ;
morphologyEx ( img, result[ "顶帽" ] , MORPH_TOPHAT, kernel) ;
morphologyEx ( img, result[ "黑帽" ] , MORPH_BLACKHAT, kernel) ;
}
private:
Mat img;
map< string, Mat> result;
} ;
int main ( )
{
Morphology* pmorph = new Morphology;
pmorph-> TestMorphology ( ) ;
pmorph-> Show ( ) ;
return 0 ;
}