一、图像腐蚀和膨胀
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <iostream>
using namespace std;
using namespace cv;
Mat g_srcImage,g_dstImage;
int g_nTrackbarNumber = 0;//0:erode,1:dilate
int g_nStructElementSize = 3;
void process();
void on_TrackbarNumChange(int,void *);
void on_ElementSizeChange(int,void *);
void process()
{
Mat element = getStructuringElement(MORPH_RECT,Size(2*g_nStructElementSize+1,2*g_nStructElementSize+1),Point(g_nStructElementSize,g_nStructElementSize));
if(g_nTrackbarNumber == 0)
erode(g_srcImage,g_dstImage,element);
else
dilate(g_srcImage,g_dstImage,element);
imshow("效果图",g_dstImage);
}
void on_TrackbarNumberChange(int,void *)
{
process();
}
void on_ElementSizeChange(int,void *)
{
process();
}
int main(int argc,char *argv[])
{
system("color 5E");
if(argc!=2)
{
cout<<"the format of input is error"<<endl;
return 0;
}
g_srcImage = imread(argv[1]);
namedWindow("原图");
imshow("原图",g_srcImage);
namedWindow("效果图");
Mat element = getStructuringElement(MORPH_RECT,Size(2*g_nStructElementSize+1,2*g_nStructElementSize+1),Point(g_nStructElementSize,g_nStructElementSize));
erode(g_srcImage,g_dstImage,element);
imshow("效果图",g_dstImage);
createTrackbar("腐蚀/膨胀","效果图",&g_nTrackbarNumber,1,on_TrackbarNumberChange);
createTrackbar("内核尺寸","效果图",&g_nStructElementSize,21,on_ElementSizeChange);
waitKey(0);
return 0;
}
1、原图 2、腐蚀 3、膨胀
腐蚀的作用:减少图像的高亮部分 膨胀的作用:增加图像的高亮部分
二、开运算(先腐蚀后膨胀)
特点:可以消除小物体,在纤细点处分离物体,并且在平滑较大物体的边界的同时不改变其面积。
void open_operation()
{
Mat element = getStructuringElement(MORPH_RECT,Size(2*3+1,2*3+1),Point(3,3));
erode(g_srcImage,g_dstImage,element);
dilate(g_dstImage,g_dstImage,element);
imshow("open",g_dstImage);
imwrite("open.jpg",g_dstImage);
}
(2)、闭运算(先膨胀后腐蚀)
特点:能够排除小型黑洞
void close_operation()
{
Mat element = getStructuringElement(MORPH_RECT,Size(2*3+1,2*3+1),Point(3,3));
dilate(g_srcImage,g_dstImage,element);
erode(g_dstImage,g_dstImage,element);
imshow("close",g_dstImage);
imwrite("close.jpg",g_dstImage);
}
(3)、形态学梯度(膨胀图与腐蚀图作差运算)
特点:对二值图像进行这一操作可以将团块的边缘突出来
void gradient_operation()
{
Mat g_dstImage1,g_dstImage2;
Mat element = getStructuringElement(MORPH_RECT,Size(2*3+1,2*3+1),Point(3,3));
dilate(g_srcImage,g_dstImage,element);
erode(g_srcImage,g_dstImage1,element);
g_dstImage2 = g_dstImage - g_dstImage1;
imshow("gradient",g_dstImage2);
imwrite("gradient.jpg",g_dstImage2);
}
(4)、顶帽运算(原图与开运算结果图的差)
特点:放大裂缝或者局部低亮度的区域,往往用来比邻近点亮的斑块
void Uhat_operation()
{
Mat element = getStructuringElement(MORPH_RECT,Size(2*3+1,2*3+1),Point(3,3));
erode(g_srcImage,g_dstImage,element);
dilate(g_dstImage,g_dstImage,element);
Mat dst = g_srcImage - g_dstImage;
imshow("Uphat",dst);
imwrite("Uphat.jpg",dst);
}
(5)、黑帽运算(闭运算结果图与原图之差)
特点:突出比原图轮廓周围区域更暗的区域,用来分离比邻近点暗一些的斑块
void Bhat_operation()
{
Mat element = getStructuringElement(MORPH_RECT,Size(2*3+1,2*3+1),Point(3,3));
dilate(g_srcImage,g_dstImage,element);
erode(g_dstImage,g_dstImage,element);
Mat dst = g_dstImage - g_srcImage;
imshow("Bhat",dst);
imwrite("Bhat.jpg",dst);
}
其实Opencv已经提供了一个API接口来执行图像的形态学变换。
void morphologyEx(InputArray src,OutputArray dst,int op,InputArray kernel,
Point auchor = Point(-1,-1),intiterations = 1,int borderType = BORDER_CONSTANT,
const Scalar &borderValue = morphologyDefaultBorderValue());
参数一、二:分别是输入输出阵列
参数三:形态学运算的类型:MORPH_OPEN(开运算)、MORPH_CLOSE(闭运算)、MORPH_GRADIENT(形态学梯度)
MORPH_TOPHAT(顶帽)、MORPH_BLACKHAT(黑帽)、MORPH_ERODE(腐蚀)、MORPH_DILATE(膨胀)
参数四:内核大小,若为NULL则默认3*3,一般通过函数getStructuringElement获取内核
参数五:锚的位置,(-1,-1)默认中心点
参数六:迭代使用函数的次数,默认1
参数七:边界类型,有默认值
参数八:有默认值一般不需要管
示例:
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc,char *argv[])
{
Mat g_srcImage = imread(argv[1]);
if(g_srcImage.empty())
{
cout<<"打开图像失败"<<endl;
return -1;
}
namedWindow("原图");
namedWindow("效果图");
imshow("原图",g_srcImage);
Mat element = getStructuringElement(MORPH_RECT,Size(15,15));
morphologyEx(g_srcImage,g_srcImage,MORPH_OPEN,element);
imshow("效果图",g_srcImage);
waitKey(0);
return 0;
}
----------------(内容来自opencv3编程入门 毛星云pdf)