问题
I have a 3D image and I want to calculate the Hessian of Gaussian eigenvalues for this image. I would like to have the three eigenvalues of the Hessian approximation for each voxel. This feature seems to be something very common in image processing.
Is there an existing implementation of this feature (like scipy.ndimage.laplace for laplacian calculation)? And is there one that parallels calculations?
I tried to do it manually, through numpy operations but its not optimal because:
I have to compute the Hessian on all the Voxels which tack time and lot of RAM
Output data seems extremely noisy (it may be because of data type conversion during operations)
the sum of eigenvalues is not equal to the laplacian.
Ideally I'm just looking for an existing implementation, I put this code as an example of the last statement.
import numpy as np
import scipy.ndimage as sn
import h5py
import time
def hessian_eigenvalues(x,Mask):
H=hessian(x)
t2=time.time()
print(" Calculate feature: Hessian eigenvalues")
eigen=np.linalg.eigvals(H[Mask])
print(" Feature calculated ---time: ",time.time()-t2)
del H
return eigen
def hessian(x):
t2=time.time()
print(" Calculate feature: Hessian ")
x_grad = np.gradient(x)
hessian = np.empty(x.shape + (x.ndim, x.ndim) , dtype=x.dtype)
for k, grad_k in enumerate(x_grad):
# iterate over dimensions
# apply gradient again to every component of the first derivative.
tmp_grad = np.gradient(grad_k)
for l, grad_kl in enumerate(tmp_grad):
hessian[ :, :,:,k, l] = grad_kl
print(" Feature calculated ---time: ",time.time()-t2)
return hessian
t2=time.time()
print(" Loading special feature: raw datas ")
raw=h5py.File(r"Datas\9003\Variables\raw.h5", 'r')
Base_data=np.asarray(raw['exported_data'][:,:,:],dtype=np.uint16)
raw.close
print(" Feature loaded ---time: ",time.time()-t2)
t2=time.time()
print(" Loading special feature: masque")
m = h5py.File(r"Datas\9003\Masques\9003_masque.h5", 'r')
Mask=np.nonzero(m["exported_data"][:,:,:])
m.close
print(" Feature loaded ---time: ",time.time()-t2)
eig = hessian_eigenvalues(Base_data,Mask)
来源:https://stackoverflow.com/questions/62581722/hessian-of-gaussian-eigenvalues-for-3d-image-with-python