目录
腐蚀 | cv::erode() |
膨胀 | cv::dilate() |
开运算 | cv::morphologyEx(in,out,MORPH_OPEN,...) |
闭运算 | cv::morphologyEx(in,out,MORPH_CLOSE,...) |
形态学梯度 | cv::morphologyEx(in, out, MORPH_GRADIENT, ...); |
顶帽 | cv::morphologyEx(in, out, MORPH_TOPHAT, ...) |
黑帽 | cv::morphologyEx(in, out, MORPH_BLACKHAT, ...) |
结构元素 | cv::Mat=cv::getStructuringElement() |
1.腐蚀
2.膨胀
3.开运算-先腐蚀再膨胀
4.闭运算-先膨胀再腐蚀
5.形态学梯度
这是图像膨胀和腐蚀之间的区别,结果看起来像对象的轮廓。
6.顶帽
输入图像和图像开运算之差
![](https://i-blog.csdnimg.cn/blog_migrate/9f089b42e66283e9d4545ac93f0cb76f.png)
7.黑帽
输入图像和图像闭运算之差
代码1:
#include<opencv2/opencv.hpp>
#include<iostream>
#include<imgproc.hpp>
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
Mat image = imread("C:/Users/YY/Pictures/Saved Pictures/A.bmp");
Mat out;
Mat k = (Mat_<double>(3, 3) << 1, 1, 1, 1, 1, 1, 1, 1, 1);
erode(image, out, k);
dilate(image, out, k);
morphologyEx(image, out, MORPH_OPEN, k);
morphologyEx(image, out, MORPH_CLOSE, k);
morphologyEx(image, out, MORPH_GRADIENT, k);
morphologyEx(image, out, MORPH_TOPHAT, k);
morphologyEx(image, out, MORPH_BLACKHAT, k);
imshow("显示", out);
imshow("原图", image);
waitKey(0);
destroyAllWindows();
return 0;
}
8.结构元素
int main(int argc, char** argv) {
Mat out;
//矩形内核
out = getStructuringElement(MORPH_RECT, Size(5, 5));
cout << out << endl;
//椭圆内核
out = getStructuringElement(MORPH_ELLIPSE, Size(5, 5));
cout << out << endl;
//十字内核
out = getStructuringElement(MORPH_CROSS, Size(5, 5));
cout << out << endl;
return 0;
}
矩形
[ 1, 1, 1, 1, 1;
1, 1, 1, 1, 1;
1, 1, 1, 1, 1;
1, 1, 1, 1, 1;
1, 1, 1, 1, 1]
椭圆
[ 0, 0, 1, 0, 0;
1, 1, 1, 1, 1;
1, 1, 1, 1, 1;
1, 1, 1, 1, 1;
0, 0, 1, 0, 0]
十字
[ 0, 0, 1, 0, 0;
0, 0, 1, 0, 0;
1, 1, 1, 1, 1;
0, 0, 1, 0, 0;
0, 0, 1, 0, 0]