二值化图像降噪具体实现代码C
时间: 2023-10-17 22:05:35 浏览: 27
二值化图像的降噪可以采用形态学操作中的开运算,具体实现代码如下:
```c
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat src = imread("lena.jpg", IMREAD_GRAYSCALE); // 读取灰度图像
if (src.empty())
{
cout << "Can't open the image!" << endl;
return -1;
}
// 二值化
Mat binary;
threshold(src, binary, 128, 255, THRESH_BINARY);
// 开运算
Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3));
Mat opened;
morphologyEx(binary, opened, MORPH_OPEN, kernel);
// 显示结果
namedWindow("src", WINDOW_NORMAL);
namedWindow("opened", WINDOW_NORMAL);
imshow("src", src);
imshow("opened", opened);
waitKey(0);
return 0;
}
```
其中,`threshold`函数用于将灰度图像二值化,`getStructuringElement`函数生成一个矩形结构元素,`morphologyEx`函数实现形态学开运算操作,将二值图像中的小噪点去除。最终结果通过`imshow`函数显示出来。