jupytext | kernelspec | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
(image_processing)=
+++
We start with an adequate definition of an image, respectively of important structural parts of an image. In view of mathematical generalization we will not make a specific difference between classical images (2d) and volumetric images (3d). Even movies, e.g.
- The idealized (continuous) image as a function
$u(x)$ ,$u:\Omega \rightarrow \mathbb{R}$ , where$\Omega \subset \mathbb{R}^d$ denotes the domain of the image (usually a rectangle or cube). - The digital (discrete) image as a matrix (tensor)
$u_{i_1,\cdots i_d}$ ,$u \in \mathbb{R}^{N_1 \times N_2 \times \cdots \times N_d}$ .
Between the idealized and digital version of images there exists an obvious connection, which is also defined via the visualization of images. For this, one divides the domain
At first sight, this definition of an idealized (continuous) image seems to be an arbitrary mathematical idealization. However, in practice it turns out that the definition is fundamental for many different aspects of image processing, e.g. the following:
Resolution. Digital (discrete) images can be available in many different resolutions, i.e. with different amount of pixels. To understand the correspondence between different resolutions, it is very useful to interpret all possible digital images as discrete versions (via mean value computation in pixels) of one underlying continuous image. Via this consistency in the transition between continuous and digital images, also transitions between digital images of different resolutions is clearly defined.
Features. Many properties of images which can intuitively be perceived clearly, are difficult to define in digital images. An important example of this observation are edges, which we understand as transitions between very different gray values. While the definition of edges in the continuous image as the set of discontinuities of the function
Modeling. The mathematical modeling of image processing tasks can be performed in the continuous setup in a unified manner. Here one has all fundamental concepts around integration and differentiation available. We will see that those are in nearly all techniques of enormous importance. Once a variational model has been constructed for a continuous image, via discretization it can be consistently transferred to all discrete images, independent of the resolution. In particular, also the condition of such a problem is interesting, which, for high resolution with reasonable discretization, converges to the condition of the continuous problem. Hence, one should try to analyze the condition of the continuous problem, which can be realized especially via an adequate choice of regularization functionals and leads to interesting mathematical questions, which however in most practical applications can be answered sufficiently.
Algorithms. Also the design of algorithms gets more consistent if one assumes an underlying continuous image. For example, for an iterative algorithm, while increasing resolution, one wants to avoid a significant increase in number of necessary iterations to achieve a certain resolution. This is guaranteed if one obtains an algorithm as a discretization of an algorithm for the continuous image. The latter has to be constructed as an algorithm for functions, i.e. in function spaces, in analogy to methods for partial differential equations.
We understand noise as the undesired disturbances of intensities in an image. In nearly all imaging devices, like microscopes, different tomographs or even simple digital cameras, one obtains noise effects in measured data. In the following we will introduce the characterization of different noise models in images.
Among different inverse problems in imaging, dependent on the underlying physics, one can observe different types of noise in measured imaging data. Considering the noise as a statistical random process specific noise types can be characterized by specific distributions. To model the noise a-priori, typical choices are Gauss-, Laplace- or Poisson-distributions, but also combinations of those. Obvious errors in the imaging process are intensity errors. One can see those errors as realizations of independent random variables, acting on each pixel separately.
The simplest model of intensity errors is additive noise. For a discrete image
If every random variable follows a Gauss-distribution, it is called additive Gaussian noise. Other common additive noise models are given by assuming a Laplace-, uniform- or also Poisson-distribution (with constant parameter) instead. In analogy, a model for multiplicative noise is given by
A typical case for multiplicative noise is given by Gamma distributed random variables. Particularly, in view of several biomedical imaging methods there are known noise models, like Poisson noise or salt-and-pepper noise, which are defined by a dependency on
i.e. those are neither additive nor multiplicative. Errors in photon counting, for instance in several areas of tomography, microscopy, but also in CCD sensors of digital cameras or astronomy systems, are typically modeled as Poisson noise. Some examples of various noise types applied to a test images are shown below.
:tags: [hide-input]
# import libaries
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['figure.dpi'] = 300
from skimage import data
from skimage.util import random_noise
from myst_nb import glue
# load test image
camera = data.camera()
# plot
sigma = .1
fig, ax = plt.subplots(1,4,figsize=(10,10))
for i in range(len(ax)):
ax[i].axis('off')
plt.gray()
ax[0].imshow(camera)
ax[0].set_title('Original image')
ax[1].imshow(random_noise(camera, mode='gaussian', var=sigma**2))
ax[1].set_title('Gaussian noise')
ax[2].imshow(random_noise(camera, mode='poisson'))
ax[2].set_title('Poisson noise')
ax[3].imshow(random_noise(camera, mode='s&p'))
ax[3].set_title('Salt and pepper noise')
plt.show()
glue("noisy_images", fig, display=False)
Before we focus on different filtering methods, we should discuss different methods for validating the quality of a method. A simple validation would for sure be a number, which becomes zero in case of perfect denoising, respectively increases with worse quality (or vice versa). To test a denoising method with artificial data one can simply compute the distance between the denoised and the clean image, with regard to an adequate norm. If we use the notation
To avoid scaling issues (and error propagation) it is often more reasonable to consider a relative error with respect to the noise, i.e.
or the scaled error
A variant of the scaled error is the commonly used Signal-to-Noise ratio (SNR). The naming is based on the idea that
and in the discrete case the scaled
Then as SNR one obtains
Note the reversal of monotony. The SNR is large for good quality and low for bad quality of a reconstruction. An additional widely used variant of the SNR is the Peak-Signal-to-Noise ratio (PSNR)
Here one compares the noise with the peak in an image, i.e.
In this section we focus on filtering methods for image denoising. Denoising is one of the most important tasks in digital image processing because it finds various applications beyond fluorescence microscopy and forms a well understood basis for many other image processing challenges in inverse problems. Denoising is the (inverse) problem of removing the noise from images while keeping significant information in the data. In applications, denoising methods are often applied as pre- or post processing methods to better analyze images and to be able to extract specific features, e.g. edges or corners, more effectively. In the following we particularly focus on the relationship between denoising filters, partial differential equations (PDEs) and related variational methods. We will start with simple linear diffusion filters an end with an outlook on a famous nonlinear variational method for denoising.
The term filter has its origin in signal analysis, as a procedure which yields only a part of the signal (resp. the image). The hope in denoising is for sure to filter the clean from the noisy signal. Local smoothing filters are based on the idea, that in a local neighborhood similar gray- or color values occur. Therefore one can try to replace the image by a local averaging. In this process also random distortions (the noise) will be averaged out. If one assumes, as introduced above, noise based on independent and identically distributed (iid) random variables, then also the variance of the signal should be reduced in this way. A local linear smoothing filter has the general forms
where
or in the discrete case as
$$ (G_\epsilon*F){ij} = \frac{1}{\epsilon^d} \sum{k,\ell} G\left(\frac{x_{ij}-x_{k\ell}}\epsilon\right) F_{k\ell} , . $$
In the discrete case it is important to choose
In practice local smoothing methods actually reduce the noise, however there is also a potential problem of such filters, namely over smoothing. In particular this affects edges, since at an edge the assumption having locally similar gray values is mostly violated. For example, if we think of a black-white edge, local gray values will be averaged to gray at the edge. In this way the edge (discontinuity) is getting averaged to a continuous transition of gray values, which as a result looks like an optically blurred edge.
For further understanding we consider the discrete case and the special local filter of the form
i.e. the filter only acts on neighboring pixels. Hereby
With the filter we obtain a systematic error, i.e.
$$ \mathbb{E}(\hat{U}{ij}) &=& (1-4 \alpha) \mathbb{E}(F{ij}) + \alpha( \mathbb{E}(F_{i-1j}) + \mathbb{E}(F_{i+1j}) + \mathbb{E}(F_{ij-1}) + \mathbb{E}(F_{ij+1})) \ &=& (1-4 \alpha) \hat F_{ij} + \alpha( \hat F_{i-1j} + \hat F_{i+1j} + \hat F_{ij-1} + \hat F_{ij+1}), $$
and hence in general $\mathbb{E}(\hat{U}{ij}) \neq \hat F{ij}$. So the filter has a particular disadvantage, which one should only accept, if at least the mean error gets smaller, so we should analyze the following term.
$$ \mathbb{E}((\hat{U}{ij} - \hat F{ij})^2) &=& \mathbb{E}((\hat{U}{ij} - \mathbb{E}(\hat{U}{ij}) + \mathbb{E}(\hat{U}{ij})- \hat F{ij})^2) \ &=& \mathbb{E}((\hat{U}{ij} - \mathbb{E}(\hat{U}{ij}))^2) + (\mathbb{E}(\hat{U}{ij})- \hat F{ij})^2 $$
We start with the first term and obtain due to independence of the
$$ \hat{U}{ij} - \mathbb{E}(\hat{U}{ij}) &=& (1-4 \alpha) (F_{ij}-\hat F_{ij}) + \alpha( F_{i-1j} - \hat F_{i-1j} + F_{i+1j} - \hat F_{i+1j} + \ && F_{ij-1} - \hat F_{ij-1} + F_{ij+1}- \hat F_{ij+1}) \ &=& (1-4 \alpha) \delta_{ij}^\sigma + \alpha( \delta_{i-1j}^\sigma + \delta_{i+1j}^\sigma + \delta_{ij-1}^\sigma + \delta_{ij+1}^\sigma), $$
where
$$
\mathbb{E}((\hat{U}{ij} - \mathbb{E}(\hat{U}{ij}))^2) &=& (1-4\alpha)^2 \mathbb{E}((\delta_{ij}^\sigma)^2) + \alpha^2 (\mathbb{E}((\delta_{i+1j}^\sigma)^2) + \mathbb{E}((\delta_{i-1j}^\sigma)^2)+\&& \mathbb{E}((\delta_{ij+1}^\sigma)^2)+\mathbb{E}((\delta_{ij-1}^\sigma)^2)) \
&=& (1-4\alpha)^2 \sigma^2 + 4 \alpha \sigma^2 = (1-8\alpha +20 \alpha^2) \sigma^2.
$$
The noise gets reduced by the filter, because this part of the error is smaller than
$$ \mathbb{E}(\hat{U}{ij})- \hat F{ij} = \alpha ( \hat{F}{i-1j} + \hat{F}{i+1j} + \hat{F}{ij-1} + \hat{F}{ij+1}-4\hat{F}_{ij}) . $$
For simplicity we assume that $\hat{F}{ij}$ is the pixel value at index $(ih,jh)$, where $h$ denotes the (small) pixel size. So we have the idea that $\hat{F}{ij} = \hat{f}(x_{ij})$ for an adequate gray value function
$$ \hat{F}{i-1j} + \hat{F}{i+1j} - 2 \hat{F}_{ij} = \frac{\partial^2\hat{f}}{\partial x_1^2}(\xi_1) h^2 $$
and
$$ \hat{F}{ij-1} + \hat{F}{ij+1}- 2 \hat{F}_{ij} = \frac{\partial^2\hat{f}}{\partial x_2^2}(\xi_2) h^2 . $$
Thus we can estimate the second part o the error by
$$ (\mathbb{E}(\hat{U}{ij})- \hat F{ij})^2 \leq 4 \max\left{ |\frac{\partial^2\hat{f}}{\partial x_1^2}|\infty,|\frac{\partial^2\hat{f}}{\partial x_2^2}|\infty \right} \alpha^2 h^4 . $$
As a result we finally obtain the estimate
$$ \mathbb{E}((\hat{U}{ij} - \hat F{ij})^2) \leq (1-8\alpha+20 \alpha^2) \sigma^2 + 4 \max\left{ |\frac{\partial^2\hat{f}}{\partial x_1^2}|\infty,|\frac{\partial^2\hat{f}}{\partial x_2^2}|\infty \right} \alpha^2 h^4. $$
In particular for
:tags: [hide-input]
# import libaries
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['figure.dpi'] = 300
from skimage import data
from skimage.util import random_noise
from scipy.signal import convolve2d as conv2
# load test image
camera = data.camera()
# add noise
sigma = .1
camera_noisy = random_noise(camera, mode='gaussian', var=sigma**2)
# filter
alpha = .2
filter = np.array([[0, alpha, 0],[alpha, 1-4*alpha,alpha],[0,alpha,0]])
camera_filtered = conv2(camera_noisy, filter, 'same')
# plot
fig, ax = plt.subplots(1,2,figsize=(10,10))
for i in range(len(ax)):
ax[i].axis('off')
plt.gray()
ax[0].imshow(camera_noisy)
ax[0].set_title('Noisy image')
ax[1].imshow(camera_filtered)
ax[1].set_title('Filtered image')
plt.show()
glue("linear_filter", fig, display=False)
A useful analysis of images can be achieved by applying the Fourier transform
which transfers a function into the frequency space. Here, the value of the Fourier transform at
Very similar to smoothing filters, or actually just another way of looking at things, are frequency space filters which are given in the following form
where
The original motivation of constructing frequency space filters is slightly different from those of local smoothing. In frequency space the central idea is that noise often corresponds to high frequencies and therefore one should at least dampen those frequencies. Such filters are called lowpass filters since only the small frequencies remain unchanged (in analogy to a hardware filter for signals where only low frequency components can pass). The simplest choice of a function
In this way
Interestingly, one can interpret local smoothing filters also in the context of PDE methods in form of so-called linear diffusion filters. For this, one can see
Under the assumption that
where
with initial value
where
As we have seen, linear and nonlinear filters lead, in reasonable asymptotics, to the solution of parabolic differential equations of the form
where the denoised image then corresponds to the solution of this equation at a specific point in time
In the previous subsection we have related the local smoothing filter to a PDE method. Now we would like to transition to an interpretation in the sense of variational methods. In comparison to the previous subsection we now assume that the parameter
The regularization functional, i.e.
can be interpreted as a Taylor expansion of first order of the functional
around
:label: L2L2Denoising
\frac{1}{2} \int_\Omega (u-f)^2~dx \:+\: \frac{\alpha}{2}\int_\Omega |\nabla u|^2~dx ~\rightarrow~ \min_u \ .
Till now, in all previous methods, we have always focussed on additive Gaussian noise. Although the described filter methods can often be realized in a simple and efficient way, variational methods have important advantages in comparison. With variational methods one has the flexibility to model data fidelity terms and regularization terms. According to a-prior knowledge about the noise as well as about the type (e.g. features) of images to be expected, those terms can easily be adapted, extended and combined.
Now we consider for
:label: denoisingFunktionalAllgemein
D(u,f) \:+\: \alpha \: R(u) ~\rightarrow~ \min_u \ .
The data fidelity term should be chosen in a way that
- Additive Gaussian noise leads to
$D(u,f) = \frac{1}{2}\left| u-f \right|^2_2$ - Additive Laplacian noise leads to
$D(u,f) = \left| u-f \right|_1$ - Poisson noise leads to
$D(u,f) = \int_\Omega \left( f \log \frac{f}u : - : f : + : u \right) : \mathrm{d}x.$
The regularization functional in {eq}denoisingFunktionalAllgemein
should be constructed in such a way that L2L2Denoising
we also have a homogeneous smoothing similar to the linear smoothing filter, i.e. noise but also edges in in the image get penalized. Often this regularization with the
with the total variation as regularisation term
$$ \text{TV}(u) \equiv \sup_{\substack{g \in {C_0^\infty(\Omega,\mathbb{R}^d)}\||g||\infty \leq 1}} : \int\Omega u \nabla \cdot g \ . $$
If one restricts to the function space
:tags: [hide-input]
# import libaries
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['figure.dpi'] = 300
from skimage import data
from skimage.util import random_noise
from skimage.restoration import denoise_tv_chambolle
# load test image
camera = data.camera()
# add noise
sigma = .1
camera_noisy = random_noise(camera, mode='gaussian', var=sigma**2)
# filter
alpha = .2
camera_filtered = denoise_tv_chambolle(camera_noisy, weight=alpha)
# plot
fig, ax = plt.subplots(1,2,figsize=(10,10))
for i in range(len(ax)):
ax[i].axis('off')
plt.gray()
ax[0].imshow(camera_noisy)
ax[0].set_title('Noisy image')
ax[1].imshow(camera_filtered)
ax[1].set_title('Filtered image')
plt.show()
An interesting problem that occurs in many imaging, image- and signal processing applications, in particular in microscopy, is the deblurring or \textit{deconvolution} of signals from a (known, linear) degradation. Deconvolution of a signal can be modeled as solving the inverse problem of the convolution, which reads as
:label: convIP
f(x) = (Ku)(x) := \int_{\mathbb{R}^n} k(x-x')u(x) \, \mathrm{d}x'~.
Here convIP
to
:label: convIPFourier
f = (2\pi)^{\frac{n}{2}} F^{-1} ( F(u) F(k) ) \, .
It is important to note that the inverse Fourier transform is indeed the unique, inverse operator of the Fourier transform in the Hilbert space convIPFourier
to solve it for
:label: convSol
u = (2\pi)^{-\frac{n}{2}} F^{-1} \left( \frac{F(f)}{F(k)} \right) \, ,
and hence, we allegedly can recover convIP
. Further we assume that instead of the blurry image convSol
with noise input measurement
:label: converror
(2\pi)^{\frac{n}{2}} \, \left| u-u^{\delta} \right| = \left| F^{-1} \left( \frac{F(f-f^\delta)}{F(k)} \right) \right| = \left| F^{-1} \left( \frac{F(n^\delta)}{F(k)} \right) \right| \, .
As the convolution kernel converror
becomes fairly small, whereas the numerator will be non-zero as the noise is of high frequency. Thus, in the limit the solution will not depend continuously on the data and the convolution problem therefore be ill-posed.
+++
+++
Let
be an (idealized) continuous image defined in the domain
and
The domain is divided equidistantly into
- Derive bounds for the maximal difference of neighboring pixels in
$U$ .
Now let us consider a step function as an (idealized) continuous 1d signal
in the domain
-
Specify the values of the digital image in the pixels without the step.
-
Specify the value of the pixel with the step dependent on the position within this pixel.
+++
The SciKit-image package offers many standard image processing algorithms as well as functionality to add noise and benchmark images:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['figure.dpi'] = 300
from skimage import data
from skimage.util import random_noise
# load test image
image = data.coins()
# add noise
sigma = .1
image_noisy = random_noise(image, mode='gaussian', var=sigma**2)
# plot
fig, ax = plt.subplots(1,2,figsize=(10,10))
for i in range(len(ax)):
ax[i].axis('off')
plt.gray()
ax[0].imshow(image)
ax[0].set_title('ground-truth image')
ax[1].imshow(image_noisy)
ax[1].set_title('noisy image')
plt.show()
-
Take the coins image and add different types and strengths of noise (Gaussian, Laplace, data dependent Poisson) and plot the results. You can add random noise to an image using the
random_noise
function. -
Implement the local linear filter learned in the lecture as an iterative scheme to remove the noise. How can you achieve different degrees of smoothness? What do you observe at edges?
-
Apply also different filters available in skimage (e.g., Gaussian). Verify the relationship between the amount of noise you add to the image and the strength of the filter you have to apply. Plot a graph with different PSNR values for different smoothness parameters to validate the "best" result. You can easily apply various filters using the
filters
module. -
Apply the nonlinear total variation (TV) denoising filter to your previous test scenario. Verify the preservation of edges of the coins. Vary the regularization parameter. What is the systematic error of TV denoising? The filter is available in Python as
denoise_tv_chambolle