opencv 小波去噪 C++
时间: 2023-09-03 08:06:11 浏览: 167
下面是一个使用OpenCV和C++进行小波去噪的示例代码:
``` c++
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/photo/photo.hpp>
#include <opencv2/ml/ml.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/video/video.hpp>
#include <opencv2/imgcodecs/imgcodecs.hpp>
#include <opencv2/videoio/videoio.hpp>
#include <opencv2/core/utils/logger.hpp>
#include <opencv2/core/types.hpp>
#include <opencv2/core/mat.hpp>
#include <opencv2/core/types_c.h>
#include <opencv2/core/core_c.h>
#include <opencv2/core/cvdef.h>
#include <opencv2/core/hal/interface.h>
#include <opencv2/core/hal/hal.hpp>
#include <opencv2/core/ocl.hpp>
#include <opencv2/core/persistence.hpp>
#include <opencv2/core/version.hpp>
#include <opencv2/core/cuda.hpp>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
// 加载图像
Mat img = imread("lena.jpg", IMREAD_GRAYSCALE);
// 进行小波变换
Mat dst;
pyrDown(img, dst);
pyrUp(dst, dst, img.size());
Mat coeffs;
pyrDown(dst, coeffs);
pyrUp(coeffs, coeffs, dst.size());
coeffs = img - coeffs;
// 对小波系数进行阈值处理,设定阈值为sigma*sqrt(2*log2(n))
double sigma = 1.5;
double T = sigma * sqrt(2 * log2(img.size().area()));
Mat coeffs_thresh = coeffs.clone();
for (int i = 0; i < coeffs.rows; i++)
{
for (int j = 0; j < coeffs.cols; j++)
{
if (coeffs.at<uchar>(i, j) < T)
{
coeffs_thresh.at<uchar>(i, j) = 0;
}
}
}
// 进行小波逆变换
pyrUp(coeffs_thresh, coeffs_thresh, dst.size());
pyrDown(coeffs_thresh, coeffs_thresh);
pyrUp(coeffs_thresh, coeffs_thresh, img.size());
Mat img_denoised = img - coeffs_thresh;
// 显示原图和去噪后的图像
imshow("Original", img);
imshow("Denoised", img_denoised);
waitKey(0);
destroyAllWindows();
return 0;
}
```
在这个例子中,我们使用了pyrDown和pyrUp函数对图像进行小波分解和逆变换,阈值使用了sigma*sqrt(2*log2(n))的公式进行计算,其中sigma为一个参数,可以根据具体情况进行调整。
阅读全文