opencv去除图像正弦噪声的c++代码
时间: 2023-11-06 17:59:29 浏览: 58
以下是使用opencv去除图像正弦噪声的c++代码:
```
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat image = imread("image.jpg", IMREAD_GRAYSCALE); //读取图像并转为灰度图像
if (image.empty())
{
cout << "Could not open or find the image" << endl;
return -1;
}
Mat noise = Mat::zeros(image.rows, image.cols, CV_8U); //生成正弦噪声
float freq = 20; //正弦噪声的频率
float ampl = 30; //正弦噪声的振幅
for (int i = 0; i < noise.rows; i++)
{
for (int j = 0; j < noise.cols; j++)
{
noise.at<uchar>(i, j) = ampl * sin(freq * i) + ampl * sin(freq * j);
}
}
Mat noisyImage = image + noise; //图像添加噪声
imshow("Noisy Image", noisyImage); //显示带噪声的图像
Mat denoisedImage;
medianBlur(noisyImage, denoisedImage, 5); //中值滤波去除噪声
imshow("Denoised Image", denoisedImage); //显示去噪后的图像
waitKey(0); //等待按键
return 0;
}
```
该代码首先读取一张图像,并生成正弦噪声。然后将正弦噪声添加到图像中,并使用中值滤波去除噪声。最后,显示带噪声的图像和去噪后的图像。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)