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

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

我正在使用蒙版从 fft 变换图像中剪切一些频率。

我的代码是:

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)

我的面具是:

# 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

然后我将蒙版应用于傅里叶变换图像:

fshift = dft_shift*mask

我尝试绘制遮罩,但出现尺寸错误,我必须使用下面的代码创建一个新的遮罩才能打印它。

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

我的问题是为什么我们必须在掩码中使用 (rows, cols, 2) 而不是 (rows, cols)。为什么我们需要这两个渠道?

通常,图像有 1 个通道(灰度)或 3 个通道(RGB)。所以应用于它们的蒙版应该具有相同数量的通道。

在您的例子中,您将蒙版应用于傅里叶变换的结果。傅里叶变换是频率的复值函数。返回的两个通道分别是变换的 realimaginary 部分。如果您在上面应用遮罩,则需要两个通道。

您可以看到 cv2.dft 的工作原理 here

干杯!