diff options
author | Daniil Kazantsev <dkazanc@hotmail.com> | 2018-05-01 12:07:30 +0100 |
---|---|---|
committer | Daniil Kazantsev <dkazanc@hotmail.com> | 2018-05-01 12:07:30 +0100 |
commit | 5ef87da22a31868fd88c7f0ab4c2201e816e92ed (patch) | |
tree | e2f0d8e996dbf78bd3763724a2775ad90c200def /Wrappers/Python/demos | |
parent | c04d2d000abb5c4d98b11f60dbaadaff3e2b3ff8 (diff) | |
download | regularization-5ef87da22a31868fd88c7f0ab4c2201e816e92ed.tar.gz regularization-5ef87da22a31868fd88c7f0ab4c2201e816e92ed.tar.bz2 regularization-5ef87da22a31868fd88c7f0ab4c2201e816e92ed.tar.xz regularization-5ef87da22a31868fd88c7f0ab4c2201e816e92ed.zip |
inpaint demo
Diffstat (limited to 'Wrappers/Python/demos')
-rw-r--r-- | Wrappers/Python/demos/demo_cpu_inpainters.py | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/Wrappers/Python/demos/demo_cpu_inpainters.py b/Wrappers/Python/demos/demo_cpu_inpainters.py new file mode 100644 index 0000000..a022bc8 --- /dev/null +++ b/Wrappers/Python/demos/demo_cpu_inpainters.py @@ -0,0 +1,143 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Demonstration of CPU inpainters +@authors: Daniil Kazantsev, Edoardo Pasca +""" + +import matplotlib.pyplot as plt +import numpy as np +import os +import timeit +from scipy import io +from ccpi.filters.regularisers import NDF_INP +from qualitymetrics import rmse +############################################################################### +def printParametersToString(pars): + txt = r'' + for key, value in pars.items(): + if key== 'algorithm' : + txt += "{0} = {1}".format(key, value.__name__) + elif key == 'input': + txt += "{0} = {1}".format(key, np.shape(value)) + elif key == 'refdata': + txt += "{0} = {1}".format(key, np.shape(value)) + else: + txt += "{0} = {1}".format(key, value) + txt += '\n' + return txt +############################################################################### +#%% +# read sinogram and the mask +filename = os.path.join(".." , ".." , ".." , "data" ,"SinoInpaint.mat") +sino = io.loadmat(filename) +sino_no_cut = sino.get('Sinogram') +Mask = sino.get('Mask') +[angles_dim,detectors_dim] = sino_no_cut.shape +sinogram = sino_no_cut/np.max(sino_no_cut) +#apply mask to sinogram +sino_cut = sinogram*(1-Mask) + +plt.figure(1) +plt.subplot(121) +plt.imshow(sino_cut,vmin=0.0, vmax=1) +plt.title('Missing Data sinogram') +plt.subplot(122) +plt.imshow(Mask) +plt.title('Mask') +plt.show() +#%% +print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%") +print ("___Inpainting using linear diffusion (2D)__") +print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%") + +## plot +fig = plt.figure(2) +plt.suptitle('Performance of linear inpainting using the CPU') +a=fig.add_subplot(1,2,1) +a.set_title('Missing data sinogram') +imgplot = plt.imshow(sino_cut,cmap="gray") + +# set parameters +pars = {'algorithm' : NDF_INP, \ + 'input' : sino_cut,\ + 'maskData' : Mask,\ + 'regularisation_parameter':6000,\ + 'edge_parameter':0.0,\ + 'number_of_iterations' :5000 ,\ + 'time_marching_parameter':0.000075,\ + 'penalty_type':1 + } + +start_time = timeit.default_timer() +ndf_inp_linear = NDF_INP(pars['input'], + pars['maskData'], + pars['regularisation_parameter'], + pars['edge_parameter'], + pars['number_of_iterations'], + pars['time_marching_parameter'], + pars['penalty_type']) + +rms = rmse(sinogram, ndf_inp_linear) +pars['rmse'] = rms + +txtstr = printParametersToString(pars) +txtstr += "%s = %.3fs" % ('elapsed time',timeit.default_timer() - start_time) +print (txtstr) +a=fig.add_subplot(1,2,2) + +# these are matplotlib.patch.Patch properties +props = dict(boxstyle='round', facecolor='wheat', alpha=0.75) +# place a text box in upper left in axes coords +a.text(0.15, 0.25, txtstr, transform=a.transAxes, fontsize=14, + verticalalignment='top', bbox=props) +imgplot = plt.imshow(ndf_inp_linear, cmap="gray") +plt.title('{}'.format('Linear diffusion inpainting results')) +#%% +print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%") +print ("_Inpainting using nonlinear diffusion (2D)_") +print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%") + +## plot +fig = plt.figure(3) +plt.suptitle('Performance of nonlinear diffusion inpainting using the CPU') +a=fig.add_subplot(1,2,1) +a.set_title('Missing data sinogram') +imgplot = plt.imshow(sino_cut,cmap="gray") + +# set parameters +pars = {'algorithm' : NDF_INP, \ + 'input' : sino_cut,\ + 'maskData' : Mask,\ + 'regularisation_parameter':80,\ + 'edge_parameter':0.00009,\ + 'number_of_iterations' :1500 ,\ + 'time_marching_parameter':0.000008,\ + 'penalty_type':1 + } + +start_time = timeit.default_timer() +ndf_inp_nonlinear = NDF_INP(pars['input'], + pars['maskData'], + pars['regularisation_parameter'], + pars['edge_parameter'], + pars['number_of_iterations'], + pars['time_marching_parameter'], + pars['penalty_type']) + +rms = rmse(sinogram, ndf_inp_nonlinear) +pars['rmse'] = rms + +txtstr = printParametersToString(pars) +txtstr += "%s = %.3fs" % ('elapsed time',timeit.default_timer() - start_time) +print (txtstr) +a=fig.add_subplot(1,2,2) + +# these are matplotlib.patch.Patch properties +props = dict(boxstyle='round', facecolor='wheat', alpha=0.75) +# place a text box in upper left in axes coords +a.text(0.15, 0.25, txtstr, transform=a.transAxes, fontsize=14, + verticalalignment='top', bbox=props) +imgplot = plt.imshow(ndf_inp_nonlinear, cmap="gray") +plt.title('{}'.format('Nonlinear diffusion inpainting results')) +#%%
\ No newline at end of file |