This commit is contained in:
2026-03-24 20:16:28 +08:00
parent 275a44cfc8
commit a2c29f4925

34
Morphology/erode.cpp Normal file
View File

@@ -0,0 +1,34 @@
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main() {
// 1. 读取图像 (0 表示以灰度模式读取)
Mat src = imread("../img/1.png", IMREAD_GRAYSCALE);
if (src.empty()) {
cout << "无法加载图像,请检查文件路径!" << endl;
return -1;
}
// 2. 定义结构元素 (Kernel)
// 使用 getStructuringElement 创建一个 5x5 的矩形结构元素
Mat kernel = getStructuringElement(MORPH_RECT, Size(5, 5));
// 3. 执行腐蚀操作
Mat eroded_image;
// 参数:输入, 输出, 结构元素, 锚点(默认-1,-1), 迭代次数
erode(src, eroded_image, kernel, Point(-1, -1), 1);
// 4. 显示结果
imshow("Original Image", src);
imshow("Eroded Image", eroded_image);
// 等待按键后关闭窗口
waitKey(0);
destroyAllWindows();
return 0;
}