C++编写代码实现“空域平滑”、“空域锐化”、“高斯低通滤波”、“高斯高通滤波”、“巴特沃斯低通滤波”和“巴特沃斯高通滤波”等功能
时间: 2023-12-25 13:04:37 浏览: 174
以下是C++编写代码实现空域平滑、空域锐化、高斯低通滤波、高斯高通滤波、巴特沃斯低通滤波和巴特沃斯高通滤波的功能的例子:
1. 空域平滑(均值滤波)
```cpp
#include <opencv2/opencv.hpp>
using namespace cv;
int main() {
Mat image = imread("input.jpg");
Mat smoothed;
blur(image, smoothed, Size(3, 3));
imshow("Smoothed Image", smoothed);
waitKey(0);
return 0;
}
```
2. 空域锐化(拉普拉斯滤波)
```cpp
#include <opencv2/opencv.hpp>
using namespace cv;
int main() {
Mat image = imread("input.jpg");
Mat sharpened;
Mat blurred;
GaussianBlur(image, blurred, Size(3, 3), 0);
addWeighted(image, 1.5, blurred, -0.5, 0, sharpened);
imshow("Sharpened Image", sharpened);
waitKey(0);
return 0;
}
```
3. 高斯低通滤波
```cpp
#include <opencv2/opencv.hpp>
using namespace cv;
int main() {
Mat image = imread("input.jpg");
Mat filtered;
GaussianBlur(image, filtered, Size(3, 3), 0);
imshow("Filtered Image", filtered);
waitKey(0);
return 0;
}
```
4. 高斯高通滤波
```cpp
#include <opencv2/opencv.hpp>
using namespace cv;
int main() {
Mat image = imread("input.jpg");
Mat filtered;
Mat blurred;
GaussianBlur(image, blurred, Size(3, 3), 0);
subtract(image, blurred, filtered);
imshow("Filtered Image", filtered);
waitKey(0);
return 0;
}
```
5. 巴特沃斯低通滤波
```cpp
#include <opencv2/opencv.hpp>
using namespace cv;
int main() {
Mat image = imread("input.jpg");
Mat filtered;
Mat padded;
int dft_M = getOptimalDFTSize(image.rows);
int dft_N = getOptimalDFTSize(image.cols);
copyMakeBorder(image, padded, 0, dft_M - image.rows, 0, dft_N - image.cols, BORDER_CONSTANT, Scalar::all(0));
Mat planes[] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F)};
Mat complexI;
merge(planes, 2, complexI);
dft(complexI, complexI);
float cutoff_frequency = 30.0;
float order = 2.0;
float d0 = cutoff_frequency * dft_M / 100.0;
for (int i = 0; i < complexI.rows; i++) {
for (int j = 0; j < complexI.cols; j++) {
float d = sqrt(pow(i - complexI.rows / 2, 2) + pow(j - complexI.cols / 2, 2));
float h = 1.0 / (1.0 + pow(d / d0, 2 * order));
complexI.at<Vec2f>(i, j)[0] *= h;
complexI.at<Vec2f>(i, j)[1] *= h;
}
}
idft(complexI, filtered, DFT_SCALE | DFT_REAL_OUTPUT);
filtered = filtered(Rect(0, 0, image.cols, image.rows));
imshow("Filtered Image", filtered);
waitKey(0);
return 0;
}
```
6. 巴特沃斯高通滤波
```cpp
#include <opencv2/opencv.hpp>
using namespace cv;
int main() {
Mat image = imread("input.jpg");
Mat filtered;
Mat padded;
int dft_M = getOptimalDFTSize(image.rows);
int dft_N = getOptimalDFTSize(image.cols);
copyMakeBorder(image, padded, 0, dft_M - image.rows, 0, dft_N - image.cols, BORDER_CONSTANT, Scalar::all(0));
Mat planes[] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F)};
Mat complexI;
merge(planes, 2, complexI);
dft(complexI, complexI);
float cutoff_frequency = 30.0;
float order = 2.0;
float d0 = cutoff_frequency * dft_M / 100.0;
for (int i = 0; i < complexI.rows; i++) {
for (int j = 0; j < complexI.cols; j++) {
float d = sqrt(pow(i - complexI.rows / 2, 2) + pow(j - complexI.cols / 2, 2));
float h = 1.0 / (1.0 + pow(d0 / d, 2 * order));
complexI.at<Vec2f>(i, j)[0] *= h;
complexI.at<Vec2f>(i, j)[1] *= h;
}
}
idft(complexI, filtered, DFT_SCALE | DFT_REAL_OUTPUT);
filtered = filtered(Rect(0, 0, image.cols, image.rows));
imshow("Filtered Image", filtered);
waitKey(0);
return 0;
}
```
阅读全文