Group-8 DIP MiniProject

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Mini Project Report

Entitled

Local Histogram and Histogram Specification


Submitted to the Department of Electronics Engineering in Fulfilment for the Requirements
for the Degree of

Bachelor of Technology

(Electronics and Communication)

: Presented & Submitted By :

Karni Tarun (U20EC123)


Kadimi Sri Surya Sashank (U20EC145)
Jamula Durga Prasad (U20EC172)
B.TECH. III(EC), 5th Semester

: Guided By :

Dr. Jignesh N. Sarvaiya


Professor, DoECE

(Year: 2022-23)

DEPARTMENT OF ELECTRONICS ENGINEERING

SARDAR VALLABHBHAI NATIONAL INSTITUTE OF TECHNOLOGY

Surat - 395 007, Gujarat, India


Sardar Vallabhbhai National Institute of Technology
Surat - 395 007, Gujarat, India

DEPARTMENT OF ELECTRONICS ENGINEERING

CERTIFICATE

This is to certify that the Mini Project Report entitled “Local Histogram and Histogram
Specification” is presented & submitted by Karni Tarun, bearing Roll No. U20EC123; Kadimi
Sri Surya Sashank, bearing Roll No. U20EC145; Jamula Durga Prasad, bearing Roll No.
U20EC172, of B.Tech. III, 5th Semester in the fulfillment of the requirement for the award of
B.Tech. Degree in Electronics & Communication Engineering for academic year 2022-23.
They have successfully and satisfactorily completed their Mini Project in all respects.

Dr. Jignesh N. Sarvaiya


Professor, DoECE
Abstract
Histogram specifications have been used successfully in digital image processing for
many years. Histogram equalization is primarily used as an image enhancement tech-
nique.
Histogram Equalization can produce good contrast with almost no effort in algo-
rithm inputs or computational time. More elaborate histograms can address HE prob-
lems at the cost of defining the final histograms in novel ways that may require addi-
tional processing time but are still fast enough to be considered for real-time applica-
tions. This paper proposes a new method for specifying a histogram to improve image
contrast. To support our belief in histogram specification techniques, we also discuss
methods for modifying images, such as to aid segmentation approaches. As proponents
of these techniques, we would like to emphasize the ability of this image-processing
approach to do more than just enhance images.

vii
Chapter 1
Local Histogram and Histogram Specification
WHAT MAKES A GOOD CONTRAST? That is an excellent question because the
image quality is highly subjective, and, as the saying goes, beauty is in the eyes of the
beholder. It depends on the user, and we would like to add that it also depends on the
scene. Developing black-and-white photographs in the darkroom required a test strip,
which was created by exposing the photographic paper to various exposure times, as
shown in Fig. 1. The winning exposure time was the best contrast, which usually meant
one with very dark blacks and very light whites and everything in between.According to
this last statement, histogram equalisation must be one of the most effective techniques.
If only life were that simple. Although HE is the best algorithm, the fact that there is
still a lot of research being done in this area suggests that there is more to be done.
Now, good contrast is also affected by the scene. If you’re photographing a zebra in a
zoo, you’ll want those black and whites, as well as all of the grey level values that can
appear in the background.

1.1 Image Histogram


An image histogram is a plot that gives the number of occurrences of the different pixel
values in the image. Histograms are extensively used to enhance images or extract
useful information from them. Figure 1 shows an eight-bit grayscale image.

Figure 1.1: Flowers Image []

The Histogram of this image is shown in below figure

1
Chapter 1. Local Histogram and Histogram Specification

Figure 1.2: Histogram of Flowers image []

The pixel intensities are shown on the x-axis of an image histogram. There are 256
different pixel values in the eight-bit example shown in Figure 2. (the x-axis ranges
from 0 to 255). The number of pixels for a given pixel value is specified on the his-
togram’s y-axis. A bin of the histogram is any of the possible values of the histogram’s
x-axis.

While the histogram in Figure 2 has the most bins possible for an 8-bit grayscale image,
we sometimes prefer to reduce the number of bins. In this case, we combine adjacent
x-axis values and represent them as a single bin.

1.1.1 Brightness
Brightness is a relative term. It depends on your visual perception. Since brightness is
a relative term, so brightness can be defined as the amount of energy output by a source
of light relative to the source we are comparing it to.

1.1.2 Contrast
Contrast can be simply explained as the difference between maximum and minimum
pixel intensity in an image. Contrast = maximum pixel intensity(subtracted by) mini-
mum pixel intensity.

2
1.2. Enhancement By Histogram Equalization

