Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Mat myDFT(const Mat & src)
- {
- Mat result(src.size(), CV_32FC2, Scalar::all(0));
- //calc DFT for each row
- vector<vector<complex<double>>> fourierImageForRows;
- for (int row = 0; row < src.rows; row++)
- {
- vector<complex<double>> rowFourier;
- for (int u = 0; u < src.cols; u++)//u - частотная переменная
- {
- double coeff = -2 * PI * u / src.cols;
- double a = cos(coeff);
- double b = sin(coeff);
- complex<double> W(a, b);
- complex<double> W_0(a, b);
- double real = src.at<float>(row, 0);
- double imag = 0;
- for (int n = 1; n < src.cols; n++)
- {
- double magn = src.at<float>(row, n);
- real += magn * W.real();
- imag += magn * W.imag();
- W *= W_0;//ЗАЧЕМ
- }
- complex<double>res(real, -imag);
- rowFourier.emplace_back(res);
- }
- fourierImageForRows.emplace_back(rowFourier);
- }
- //calc DFT for cols
- vector<vector<complex<double>>> resultFourier;
- for (int col = 0; col < src.cols; col++)
- {
- vector<complex<double>> rowFourier;
- for (int w = 0; w < src.rows; w++)
- {
- double coeff = -2 * PI * w / src.rows;
- double a = cos(coeff);
- double b = sin(coeff);
- complex<double> W(a, b);
- complex<double> W_0(a, b);
- double real = fourierImageForRows[0][col].real() * 1;
- double imag = -fourierImageForRows[0][col].imag();
- for (int n = 1; n < src.rows; n++)
- {
- complex<double> value = fourierImageForRows[n][col];
- complex<double> res = W * conj(value);
- real += res.real();
- imag += res.imag();
- W *= W_0;
- }
- complex<double>res(real, imag);
- rowFourier.emplace_back(res);
- result.at<Vec2f>(w, col) = Vec2f(real, imag);
- }
- }
- return result;//получаем изображение в частотном представлении
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement