严重性 代码 说明 项目 文件 行 禁止显示状态 错误(活动) E0020 未定义标识符 "M_PI" test7 D:\桌面\数字信号处理实验\test7\test7\test7.cpp 19 错误 C2065 “M_PI”: 未声明的标识符 test7 d:\桌面\数字信号处理实验\test7\test7\test7.cpp 19
时间: 2023-07-08 10:52:12 浏览: 209
抱歉,M_PI是Linux系统下的宏定义,如果在Windows系统下编译会出现未定义标识符的错误。可以手动定义M_PI宏定义来解决这个问题。可以在代码中添加如下代码:
```c++
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
```
这样就可以在Windows系统下使用M_PI了。完整代码如下:
```c++
#include <opencv2/opencv.hpp>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
using namespace cv;
int main() {
// 读取图像
Mat image = imread("image.jpg");
// 定义大气湍流的PSF矩阵
int psf_size = 15;
Mat psf(psf_size, psf_size, CV_32F, Scalar::all(0));
float k = 0.01; // 参数K
float sum = 0;
for (int i = 0; i < psf_size; i++) {
for (int j = 0; j < psf_size; j++) {
float x = i - psf_size / 2;
float y = j - psf_size / 2;
float r = sqrt(x * x + y * y);
psf.at<float>(i, j) = (k / (r * r + k)) / M_PI;
sum += psf.at<float>(i, j);
}
}
psf /= sum; // 归一化
// 对图像做模拟退化
Mat degraded;
filter2D(image, degraded, -1, psf);
// 直接逆滤波复原
Mat restored = Mat::zeros(degraded.size(), degraded.type());
for (int i = 0; i < psf_size; i++) {
for (int j = 0; j < psf_size; j++) {
if (psf.at<float>(i, j) != 0) {
psf.at<float>(i, j) = 1 / psf.at<float>(i, j);
}
}
}
filter2D(degraded, restored, -1, psf);
// 维纳滤波复原
Mat restored2 = Mat::zeros(degraded.size(), degraded.type());
float snr = 1; // 信噪比
float lambda = snr / (snr + 1);
Mat psf2 = psf.clone();
for (int i = 0; i < psf_size; i++) {
for (int j = 0; j < psf_size; j++) {
if (psf.at<float>(i, j) != 0) {
psf2.at<float>(i, j) = psf.at<float>(i, j) / (psf.at<float>(i, j) * psf.at<float>(i, j) + lambda);
}
}
}
filter2D(degraded, restored2, -1, psf2);
// 显示图像
imshow("image", image);
imshow("degraded", degraded);
imshow("restored", restored);
imshow("restored2", restored2);
waitKey(0);
return 0;
}
```
阅读全文