1.2 Enhancement By Histogram Equalization


Modifying an image such that its histogram has a uniform distribution usually yields a
better contrast. The technique is known as the HE, and the transformation T(r) needed
to obtain this equalization can be formulated as

Z r
s = T (r) = pr (w)dw (1.1)
0

where r is the original pixel’s intensity value, si is the transformed image’s pixel
value, and pr(r) is the probability density function (PDF) associated with the original
image. The image is assumed to be the result of a continuous random variable, and the
histogram resembles its PDF. In this paper, PDF refers to a normalised histogram with
an area equal to one. The HE is then calculated by using the original image’s cumulative
density function (CDF) as the transformation function, as expressed in (1). It is simple
to demonstrate that the transformed image’s PDF is uniformly distributed. (1) becomes
an image with L gray-level values in its discrete form. For images with noise and/or
a constant background, the HE can produce poor results. Because we are focusing on
image enhancement for general photography, we are not concerned with noise; while
using telephoto lenses of, say, f500 mm can introduce haze noise, we are assuming that
inexpensive cameras have zoom capabilities of up to f75 mm, which does not introduce
this type of noise.

Figure 1.3: (a) Original image. (b) Results obtained using the HE. []

Nevertheless, the HE can produce positive results. As can be seen in Fig. 2(a), an
original image has been modified by the HE (b). The brightening of the dark shadows
on the woman’s face allows us to see more details in the HE result.

3
Chapter 1. Local Histogram and Histogram Specification

1.2.1 Example
The process for histogram equalization is as follows:
Step 1: Obtain the histogram
For example, if the image is grayscale with 256 distinct intensity levels i (where i = 0
[black], 1, 2, . . . . 253, 254, 255 [white]), the probability that a pixel chosen at random
will have an intensity level i is as follows:
Pi=number of pixels of intensity level/Total number of pixel values

Step 2: Obtain the cumulative distribution function CDF


The cumulative distribution function H(j) is defined as the probability H of a randomly
selected pixel taking one of the intensity values from 0 through j (inclusive). Therefore,
given our normalized histogram h(i) from above, we have the following formula:
j
X
CDF = H(j) = h(i) where j = 0, 1, . . . ., 254, 255 (1.2)
i=0

Step 3: Calculate the transformation T to map the old intensity values to new
intensity values
Let K represent the total number of possible intensity values (e.g. 256). j is the old
intensity value, and T(j) is the new intensity value.

T (j) = floor ((K − 1) ∗ CDFj ) (1.3)

Step 4: Given the new mappings of intensity values, we can use a lookup table
to transform each pixel in the input image to a new intensity The result of this
transformation is a new histogram which corresponds to a new output image.
.

1.3 Histogram Specification (HS)


For z [0, 1], the HS produces an image with a PDF that follows a specified shape fZ(z).
If HE is applied to this final image, the result will be an image with a uniform PDF, i.e.,
Z r
s = T (r) = pr (w)dw (1.4)
0

Equations (1) and (2) can be combined to create the transformation function that pro-
duces the specified histogram, z = G1(s) = G1[T(r)]. The simple implementation of the
HS for digital normalised images with L gray-level values is based on the formulation
of

4
1.3. Histogram Specification (HS)

k
X 1 2
sk = T (rk ) = fR (rj ) for k = 0, , ,...,1 (1.5)
j=0
L−1 L−1

k
X 1 2
sk = T (rk ) = fR (rj ) for k = 0, , ,...,1 (1.6)
j=0
L−1 L−1

One can see how HS can potentially improve images if we know ahead of time
which histogram will result in a better image.That is a big if, especially if the algorithm
proposed here is fully automatic. It is one thing to have a few parameters to adjust for an
image enhancement technique to be used in, say, general photography, but specifying L
1 values for a final histogram is quite discouraging.

Figure 1.4: (a) Original image. (b) Histogram equalization.

1.3.1 Example
Let us suppose we have two images, an input image and a specified image. We want to
use histogram matching to force the input image to have a histogram that is the shape
of the histogram of the specified image. The first few steps are similar to histogram
equalization, except we are performing histogram equalization on two images (original
image and the specific image).
Step 1:Obtain the histogram for both the input image and the specified image
(same method as in histogram equalization) For example, if both images are grayscale
with 256 distinct intensity levels i (where i = 0 [black], 1, 2, . . . . 253, 254, 255 [white]),
the probability that a pixel chosen at random will have an intensity level i is as follows:

hinput (i) = pi
= (number of pixels of intensity level i/ total number of pixels)
= normalized histogram
= probability density function PDF

