Gray Level Image Processing

histogramme

Method

To make the histogram of an image, it's count how many pixels exist for each possible value that a pixel can take. If we have a gray level image where the values ??are represented to 8 bits, we will have a histogram of 255 values. 8 bits gives 28 possible values .
To make this processing, we create a vector hist whose size is defined by the number of possible different value of pixel (ie size = 2k where k is the number of bits for gray level ) and we put all of them to zero, then we must scan the entire picture, line by line and column by column. For each value of a pixel, we increase the value of the histogram associated with that value. for example, if we find the pixel value 200 then hist (200) = hist (200) + 1.

Here is the program that performs the histogram.

```clear all; close all; [filename, pathname] = uigetfile({'*.jpg;*.tif;*.PNG;*.gif;*.bmp','All Image Files';...           '*.*','All Files' },'mytitle',...           'C:\Work\myfile.jpg') % récupération du chemin du fichier x = imread(filename); % récupération des données if (length(size(x))>2)% si l'image en niveau de gris est sur 3 composantes couleurs     x=x(:,:,1);% on prend un seul plan end % nombre de bit de la valeur des pixels k = whos('x'); if k.class == 'uint8'     k=8; elseif k.class == 'int16'     k=16; elseif k.class == 'int32'     k=32; elseif k.class == 'int64'     k=64 end   %taille de la matrice [ligne colonne]=size(x); hist = zeros(2^k,1);% création du vecteur de résultat  for i=1:ligne %on parcourt toutes les lignes      for j=1:colonne %on parcourt toutes les colonnes          hist(x(i,j)+1) = hist(x(i,j)+1)+1;% on compte les valeurs      end  end  % affichage de l'image et de l'histogramme  figure(1)  subplot(222)  imshow(x)  subplot(221)  plot([0:(2^k)-1],hist)```

The program have been applyed on left image, the result is shown to the right.

The histogram gives us information on the distribution of pixel values ??in the image. An histogram with many values ??near zero mean a picture under exposed (dark image) while an histogram which have high values lead an over exposed image (image very bright). The average of this histogram shows the value which separates the histogram into two identical parts. It shows where is the majority of pixels. With the previous example we obtain a mean value of 116.9947, it is called Average Optical Density (AOD). It is calculated as .

`aod=sum(hist.*[0:(2^k)-1]')/(ligne*colonne)`

Linear correction

On certain image, the histogram is used to apply a correction. Imagine an image where the large majority (see all) of pixels are located on a small part of possible values. Example only from 30 to 100 for an image of 8 bits (255 values at all). In this case the color (gray level) are distributed over a very short range. It difficult to differentiate them with the eye. The correction will help to spread the colors over the entire range. With the above example the pixels with ??30 values will be put to a zero value and the others will be spread over all the 255 values possible.

```for i=1:ligne        for j=1:colonne          img(i,j) = round((img(i,j)-bas)*(255/haut));        end      end```

Where bas is the value which will go to zero and haut the value which will go to 255 (or the maximal)

let'see the result on an under exposed image.

Logarithmic Correction

This type of correction is applied to the image with a high concentration of point either in the dark (close to zero) in the light (close to 255). The goal is to spread the pixels in a nonlinear way. If it's a dark image, we will space the pixel which are low values and close in them when they are high. the same methodology applies if the picture is bright but the reasoning in reverse ( inverse the logaritm fonction ).
How do it? We will make the correction as linear but instead of applying a proportional transformation, it is logarithmic. for example an image or k = 8, the logarithm is 0 to 255, as the next equation:

```for i= 1:255     hist(i)=round(log(i)*((2^k)/log(2^k))); end plot(hist)```

We find the following curve:

We will again go through all pixels and each pixel value found (abscyss) we associate the value log (ordinate). In this way dark pixels are spaced in the color space and the eye see them better. When the pixel are white, which are less many, they are condensed on very little range value (they are less important because fewer many).
The result:

Correction by cumulative sum of histogram

This time, we will use the histogram of the original image to construct a nonlinear function. For this function we will follow several steps.

• normalize the histogram function: the sum of the vector histogram is 1. we have so:

$hist=\genfrac{}{}{0.1ex}{}{hist}{ligne×colonne}$

• do the cumulative sum of the histogram:

$histsum\left(i\right)=\sum _{a=1}^{i}hist\left(a\right)$

• denormalization of the nonlinear function set for a max = 2*k.

$histsum=histsum\cdot {2}^{k}$

Then, as for Previous corrections, we apply the function for each pixel.
Matlab program:

```clear all; close all; [filename, pathname] = uigetfile({'*.jpg;*.tif;*.PNG;*.gif;*.bmp','All Image Files';...           '*.*','All Files' },'mytitle',...           'C:\Work\myfile.jpg') % récuperation du chemin du fichier x = imread(filename); % récuperation des données if (length(size(x))>2)% si l'image en niveau de gris est sur 3 composantes couleurs     x=x(:,:,1);% on prend un seul plan end % nombre de bit de la valeur des pixel info = whos('x'); if strcmp(info.class,'uint8')     k=8; elseif strcmp(info.class,'uint16')     k=16; elseif strcmp(info.class,'uint32')     k=32; elseif strcmp(info.class,'uint64')     k=64; end   %taille de la matrice [ligne colonne]=size(x); hist = zeros(2^k,1);% création du vecteur de résultat  for i=1:ligne %on parcourt toutes les lignes      for j=1:colonne %on parcourt toutes les colonnes          hist(x(i,j)+1) = hist(x(i,j)+1)+1;% on compte les valeurs      end  end  %normalisation d'histogramme  hist=hist/(ligne*colonne);  %somme cumulative de l'histogramme normalisé  histsum = cumsum(hist);   % dénormalisation  histsum=round(histsum*2^k);  % affichage de l'image et de l'histogramme  figure(1)  subplot(222)  imshow(x)  subplot(221)  plot([0:(2^k)-1],hist)       for i=1:ligne      for j=1:colonne          x(i,j) = histsum(x(i,j)+1);      end   end       hist = zeros(2^k,1);  for i=1:ligne      for j=1:colonne          hist(x(i,j)+1) = hist(x(i,j)+1)+1;      end  end  subplot(224)  imshow(x)  subplot(223)  plot([0:(2^k)-1],hist)          ```

And the result