def remove_outlier_cuda(arr, dif, size=3, axis=0): """ Remove high intensity bright spots from a 3D array along axis 0 dimension using GPU. Parameters ---------- arr : ndarray Input array. dif : float Expected difference value between outlier value and the median value of the array. size : int Size of the median filter. axis : int, optional Axis along which outlier removal is performed. Returns ------- ndarray Corrected array. Example ------- >>> import tomocuda >>> tomocuda.remove_outlier_cuda(arr, dif, 5) For more information regarding install and using tomocuda, check https://github.com/kyuepublic/tomocuda for more information """ arr = dtype.as_float32(arr) dif = np.float32(dif) try: import tomocuda winAllow = range(2, 16) if(axis != 0): arr = np.swapaxes(arr, 0, axis) if size in winAllow: prjsize = arr.shape[0] loffset = int(size/2) roffset = int((size-1)/2) imsizex = arr.shape[2] imsizey = arr.shape[1] filter = tomocuda.mFilter(imsizex, imsizey, prjsize, size) out = np.zeros(shape=(prjsize, imsizey, imsizex), dtype=np.float32) for step in range(prjsize): im_noisecu = arr[step].astype(np.float32) im_noisecu = np.lib.pad(im_noisecu, ((loffset, roffset), (loffset, roffset)), 'symmetric') im_noisecu = im_noisecu.flatten() filter.setCuImage(im_noisecu) filter.run2DRemoveOutliner(size, dif) results = filter.retreive() results = results.reshape(imsizey, imsizex) out[step] = results if(axis != 0): out = np.swapaxes(out, 0, axis) else: warnings.warn("Window size not support, using cpu outlier removal") out = remove_outlier(arr, dif, size) except ImportError: warnings.warn("The tomocuda is not support, using cpu outlier removal") out = remove_outlier(arr, dif, size) return out
def median_filter_cuda(arr, size=3, axis=0): """ Apply median filter to 3D array along 0 axis with GPU support. The winAllow is for A6000, Tian X support 3 to 8 Parameters ---------- arr : ndarray Input array. size : int, optional The size of the filter. axis : int, optional Axis along which median filtering is performed. Returns ------- ndarray Median filtered 3D array. Example ------- import tomocuda tomocuda.remove_outlier_cuda(arr, dif, 5) For more information regarding install and using tomocuda, check https://github.com/kyuepublic/tomocuda for more information """ try: import tomocuda winAllow = range(2, 16) if(axis != 0): arr = np.swapaxes(arr, 0, axis) if size in winAllow: loffset = int(size/2) roffset = int((size-1)/2) prjsize = arr.shape[0] imsizex = arr.shape[2] imsizey = arr.shape[1] filter = tomocuda.mFilter(imsizex, imsizey, prjsize, size) out = np.zeros( shape=(prjsize, imsizey, imsizex), dtype=np.float32) for step in range(prjsize): # im_noisecu = arr[:][step][:].astype(np.float32) im_noisecu = arr[step].astype(np.float32) im_noisecu = np.lib.pad(im_noisecu, ((loffset, roffset), (loffset, roffset)), 'symmetric') im_noisecu = im_noisecu.flatten() filter.setCuImage(im_noisecu) filter.run2DFilter(size) results = filter.retreive() results = results.reshape(imsizey, imsizex) out[step] = results if(axis != 0): out = np.swapaxes(out, 0, axis) else: warnings.warn("Window size not support, using cpu median filter") out = median_filter(arr, size, axis) except ImportError: warnings.warn("The tomocuda is not support, using cpu median filter") out = median_filter(arr, size, axis) return out