5
Chapter 1. Local Histogram and Histogram Specification

hspecified (i) = pi
= (number of pixels of intensity level i/ total number of pixels)
= normalized histogram
= probability density function PDF
Step 2: Obtain the cumulative distribution function CDF for both the input image
and the specified image (same method as in histogram equalization) The cumulative
distribution function H(j) is defined as the probability H of a randomly selected pixel
taking one of the intensity values from 0 through j (inclusive). Therefore, given our
normalized histograms h(i) from above, we have the following formula:

j
X
CDF = H(j) = h(i) where j = 0, 1, . . . . . . , 254, 255 (1.7)
i=0

Step 3: Calculate the transformation T to map the old intensity values to new
intensity values for both the input image and specified image (same method as in
histogram equalization) Let K represent the total number of possible intensity values
(e.g. 256). j is the old intensity value, and T(j) is the new intensity value.

Tinput (j) = floor ((K − 1) ∗ ∗ CDFj )


(1.8)
Tspecified (j) = floor ((K − 1) ∗ CDFj )

Step 4: Use the transformed intensity values for both the input image and specified
image to map the intensity values of the input image to new values We go through
each available intensity value j one at a time, doing the following steps:
See what the transformed intensity value is for the input image given the intensity
value j. Let us call this Tinput(j). We then find the Tspecified(j) that is closest to
Tinput(j) and make a note of what j is. For example, if j = 4:

Tinput (4) = 3
(1.9)
Tspecified (1) = 3

we map all intensity values of 4 in the input image to 1. Here is another example. Let
us suppose that
Tinput (5) = 4
(1.10)
Tspecified (2) = 4
Therefore, we map all intensity values of 5 in the input image to 2.
After we have gone through all available intensity values and performed all the
mappings, we have our output image which has a histogram that will approximately
match the shape of the unequalized specified histogram.

6
1.4. Applications of Histograms

1.4 Applications of Histograms


• In digital image processing, histograms are used for simple calculations in soft-
ware.

• The brightness of the image can be adjusted by having the details of its histogram.

• The contrast of the image can be adjusted according to the need by having details
of the x-axis of a histogram.

• It is used for image equalization. Gray level intensities are expanded along the
x-axis to produce a high contrast image.

• Histograms are used in thresholding as it improves the appearance of the image.

• If we have input and output histogram of an image, we can determine which type
of transformation is applied in the algorithm.

1.5 Conclusion
A contrast adjustment mapping can be used to stretch the pixel values of a given image
to occupy the full range of available values. This method considers the minimum and
the maximum pixel values of the image even if they appear only once in the image.In
contrast, the histogram equalization method takes the number of occurrences of the
different pixel values into account and attempts to flatten the image histogram. This
method can improve the overall contrast of the image at the cost of reducing the contrast
in the regions of the image that correspond to the less frequent pixel values.

***

7
Code for Histogram Equalization:

clc;
clear all;
close all;
rk=[0 1 2 3 4 5 6 7];
nk=[790 1023 850 656 329 245 122 81];
i=790;
%Running sum of nk=i
for j=1:7
i(j+1)=i(j)+nk(j+1);
end
i
N=i(8)
%Finding PDF
Pdf=0;
for k=1:8
Pdf(k)=nk(k)/N;
end
Pdf
%Finding CDF
Cdf(1)=Pdf(1);
for k=2:8
Cdf(k)=Cdf(k-1)+Pdf(k);
end
Cdf
%Finding Sk values
Sk=0;
for k=1:8
Sk(k)=round(7*Cdf(k));
end
ak=nk;

c=0;

for k=1:7
if Sk(k+1)==Sk(k)
ak(k+1)=ak(k)+ak(k+1);
c=c+1;
if c==1

l=max(ak(k+1));
end
if c==3
l1=max(ak(k+1));
end
end
end
dk=ak;
b=0;
for k=1:7
if Sk(k+1)==Sk(k)
b=b+1;
if b==1
dk(k)=l;
dk(k+1)=l;
end
if b==3
dk(k)=l1;
dk(k+1)=l1;
dk(k-1)=l1;
end
end
end
dk
Sk
subplot(2,1,1);
stem(rk,nk); title('Input Image');
subplot(2,1,2);
stem(Sk,dk); title('Equalized Histogram');
Output:
Code for Histogram Matching:
clc;
clear all;
close all;
rk=[0 1 2 3 4 5 6 7];
nk=[790 1023 850 656 329 245 122 81];
i=790;
%Running sum of nk
for j=1:7
i(j+1)=i(j)+nk(j+1);
end
i
N=i(8)
%Finding PDF
Pdf=0;
for k=1:8
Pdf(k)=nk(k)/N;
end
Pdf
%Finding CDF
Cdf(1)=Pdf(1);
for k=2:8
Cdf(k)=Cdf(k-1)+Pdf(k);
end
Cdf
%Finding Sk values
Sk=0;
for k=1:8
Sk(k)=round(7*Cdf(k));
end
ak=nk;

