c++ opencv 腐蚀膨胀
时间: 2023-12-06 15:38:40 浏览: 103
C++与opencv自己实现腐蚀和膨胀函数.zip
以下是C++与OpenCV实现二值形态学和灰度形态学的腐蚀膨胀处理的方法:
1. 二值形态学的腐蚀处理
```c++
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
Mat src = imread("binary_image.png", IMREAD_GRAYSCALE);
Mat dst;
Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3));
erode(src, dst, kernel);
imshow("Eroded Image", dst);
waitKey(0);
return 0;
}
```
2. 二值形态学的膨胀处理
```c++
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
Mat src = imread("binary_image.png", IMREAD_GRAYSCALE);
Mat dst;
Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3));
dilate(src, dst, kernel);
imshow("Dilated Image", dst);
waitKey(0);
return 0;
}
```
3. 灰度形态学的腐蚀处理
```c++
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
Mat src = imread("gray_image.png", IMREAD_GRAYSCALE);
Mat dst;
Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3));
erode(src, dst, kernel);
imshow("Eroded Image", dst);
waitKey(0);
return 0;
}
```
4. 灰度形态学的膨胀处理
```c++
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
Mat src = imread("gray_image.png", IMREAD_GRAYSCALE);
Mat dst;
Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3));
dilate(src, dst, kernel);
imshow("Dilated Image", dst);
waitKey(0);
return 0;
}
```
阅读全文