简体   繁体   中英

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

I'm using mask to cut some frequencies from fft transformed image.

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. Why do we need these two channels?

Generally, images have either 1 channel (grayscale) or 3 channels (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

Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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