For the following exercices, you need Python 3 with some basic librairies (see below). All images necessary for the session are available here.
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.
For some parts of the session (cells with commands written as todo_something
...), you are supposed to code by yourself.
path = '../im/'
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
The following line will be used to import the solutions of the practical session. Do not use it for the moment.
from TP_radiometrie import *
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 are 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 to $[0,1]$.
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.
imrgb =plt.imread(path+"parrot.png")
Display the image size.
[nrow,ncol,nch]=imrgb.shape
print(nrow,ncol,nch)
495 495 3
You can use plt.imshow()
to display the 3D numpy array imrgb
as an image.
plt.figure(figsize=(7, 7))
plt.imshow(imrgb)
plt.show()
It might be useful to convert the color image to gray levels. 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$$
imgray = np.sum(imrgb,2)/3
plt.figure(figsize=(7, 7))
plt.imshow(imgray,cmap='gray',vmin=0,vmax=1)
plt.show()
Now use a custom weighted averaging of the three channels, that reflects better human perception: $$I_{gs}=0.21R+0.72G+0.07B$$
imgray2 = 0.21*imrgb[:,:,0] + 0.72*imrgb[:,:,1] + 0.07*imrgb[:,:,2]
# you can also do: imgray2 = np.average(imrgb,axis=2,weights=[0.21,0.71,0.07])
plt.figure(figsize=(7, 7))
plt.imshow(imgray2,cmap='gray',vmin=0,vmax=1)
plt.show()
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.
imrgb
imhisto, bins = np.histogram(imgray, range=(0,1), bins = 256)
imhisto = imhisto/np.sum(imhisto)
np.cumsum()
which cumulates the values of a vector from left to right.imhistocum = np.cumsum(imhisto)
values = (bins[1:]+bins[:-1])/2
fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(15, 5))
axes[0].imshow(imgray,cmap='gray',vmin=0,vmax=1)
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()
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.
imeq = imhistocum[np.uint8(imgray*255)]
We can now display the resulting equalized image.
plt.figure(figsize=(5, 5))
plt.imshow(imeq,cmap = 'gray',vmin=0,vmax=1)
plt.show()
Compute and plot also the corresponding histograms and cumulative histograms of the equalized image.
def comp_histos(im):
imhisto, bins = np.histogram(im, range=(0,1), bins = 256)
imhisto = imhisto/np.sum(imhisto)
imhistocum = np.cumsum(imhisto)
return imhisto, bins, imhistocum
def plot_histos(im):
imhisto, bins, imhistocum = comp_histos(im)
values = (bins[1:]+bins[:-1])/2
fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(15, 5))
axes[0].imshow(im,cmap='gray',vmin=0,vmax=1)
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()
plot_histos(imeq)
Now, apply the previous histogram equalization to the two images parrot_bright
and parrot_dark
, plot the corresponding histograms and cumulative histograms. Comment the results and explain the observed differences.
imrgb1 = plt.imread(path+'parrot_bright.png')
imrgb2 = plt.imread(path+'parrot_dark.png')
imeq1,imeq2 = todo_equalization(imrgb1), todo_equalization(imrgb2)
plot_histos(imeq1)
plot_histos(imeq2)
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$.
buenos1=plt.imread(path+'buenosaires3.png')
buenos2=plt.imread(path+'buenosaires4.png')
v = buenos1[:,:,0]
u = buenos2[:,:,1]
[nrowu,ncolu]=u.shape
[nrowv,ncolv]=v.shape
u_sort,index_u=np.sort(u,axis=None),np.argsort(u,axis=None)
[v_sort,index_v]=np.sort(v,axis=None),np.argsort(v,axis=None)
uspecifv= np.zeros(nrowu*ncolu)
uspecifv[index_u] = v_sort
uspecifv = uspecifv.reshape(nrowu,ncolu)
We can now display the result.
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()
2. 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$ ?
# First we compute the "translation value", i.e. the difference between the mean grey levels of the two images:
diff_mean_uv = np.mean(v-u)
# Now we apply the translation:
u_trans = u + diff_mean_uv
# The operation may have produced values outside the range [0,1], so we need to do a correction:
u_trans = np.maximum(0,np.minimum(1,u_trans))
# We plot the resulting image, together with the specification of u on v:
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(15, 5))
axes[0].set_title('translated image')
axes[0].imshow(np.tile(u_trans[:,:,None],(1,1,3)),'gray',vmin=0,vmax=1)
axes[1].set_title('specification')
axes[1].imshow(uspecifv,'gray',vmin=0,vmax=1)
fig.tight_layout()
plt.show()
3. Same question by applying an affine transform to $u$ so that its mean and variance match the ones of $v$.
# We can first multiply u by the quotient of standard deviations between v and u, in order to get equal variances:
u_aff = u * (np.std(v)/np.std(u))
# Now we can do the same translation as previously:
u_aff = u_aff + np.mean(v-u_aff)
# Correction to fit in the range [0,1]:
u_aff = np.maximum(0,np.minimum(1,u_aff))
# We plot the resulting image, together with the specification of u on v:
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(15, 5))
axes[0].set_title('affine transformed image')
axes[0].imshow(u_aff,'gray',vmin=0,vmax=1)
axes[1].set_title('specification')
axes[1].imshow(uspecifv,'gray',vmin=0,vmax=1)
fig.tight_layout()
plt.show()
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.
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=(14, 10))
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()
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.
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
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)
Display the noisy image and its histogram for different values of $\sigma$.
What do you observe ? What is the relation between the histogram of $u$ and the one of $u+b$ ?
imgray = np.mean(plt.imread(path+'parrot.png'),2)
todo_noise_histograms(imgray)
Uniform noise Same questions with $b$ a uniform additive noise.
imgray = np.mean(plt.imread(path+'parrot.png'),2)
todo_uniform_noise_histograms(imgray)
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\}$.
/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)+(tab>=p/100)*u;
imgray = np.mean(plt.imread(path+'parrot.png'),2)
todo_impulse_noise_histograms(imgray)
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.
u = np.mean(plt.imread(path+'simpson512.png'),2)
K = 20
# uniform quantization, several values of K
tabK = [50,30,20,10,5,2]
nK = len(tabK)
plt.figure(figsize=(16, 10))
for i, K in enumerate(tabK):
u_quant = (np.floor(K*u)+.5)/K
plt.subplot(2,np.ceil(nK/2),i+1)
plt.imshow(u_quant,cmap='gray',vmin=0,vmax=1)
plt.show()
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}].$
u = np.mean(plt.imread(path+'simpson512.png'),2)
K = 20
# histogram quantization
plt.figure(figsize=(16, 10))
tabK = [50,30,20,10,5,2] # several values of K
nK = len(tabK)
for i, K in enumerate(tabK):
im_quant = todo_histogram_quantize(u,K)
plt.subplot(2,np.ceil(nK/2),i+1)
plt.title('K='+str(K))
plt.imshow(im_quant,cmap='gray',vmin=0,vmax=1)
plt.tight_layout()
plt.show()
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.
imgray = np.mean(plt.imread(path+'simpson512.png'),2)
u_quant = todo_Lloyd_Max_quantize(imgray,K=10,plot=True)
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.
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.
Try with different levels of noise.
imgray = np.mean(plt.imread(path+'simpson512.png'),2)
todo_dither(imgray,K=10,sigma=5/255)
imgray = np.mean(plt.imread(path+'lighthouse.bmp'),2) / 255
todo_dither(imgray,K=2,sigma=10/255)
imgray = plt.imread(path+'falaise.jpg') / 255
todo_dither(imgray,K=2,sigma=60/255)