OpenCV/Python: Colorbar in fft magnitude
<p>I'm using opencv in python 2.7.</p>

<ol>
<li>The colormap is oversized. How to shrink it to has the same length as the image?</li>
<li>How do I explain the value/range of magnitude?</li>
</ol>

<p>This is my code:</p>

<pre><code>import numpy as np
import cv2
from matplotlib import pyplot as plt

img = cv2.imread('messi.jpg',0)

dft = cv2.dft(np.float32(img),flags = cv2.DFT_COMPLEX_OUTPUT)
dft_shift = np.fft.fftshift(dft)

magnitude_spectrum = np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1]))

plt.subplot(131),plt.imshow(img, cmap = 'gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(132),plt.imshow(magnitude_spectrum, cmap = 'gray'), plt.colorbar(cmap = 'gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.show() 
</code></pre>

<p><a href="http://i.stack.imgur.com/iNEYT.png" rel="nofollow"><img src="http://i.stack.imgur.com/iNEYT.png" alt="enter image description here"></a></p>

<p>I found the solution.</p>

<p>To resize the <code>colorbar</code> I use <code>fraction</code> parameter and it's corresponding value in <code>colorbar</code>. Thanks to <a href="http://stackoverflow.com/questions/18195758/set-matplotlib-colorbar-size-to-match-graph/26720422#26720422">bejota's answer</a>.</p>

<p>Regarding the magnitude value, I found that as brighter are the vertices in the magnitude image the greater the contrast in brightness of the original grayscale image. Try the code below using various images.</p>

<p>This is my final code:</p>

<pre><code>import numpy as np
import cv2
from matplotlib import pyplot as plt

img = cv2.imread('messi.jpg',0)

dft = cv2.dft(np.float32(img),flags = cv2.DFT_COMPLEX_OUTPUT)
dft_shift = np.fft.fftshift(dft)

magnitude_spectrum = np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1]))


plt.subplot(121),plt.imshow(img, cmap = 'gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(magnitude_spectrum, cmap = 'gray'), plt.colorbar(cmap = 'gray',fraction=0.03, pad=0.04)
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.show() 
</code></pre>

<p>And this is the result:
<a href="http://i.stack.imgur.com/N1uXk.png" rel="nofollow"><img src="http://i.stack.imgur.com/N1uXk.png" alt="enter image description here"></a></p>

python opencv fft magnitude