diff options
author | Daniil Kazantsev <dkazanc@hotmail.com> | 2018-12-02 19:01:42 +0000 |
---|---|---|
committer | Daniil Kazantsev <dkazanc@hotmail.com> | 2018-12-02 19:01:42 +0000 |
commit | a48c9e69e941ec4046aca9d5d6ec453b9e9debdc (patch) | |
tree | f62cbc2b1d51aff9aaff14e1675f932f1922dde8 /Wrappers/Python/src | |
parent | d252fcf6889855bb276cf6f9bf516e61910c064f (diff) | |
download | regularization-a48c9e69e941ec4046aca9d5d6ec453b9e9debdc.tar.gz regularization-a48c9e69e941ec4046aca9d5d6ec453b9e9debdc.tar.bz2 regularization-a48c9e69e941ec4046aca9d5d6ec453b9e9debdc.tar.xz regularization-a48c9e69e941ec4046aca9d5d6ec453b9e9debdc.zip |
cythonised nltv and updated demo, readme, bash run added
Diffstat (limited to 'Wrappers/Python/src')
-rw-r--r-- | Wrappers/Python/src/cpu_regularisers.pyx | 80 |
1 files changed, 66 insertions, 14 deletions
diff --git a/Wrappers/Python/src/cpu_regularisers.pyx b/Wrappers/Python/src/cpu_regularisers.pyx index b056bba..e51e6d8 100644 --- a/Wrappers/Python/src/cpu_regularisers.pyx +++ b/Wrappers/Python/src/cpu_regularisers.pyx @@ -27,10 +27,9 @@ cdef extern float Diffusion_CPU_main(float *Input, float *Output, float lambdaPa cdef extern float Diffus4th_CPU_main(float *Input, float *Output, float lambdaPar, float sigmaPar, int iterationsNumb, float tau, int dimX, int dimY, int dimZ); cdef extern float TNV_CPU_main(float *Input, float *u, float lambdaPar, int maxIter, float tol, int dimX, int dimY, int dimZ); cdef extern float dTV_FGP_CPU_main(float *Input, float *InputRef, float *Output, float lambdaPar, int iterationsNumb, float epsil, float eta, int methodTV, int nonneg, int printM, int dimX, int dimY, int dimZ); -cdef extern float PatchSelect_CPU_main(float *Input, unsigned short *H_i, unsigned short *H_j, unsigned short *H_k, float *Weights, long dimX, long dimY, long dimZ, int SearchWindow, int SimilarWin, int NumNeighb, float h); +cdef extern float PatchSelect_CPU_main(float *Input, unsigned short *H_i, unsigned short *H_j, unsigned short *H_k, float *Weights, int dimX, int dimY, int dimZ, int SearchWindow, int SimilarWin, int NumNeighb, float h, int switchM); cdef extern float Nonlocal_TV_CPU_main(float *A_orig, float *Output, unsigned short *H_i, unsigned short *H_j, unsigned short *H_k, float *Weights, int dimX, int dimY, int dimZ, int NumNeighb, float lambdaReg, int IterNumb); - cdef extern float Diffusion_Inpaint_CPU_main(float *Input, unsigned char *Mask, float *Output, float lambdaPar, float sigmaPar, int iterationsNumb, float tau, int penaltytype, int dimX, int dimY, int dimZ); cdef extern float NonlocalMarching_Inpaint_main(float *Input, unsigned char *M, float *Output, unsigned char *M_upd, int SW_increment, int iterationsNumb, int trigger, int dimX, int dimY, int dimZ); cdef extern float TV_energy2D(float *U, float *U0, float *E_val, float lambdaPar, int type, int dimX, int dimY); @@ -450,7 +449,6 @@ def Diff4th_3D(np.ndarray[np.float32_t, ndim=3, mode="c"] inputData, return outputData - #****************************************************************# #***************Patch-based weights calculation******************# #****************************************************************# @@ -458,31 +456,85 @@ def PATCHSEL_CPU(inputData, searchwindow, patchwindow, neighbours, edge_paramete if inputData.ndim == 2: return PatchSel_2D(inputData, searchwindow, patchwindow, neighbours, edge_parameter) elif inputData.ndim == 3: - return 1 -# PatchSel_3D(inputData, searchwindow, patchwindow, neighbours, edge_parameter) -def PatchSel_2D(np.ndarray[np.float32_t, ndim=2, mode="c"] inputData, + return PatchSel_3D(inputData, searchwindow, patchwindow, neighbours, edge_parameter) +def PatchSel_2D(np.ndarray[np.float32_t, ndim=2, mode="c"] inputData, int searchwindow, int patchwindow, int neighbours, float edge_parameter): cdef long dims[3] - dims[0] = inputData.shape[0] - dims[1] = inputData.shape[1] - dims[2] = neighbours + dims[0] = neighbours + dims[1] = inputData.shape[0] + dims[2] = inputData.shape[1] + cdef np.ndarray[np.float32_t, ndim=3, mode="c"] Weights = \ - np.zeros([dims[0],dims[1],dims[2]], dtype='float32') + np.zeros([dims[0], dims[1],dims[2]], dtype='float32') cdef np.ndarray[np.uint16_t, ndim=3, mode="c"] H_i = \ - np.zeros([dims[0],dims[1],dims[2]], dtype='uint16 ') + np.zeros([dims[0], dims[1],dims[2]], dtype='uint16') cdef np.ndarray[np.uint16_t, ndim=3, mode="c"] H_j = \ - np.zeros([dims[0],dims[1],dims[2]], dtype='uint16 ') + np.zeros([dims[0], dims[1],dims[2]], dtype='uint16') # Run patch-based weight selection function - PatchSelect_CPU_main(&inputData[0,0], &H_i[0,0,0], &H_j[0,0,0], &H_i[0,0,0], &Weights[0,0,0], dims[1], dims[0], 1, searchwindow, patchwindow, neighbours, edge_parameter) + PatchSelect_CPU_main(&inputData[0,0], &H_j[0,0,0], &H_i[0,0,0], &H_i[0,0,0], &Weights[0,0,0], dims[2], dims[1], 0, searchwindow, patchwindow, neighbours, edge_parameter, 1) return H_i, H_j, Weights - + +def PatchSel_3D(np.ndarray[np.float32_t, ndim=3, mode="c"] inputData, + int searchwindow, + int patchwindow, + int neighbours, + float edge_parameter): + cdef long dims[4] + dims[0] = inputData.shape[0] + dims[1] = inputData.shape[1] + dims[2] = inputData.shape[2] + dims[3] = neighbours + + cdef np.ndarray[np.float32_t, ndim=4, mode="c"] Weights = \ + np.zeros([dims[3],dims[0],dims[1],dims[2]], dtype='float32') + + cdef np.ndarray[np.uint16_t, ndim=4, mode="c"] H_i = \ + np.zeros([dims[3],dims[0],dims[1],dims[2]], dtype='uint16') + + cdef np.ndarray[np.uint16_t, ndim=4, mode="c"] H_j = \ + np.zeros([dims[3],dims[0],dims[1],dims[2]], dtype='uint16') + + cdef np.ndarray[np.uint16_t, ndim=4, mode="c"] H_k = \ + np.zeros([dims[3],dims[0],dims[1],dims[2]], dtype='uint16') + + # Run patch-based weight selection function + PatchSelect_CPU_main(&inputData[0,0,0], &H_i[0,0,0,0], &H_j[0,0,0,0], &H_k[0,0,0,0], &Weights[0,0,0,0], dims[2], dims[1], dims[0], searchwindow, patchwindow, neighbours, edge_parameter, 1) + return H_i, H_j, H_k, Weights + + +#****************************************************************# +#***************Non-local Total Variation******************# +#****************************************************************# +def NLTV_CPU(inputData, H_i, H_j, H_k, Weights, regularisation_parameter, iterations): + if inputData.ndim == 2: + return NLTV_2D(inputData, H_i, H_j, Weights, regularisation_parameter, iterations) + elif inputData.ndim == 3: + return 1 +def NLTV_2D(np.ndarray[np.float32_t, ndim=2, mode="c"] inputData, + np.ndarray[np.uint16_t, ndim=3, mode="c"] H_i, + np.ndarray[np.uint16_t, ndim=3, mode="c"] H_j, + np.ndarray[np.float32_t, ndim=3, mode="c"] Weights, + float regularisation_parameter, + int iterations): + + cdef long dims[2] + dims[0] = inputData.shape[0] + dims[1] = inputData.shape[1] + neighbours = H_i.shape[0] + + cdef np.ndarray[np.float32_t, ndim=2, mode="c"] outputData = \ + np.zeros([dims[0],dims[1]], dtype='float32') + + # Run nonlocal TV regularisation + Nonlocal_TV_CPU_main(&inputData[0,0], &outputData[0,0], &H_i[0,0,0], &H_j[0,0,0], &H_i[0,0,0], &Weights[0,0,0], dims[1], dims[0], 0, neighbours, regularisation_parameter, iterations) + return outputData #*********************Inpainting WITH****************************# #***************Nonlinear (Isotropic) Diffusion******************# |