def correlate(self, imgfft):
#Very much related to the convolution theorem, the cross-correlation
#theorem states that the Fourier transform of the cross-correlation of
#two functions is equal to the product of the individual Fourier
#transforms, where one of them has been complex conjugated:
if self.imgfft is not 0 or imgfft.imgfft is not 0:
imgcj = np.conjugate(self.imgfft)
imgft = imgfft.imgfft
prod = deepcopy(imgcj)
for x in range(imgcj.shape[0]):
for y in range(imgcj.shape[0]):
prod[x][y] = imgcj[x][y] * imgft[x][y]
cc = Corr( np.real(fft.ifft2(fft.fftshift(prod)))) # real image of the correlation
# adjust to center
cc.data = np.roll(cc.data, int(cc.data.shape[0] / 2), axis = 0)
cc.data = np.roll(cc.data, int(cc.data.shape[1] / 2), axis = 1)
else:
raise FFTnotInit()
return cc
python类ifft2()的实例源码
def ift(self):
return MyImage(np.real(fft.ifft2(fft.fftshift(self.ft))))
def ift(self):
self.imgifft = MyImage(np.real(fft.ifft2(fft.fftshift(self.imgfft))))
def cross_corr(img1,img2,mask=None):
'''Compute the autocorrelation of two images.
Right now does not take mask into account.
todo: take mask into account (requires extra calculations)
input:
img1: first image
img2: second image
mask: a mask array
output:
the autocorrelation of the two images (same shape as the correlated images)
'''
#if(mask is not None):
# img1 *= mask
# img2 *= mask
#img1_mean = np.mean( img1.flat )
#img2_mean = np.mean( img2.flat )
# imgc = fftshift( ifft2(
# fft2(img1/img1_mean -1.0 )*np.conj(fft2( img2/img2_mean -1.0 ))).real )
#imgc = fftshift( ifft2(
# fft2( img1/img1_mean )*np.conj(fft2( img2/img2_mean ))).real )
imgc = fftshift( ifft2(
fft2( img1 )*np.conj(fft2( img2 ))).real )
#imgc /= (img1.shape[0]*img1.shape[1])**2
if(mask is not None):
maskc = cross_corr(mask,mask)
imgc /= np.maximum( 1, maskc )
return imgc
FaceRecognizer.py 文件源码
项目:Gabor-Filter-Face-Extraction
作者: duycao2506
项目源码
文件源码
阅读 25
收藏 0
点赞 0
评论 0
def extractFeatures(self, img):
"A vector of 2n elements where n is the number of theta angles"
"and 2 is the number of frequencies under consideration"
filters = self.build_filters(img.shape[0],img.shape[1],5,(0.75,1.5),2,1,np.pi/2.0)
fft_filters = [np.fft.fft2(i) for i in filters]
img_fft = np.fft.fft2(img)
a = img_fft * fft_filters
s = [np.fft.ifft2(i) for i in a]
k = [p.real for p in s]
return k
FaceRecognizer.py 文件源码
项目:Gabor-Filter-Face-Extraction
作者: duycao2506
项目源码
文件源码
阅读 19
收藏 0
点赞 0
评论 0
def fft_convolve2d(self, x,y):
""" 2D convolution, using FFT"""
fr = fft2(x)
fr2 = fft2(np.flipud(np.fliplr(y)))
m,n = fr.shape
cc = np.real(ifft2(fr*fr2))
cc = np.roll(cc, -m/2+1,axis=0)
cc = np.roll(cc, -n/2+1,axis=1)
return cc
def xcorr(imageA, imageB):
FimageA = _fft.fft2(imageA)
CFimageB = _np.conj(_fft.fft2(imageB))
return _fft.fftshift(_np.real(_fft.ifft2((FimageA * CFimageB)))) / _np.sqrt(imageA.size)
def ift2(G, dfx, dfy):
Nx = G.shape[1]
Ny = G.shape[0]
return ifftshift(ifft2(ifftshift(G))) * Nx * Ny * dfx * dfy
test_3dgauss_and_3d_plotting.py 文件源码
项目:CoherentXrayImaging
作者: susannahammarberg
项目源码
文件源码
阅读 23
收藏 0
点赞 0
评论 0
def plot_crystal2D():
plt.figure()
plt.subplot(221)
plt.imshow(crystal, cmap='gray')
plt.title('crystal')
plt.axis('off')
plt.subplot(223)
test=crystal_filter2D*crystal_fourier
plt.imshow(np.log10(abs(test)), cmap='gray')
plt.title('log10 |FFT2| of crystal')
plt.axis('off')
plt.subplot(222)
plt.imshow(crystal_skewed, cmap='gray')
plt.xlabel(' x_s')
plt.ylabel(' y_s')
plt.title('crystal in skewed coordinates')
plt.axis('off')
plt.subplot(224)
plt.imshow(np.log10(abs(fft_crystal_skewed)), cmap='gray')
plt.xlabel(' 1/x_s')
plt.ylabel(' 1/y_s')
plt.title('log10|FFT| of crystal in skewed coordinates')
plt.axis('off')
# plot filtered 2D retrived crystal2D
plt.figure
fig,ax = plt.subplots(1)
plt.imshow(abs((fft.ifft2(fft.ifftshift(test)))))
#plot_crystal2D()
#plt.figure()
#plt.imshow(projectedCrystal)
#plt.title('Orthoganl projection of 3D crystal into x-y-plane')
#plt.xlabel(' x')
#plt.ylabel(' y')
#savefig('test.jpg')
########################plot 3D crystal in all plane cuts