#!/usr/bin/env python # coding: utf-8 #

Introduction to image processing - Radiometry

# # ## Introduction # # For the following exercices, you need Python 3 with some basic librairies (see below). # All images necessary for the session are available [here](https://plmlab.math.cnrs.fr/glaunes/tp_enseignement/-/tree/master/M2_Percep/Partie1/im). # # If you use your own Python 3 install, you should download the images, put them in a convenient directory and update the path in the next cell. # In[1]: path = '../im/' # In[2]: import numpy as np import matplotlib.pyplot as plt get_ipython().run_line_magic('matplotlib', 'inline') # ## Load and display a color image # # A color image is made of three channels : red, green and blue. A color image in $\mathbb{R}^{N\times M}$ is stored as a $N\times M\times 3$ matrix. # # # # # **Be careful with the functions `plt.imread()` and `plt.imshow()` of `matplotlib`.** # - `plt.imread()` reads png images as numpy arrays of floating points between 0 and 1, but it reads jpg or bmp images as numpy arrays of 8 bit integers. # # - In this practical session, we assume images encoded as floating point values between 0 and 1, so if you load a jpg or bmp file you must convert the image to float type and normalize its values. # # - If 'im' is an image encoded as a double numpy array, `plt.imshow(im)` will display all values above 1 in white and all values below 0 in black. If the image 'im' is encoded on 8 bits though, `plt.imshow(im)` will display 0 in black and 255 in white. # # In[3]: imrgb = plt.imread(path+'parrot.png') #
# Display the image size: # In[4]: [nrow,ncol,nch] = imrgb.shape print(nrow,ncol,nch) # # You can use `plt.imshow()` to display the 3D numpy array `imrgb` as an image: # In[5]: plt.figure(figsize=(7, 7)) plt.imshow(imrgb) plt.show() # # It might be useful to convert the color image to gray level. This can be done by averaging the three channels, or by computing another well chosen linear combination of the coordinates R, G and B. First we try with simple averaging $$I_{gs}=(R+G+B)/3$$ # In[6]: imgray = np.mean(imrgb,2) plt.figure(figsize=(7, 7)) plt.imshow(imgray,cmap='gray') plt.show() # **1. Now use a custom weighted averaging of the three channels, that reflects better human perception:** # $$I_{gs}=0.21 R + 0.72 G + 0.07 B$$ # In[7]: # to do: question 1 # ## Histograms and contrast enhancement # # ### Computing histograms # # In the following, we compute and display the gray level histogram and the cumulative histogram of an image. # # The cumulative histogram of a discrete image $u$ is an increasing function defined on $\mathbb{R}$ by # $$H_u(\lambda)=\frac{1}{|\Omega|}\#{\{\textbf{x};\;u(\textbf{x})\leq \lambda\}}.$$ # The histogram of $u$ is the derivative of $H_u$ in the sense of distributions. # # # a. We compute the histogram of the image `imgray`: # In[8]: imhisto, bins = np.histogram(imgray, range=(0,1), bins = 256) imhisto = imhisto/np.sum(imhisto) # # b. We now compute the corresponding cumulative histogram thanks to the function `np.cumsum()`which cumulates the values of a vector from left to right: # In[9]: imhistocum = np.cumsum(imhisto) # # c. We display the image, histogram and cumulative histogram: # In[10]: values = (bins[1:]+bins[:-1])/2 fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(15, 5)) axes[0].imshow(imgray,cmap='gray') axes[0].set_title('parrot image, gray level') axes[1].bar(values,imhisto,width=1/256) axes[1].set_title('histogram') axes[2].bar(values,imhistocum,width=1/256) axes[2].set_title('cumulative histogram') fig.tight_layout() plt.show() # ### Histogram equalization # # If $u$ is a discrete image and $h_u$ its gray level distribution, histogram equalization consists in # applying a contrast change $g$ (increasing function) to $u$ such that $h_{g(u)}$ is as close as possible to a constant distribution. We can compute directly $$H_u(u)*255.$$ # # To this aim, we can apply directly the vector `imhistocum` (which can be seen as a function from $\{0,\dots,255\}$ into $[0,1]$) to the numpy array `imgray`. Since `imgray` has values between $0$ and $1$, it is necessary to multiply it by $255$ and cast it as a 8-bit array: # In[11]: imeq = imhistocum[np.uint8(255*imgray)] # # We can now display the resulting equalized image: # In[12]: plt.figure(figsize=(7, 7)) plt.imshow(imeq,cmap = 'gray') plt.show() # **2. Compute and plot also the corresponding histograms and cumulative histograms of the equalized image.** # In[13]: # to do: question 2 # **3. Now, apply the previous histogram equalization to the two images `parrot_bright.png` and `parrot_dark.png`, plot the equalized images, the corresponding histograms and cumulative histograms. Comment the results and explain the observed differences.** # In[14]: # to do: question 3 # ### Histogram specification # # If $u$ is a discrete image and $h_u$ its gray level distribution, histogram specification consists in applying a contrast change $g$ (an increasing function) to $u$ such that $h_{g(u)}$ is as close as possible to a target discrete probability distribution $h_t$. Specification is particularly useful to compare two images of the same scene (in this case the target distribution is the histogram of the second image $v$). # # We start by reading our two images $u$ and $v$: # In[15]: buenos1=plt.imread(path+'buenosaires4.png') buenos2=plt.imread(path+'buenosaires3.png') u = buenos1[:,:,0] v = buenos2[:,:,0] [nrowu,ncolu]=u.shape [nrowv,ncolv]=v.shape # # Now, histogram specification between two grey level images $u$ and $v$ can be computed easily by sorting the pixels of both images and by replacing each gray level in $u$ by the gray level of similar rank in $v$: # In[16]: index_u = np.argsort(u,axis=None) u_sort = u.flatten()[index_u] #np.sort(u,axis=None) index_v = np.argsort(v,axis=None) v_sort = v.flatten()[index_v] #np.sort(v,axis=None) uspecifv = np.zeros(nrowu*ncolu) uspecifv[index_u] = v_sort uspecifv = uspecifv.reshape(nrowu,ncolu) #
# We can now display the result. # In[17]: fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(15, 5)) axes[0].set_title('image u') axes[0].imshow(u,'gray') axes[1].set_title('image v') axes[1].imshow(v,'gray') axes[2].set_title('image specification') axes[2].imshow(uspecifv,'gray') fig.tight_layout() plt.show() # **4. Try to translate the grey levels of $u$ such that it has the same mean grey level than $v$ and display the result. Is it similar to the specification of $u$ on $v$ ?** # In[18]: # to do: question 4 # **5. Same question by applying an affine transform to $u$ so that its mean and variance match the ones of $v$.** # In[19]: # to do: question 5 # ### Midway histogram # # The Midway histogram between two histograms $h_u$ et $h_v$ is defined from it cumulative function $H_{midway}$ : # $$H_{midway}=\left( \frac{H_u^{-1}+H_v^{-1}}{2}\right)^{-1}.$$ # The goal is to modify the contrast of both images $u$ and $v$ in order to give them the same intermediary grey level distribution. # In[20]: u_midway=np.zeros(len(index_u)) v_midway=np.zeros(len(index_v)) u_midway[index_u] = (u_sort + v_sort)/2 v_midway[index_v] = (u_sort + v_sort)/2 u_midway = u_midway.reshape(nrowu,ncolu) v_midway = v_midway.reshape(nrowv,ncolv) #Display the results fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(10, 8)) axes[0,0].set_title('image u') axes[0,0].imshow(u,'gray') axes[0,1].set_title('image v') axes[0,1].imshow(v,'gray') axes[1,0].set_title('image u_midway') axes[1,0].imshow(u_midway,'gray') axes[1,1].set_title('image v_midway') axes[1,1].imshow(v_midway,'gray') fig.tight_layout() plt.show() # ### Simple transformations # # In this exercice, you are asked to perform simple transformations on an image and find out what happens on the corresponding histogram : thresholding, affine transformation, gamma correction. # ### Effect of Noise on histograms # # In the following, we want to create different noisy versions of an image $u$ and observe how the histogram $h_u$ is transformed. # # **Gaussian noise** # # 6 a) Write a function adding a gaussian noise $b$ to the image $u$. An image of gaussian noise of mean $0$ and of standard deviation $\sigma$ is obtained with the command # # sigma*np.random.randn(nrow,ncol) # # 6 b) Display the noisy image and its histogram for different values of $\sigma$. # # 6 c) What do you observe ? What is the relation between the histogram of $u$ and the one of $u+b$ ? # In[21]: imgray = np.mean(plt.imread(path+'parrot.png'),2) # to do: question 6 # **Uniform noise** 7. Same questions with $b$ a uniform additive noise. # In[22]: # to do: question 7 # **Impulse noise** Let us recall that impulse noise destroy randomly a proportion $p$ of the pixels in $u$ and replace their values by uniform random values between $0$ and $255$. Mathematically, this can be modeled as $u_b= (1-X)u+XY$, where $X$ follows a Bernouilli law of parameter $p$ and $Y$ follows a uniform law on $\{0,\dots 255\}$. # # 8 a) Write a function adding impulse noise of parameter p to an image u. # /Hint/ : you can start by using the function ~rand~ to create a table `tab` of random numbers following the uniform law on $[0,1[$ # # tab = np.random.rand(u.shape[0],u.shape[1]) # # and then replace randomly $p\%$ of the pixels of $u$ by a random grey level # # ub = 255*np.random.rand(u.shape[0],u.shape[1])*(tab

