test_dft.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/env python
  2. '''
  3. Test for disctrete fourier transform (dft)
  4. '''
  5. # Python 2/3 compatibility
  6. from __future__ import print_function
  7. import cv2 as cv
  8. import numpy as np
  9. import sys
  10. from tests_common import NewOpenCVTests
  11. class dft_test(NewOpenCVTests):
  12. def test_dft(self):
  13. img = self.get_sample('samples/data/rubberwhale1.png', 0)
  14. eps = 0.001
  15. #test direct transform
  16. refDft = np.fft.fft2(img)
  17. refDftShift = np.fft.fftshift(refDft)
  18. refMagnitide = np.log(1.0 + np.abs(refDftShift))
  19. testDft = cv.dft(np.float32(img),flags = cv.DFT_COMPLEX_OUTPUT)
  20. testDftShift = np.fft.fftshift(testDft)
  21. testMagnitude = np.log(1.0 + cv.magnitude(testDftShift[:,:,0], testDftShift[:,:,1]))
  22. refMagnitide = cv.normalize(refMagnitide, 0.0, 1.0, cv.NORM_MINMAX)
  23. testMagnitude = cv.normalize(testMagnitude, 0.0, 1.0, cv.NORM_MINMAX)
  24. self.assertLess(cv.norm(refMagnitide - testMagnitude), eps)
  25. #test inverse transform
  26. img_back = np.fft.ifft2(refDft)
  27. img_back = np.abs(img_back)
  28. img_backTest = cv.idft(testDft)
  29. img_backTest = cv.magnitude(img_backTest[:,:,0], img_backTest[:,:,1])
  30. img_backTest = cv.normalize(img_backTest, 0.0, 1.0, cv.NORM_MINMAX)
  31. img_back = cv.normalize(img_back, 0.0, 1.0, cv.NORM_MINMAX)
  32. self.assertLess(cv.norm(img_back - img_backTest), eps)
  33. if __name__ == '__main__':
  34. NewOpenCVTests.bootstrap()