c=0;

for k=1:7
if Sk(k+1)==Sk(k)
ak(k+1)=ak(k)+ak(k+1);
c=c+1;
if c==1

l=max(ak(k+1));
end
if c==3
l1=max(ak(k+1));
end
end
end
dk=ak;
b=0;
for k=1:7
if Sk(k+1)==Sk(k)
b=b+1;
if b==1
dk(k)=l;
d(k+1)=l;
end
if b==3
dk(k)=l1;
dk(k+1)=l1;
dk(k-1)=l1;
end
end
end
dk

figure(1);
subplot(2,1,1);
stem(rk,nk); title('Original Image Histogram');
xlabel('Gray Level Values');ylabel('No of Pixels');
subplot(2,1,2);
stem(Sk,dk); title('Equalized Histogram');
xlabel('Gray Level Values');ylabel('No of Pixels');

%%
%reference Image

rk=[0 1 2 3 4 5 6 7];
nk1=[0 0 0 614 819 1230 819 614];
i1=0;
%Running sum of nk1
for j=1:7
i1(j+1)=i1(j)+nk1(j+1);
end
i1
N1=i(8)
%Finding PDF1
Pdf1=0;
for k=1:8
Pdf1(k)=nk1(k)/N1;
end
Pdf1
%Finding CDF1
Cdf1(1)=Pdf1(1);
for k=2:8
Cdf1(k)=Cdf1(k-1)+Pdf1(k);
end
Cdf1
%Finding Zk values
Zk=0;
for k=1:8
Zk(k)=round(7*Cdf1(k));
end
ak1=nk1;

c1=0;

for k=1:7
if Zk(k+1)==Zk(k)
ak1(k+1)=ak1(k)+ak1(k+1);
c1=c1+1;
if c1==1
l0=max(ak1(k+1));
end
if c1==3
l11=max(ak1(k+1));
end
end
end
dk1=ak1;
b1=0;
for k=1:7
if Zk(k+1)==Zk(k)
b1=b1+1;
if b1==10
dk1(k)=l0;
dk1(k+1)=l0;
end
if b1==3
dk1(k)=l11;
dk1(k+1)=l11;
dk1(k-1)=l11;
end
end
end
dk1
Zk
figure(2);
subplot(2,1,1);
stem(rk,nk1); title('Reference Image Histogram');
xlabel('Gray Level Values');ylabel('No of Pixels');
subplot(2,1,2);
stem(Zk,dk1); title('Equalized Histogram of Reference Image');
xlabel('Gray Level Values');ylabel('No of Pixels');

%%
%mapping
g=0;
w=0;
x=0;
Zq=0;
for k=1:8
for m=1:8
if Zk(k)==(Sk(m))+1
g(k)=dk(m+1);

end
if Zk(k)==Sk(m)
g(k)=dk(m);

end

end
end

for k=1:8
if g(k)==0
Zq(k)=0;
else
Zq(k)=k-1;
end
end

figure(3);
g
rk
Sk
Zq
stem(Zq,g);
title('Matched Histogram');
xlabel('Gray Level Values');ylabel('No of Pixels');

Output:
Command Window:

i=

790 1813 2663 3319 3648 3893 4015 4096

N=

4096

Pdf =

0.1929 0.2498 0.2075 0.1602 0.0803 0.0598 0.0298 0.0198

Cdf =

0.1929 0.4426 0.6501 0.8103 0.8906 0.9504 0.9802 1.0000


dk =

790 1023 850 985 985 448 448 448

i1 =

0 0 0 614 1433 2663 3482 4096

N1 =

4096

Pdf1 =

0 0 0 0.1499 0.2000 0.3003 0.2000 0.1499

Cdf1 =

0 0 0 0.1499 0.3499 0.6501 0.8501 1.0000

dk1 =
0 0 0 614 819 1230 819 614

Zk =

0 0 0 1 2 5 6 7

g=

0 0 0 790 1023 850 985 448

rk =

0 1 2 3 4 5 6 7

Sk =

1 3 5 6 6 7 7 7

Zq =

0 0 0 3 4 5 6 7

>>

You might also like