opencv去除图像的正弦噪声,c++代码
时间: 2023-11-04 19:10:53 浏览: 45
以下是使用opencv去除图像正弦噪声的c代码示例:
```c
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
Mat img = imread("noisy_image.jpg", IMREAD_GRAYSCALE); // 读入带有正弦噪声的灰度图像
Mat smoothed;
GaussianBlur(img, smoothed, Size(5, 5), 0); // 高斯平滑去除噪声
Mat filtered;
medianBlur(smoothed, filtered, 5); // 中值滤波进一步去除噪声
imshow("Original Image", img);
imshow("Filtered Image", filtered);
waitKey(0);
return 0;
}
```
在这个示例中,我们使用了opencv中的高斯平滑和中值滤波函数来去除图像的正弦噪声。首先,我们读入带有噪声的灰度图像,然后使用高斯平滑函数对图像进行平滑处理。接着,我们再使用中值滤波函数进一步去除噪声。最后,我们将原始图像和处理后的图像显示在屏幕上。
相关问题
opencv去除图像正弦噪声的c++代码
以下是使用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;
}
```
该代码首先读取一张图像,并生成正弦噪声。然后将正弦噪声添加到图像中,并使用中值滤波去除噪声。最后,显示带噪声的图像和去噪后的图像。
opencv通过傅里叶变换去除图像正弦噪声的c++代码
以下是使用OpenCV进行傅里叶变换去除图像正弦噪声的C ++代码示例:
```c++
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
Mat img = imread("lena.jpg", IMREAD_GRAYSCALE); // 读取灰度图像
if (img.empty())
{
cout << "Failed to load image" << endl;
return -1;
}
// 添加正弦噪声
int amplitude = 50;
float frequency = 0.1;
for (int i = 0; i < img.rows; i++)
{
for (int j = 0; j < img.cols; j++)
{
img.at<uchar>(i, j) += amplitude * sin(frequency * j);
}
}
// 显示原始图像
imshow("Original Image", img);
// 执行傅里叶变换
Mat planes[] = { Mat_<float>(img), Mat::zeros(img.size(), CV_32F) };
Mat complexImg;
merge(planes, 2, complexImg);
dft(complexImg, complexImg);
// 去除正弦噪声
for (int i = 0; i < complexImg.rows; i++)
{
for (int j = 0; j < complexImg.cols; j++)
{
float real = complexImg.at<Vec2f>(i, j)[0];
float imag = complexImg.at<Vec2f>(i, j)[1];
// 计算频率
float freq = sqrt(i * i + j * j);
// 如果频率在噪声频率附近,则将该频率的幅度设置为0
if (freq > frequency * img.cols - amplitude && freq < frequency * img.cols + amplitude)
{
complexImg.at<Vec2f>(i, j)[0] = 0;
complexImg.at<Vec2f>(i, j)[1] = 0;
}
}
}
// 执行傅里叶逆变换
Mat filteredImg;
idft(complexImg, filteredImg, DFT_SCALE | DFT_REAL_OUTPUT);
// 显示去噪后的图像
imshow("Filtered Image", filteredImg);
waitKey(0);
return 0;
}
```
在这个示例中,我们首先加载一张灰度图像,并添加了一个正弦噪声。然后,我们执行傅里叶变换来将图像从空间域转换到频率域。接下来,我们遍历频率域中的每个像素,并检查其频率是否在噪声频率附近。如果是,则将该频率的幅度设置为零,以去除噪声。最后,我们执行傅里叶逆变换以将图像从频率域转换回空间域,并显示去噪后的图像。