Introduction To OpenCV Shashi 02
Introduction To OpenCV Shashi 02
Shashikant Dugad,
IISER Mohali
https://cse.usf.edu/~r1k/MachineVisionBook/MachineVision.files/
g e ?
Ima
s
at i
Wh
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
𝐫" 𝐳 𝐟 𝐟
= , 𝐱! = 𝐳
𝐱 and 𝐲! = 𝐳
𝐲 𝒙" = 𝒋 −
𝒎$𝟏
and 𝒚" = − 𝒊 −
𝒏$𝟏
𝐫 𝐟 𝟐 𝟐
Image size: N X M
𝑟= 𝑥 ! + 𝑦 ! and 𝑟 " = 𝑥"! + 𝑦"!
Max = Maximum(M[i,j])
Other methods:
Histogram Equalization…
• 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
● 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, ∑ 𝒑(𝑰) = 𝟏
● 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:
𝑰 𝑰
𝟏
𝑭𝒓 𝑰 = 𝑷(𝒓 ≤ 𝑰) = , 𝒑𝒓 𝒊 = 𝟐 , 𝒏𝒊
𝑵
𝒊/𝟎 𝒊/𝟎
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
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
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.
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