Home | History | Annotate | Download | only in python2
      1 #!/usr/bin/env python
      2 
      3 import cv2
      4 import numpy as np
      5 import sys
      6 
      7 
      8 def shift_dft(src, dst=None):
      9     '''
     10         Rearrange the quadrants of Fourier image so that the origin is at
     11         the image center. Swaps quadrant 1 with 3, and 2 with 4.
     12 
     13         src and dst arrays must be equal size & type
     14     '''
     15 
     16     if dst is None:
     17         dst = np.empty(src.shape, src.dtype)
     18     elif src.shape != dst.shape:
     19         raise ValueError("src and dst must have equal sizes")
     20     elif src.dtype != dst.dtype:
     21         raise TypeError("src and dst must have equal types")
     22 
     23     if src is dst:
     24         ret = np.empty(src.shape, src.dtype)
     25     else:
     26         ret = dst
     27 
     28     h, w = src.shape[:2]
     29 
     30     cx1 = cx2 = w/2
     31     cy1 = cy2 = h/2
     32 
     33     # if the size is odd, then adjust the bottom/right quadrants
     34     if w % 2 != 0:
     35         cx2 += 1
     36     if h % 2 != 0:
     37         cy2 += 1
     38 
     39     # swap quadrants
     40 
     41     # swap q1 and q3
     42     ret[h-cy1:, w-cx1:] = src[0:cy1 , 0:cx1 ]   # q1 -> q3
     43     ret[0:cy2 , 0:cx2 ] = src[h-cy2:, w-cx2:]   # q3 -> q1
     44 
     45     # swap q2 and q4
     46     ret[0:cy2 , w-cx2:] = src[h-cy2:, 0:cx2 ]   # q2 -> q4
     47     ret[h-cy1:, 0:cx1 ] = src[0:cy1 , w-cx1:]   # q4 -> q2
     48 
     49     if src is dst:
     50         dst[:,:] = ret
     51 
     52     return dst
     53 
     54 if __name__ == "__main__":
     55 
     56     if len(sys.argv)>1:
     57         im = cv2.imread(sys.argv[1])
     58     else :
     59         im = cv2.imread('../data/baboon.jpg')
     60         print "usage : python dft.py <image_file>"
     61 
     62     # convert to grayscale
     63     im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
     64     h, w = im.shape[:2]
     65 
     66     realInput = im.astype(np.float64)
     67 
     68     # perform an optimally sized dft
     69     dft_M = cv2.getOptimalDFTSize(w)
     70     dft_N = cv2.getOptimalDFTSize(h)
     71 
     72     # copy A to dft_A and pad dft_A with zeros
     73     dft_A = np.zeros((dft_N, dft_M, 2), dtype=np.float64)
     74     dft_A[:h, :w, 0] = realInput
     75 
     76     # no need to pad bottom part of dft_A with zeros because of
     77     # use of nonzeroRows parameter in cv2.dft()
     78     cv2.dft(dft_A, dst=dft_A, nonzeroRows=h)
     79 
     80     cv2.imshow("win", im)
     81 
     82     # Split fourier into real and imaginary parts
     83     image_Re, image_Im = cv2.split(dft_A)
     84 
     85     # Compute the magnitude of the spectrum Mag = sqrt(Re^2 + Im^2)
     86     magnitude = cv2.sqrt(image_Re**2.0 + image_Im**2.0)
     87 
     88     # Compute log(1 + Mag)
     89     log_spectrum = cv2.log(1.0 + magnitude)
     90 
     91     # Rearrange the quadrants of Fourier image so that the origin is at
     92     # the image center
     93     shift_dft(log_spectrum, log_spectrum)
     94 
     95     # normalize and display the results as rgb
     96     cv2.normalize(log_spectrum, log_spectrum, 0.0, 1.0, cv2.NORM_MINMAX)
     97     cv2.imshow("magnitude", log_spectrum)
     98 
     99     cv2.waitKey(0)
    100     cv2.destroyAllWindows()
    101