burli
January 25, 2022, 6:51pm
1
Hi, I want to filter an image with FFT. I followed this tutorial
#!/usr/bin/env python
__author__ = "Sreenivas Bhattiprolu"
__license__ = "Feel free to copy, I appreciate if you acknowledge Python for Microscopists"
# https://youtu.be/Wka_XhcZAcQ
import cv2
from matplotlib import pyplot as plt
import numpy as np
img = cv2.imread('images/BSE.jpg', 0) # load an image
#Output is a 2D complex array. 1st channel real and 2nd imaginary
#For fft in opencv input image needs to be converted to float32
dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT)
#Rearranges a Fourier transform X by shifting the zero-frequency
#component to the center of the array.
#Otherwise it starts at the tope left corenr of the image (array)
This file has been truncated. show original
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?
Hi @burli
About how to apply a mask on an numpy image, I found this:
As I believe you are trying to filter the image, I suggest exploring the use of gaussians, multiplying the image with a centered gaussian.
maybe the following code worth to try if you still trying to solve your previous question.
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()
burli
January 26, 2022, 5:37am
4
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
burli
January 26, 2022, 6:01am
5
I need to learn more about numpy arrays. Thanks for the link.
be sure you opened your image as gray scale ( second parameter of imread must be cv2.IMREAD_GRAYSCALE)
1 Like
must be cv2.IMREAD_GRAYSCALE
because magic numbers are cryptic
2 Likes
burli
January 26, 2022, 4:05pm
8
Sorry, my fault. I copied your code, but replaced a part with a for loop