Lets Learn together... Happy Reading

" Two roads diverged in a wood, and I,
I took the one less traveled by,
And that has made all the difference "-Robert Frost

Balance Contrast Enhancement Technique (BCET)

Input Image: http://www.freeimages.co.uk




This technique provides solution to biased color (RGB) composition. The contrast of the image can be stretched or compressed without changing the histogram pattern of the input image(x). The solution is based on the parabolic function obtained from the input image.



The general form of the parabolic function is defined as

The three coefficientsa’,’band ‘c’are derived from the following inputs ,
·        Minimum value of the output image(y)
·        Maximum value of the output image
·        Mean value of the output image



Where
·       ‘l’ represents the minimum value of the input image
·       ‘h’ denotes the maximum value of the input image
·       ‘e’ denotes the mean value of the input image
·       ‘L’ represents the minimum value of the output image
·       ‘H’ denotes the maximum value of the output image
·       ‘E’ denotes the mean value of the output image
·         ‘s’ denotes the mean square sum of the input image

MATLAB CODE:

I = imread('pout.tif'); %READ THE INPUT IMAGE
figure,subplot(121), imshow(I);title('INPUT IMAGE');
x    = double(I); % INPUT IMAGE
Lmin = min(x(:)); % MINIMUM OF INPUT IMAGE
Lmax = max(x(:)); % MAXIMUM OF INPUT IMAGE
Lmean = mean(x(:)); %MEAN OF INPUT IMAGE
LMssum = mean(x(:).^2); %MEAN SQUARE SUM OF INPUT IMAGE

Gmin = 0; %MINIMUM OF OUTPUT IMAGE
Gmax = 255; %MAXIMUM OF OUTPUT IMAGE
Gmean = 110; %MEAN OF OUTPUT IMAGE

bnum = Lmax.^2*(Gmean-Gmin) - LMssum*(Gmax-Gmin) + Lmin.^2*(Gmax-Gmean);
bden = 2*(Lmax*(Gmean-Gmin)-Lmean*(Gmax-Gmin)+Lmin*(Gmax-Gmean));

b = bnum/bden;

a = (Gmax-Gmin)/((Lmax-Lmin)*(Lmax+Lmin-2*b));

c = Gmin - a*(Lmin-b).^2;

y = a*(x-b).^2+c; %PARABOLIC FUNCTION
y = uint8(y);

subplot(122),imshow(y);title('OUTPUT IMAGE');



I_hist = imhist(I(:));
O_hist = imhist(y(:));

figure,stem([0:255],I_hist);hold on;stem([0:255],O_hist);legend INPUT OUTPUT




EXPLANATION:
Set the minimum value to zero and maximum value to 255. Set the mean value to 110.
From the histogram of the output image, it is evident that the minimum value is zero and maximum value 255. The histogram is stretched but the shape is retained.

EXERCISE:
·         Try with different minimum, maximum and mean values for the input image to study the result.
·         Compare the results obtained using ‘Linear contrast Enhancement technique’ and comment your answers below


Let’s try the BCET solution on the RGB image.
Save the below code as a function and name it as ‘BCET.m’

MATLAB CODE:
function y=BCET(Gmin,Gmax,Gmean,x)

    x    = double(x); % INPUT IMAGE
    Lmin = min(x(:)); % MINIMUM OF INPUT IMAGE
    Lmax = max(x(:)); % MAXIMUM OF INPUT IMAGE
    Lmean = mean(x(:)); %MEAN OF INPUT IMAGE
    LMssum = mean(x(:).^2); %MEAN SQUARE SUM OF INPUT IMAGE


    bnum = Lmax.^2*(Gmean-Gmin) - LMssum*(Gmax-Gmin) + Lmin.^2*(Gmax-Gmean);
    bden = 2*(Lmax*(Gmean-Gmin)-Lmean*(Gmax-Gmin)+Lmin*(Gmax-Gmean));

    b = bnum/bden;

    a = (Gmax-Gmin)/((Lmax-Lmin)*(Lmax+Lmin-2*b));

    c = Gmin - a*(Lmin-b).^2;

    y = a*(x-b).^2+c; %PARABOLIC FUNCTION
    y = uint8(y);
end


Now, open script in the editor and try the following code. Make sure the function BCET.m is available in the current working directory.

