1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| void butterworthLowpassFilter(const Mat& srcImage, Mat& dstImage, const double cutoffFrequency, const int order) { int channels = srcImage.channels(); vector<Mat> srcImage_planes(channels); vector<Mat> dstImage_planes(channels); split(srcImage, srcImage_planes); for (int c = 0; c < srcImage.channels(); c++) { int rows = srcImage_planes[c].rows; int cols = srcImage_planes[c].cols; int centerRow = rows / 2; int centerCol = cols / 2;
Mat dft_planes[] = { Mat_<float>(srcImage_planes[c]), Mat::zeros(srcImage_planes[c].size(), CV_32F) };
Mat dft_input; merge(dft_planes, 2, dft_input);
Mat dft_output; dft(dft_input, dft_output);
split(dft_output, dft_planes);
Mat frequency_spectrum; magnitude(dft_planes[0], dft_planes[1], frequency_spectrum);
frequency_spectrum += Scalar::all(1); log(frequency_spectrum, frequency_spectrum); normalize(frequency_spectrum, frequency_spectrum, 0, 1, NORM_MINMAX);
centerMat(frequency_spectrum); centerMat(dft_planes[0]); centerMat(dft_planes[1]);
for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { double distance = std::sqrt(std::pow(i - centerRow, 2) + std::pow(j - centerCol, 2));
double transferFunction = 1 / (1 + std::pow(distance / cutoffFrequency, 2 * order));
dft_planes[0].at<float>(i, j) *= transferFunction; dft_planes[1].at<float>(i, j) *= transferFunction; } }
Mat idft_input; merge(dft_planes, 2, idft_input);
Mat idft_output; idft(idft_input, idft_output);
Mat idft_planes[] = { Mat::zeros(dft_output.size(), CV_32F), Mat::zeros(dft_output.size(), CV_32F) }; split(idft_output, idft_planes);
magnitude(idft_planes[0], idft_planes[1], dstImage_planes[c]); normalize(dstImage_planes[c], dstImage_planes[c], 0, 1, NORM_MINMAX); } merge(dstImage_planes, dstImage);
}
|