=p/100)*u; # # 8 b) Display the noisy image and its histogram for different values of p. What is the relation between the histogram of u and the one of u_b ? # In[23]: # to do: question 8 # ## Image Quantization # # ### Quantization # # Image quantization consists in reducing the set of grey levels $Y = \{ y_0,\dots y_{n-1} \}$ or colors of an image $u$ into a smaller set of quantized values $\{q_0,\dots q_{p-1}\}$ ($p < n$). This operation is useful for displaying an image $u$ on a screen that supports a smaller number of colors (this is needed with a standard screen if $u$ is coded on more than 8 bits by channel). # # A quantization operator $Q$ is defined by the values $(q_i)_{i=0, \dots p-1}$ and $(t_j)_{j=0,\dots p}$ such that # $$ t_0 \leq q_0 \leq t_1 \leq q_1 \leq \dots q_{p-1} \leq t_p,\text{ and } Q(\lambda)=q_i \text{ if } t_i \leq \lambda < t_{i+1}.$$ # # # *Uniform Quantization* # Uniform quantization consists in dividing the set $Y$ in $p$ regular intervals. # # 9. Use uniform quantization on a gray level image (try different numbers $K$ of grey levels) and display the result. For which value of $K$ do you start to see a difference with the original image ? # # In[24]: u = np.mean(plt.imread(path+'simpson512.png'),2) # to do: question 9 # *Histogram-based Quantization* # This consists in choosing $t_i=\min \{\lambda; H_u(\lambda) \geq \frac{i}{p} \}$, and the $q_i$ are defined as the barycenters of the intervals $[t_i,t_{i+1}].$ # # 10 a) Show that this boils down to an histogram equalization followed by a uniform quantization # # 10 b) Apply this quantization on a gray level image and display the result. Same question on the limit value $K$ for which we perceive a difference with the original image. # In[25]: # to do: question 10 # *Lloyd-Max quantization* # This quantization consists in minimizing the least square error # $$LSE((q_i)_{i=1\dots p-1},(t_i)_{i=1\dots p})= \sum_{i=0}^{p-1} \int_{t_i}^{t_{i+1}} h(\lambda) (\lambda -q_i)^2.$$ # It is equivalent to the algorithm `Kmeans` in one dimension. # # 11 a) Write the optimality conditions that should be satisfied by the solution $\{(\widehat{q_i}),(\widehat{t_i})\}$. # # 11 b) Write a function which minimizes the least square error by alternatively minimizing in $(q_i)_{i=0, \dots p-1}$ and $(t_j)_{j=0,\dots p}$. # # 11 c) Apply this quantization on the previous gray level image for different values of $K$ and display the result. Comment. # In[26]: imgray = np.mean(plt.imread(path+'simpson512.png'),2) # to do : question 11 # ### Dithering # # Dithering consists in adding intentionnally noise to an image before quantization. For instance, it can be used to convert a grey level image to black and white in such a way that the density of white dots in the new image is an increasing function of the grey level in the original image. This is particularly useful for impression or displaying. # # Let us explain how dithering works in the case of 2 grey levels (binarization). All grey levels smaller than a value $\lambda$ are replaced by $0$ and those greater than $\lambda$ are replaced by $255$. If we add a i.i.d. noise $B$ of density $p_B$ to $u$ before the binarization, then at the pixel $x$ we get # $$P[u(x) + B(x) > \lambda] = P[B(x) > \lambda - u(x) ] = \int_{\lambda - u(x)}^{+\infty} p_B(s)ds,$$ # which is an increasing function of the value $u(x)$. The probability that $x$ turns white in the dithered image is thus an increasing function of its original grey level. # # **12 a) Perform dithering in order to quantize a gray level image on 10 levels (you can add a small Gaussian noise of std 5/255 for instance). Compare the result with the previous quantizations without dithering.** # # **12 b) Try with different levels of noise.** # # In[27]: # to do : question 12 # In[ ]: