简体   繁体   English

OpenCV/Python:fftimage 上的掩码 - 为什么我们需要两个通道?

[英]OpenCV/Python: Mask on fftimage - Why do we need two channels?

I'm using mask to cut some frequencies from fft transformed image.我正在使用掩码从 fft 转换图像中剪切一些频率。

My code is:我的代码是:

img = cv2.imread('messi.jpg',0)
rows, cols = img.shape
crow, ccol = rows/2 , cols/2     # center
dft = cv2.dft(np.float32(img),flags = cv2.DFT_COMPLEX_OUTPUT)
dft_shift = np.fft.fftshift(dft)

And my mask is:我的面具是:

# create a mask first, center square is 0, remaining all ones
mask = np.ones((rows, cols, 2), np.uint8)
mask[crow-30:crow+30, ccol-30:ccol+30] = 0

Then I apply the mask to the fourier transformed image:然后我将蒙版应用于傅立叶变换后的图像:

fshift = dft_shift*mask

I tried to plot the mask, but I got a dimension error and I have to create a new one using the code below in order to print it.我试图绘制遮罩,但出现尺寸错误,我必须使用下面的代码创建一个新的遮罩才能打印它。

printMask = np.ones(img.shape, np.uint8)
printMask[crow-30:crow+30, ccol-30:ccol+30] = 0

My question is why do we have to use (rows, cols, 2) instead of (rows, cols) in a mask.我的问题是为什么我们必须在掩码中使用(rows, cols, 2)而不是(rows, cols) Why do we need these two channels?为什么我们需要这两个渠道?

Generally, images have either 1 channel (grayscale) or 3 channels (RGB).通常,图像具有 1 个通道(灰度)或 3 个通道(RGB)。 So masks applied to them should have the same amount of channels.因此,应用于它们的蒙版应该具有相同数量的通道。

In your case, you are applying the mask to the result of the Fourier transform.在您的情况下,您将蒙版应用于傅立叶变换的结果。 The Fourier transform is a complex-valued function of frequency.傅立叶变换是频率的复值函数。 The two channels returned are the real and imaginary part of the transform, respectively.返回的两个通道分别是变换的实部部。 If you are applying a mask over that you would then need two channels.如果您在上面应用蒙版,则需要两个通道。

You can see how cv2.dft works here你可以在这里看到cv2.dft是如何工作的

Cheers!干杯!

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

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