简体   繁体   English

OpenCV / Python:颜色栏(以英尺为单位)

[英]OpenCV/Python: Colorbar in fft magnitude

I'm using opencv in python 2.7. 我在python 2.7中使用opencv。

  1. The colormap is oversized. 颜色图过大。 How to shrink it to has the same length as the image? 如何缩小到与图像相同的长度?
  2. How do I explain the value/range of magnitude? 我如何解释数值/幅度范围?

This is my 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() 

在此处输入图片说明

I found the solution. 我找到了解决方案。

To resize the colorbar I use fraction parameter and it's corresponding value in colorbar . 为了调整colorbar大小,我使用了fraction参数及其在colorbar的对应值。 Thanks to bejota's answer . 感谢bejota的回答

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. 使用各种图像尝试以下代码。

This is my final 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() 

And this is the result: 结果如下: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM