I wrote code using c++ and opencv 3.4.3 to apply HighPass Gaussian filter using dft, but in result i got something like this(looks like there are 2 images, original and inverted original image) (https://i.imgur.com/jtIwxq1.png) i used this algoritm:Firstly i calculate DFT, then applying mask on dft and did inverse transform. Here my code, wwhats wrong with it?
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
void takeDFT(Mat& input, Mat& output)
{
Mat originalComplex[2] = { input, Mat::zeros(input.size(), CV_32F) };
Mat dftReady;
merge(originalComplex, 2, dftReady);
Mat dftIMG;
dft(dftReady, dftIMG, DFT_COMPLEX_OUTPUT);
output = dftIMG;
}
void createGaussian(Size& size, Mat& output, int uX, int uY, float sigmaX, float sigmaY)
{
Mat temp = Mat(size, CV_32F);
for (int r = 0; r < size.height; r++)
{
for (int c = 0; c < size.width; c++)
{
float x = ((c - uX)*((float)c - uX)) / (2.0f*sigmaX*sigmaX);
float y = ((r - uY)*((float)r - uY)) / (2.0f*sigmaY*sigmaY);
float value = 1 - exp(-(x + y));
temp.at<float>(r, c) = value;
}
}
normalize(temp, temp, 0.0f, 1.0f, NORM_MINMAX);
output = temp;
//imshow("temp", temp);
}
void invertDFT(Mat& input, Mat& output)
{
Mat inverse;
Mat applyGaussian;
Mat originalComplex[2] = { input, Mat::zeros(input.size(), CV_32F) };
split(input, originalComplex);
Mat outputGauss;
Size size = Size(input.cols, input.rows);
createGaussian(size, outputGauss, input.cols/2, input.rows/2, 10, 10);
for (int r = 0; r < originalComplex[1].rows; r++)
{
for (int c = 0; c < originalComplex[1].cols; c++)
{
originalComplex[0].at<float>(r, c) = originalComplex[0].at<float>(r, c)*outputGauss.at<float>(r, c);
}
}
normalize(originalComplex[0], originalComplex[0], 0.0f, 1.0f, NORM_MINMAX);
merge(originalComplex, 2, applyGaussian);
dft(applyGaussian, inverse, DFT_INVERSE | DFT_REAL_OUTPUT | DFT_SCALE);
output = inverse;
}
int main()
{
Mat original = imread("amumu.jpg", CV_LOAD_IMAGE_GRAYSCALE);
imshow("Original", original);
Mat originalFloat;
original.convertTo(originalFloat, CV_32FC1, 1.0 / 255.0);
Mat dftOriginal;
takeDFT(originalFloat, dftOriginal);
Mat invertedDFT;
invertDFT(dftOriginal, invertedDFT);
imshow("Inverted Image", invertedDFT);
waitKey();
}
Comments
Post a Comment