# How to convert an image to FFT mask?

**URL:** <https://forum.opencv.org/t/how-to-convert-an-image-to-fft-mask/7280>\
**Category:** Python\
**Created:** [January 25, 2022, 6:51pm UTC](https://forum.opencv.org/t/how-to-convert-an-image-to-fft-mask/7280 "2022-01-25T18:51:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![burli](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/burli/32/4178_2.png) [@burli](https://forum.opencv.org/u/burli)\
**Post date:** [January 25, 2022, 6:51pm UTC](https://forum.opencv.org/t/how-to-convert-an-image-to-fft-mask/7280/1 "2022-01-25T18:51:23Z")

</div>

Hi, I want to filter an image with FFT. I followed this tutorial

> <https://github.com/bnsreenu/python_for_microscopists/blob/master/106_image_filters_using_fourier_transform_DFT.py>

It works so far, but it uses a simple circular mask in the center. I want to use an image as mask. The image is grayscale and has only black and white.

How can I convert the image so that it works as a mask?

---

<div class="post-metadata">

**Author:** ![Alejandro\_Silvestri](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/alejandro_silvestri/32/357_2.png) [@Alejandro\_Silvestri](https://forum.opencv.org/u/Alejandro_Silvestri)\
**Post date:** [January 25, 2022, 9:38pm UTC](https://forum.opencv.org/t/how-to-convert-an-image-to-fft-mask/7280/2 "2022-01-25T21:38:28Z")

</div>

Hi @burli

About how to apply a mask on an numpy image, I found this:

> <https://stackoverflow.com/questions/2169605/use-numpy-to-mask-an-image-with-a-pattern>

As I believe you are trying to filter the image, I suggest exploring the use of gaussians, multiplying the image with a centered gaussian.

---

<div class="post-metadata">

**Author:** ![sturkmen](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/sturkmen/32/13_2.png) [@sturkmen](https://forum.opencv.org/u/sturkmen)\
**Post date:** [January 25, 2022, 11:01pm UTC](https://forum.opencv.org/t/how-to-convert-an-image-to-fft-mask/7280/3 "2022-01-25T23:01:19Z")

</div>

maybe the following code worth to try if you still trying to solve your previous question.

```auto
import cv2
import numpy as np

img = cv2.imread('d:/test/pt4.png', cv2.IMREAD_GRAYSCALE) # load an image as GRAYSCALE

ed = cv2.ximgproc.createEdgeDrawing()
ed.detectEdges(img)
grad_img = np.float32(img)

grad_img = ed.getGradientImage()

cv2.imshow("",grad_img*255)
cv2.waitKey()

```

![a](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/7/7955cc78c1aa7ef87c5613baef88e628faa754e7.png)

---

<div class="post-metadata">

**Author:** ![burli](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/burli/32/4178_2.png) [@burli](https://forum.opencv.org/u/burli)\
**Post date:** [January 26, 2022, 5:37am UTC](https://forum.opencv.org/t/how-to-convert-an-image-to-fft-mask/7280/4 "2022-01-26T05:37:35Z")

</div>

Sadly doesn’t work. Get the error

ed.detectEdges  
cvw.error: Unknown C++ exception from OpenCV code

I have OpenCV 4.5.5.62 and Python 3.9

---

<div class="post-metadata">

**Author:** ![burli](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/burli/32/4178_2.png) [@burli](https://forum.opencv.org/u/burli)\
**Post date:** [January 26, 2022, 6:01am UTC](https://forum.opencv.org/t/how-to-convert-an-image-to-fft-mask/7280/5 "2022-01-26T06:01:15Z")

</div>

I need to learn more about numpy arrays. Thanks for the link.

---

<div class="post-metadata">

**Author:** ![sturkmen](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/sturkmen/32/13_2.png) [@sturkmen](https://forum.opencv.org/u/sturkmen)\
**Post date:** [January 26, 2022, 7:38am UTC](https://forum.opencv.org/t/how-to-convert-an-image-to-fft-mask/7280/6 "2022-01-26T07:38:08Z")

</div>

> [@sturkmen](#):
>
> `img = cv2.imread('d:/test/pt4.png', cv2.IMREAD_GRAYSCALE) # load an image`

be sure you opened your image as gray scale ( second parameter of imread must be cv2.IMREAD\_GRAYSCALE)

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [January 26, 2022, 7:55am UTC](https://forum.opencv.org/t/how-to-convert-an-image-to-fft-mask/7280/7 "2022-01-26T07:55:18Z")

</div>

must be `cv2.IMREAD_GRAYSCALE` because magic numbers are cryptic

---

<div class="post-metadata">

**Author:** ![burli](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/burli/32/4178_2.png) [@burli](https://forum.opencv.org/u/burli)\
**Post date:** [January 26, 2022, 4:05pm UTC](https://forum.opencv.org/t/how-to-convert-an-image-to-fft-mask/7280/8 "2022-01-26T16:05:39Z")

</div>

Sorry, my fault. I copied your code, but replaced a part with a for loop
