Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Inverse Fourier gives black figure

Hi, i try to apply the inverse of fourier Trasform to a Image Sperctrum but it does not work, i end up with a black image.

Mat Lena;
   images[0].copyTo(Lena);

   Mat padded;                            //expand input image to optimal size
      int m = getOptimalDFTSize( Lena.rows );
      int n = getOptimalDFTSize( Lena.cols ); // on the border add zero values
      copyMakeBorder(Lena, padded, 0, m - Lena.rows, 0, n - Lena.cols, BORDER_CONSTANT, Scalar::all(0));

      Mat planes[] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F)};
      Mat complexI;
      merge(planes, 2, complexI);         // Add to the expanded another plane with zeros

      dft(complexI, complexI);            // this way the result may fit in the source matrix
      Mat_<float> magI;

      split(complexI, planes);                   // planes[0] = Re(DFT(I), planes[1] = Im(DFT(I))
      magnitude(planes[0], planes[1], planes[0]);// planes[0] = magnitude
      magI=planes[0];

      //shifting

      dftshift(magI);

      magI(cv::Rect(magI.cols/2 - 25, magI.rows/2 -25, 50, 50)) = 250.0f;

       dftshift(magI);


      Mat_<float> filtered;
      //from polar to car
      //phase(planes[1], planes[0], planes[1], false);
      polarToCart(magI,planes[1],planes[0],planes[1],false);
      //merge

      Mat_<Vec2f> complexI_2;
      merge(planes, 2, complexI_2);
      dft(complexI_2, filtered,  DFT_INVERSE + DFT_SCALE + DFT_REAL_OUTPUT, 0);
      normalize(filtered, filtered, 0, 1, CV_MINMAX);

      namedWindow("Filtered Im", CV_WINDOW_AUTOSIZE );
      imshow("Filtered Im", filtered );

      namedWindow("ImageHisto", CV_WINDOW_AUTOSIZE );
      imshow("ImageHisto", histo(Lena) );

      namedWindow("FilteredImage Histo", CV_WINDOW_AUTOSIZE );
      imshow("FilteredImage Histo", histo(filtered) );


    }

Everything is working until i modify the magnitude. I get the modified spectrum (magI) end then i am suppose to compute the inverse Trasform. I think the problem could be in PolarToCar because it takes as parameter the magnitude and the phase, but i am not sure i actually have the phase, i think i have the imaginary part of the spectrum stored in planes[1]. thanks

Inverse Fourier gives black figure

Hi, i try to apply the inverse of fourier Trasform to a Image Sperctrum but it does not work, i end up with a black image.

Mat Lena;
   images[0].copyTo(Lena);

   Mat padded;                            //expand input image to optimal size
      int m = getOptimalDFTSize( Lena.rows );
      int n = getOptimalDFTSize( Lena.cols ); // on the border add zero values
      copyMakeBorder(Lena, padded, 0, m - Lena.rows, 0, n - Lena.cols, BORDER_CONSTANT, Scalar::all(0));

      Mat planes[] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F)};
      Mat complexI;
      merge(planes, 2, complexI);         // Add to the expanded another plane with zeros

      dft(complexI, complexI);            // this way the result may fit in the source matrix
      Mat_<float> magI;

      split(complexI, planes);                   // planes[0] = Re(DFT(I), planes[1] = Im(DFT(I))
      magnitude(planes[0], planes[1], planes[0]);// planes[0] = magnitude
      magI=planes[0];

      //shifting

      dftshift(magI);

      magI(cv::Rect(magI.cols/2 - 25, magI.rows/2 -25, 50, 50)) = 250.0f;

       dftshift(magI);


      Mat_<float> filtered;
      //from polar to car
      //phase(planes[1], planes[0], planes[1], false);
      polarToCart(magI,planes[1],planes[0],planes[1],false);
      //merge

      Mat_<Vec2f> complexI_2;
      merge(planes, 2, complexI_2);
      dft(complexI_2, filtered,  DFT_INVERSE + DFT_SCALE + DFT_REAL_OUTPUT, 0);
      normalize(filtered, filtered, 0, 1, CV_MINMAX);

      namedWindow("Filtered Im", CV_WINDOW_AUTOSIZE );
      imshow("Filtered Im", filtered );

      namedWindow("ImageHisto", CV_WINDOW_AUTOSIZE );
      imshow("ImageHisto", histo(Lena) );

      namedWindow("FilteredImage Histo", CV_WINDOW_AUTOSIZE );
      imshow("FilteredImage Histo", histo(filtered) );


    }

Everything is working until i modify the magnitude. I get the modified spectrum (magI) end then i am suppose to compute the inverse Trasform. I think the problem could be in PolarToCar because it takes as parameter the magnitude and the phase, but i am not sure i actually have the phase, i think i have the imaginary part of the spectrum stored in planes[1]. thanks

click to hide/show revision 3
retagged

updated 2013-09-24 13:45:00 -0600

berak gravatar image

Inverse Fourier gives black figure

Hi, i try to apply the inverse of fourier Trasform to a Image Sperctrum but it does not work, i end up with a black image.

Mat Lena;
   images[0].copyTo(Lena);

   Mat padded;                            //expand input image to optimal size
      int m = getOptimalDFTSize( Lena.rows );
      int n = getOptimalDFTSize( Lena.cols ); // on the border add zero values
      copyMakeBorder(Lena, padded, 0, m - Lena.rows, 0, n - Lena.cols, BORDER_CONSTANT, Scalar::all(0));

      Mat planes[] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F)};
      Mat complexI;
      merge(planes, 2, complexI);         // Add to the expanded another plane with zeros

      dft(complexI, complexI);            // this way the result may fit in the source matrix
      Mat_<float> magI;

      split(complexI, planes);                   // planes[0] = Re(DFT(I), planes[1] = Im(DFT(I))
      magnitude(planes[0], planes[1], planes[0]);// planes[0] = magnitude
      magI=planes[0];

      //shifting

      dftshift(magI);

      magI(cv::Rect(magI.cols/2 - 25, magI.rows/2 -25, 50, 50)) = 250.0f;

       dftshift(magI);


      Mat_<float> filtered;
      //from polar to car
      //phase(planes[1], planes[0], planes[1], false);
      polarToCart(magI,planes[1],planes[0],planes[1],false);
      //merge

      Mat_<Vec2f> complexI_2;
      merge(planes, 2, complexI_2);
      dft(complexI_2, filtered,  DFT_INVERSE + DFT_SCALE + DFT_REAL_OUTPUT, 0);
      normalize(filtered, filtered, 0, 1, CV_MINMAX);

      namedWindow("Filtered Im", CV_WINDOW_AUTOSIZE );
      imshow("Filtered Im", filtered );

      namedWindow("ImageHisto", CV_WINDOW_AUTOSIZE );
      imshow("ImageHisto", histo(Lena) );

      namedWindow("FilteredImage Histo", CV_WINDOW_AUTOSIZE );
      imshow("FilteredImage Histo", histo(filtered) );


    }

Everything is working until i modify the magnitude. I get the modified spectrum (magI) end then i am suppose to compute the inverse Trasform. I think the problem could be in PolarToCar because it takes as parameter the magnitude and the phase, but i am not sure i actually have the phase, i think i have the imaginary part of the spectrum stored in planes[1]. thanks