Ask Your Question

how do I transcribe this code from python to c++?

asked 2015-02-03 06:04:21 -0500

updated 2015-02-03 10:16:32 -0500

Hello. I was looking how to segmenting license plate characters and I found this post where propose to use homomorphic filter. I am trying to transcribe the python code shown in the post but I don't know how to transcribe this part:

# Create Gaussian mask of sigma = 10
M = 2*rows + 1
N = 2*cols + 1
sigma = 10
(X,Y) = np.meshgrid(np.linspace(0,N-1,N), np.linspace(0,M-1,M))
centerX = np.ceil(N/2)
centerY = np.ceil(M/2)
gaussianNumerator = (X - centerX)**2 + (Y - centerY)**2

# Low pass and high pass filters
Hlow = np.exp(-gaussianNumerator / (2*sigma*sigma))
Hhigh = 1 - Hlow

# Move origin of filters so that it's at the top left corner to
# match with the input image
HlowShift = scipy.fftpack.ifftshift(Hlow.copy())
HhighShift = scipy.fftpack.ifftshift(Hhigh.copy())

# Filter the image and crop
If = scipy.fftpack.fft2(imgLog.copy(), (M,N))
Ioutlow = scipy.real(scipy.fftpack.ifft2(If.copy() * HlowShift, (M,N)))
Iouthigh = scipy.real(scipy.fftpack.ifft2(If.copy() * HhighShift, (M,N)))

I found how to create a meshgrid in opencv in this post and I have already implemented it but I don't have any idea how to translate this to OpenCV/c++:

gaussianNumerator = (X - centerX)**2 + (Y - centerY)**2

Would you please give me some ideas ? thank you very much.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2015-02-03 06:53:03 -0500

thdrksdfthmn gravatar image

updated 2015-02-04 06:49:07 -0500

As you can see here, in python, ** is the pow operator, so just do

gaussianNumerator = (X - centerX)*(X - centerX) + (Y - centerY)*(Y - centerY);

Please do not change the question, because my answer will not be relative to it anymore. Just edit it by adding more questions, for eg...

I do not thing that there is some fuction that is similar to scipy.fftpack.ifftshift in OpenCV, it is from scipy, but based on its docs, you can implement your own

edit flag offensive delete link more


thank you, but what I really want to know is how to translate that to Opencv c++. what type should be gaussianNumerator ? Mat type ? PD: sorry for my poor english.

Carlos_Hinojosa gravatar imageCarlos_Hinojosa ( 2015-02-03 07:10:43 -0500 )edit

np.linspace returns vector, np.meshgrid(vec1, vec2) returns 2 matrix and centerX and centerY are floats, so it seems that X, Y are cv::Mat of type CV_32FC1, but gaussianNumerator, I am not really sure, it could be just a float or double...or vector. It depends on the sizes of X and Y. Anyway, have you started to implement the thing? I think you can find out the answer by finding the equivalent functions in C++ and they must tell you the type of gaussianNumerator .

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-02-03 07:33:44 -0500 )edit

thank you. I figured out the type of gaussian Numerator, it's CV_32FC1 as you mentioned. also I used Mat::mul operation instead of * and I got the same result as in python.

Carlos_Hinojosa gravatar imageCarlos_Hinojosa ( 2015-02-03 07:41:32 -0500 )edit

;) So it was a cv::Mat of type CV_32FC1...

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-02-03 08:39:03 -0500 )edit

Question Tools

1 follower


Asked: 2015-02-03 06:04:21 -0500

Seen: 1,460 times

Last updated: Feb 04 '15