python实现泊松图像融合实现泊松图像融合
本文实例为大家分享了python实现泊松图像融合的具体代码,供大家参考,具体内容如下
```
from __future__ import division
import numpy as np
import scipy.fftpack
import scipy.ndimage
import cv2
import matplotlib.pyplot as plt
#sns.set(style="darkgrid")
def DST(x):
"""
Converts Scipy's DST output to Matlab's DST (scaling).
"""
X = scipy.fftpack.dst(x,type=1,axis=0)
return X/2.0
def IDST(X):
"""
Inverse DST. Python -> Matlab
"""
n = X.shape[0] x = np.real(scipy.fftpack.idst(X,type=1,axis=0))
return x/(n+1.0)
def get_grads(im):
"""
return the x and y gradients.
"""
[H,W] = im.shape
Dx,Dy = np.zeros((H,W),'float32'), np.zeros((H,W),'float32')
j,k = np.atleast_2d(np.arange(0,H-1)).T, np.arange(0,W-1)
Dx[j,k] = im[j,k+1] - im[j,k] Dy[j,k] = im[j+1,k] - im[j,k] return Dx,Dy
def get_laplacian(Dx,Dy):
"""
return the laplacian
"""
[H,W] = Dx.shape
Dxx, Dyy = np.zeros((H,W)), np.zeros((H,W))
j,k = np.atleast_2d(np.arange(0,H-1)).T, np.arange(0,W-1)
Dxx[j,k+1] = Dx[j,k+1] - Dx[j,k] Dyy[j+1,k] = Dy[j+1,k] - Dy[j,k] return Dxx+Dyy
def poisson_solve(gx,gy,bnd):
# convert to double:
gx = gx.astype('float32')
gy = gy.astype('float32')
bnd = bnd.astype('float32')
H,W = bnd.shape
L = get_laplacian(gx,gy)
# set the interior of the boundary-image to 0:
bnd[1:-1,1:-1] = 0
# get the boundary laplacian:
L_bp = np.zeros_like(L)
L_bp[1:-1,1:-1] = -4*bnd[1:-1,1:-1] \
+ bnd[1:-1,2:] + bnd[1:-1,0:-2] \
+ bnd[2:,1:-1] + bnd[0:-2,1:-1] # delta-x
L = L - L_bp
L = L[1:-1,1:-1]
# compute the 2D DST:
L_dst = DST(DST(L).T).T #first along columns, then along rows
# normalize:
[xx,yy] = np.meshgrid(np.arange(1,W-1),np.arange(1,H-1))