Commit 1a843fb2 authored by Pierre Paleo's avatar Pierre Paleo
Browse files

Unsharp with LoG

parent 8db19e7e
.vscode/
*.geany
doc/_build/
# Created by https://www.gitignore.io/api/c,c++,python
......
......@@ -16,18 +16,46 @@ class UnsharpMask(object):
A helper class for unsharp masking.
"""
def __init__(self, shape, sigma, coeff, mode="reflect"):
avail_methods = ["gaussian", "log"]
def __init__(self, shape, sigma, coeff, mode="reflect", method="gaussian"):
"""
Initialize a Unsharp mask.
UnsharpedImage = (1 + coeff)*Image - coeff * ConvolutedImage
`UnsharpedImage = (1 + coeff)*Image - coeff * ConvolutedImage`
If method == "log":
`UnsharpedImage = Image + coeff*ConvolutedImage`
Parameters
-----------
shape: tuple
Shape of the image.
sigma: float
Standard deviation of the Gaussian kernel
coeff: float
Coefficient in the linear combination of unsharp mask
mode: str, optional
Convolution mode. Default is "reflect"
method: str, optional
Method of unsharp mask. Can be "gaussian" (default) or "log" for
Laplacian of Gaussian.
"""
self.shape = shape
self.ndim = len(self.shape)
self.sigma = sigma
self.coeff = coeff
self._set_method(method)
self._set_padding_mode(mode)
self._compute_gaussian_kernel()
def _set_method(self, method):
if method not in self.avail_methods:
raise ValueError(
"Unknown unsharp method '%s'. Available are %s"
% (method, str(self.avail_methods))
)
self.method = method
def _set_padding_mode(self, mode):
self.mode = mode
if mode not in PaddingMode.values():
......@@ -48,8 +76,9 @@ class UnsharpMask(object):
if not(__have_scipy__):
raise ValueError("Need scipy to use self.unsharp()")
image_b = self._blur2d(image)
return (1 + self.coeff) * image - self.coeff * image_b
if self.method == "gaussian":
res = (1 + self.coeff) * image - self.coeff * image_b
else: # LoG
res = image + self.coeff * image_b
return res
import numpy as np
from typing import Union, Any
from ..preproc.ccd import CCDCorrection, FlatField
from ..preproc.ccd import CCDProcessing, CCDCorrection, FlatField
from ..cuda.kernel import CudaKernel
from ..cuda.medfilt import MedianFilter
from ..utils import get_cuda_srcfile
from ..utils import get_cuda_srcfile, updiv
try:
import pycuda.gpuarray as garray
......@@ -200,11 +200,15 @@ class CudaLog(CCDProcessing):
def _init_kernels(self):
self._do_clip_min = int(self.clip_min is not None)
self._do_clip_max = int(self.clip_max is not None)
self.clip_min = np.float32(self.clip_min or 0)
self.clip_max = np.float32(self.clip_max or 1)
self._nlog_srcfile = get_cuda_srcfile("ElementOp.cu")
nz, ny, nx = radios.shape
nz, ny, nx = self.radios.shape
self._nx = np.int32(nx)
self._ny = np.int32(ny)
self._nz = np.int32(nz)
self._nthreadsperblock = (16, 16, 4) # TODO tune ?
self._nblocks = tuple([updiv(n, p) for n, p in zip([nx, ny, nz], self._nthreadsperblock)])
self.nlog_kernel = CudaKernel(
"nlog",
......@@ -244,7 +248,9 @@ class CudaLog(CCDProcessing):
self._ny,
self._nz,
self.clip_min,
self.clip_max
self.clip_max,
grid=self._nblocks,
block=self._nthreadsperblock
)
return output
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment