0% found this document useful (0 votes)
11 views20 pages

Introduction To OpenCV Shashi 02

The document is a lecture on Image Processing and Machine Learning, focusing on the fundamentals of images, including pixel representation, brightness, contrast, and histogram equalization. It explains concepts such as image size, pixel intensity, and methods for enhancing image contrast through scaling and histogram equalization. The lecture references a textbook and provides visual examples to illustrate the discussed concepts.

Uploaded by

chadchampion183
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views20 pages

Introduction To OpenCV Shashi 02

The document is a lecture on Image Processing and Machine Learning, focusing on the fundamentals of images, including pixel representation, brightness, contrast, and histogram equalization. It explains concepts such as image size, pixel intensity, and methods for enhancing image contrast through scaling and histogram equalization. The lecture references a textbook and provides visual examples to illustrate the discussed concepts.

Uploaded by

chadchampion183
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

IDC410

A course on Image Processing and


Machine Learning
(Lecture 02)

Shashikant Dugad,
IISER Mohali

Shashikant R. Dugad, IISER Mohali 1


Reference for Image Processing: MACHINE VISION. by
Ramesh Jain, Rangachar Kasturi, Brian G. Schunck
Published by McGraw-Hill, Inc., ISBN 0-07-032018-7, 1995

https://cse.usf.edu/~r1k/MachineVisionBook/MachineVision.files/

g e ?
Ima
s
at i
Wh

Shashikant R. Dugad, IISER Mohali 2


What is an Image?

§ Image is a large matrix (N x M) of pixels

§ Each pixel is represented by it’s color

§ Color is a mixture of 3 fundamental colors namely Red, Green and


Blue (RGB) as per intensity of each color

§ Intensity of each of the color is represented by a single byte, thus


having a value between 0-255

§ For a Grayscale image: Pixel intensity is denoted by a single byte


varying between 0 (black) to 255 (white).
The image read with the OpenCV function imread(),
provides the colours in order of BGR (Blue, Green and Red).

Shashikant R. Dugad, IISER Mohali 3


What is an Image?
Pixel j direction (column)
Image Size: Image denoted by n X m pixels, where n is

Pixel i direction (Row)


[0, 0]
number of pixels in vertical direction (or number of rows)
and m is number of pixels in horizontal direction (or
number of columns) correspond to a size of 3nm bytes
[i, j]
(before compression) . [x, y]

Pixel Location: A location of pixel in an image M is


denoted by M(i, j) where, i is a row number (vertical

y-direction
direction) and j is a column number (horizontal direction)
[0, 0]
The image read with the OpenCV function imread(),
provides the colours in order of BGR (Blue, Green and Red). x-direction

Shashikant R. Dugad, IISER Mohali 4


Physical Object Geometry and Image Geometry

𝐫" 𝐳 𝐟 𝐟
= , 𝐱! = 𝐳
𝐱 and 𝐲! = 𝐳
𝐲 𝒙" = 𝒋 −
𝒎$𝟏
and 𝒚" = − 𝒊 −
𝒏$𝟏
𝐫 𝐟 𝟐 𝟐

Image size: N X M
𝑟= 𝑥 ! + 𝑦 ! and 𝑟 " = 𝑥"! + 𝑦"!

Shashikant R. Dugad, IISER Mohali 5


Ideal Image Size Resolution
Application
(in pixels) (in pixels/inch)

For mobile phones and portable devices with small


320 x 240 pixels 72 ppi
screens:

For emails, online sharing sites and viewing on


1024 x 768 pixels 72 ppi
standard computer monitors:

Viewing at full-screen size on LCD monitors with 5:4


1280 x 1024 pixels 72 ppi
aspect ratio

Viewing on standard definition TV sets with 4:3 aspect


720 x 576 pixels 72 ppi
ratios:

Viewing on widescreen standard definition TV sets: 1280 x 720 pixels 72 ppi

Viewing on high definition TV sets: 1920 x 1080 pixels 72 ppi

Shashikant R. Dugad, IISER Mohali 6


s t
n tra
C o
n d
a
s s
n e
h t
rig
B

Shashikant R. Dugad, IISER Mohali 7


Image Brightness and Contrast link
● Brightness of image describes the overall lightness or darkness of an image. Increasing the brightness makes dark
colours lighter and light colour whiter!
● Contrast of image describes the overall differences the difference in brightness between objects or regions of an
image. A high-contrast image has very bright highlights and very dark shadows. For example, a black dog against
a white background has good contrast, while a white rabbit running across a snowy field has poor contrast.

Shashikant R. Dugad, IISER Mohali 8


Image Brightness

Original Brightness: +40% Brightness: -40%

I’[i, j] = sF* I[i, j] if(I’[i, j])>255) then I’[i, j]=255

Scale Factor sF <1 implies make image darker

Shashikant R. Dugad, IISER Mohali 9


Image Contrast
Min = Minimum(M[i,j])

Max = Maximum(M[i,j])

Diff = Max – Min


255 ∗ (𝑀 𝑖, 𝑗 − 𝑀𝑖𝑛)
𝑀" [𝑖, 𝑗] =
𝐷𝑖𝑓𝑓

Other methods:
Histogram Equalization…

Shashikant R. Dugad, IISER Mohali 10


CHAPrnR4. ~AGEF~TEm
Contrast Algorithms

• The original image has very poor contrast since the gray values are in
a very small range.
• Histogram scaling improves the contrast but leaves gaps in the final
histogram.
Shashikant R. Dugad, IISER Mohali 11
Contrast Algorithm: Image Scaling
• A simple example of histogram modification is image scaling:
• Original image has the pixel intensity in the range of [a,b]
• In scaled-up image the pixel intensities are expanded to fill the range [z1, zk].
• The formula for mapping a pixel intensity value I in the original image range into
a pixel intensity value I' in the new image intensity range is:
𝒛𝒌 − 𝒛𝟏
𝑰! = 𝑰 − 𝐚 + 𝒛𝟏
𝒃−𝒂
• Note: a, b, z1 and zk are constants
• The problem with this scheme is that when the histogram is stretched according
to this formula, the resulting histogram has gaps between bins

Shashikant R. Dugad, IISER Mohali 12


Image Contrast:
CHAPrnR4. Before and After Scaling
~AGEF~TEmNG
~AGEF~TEmNG

The original image has very poor


contrast since the gray values are in
a very small range.

Histogram scaling improves the


contrast but leaves gaps in the final
histogram.

Top: Original image and histogram.

Bottom: Histogram Scaled Image and


resulting histogram after image
scaling.

Shashikant R. Dugad, IISER Mohali 13


Image Contrast: Histogram Equalization
● Consider a gray scale image of size N x N, thus, having N2 pixels. Position of a pixel is
denoted by (y, x)

● If the intensity, I(y, x), of a pixel is represented by m bits then.


○ the maximum intensity is Imax = (2m - 1)

● Image contrast is optimized by doing smart intensity transformation function!


○ I’(y,x) = T(I(x,y)) T is a function of intensity transformation

● For a given original image, obtain intensity histogram, n = h(I), where, a) n is number of
pixels having intensity I and b) range of intensity is 0<= I < (2m - 1) èTotal # of bins in
histogram are (2m - 1)
○ Note: ∑ 𝒏 = ∑ 𝒉(𝑰) = 𝑵𝟐
𝒏
● Probability of a pixel having intensity I is: 𝒑 𝑰 = 𝑵𝟐
where, ∑ 𝒑(𝑰) = 𝟏

Shashikant R. Dugad, IISER Mohali 14


Image Contrast: Histogram Equalization
● Let us apply following constraints on te intensity transformation function
○ T(I) must be a strictly increasing function. This makes it an injective (one-to-one)
function.
○ 0 ≤ T( I ) ≤ L-1. This makes T(I) surjective.

● The above two conditions make T(I) a bijective function, thus inversible
○ Therefore, there exist a function that provides, I = T-1(I’)

● Cumulative Distribution Function (CDF) for input image can be defined as:
𝑰 𝑰
𝟏
𝑭𝒓 𝑰 = 𝑷(𝒓 ≤ 𝑰) = , 𝒑𝒓 𝒊 = 𝟐 , 𝒏𝒊
𝑵
𝒊/𝟎 𝒊/𝟎

● CDF for final image can be written as,


𝑭𝒔 𝒙 = 𝑷 𝒔 ≤ 𝒙 = 𝑷 𝑻 𝒓 ≤ 𝒙 = 𝑷 𝒓 ≤ 𝑻3𝟏 𝒙 = 𝑭𝒓(𝑻3𝟏 (𝒙))

Shashikant R. Dugad, IISER Mohali 15


distribution function (CDF) of r is Fr (x). Now the CDF of s will be :
Image Contrast: Histogram Equalization
FS(x) = P (s ≤ x) = P (T (r) ≤ x) = P (r ≤ T − 1(x)) = F r(T − 1(x)).

We put the first condition of T(r) precisely to make the above step hold true. The second
condition is needed as s is the intensity value for the output image and so must be between o
and (L-1).

So, a pdf of s can be obtained by differentiating FS( x ) with respect to x. We get the following
relation:
ps (s) = pr (r) dr
ds

Now, if we define the transformation function as follows:


r
s = T (r) = (L − 1) ∫0 pr (x)dx

Then using this function gives us a uniform pdf for s.


ds r
dr
= (L − 1) drd ∫0 pr (x)dx = (L − 1)pr (r)

The above step used Leibnitz’s integral rule.


Shashikant UsingIISER
R. Dugad, the Mohali
above derivative, we get: 16
Then using this function gives us a uniform pdf for s.
ds r
Image Contrast: Histogram Equalization
dr
= (L − 1) drd ∫0 pr (x)dx = (L − 1)pr (r)

The above step used Leibnitz’s integral rule. Using the above derivative, we get:
ps (s) = pr (r) dr
ds
= pr (r) 1
(L−1)pr (r)
= 1
L−1

So the pdf of s is uniform. This is what we want.

Now, we extend the above continuous case to the discrete case. The natural replacement of the
integral sign is the summation. Hence, we are left with the following histogram equalization
transformation function.
(L−1)
sk = T (rk ) = (L − 1) ∑kj=0 pr (rj ) = N2
∑kj=0 nj

Since s must have integer values, any non-integer value obtained from the above function is
rounded off to the nearest integer.

Shashikant R. Dugad, IISER Mohali 17


Histogram Equalization: Actual Calculation
Image Size: 8 x 9 Pixel intensity: 3 bits L=8
Probbility
Pixel Intensity in Frequency Cumulative Pixel Intensity (I')
I*f Distribution (L-1) *CPDF I' * f
Original Image (I) (f) PDF (CPDF) in Final Image
Function (PDF)
0 2 0 0.02778 0.02778 0.19444 0 0
1 4 4 0.05556 0.08333 0.58333 1 4

2 6 12 0.08333 0.16667 1.16667 1 6

3 8 24 0.11111 0.27778 1.94444 2 16


4 10 40 0.13889 0.41667 2.91667 3 30
5 12 60 0.16667 0.58333 4.08333 4 48

6 14 84 0.19444 0.77778 5.44444 5 70


7 16 112 0.22222 1.00000 7.00000 7 112

Total = 72 336 1 Total = 286


Average Intensity = 4.6667 Average Intensity = 4

Shashikant R. Dugad, IISER Mohali 18


Image Contrast Results
CHAPrnR4. ~AGEF~TEmNG
EAR SYSTEMS 115
A B

Figure 4.2: The original image has very poor contrast since the gr
A: Original
are in a very small range. Histogram scaling improves the contrast b
gaps in the final histogram. Top: Original
B: Histogram Scaling image and histogram.
Image and resulting histogram after histogram scaling.
C: Histogram Equalization
find the value k2 such that
C Shashikant R.
k2-1 k2 19
3:The
Theoriginal
originalimage
image has
has very
very poor
poor contrast
contrast theDugad,
since the
since grayIISER
gray Mohali
values
values
.L Pi :::; ql + q2 < .L Pi.
Image before and after Histogram Equalization

https://en.wikipedia.org/wiki/Histogram_equalization
Source:
Shashikant R. Dugad, IISER Mohali 20

You might also like