diff options
Diffstat (limited to 'src/Python/ccpi')
-rw-r--r-- | src/Python/ccpi/__init__.py | 0 | ||||
-rw-r--r-- | src/Python/ccpi/filters/__init__.py | 0 | ||||
-rw-r--r-- | src/Python/ccpi/filters/regularisers.py | 214 | ||||
-rw-r--r-- | src/Python/ccpi/supp/__init__.py | 0 | ||||
-rw-r--r-- | src/Python/ccpi/supp/qualitymetrics.py | 65 |
5 files changed, 279 insertions, 0 deletions
diff --git a/src/Python/ccpi/__init__.py b/src/Python/ccpi/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/Python/ccpi/__init__.py diff --git a/src/Python/ccpi/filters/__init__.py b/src/Python/ccpi/filters/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/Python/ccpi/filters/__init__.py diff --git a/src/Python/ccpi/filters/regularisers.py b/src/Python/ccpi/filters/regularisers.py new file mode 100644 index 0000000..588ea32 --- /dev/null +++ b/src/Python/ccpi/filters/regularisers.py @@ -0,0 +1,214 @@ +""" +script which assigns a proper device core function based on a flag ('cpu' or 'gpu') +""" + +from ccpi.filters.cpu_regularisers import TV_ROF_CPU, TV_FGP_CPU, TV_SB_CPU, dTV_FGP_CPU, TNV_CPU, NDF_CPU, Diff4th_CPU, TGV_CPU, LLT_ROF_CPU, PATCHSEL_CPU, NLTV_CPU +try: + from ccpi.filters.gpu_regularisers import TV_ROF_GPU, TV_FGP_GPU, TV_SB_GPU, dTV_FGP_GPU, NDF_GPU, Diff4th_GPU, TGV_GPU, LLT_ROF_GPU, PATCHSEL_GPU + gpu_enabled = True +except ImportError: + gpu_enabled = False +from ccpi.filters.cpu_regularisers import NDF_INPAINT_CPU, NVM_INPAINT_CPU + +def ROF_TV(inputData, regularisation_parameter, iterations, + time_marching_parameter,device='cpu'): + if device == 'cpu': + return TV_ROF_CPU(inputData, + regularisation_parameter, + iterations, + time_marching_parameter) + elif device == 'gpu' and gpu_enabled: + return TV_ROF_GPU(inputData, + regularisation_parameter, + iterations, + time_marching_parameter) + else: + if not gpu_enabled and device == 'gpu': + raise ValueError ('GPU is not available') + raise ValueError('Unknown device {0}. Expecting gpu or cpu'\ + .format(device)) + +def FGP_TV(inputData, regularisation_parameter,iterations, + tolerance_param, methodTV, nonneg, printM, device='cpu'): + if device == 'cpu': + return TV_FGP_CPU(inputData, + regularisation_parameter, + iterations, + tolerance_param, + methodTV, + nonneg, + printM) + elif device == 'gpu' and gpu_enabled: + return TV_FGP_GPU(inputData, + regularisation_parameter, + iterations, + tolerance_param, + methodTV, + nonneg, + printM) + else: + if not gpu_enabled and device == 'gpu': + raise ValueError ('GPU is not available') + raise ValueError('Unknown device {0}. Expecting gpu or cpu'\ + .format(device)) +def SB_TV(inputData, regularisation_parameter, iterations, + tolerance_param, methodTV, printM, device='cpu'): + if device == 'cpu': + return TV_SB_CPU(inputData, + regularisation_parameter, + iterations, + tolerance_param, + methodTV, + printM) + elif device == 'gpu' and gpu_enabled: + return TV_SB_GPU(inputData, + regularisation_parameter, + iterations, + tolerance_param, + methodTV, + printM) + else: + if not gpu_enabled and device == 'gpu': + raise ValueError ('GPU is not available') + raise ValueError('Unknown device {0}. Expecting gpu or cpu'\ + .format(device)) +def FGP_dTV(inputData, refdata, regularisation_parameter, iterations, + tolerance_param, eta_const, methodTV, nonneg, printM, device='cpu'): + if device == 'cpu': + return dTV_FGP_CPU(inputData, + refdata, + regularisation_parameter, + iterations, + tolerance_param, + eta_const, + methodTV, + nonneg, + printM) + elif device == 'gpu' and gpu_enabled: + return dTV_FGP_GPU(inputData, + refdata, + regularisation_parameter, + iterations, + tolerance_param, + eta_const, + methodTV, + nonneg, + printM) + else: + if not gpu_enabled and device == 'gpu': + raise ValueError ('GPU is not available') + raise ValueError('Unknown device {0}. Expecting gpu or cpu'\ + .format(device)) +def TNV(inputData, regularisation_parameter, iterations, tolerance_param): + return TNV_CPU(inputData, + regularisation_parameter, + iterations, + tolerance_param) +def NDF(inputData, regularisation_parameter, edge_parameter, iterations, + time_marching_parameter, penalty_type, device='cpu'): + if device == 'cpu': + return NDF_CPU(inputData, + regularisation_parameter, + edge_parameter, + iterations, + time_marching_parameter, + penalty_type) + elif device == 'gpu' and gpu_enabled: + return NDF_GPU(inputData, + regularisation_parameter, + edge_parameter, + iterations, + time_marching_parameter, + penalty_type) + else: + if not gpu_enabled and device == 'gpu': + raise ValueError ('GPU is not available') + raise ValueError('Unknown device {0}. Expecting gpu or cpu'\ + .format(device)) +def Diff4th(inputData, regularisation_parameter, edge_parameter, iterations, + time_marching_parameter, device='cpu'): + if device == 'cpu': + return Diff4th_CPU(inputData, + regularisation_parameter, + edge_parameter, + iterations, + time_marching_parameter) + elif device == 'gpu' and gpu_enabled: + return Diff4th_GPU(inputData, + regularisation_parameter, + edge_parameter, + iterations, + time_marching_parameter) + else: + if not gpu_enabled and device == 'gpu': + raise ValueError ('GPU is not available') + raise ValueError('Unknown device {0}. Expecting gpu or cpu'\ + .format(device)) + +def PatchSelect(inputData, searchwindow, patchwindow, neighbours, edge_parameter, device='cpu'): + if device == 'cpu': + return PATCHSEL_CPU(inputData, + searchwindow, + patchwindow, + neighbours, + edge_parameter) + elif device == 'gpu' and gpu_enabled: + return PATCHSEL_GPU(inputData, + searchwindow, + patchwindow, + neighbours, + edge_parameter) + else: + if not gpu_enabled and device == 'gpu': + raise ValueError ('GPU is not available') + raise ValueError('Unknown device {0}. Expecting gpu or cpu'\ + .format(device)) + +def NLTV(inputData, H_i, H_j, H_k, Weights, regularisation_parameter, iterations): + return NLTV_CPU(inputData, + H_i, + H_j, + H_k, + Weights, + regularisation_parameter, + iterations) + +def TGV(inputData, regularisation_parameter, alpha1, alpha0, iterations, + LipshitzConst, device='cpu'): + if device == 'cpu': + return TGV_CPU(inputData, + regularisation_parameter, + alpha1, + alpha0, + iterations, + LipshitzConst) + elif device == 'gpu' and gpu_enabled: + return TGV_GPU(inputData, + regularisation_parameter, + alpha1, + alpha0, + iterations, + LipshitzConst) + else: + if not gpu_enabled and device == 'gpu': + raise ValueError ('GPU is not available') + raise ValueError('Unknown device {0}. Expecting gpu or cpu'\ + .format(device)) +def LLT_ROF(inputData, regularisation_parameterROF, regularisation_parameterLLT, iterations, + time_marching_parameter, device='cpu'): + if device == 'cpu': + return LLT_ROF_CPU(inputData, regularisation_parameterROF, regularisation_parameterLLT, iterations, time_marching_parameter) + elif device == 'gpu' and gpu_enabled: + return LLT_ROF_GPU(inputData, regularisation_parameterROF, regularisation_parameterLLT, iterations, time_marching_parameter) + else: + if not gpu_enabled and device == 'gpu': + raise ValueError ('GPU is not available') + raise ValueError('Unknown device {0}. Expecting gpu or cpu'\ + .format(device)) +def NDF_INP(inputData, maskData, regularisation_parameter, edge_parameter, iterations, + time_marching_parameter, penalty_type): + return NDF_INPAINT_CPU(inputData, maskData, regularisation_parameter, + edge_parameter, iterations, time_marching_parameter, penalty_type) + +def NVM_INP(inputData, maskData, SW_increment, iterations): + return NVM_INPAINT_CPU(inputData, maskData, SW_increment, iterations) diff --git a/src/Python/ccpi/supp/__init__.py b/src/Python/ccpi/supp/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/Python/ccpi/supp/__init__.py diff --git a/src/Python/ccpi/supp/qualitymetrics.py b/src/Python/ccpi/supp/qualitymetrics.py new file mode 100644 index 0000000..f44d832 --- /dev/null +++ b/src/Python/ccpi/supp/qualitymetrics.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- +""" +A class for some standard image quality metrics +""" +import numpy as np + +class QualityTools: + def __init__(self, im1, im2): + if im1.size != im2.size: + print ('Error: Sizes of images/volumes are different') + raise SystemExit + self.im1 = im1 # image or volume - 1 + self.im2 = im2 # image or volume - 2 + def nrmse(self): + """ Normalised Root Mean Square Error """ + rmse = np.sqrt(np.sum((self.im2 - self.im1) ** 2) / float(self.im1.size)) + max_val = max(np.max(self.im1), np.max(self.im2)) + min_val = min(np.min(self.im1), np.min(self.im2)) + return 1 - (rmse / (max_val - min_val)) + def rmse(self): + """ Root Mean Square Error """ + rmse = np.sqrt(np.sum((self.im1 - self.im2) ** 2) / float(self.im1.size)) + return rmse + def ssim(self, window, k=(0.01, 0.03), l=255): + from scipy.signal import fftconvolve + """See https://ece.uwaterloo.ca/~z70wang/research/ssim/""" + # Check if the window is smaller than the images. + for a, b in zip(window.shape, self.im1.shape): + if a > b: + return None, None + # Values in k must be positive according to the base implementation. + for ki in k: + if ki < 0: + return None, None + + c1 = (k[0] * l) ** 2 + c2 = (k[1] * l) ** 2 + window = window/np.sum(window) + + mu1 = fftconvolve(self.im1, window, mode='valid') + mu2 = fftconvolve(self.im2, window, mode='valid') + mu1_sq = mu1 * mu1 + mu2_sq = mu2 * mu2 + mu1_mu2 = mu1 * mu2 + sigma1_sq = fftconvolve(self.im1 * self.im1, window, mode='valid') - mu1_sq + sigma2_sq = fftconvolve(self.im2 * self.im2, window, mode='valid') - mu2_sq + sigma12 = fftconvolve(self.im1 * self.im2, window, mode='valid') - mu1_mu2 + + if c1 > 0 and c2 > 0: + num = (2 * mu1_mu2 + c1) * (2 * sigma12 + c2) + den = (mu1_sq + mu2_sq + c1) * (sigma1_sq + sigma2_sq + c2) + ssim_map = num / den + else: + num1 = 2 * mu1_mu2 + c1 + num2 = 2 * sigma12 + c2 + den1 = mu1_sq + mu2_sq + c1 + den2 = sigma1_sq + sigma2_sq + c2 + ssim_map = np.ones(np.shape(mu1)) + index = (den1 * den2) > 0 + ssim_map[index] = (num1[index] * num2[index]) / (den1[index] * den2[index]) + index = (den1 != 0) & (den2 == 0) + ssim_map[index] = num1[index] / den1[index] + mssim = ssim_map.mean() + return mssim, ssim_map |