Hessian of Gaussian eigenvalues for 3D image with Python

自闭症网瘾萝莉.ら 提交于 2020-06-29 03:38:27

问题


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:

  1. I have to compute the Hessian on all the Voxels which tack time and lot of RAM

  2. Output data seems extremely noisy (it may be because of data type conversion during operations)

  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!