/Qt_OpenCV/Mac/Taller_5/taller_5 (Felipe Navarro's conflicted copy 2012-05-30).cpp

https://bitbucket.org/ecivision/eci_3d_vision_project · C++ · 111 lines · 69 code · 21 blank · 21 comment · 3 complexity · 440e9c91723bc1d0fce1cdbbee50f393 MD5 · raw file

  1. #include <stdio.h>
  2. #include <fftw3.h>
  3. #include <fftw.h>
  4. #include <math.h>
  5. #include <complex>
  6. #include "taller_5.h"
  7. #include "ui_taller_5.h"
  8. #include <opencv.hpp>
  9. Taller_5::Taller_5(QWidget *parent) :
  10. QMainWindow(parent),
  11. ui(new Ui::Taller_5)
  12. {
  13. ui->setupUi(this);
  14. }
  15. Taller_5::~Taller_5()
  16. {
  17. delete ui;
  18. }
  19. void Taller_5::on_push_Gaussian_clicked()
  20. {
  21. IplImage *img1 = 0;
  22. IplImage *img2 = 0;
  23. uchar *img1_data;
  24. uchar *img2_data;
  25. fftw_complex *data_in;
  26. fftw_complex *fft;
  27. fftw_complex *ifft;
  28. fftw_plan plan_f;
  29. fftw_plan plan_b;
  30. int width, height, step;
  31. int i, j, k;
  32. /* load original image */
  33. img1 = cvLoadImage( "hola.jpg", CV_LOAD_IMAGE_GRAYSCALE );
  34. /* create new image for IFFT result */
  35. img2 = cvCreateImage( cvSize( img1->width, img1->height ),IPL_DEPTH_8U, 1 );
  36. /* get image properties */
  37. width = img1->width;
  38. height = img1->height;
  39. step = img1->widthStep;
  40. img1_data = ( uchar* ) img1->imageData;
  41. img2_data = ( uchar* ) img2->imageData;
  42. /*initialize arrays for fftw operations */
  43. //data_in = ( fftw_complex* )fftw_malloc( sizeof( fftw_complex ) * width * height );
  44. fft = ( fftw_complex* )fftw_malloc( sizeof( fftw_complex ) * (width * height));
  45. ifft = ( fftw_complex* )fftw_malloc( sizeof( fftw_complex ) * width * height );
  46. /* create plans */
  47. plan_f = fftw_plan_dft_1d( width * height, data_in, fft, FFTW_FORWARD, FFTW_ESTIMATE );
  48. plan_b = fftw_plan_dft_1d( width * height,fft, ifft, FFTW_BACKWARD, FFTW_ESTIMATE );
  49. plan_f = fftw_plan_dft_r2c_2d(width, height, img1_data , fft, FFTW_ESTIMATE);
  50. plan_b = fftw_plan_dft_c2r_2d(width, height, fft, img2_data, FFTW_ESTIMATE);
  51. /* load img1's data to fftw input */
  52. /*for( i = 0, k = 0 ; i < height ; i++ ) {
  53. for( j = 0 ; j < width ; j++ ) {
  54. data_in[k][0] = ( double )img1_data[i * step + j];
  55. data_in[k][1] = 0.0;
  56. k++;
  57. }
  58. }*
  59. /* perform FFT */
  60. fftw_execute( plan_f );
  61. /* perform IFFT */
  62. fftw_execute( plan_b );
  63. /* normalize IFFT result */
  64. for( i = 0 ; i < ( width * height ) ; i++ ) {
  65. ifft[i][0] /= ( double )( width * height );
  66. }
  67. /* copy IFFT result to img2's data */
  68. for( i = 0, k = 0 ; i < height ; i++ ) {
  69. for( j = 0 ; j < width ; j++ ) {
  70. img2_data[i * step + j] = ( uchar )ifft[k++][0];
  71. }
  72. }
  73. /* display images */
  74. cvNamedWindow( "original_image", CV_WINDOW_AUTOSIZE );
  75. cvNamedWindow( "IFFT", CV_WINDOW_AUTOSIZE );
  76. cvShowImage( "original_image", img1 );
  77. cvShowImage( "IFFT", img2 );
  78. cvWaitKey( 0 );
  79. /* free memory */
  80. cvDestroyWindow( "original_image" );
  81. cvDestroyWindow( "IFFT" );
  82. cvReleaseImage( &img1 );
  83. cvReleaseImage( &img2 );
  84. fftw_destroy_plan( plan_f );
  85. fftw_destroy_plan( plan_b );
  86. fftw_free( data_in );
  87. fftw_free( fft );
  88. fftw_free( ifft );
  89. }