MATLAB CODE:
%READ THE INPUT IMAGE
A = imread(‘landsat5.png');
%https://landsatlook.usgs.gov

%PREALLOCATE THE OUTPUT IMAGE MATRIX
Output = zeros(size(A));
R = A(:,:,1); %RED CHANNEL
G = A(:,:,2); %GREEN CHANNEL
B = A(:,:,3); %BLUE CHANNEL

figure(1),subplot(211),stem([0:255],imhist(R(:)),'r');hold on;
stem([0:255],imhist(G(:)),'g');hold on;
stem([0:255],imhist(B(:)),'b');
title('HISTOGRAM OF THE INPUT IMAGE');
legend RED GREEN BLUE

Gmin = 0; %MINIMUM VALUE OF THE OUTPUT IMAGE
Gmax = 255; %MAXIMUM VALUE OF THE OUTPUT IMAGE
Gmean = 120; %MEAN VALUE OF THE OUTPUT IMAGE

%PARABOLIC FUNCTION
R=BCET(Gmin,Gmax,Gmean,R);
G=BCET(Gmin,Gmax,Gmean,G);
B=BCET(Gmin,Gmax,Gmean,B);

Output(:,:,1)=R;
Output(:,:,2)=G;
Output(:,:,3)=B;
Output = uint8(Output);

subplot(212),stem([0:255],imhist(R(:)),'r');hold on;
stem([0:255],imhist(G(:)),'g');hold on;
stem([0:255],imhist(B(:)),'b');
title('HISTOGRAM OF THE OUTPUT IMAGE');
legend RED GREEN BLUE

figure(2),subplot(121),imshow(A);title('INPUT IMAGE');
subplot(122),imshow(Output);title('OUTPUT IMAGE');






EXPLANATION:
The minimum and maximum values for all the channels are set as 0 and 255. And the mean value is set as 120.

The mean values of the Red, Green and Blue channels are different as shown in the table below.

INPUT IMAGE
MIN VALUE
MAX VALUE
MEAN VALUE
RED COMPONENT
0
255
43
GREEN COMPONENT
0
255
53.54
BLUE COMPONENT
0
255
39.6

After applying the BCET solution, the result shown below indicates that the mean values of all the channels are balanced.

OUTPUT IMAGE
MIN VALUE
MAX VALUE
MEAN VALUE
RED COMPONENT
0
255
120
GREEN COMPONENT
0
255
119.9
BLUE COMPONENT
0
255
120

To understand the mathematical derivation and the limitations of this technique refer the following paper.

Reference:
Balance contrast enhancement technique and its application in image colour composition http://dx.doi.org/10.1080/01431169108955241
like button Like "IMAGE PROCESSING" page

Linear Contrast Enhancement

The function to perform the enhancement

The coefficient ‘a’ performs the contrast enhancement while the coefficient ‘b’ is for adjusting the brightness.
Let’s start with an example,
MATLAB CODE:
%READ THE INPUT IMAGE
I = imread('vesuvius.png');

a = 1;
b = 60;

y =  a*double(I) + b;
y =  uint8(y);

%HISTOGRAM OF THE INPUT AND OUTPUT IMAGE
hist_I=imhist(I);
hist_y=imhist(y);

figure,stem([0:255],hist_I);hold on; stem([0:255],hist_y);legend Original Enhanced
figure,subplot(1,2,1),imshow(I);title('Original Image');
       subplot(1,2,2);imshow(y);title('After LCE Enhacement');



EXPLANATION:
The function ‘y’ will give an output where the values of the pixels in the input image will be increased by 60.



The behavior of the histogram of the output image is not changed. It retains the shape of the input histogram and also the information but it is shifted to the right by 60 pixels.
If pixels are subtracted then the histogram will be shifted to the left side and the image will look darker.
The table below represents the pixel bins from 0 to 255 and the number of pixels for each bin value in the input and output image.



The total number of pixels in the input image with the value zero is 20809 after the enhancement i.e the addition of 60 to the zero value , the total number of pixels in the output image with the value 60 is 20809. While the pixel value ‘60’ in the input image has the total number of occurrence 17004 , the output image will have the same number of pixel occurrence at the pixel value 120.



EXAMPLE 2:
MATLAB CODE:
%READ THE INPUT IMAGE
I = imread('vesuvius.png');

a = 1.58;
b = 0;

y =  a*double(I) + b;
y =  uint8(y);

%HISTOGRAM OF THE INPUT AND OUTPUT IMAGE
hist_I=imhist(I);
hist_y=imhist(y);

figure,stem([0:255],hist_I);hold on; stem([0:255],hist_y);legend Original Enhanced
figure,subplot(1,2,1),imshow(I);title('Original Image');
       subplot(1,2,2);imshow(y);title('After LCE Enhacement');







The histogram of the output image has the same values as of the histogram of the input image but stretched or expanded.  The coefficients ‘a’ and ‘b’ can be modified based on the visual analysis.
The minimum value of the input image:  0
The maximum value of the input image: 161
The mean of the input image:  87.86

The minimum value of the output image:  0
The maximum value of the output image: 254
The mean of the output image: 138.84

The result of the output image indicates that the pixel values are spread between 0 and 255.






The coefficient a = 1.58

The bin number 1 is multiplied with 1.58.

NEW VALUE = 2( rounding off)

So the number of occurrence of pixel value 1 is now the number of occurrence of pixel value 2 (MARKED in GREEN)



Similarly, the bin number 3 is multiplied with 1.58

NEW VALUE = round(4.74) = 5

The number of occurrence of pixel value 3 before enhancement is 14805 after multiplying with 1.58, the value 14805 is moved to the bin 5. (MARKED in RED)



EXAMPLE 3:



When the values are normalized between 0 and 1, it will be multiplied by 255 to get the pixel values between 0 and 255.


MATLAB CODE:
I = imread('vesuvius.png');
I = double(I);
y = 255*((I-min(I(:)))/range(I(:)));

y = uint8(y);
I = uint8(I);
%HISTOGRAM OF THE INPUT AND OUTPUT IMAGE
hist_I=imhist(I);
hist_y=imhist(y);

figure,stem([0:255],hist_I);hold on; stem([0:255],hist_y);legend Original Enhanced
figure,subplot(1,2,1),imshow(I);title('Original Image');
       subplot(1,2,2);imshow(y);title('After LCE Enhacement');


Contrast Enhancement for RGB image
MATLAB CODE:
clear all
clc
close all
%READ THE INPUT IMAGE
I = imread('Landsat5.png');

%PREALLOCATE THE OUTPUT IMAGE MATRIX
Oimg = zeros(size(I));

R = I(:,:,1); %RED COMPONENT
G = I(:,:,2); %GREEN COMPONENT
B = I(:,:,3); %BLUE COMPONENT

R_hist = imhist(R);%HISTOGRAM OF THE RED COMPONENT
G_hist = imhist(G);%HISTOGRAM OF THE GREEN COMPONENT
B_hist = imhist(B);%HISTOGRAM OF THE BLUE COMPONENT

figure,stem([0:255],R_hist,'r');hold on; stem([0:255],G_hist,'g');hold on; stem([0:255],B_hist,'b');legend Red Green Blue;title('Original Image');




min(R(:)),max(R(:)),mean(R(:))
min(G(:)),max(G(:)),mean(G(:))
min(B(:)),max(B(:)),mean(B(:))


MIN VALUE
MAX VALUE
MEAN VALUE
RED COMPONENT
0
108
43.00
GREEN COMPONENT
0
121
53.54
BLUE COMPONENT
0
111
39.63



R = double(R);
G = double(G);
B = double(B);

%NORMALIZE THE PIXEL VALUES BETWEEN 0 AND 255
R = uint8(255*((R-min(R(:)))/range(R(:))));
G = uint8(255*((G-min(G(:)))/range(G(:))));
B = uint8(255*((B-min(B(:)))/range(B(:))));




min(R(:)),max(R(:)),mean(R(:))
min(G(:)),max(G(:)),mean(G(:))
min(B(:)),max(B(:)),mean(B(:))






MIN VALUE
MAX VALUE
MEAN VALUE
RED COMPONENT
0
255
101.55
GREEN COMPONENT
0
255
112.84
BLUE COMPONENT
0
255
91.0471


R_hist = imhist(R);%HISTOGRAM OF THE RED COMPONENT
G_hist = imhist(G);%HISTOGRAM OF THE GREEN COMPONENT
B_hist = imhist(B);%HISTOGRAM OF THE BLUE COMPONENT

figure,stem([0:255],R_hist,'r');hold on; stem([0:255],G_hist,'g');hold on; stem([0:255],B_hist,'b');legend Red Green Blue; title('After Enhancement');






Oimg(:,:,1)=R; %ENHANCED RED COMPONENT
Oimg(:,:,2)=G; %ENHANCED GREEN COMPONENT
Oimg(:,:,3)=B; %ENHANCED BLUE COMPONENT

Oimg = uint8(Oimg); %RGB IMAGE AFTER ENHANCEMENT


figure,subplot(1,2,1),imshow(I);title('Original Image');
       subplot(1,2,2);imshow(Oimg);title('After LCE Enhacement');


PIC: USGS/NASA Landsat



EXPLANATION:
The RGB image is read into a matrix. The Red, Green and Blue channels are normalized between 0 and 255 separately.



Also check 'Balance Contrast Enhancement Technique'
like button Like "IMAGE PROCESSING" page
Next Post Home
Google ping Hypersmash.com