Baunding Box
Baunding Box
Images
Avinash Kak
Purdue University
November 6, 2020
5:18pm
2
Texture and Color An RVL Tutorial by Avi Kak
Back to TOC
4
Texture and Color An RVL Tutorial by Avi Kak
Back to TOC
color) changes. However, as you will see in Section 5, this periodicity analysis is localized to the immediate
neighborhoods of the individual pixels. To the extent these within-neighborhood periodicity properties can be
used to recognize texture differences between the different regions in an image, I think the word “structural”
applies. ]
first-order properties are typically based on the means, the variances, etc., of the pixels. The second-order
properties involve comparing two pixels at the same time. The second-order properties, therefore, investigate
5
Texture and Color An RVL Tutorial by Avi Kak
how one pixel at some reference location relates statistically to another pixel at a location displaced from the
reference location. Some researchers have also looked at characterizing textures with third and higher order
properties. These would involve investigating the grayscale and color distributions at three or more pixels
whose coordinates must occupy specific positions vis-a-vis one another. Third and higher order texture
characterizations have been found to be too complex for a practical characterization of textures. ]
6
Texture and Color An RVL Tutorial by Avi Kak
The same goes for the second invariance: It’s highly likely that
the orientation of the texture you used for training a machine
learning algorithm would not be identical to the orientations of
the same texture in the test images.
7
Texture and Color An RVL Tutorial by Avi Kak
8
Texture and Color An RVL Tutorial by Avi Kak
Back to TOC
9
Texture and Color An RVL Tutorial by Avi Kak
2 0 1 1
0 1 2 0
1 1 1 2
0 0 1 1
d = (1, 1) (1)
10
Texture and Color An RVL Tutorial by Avi Kak
Image GLCM
Looking at the first row, what this matrix tells us is that if the
reference pixel has grayscale value 0, it is never the case that
the displaced pixel also has grayscale value 0. And that there is
only one occurrence of the reference pixel having grayscale value
0 while the displaced pixel has grayscale value of 1. And that
there is only one occurrence of the reference pixel being of
grayscale value 0, while the displaced pixel has grayscale value
of 2.
11
Texture and Color An RVL Tutorial by Avi Kak
0 3 1
3 6 1
1 1 2
12
Texture and Color An RVL Tutorial by Avi Kak
level differences. ]
vector to (1, 1), the script yields the output shown below. The
first array is the image array created with the vertical texture
that you specified, and the next array shows the GLCM matrix.
GLCM:
[0, 0, 0, 0, 0, 49]
[0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0]
[49, 0, 0, 0, 0, 0]
Texture attributes:
entropy: 1.0
contrast: 25.0
homogeneity: 0.166
On the other hand, if in the script of Section 3.3, you set the
texture type to horizontal, you get the output shown below:
14
Texture and Color An RVL Tutorial by Avi Kak
[5, 5, 5, 5, 5, 5, 5, 5]
[0, 0, 0, 0, 0, 0, 0, 0]
[5, 5, 5, 5, 5, 5, 5, 5]
[0, 0, 0, 0, 0, 0, 0, 0]
GLCM:
[0, 0, 0, 0, 0, 49]
[0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0]
[49, 0, 0, 0, 0, 0]
Texture attributes:
entropy: 1.0
contrast: 25.0
homogeneity: 0.166
which is the same as what you saw for the vertical case. This
is a consequence of how the GLCM matrix is made symmetric.
In any case, we have no problem accepting this result since the
two textures are visually the same — even though they are
oriented differently.
15
Texture and Color An RVL Tutorial by Avi Kak
GLCM:
[50, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 48]
Texture attributes:
entropy: 0.999
contrast: 0.0
homogeneity: 0.489
Here is the output of the script if you choose random for the
texture type:
GLCM:
[2, 3, 2, 2, 0, 1]
[3, 6, 4, 4, 3, 6]
[2, 4, 0, 1, 1, 8]
16
Texture and Color An RVL Tutorial by Avi Kak
[2, 4, 1, 0, 2, 4]
[0, 3, 1, 2, 0, 2]
[1, 6, 8, 4, 2, 4]
Texture attributes:
entropy: 4.70094581328
contrast: 5.9693877551
homogeneity: 0.371428571429
GLCM:
[30, 0, 0, 0, 0, 0]
[0, 12, 0, 0, 0, 0]
[0, 0, 6, 0, 0, 0]
[0, 0, 0, 6, 0, 0]
[0, 0, 0, 0, 12, 0]
[0, 0, 0, 0, 0, 32]
Texture attributes:
entropy: 2.28547139622
17
Texture and Color An RVL Tutorial by Avi Kak
contrast: 0.0
homogeneity: 0.69387755102
By the way, if you set the texture type to “None”, you get
default choices in lines (B19) through (B21) of the script. It is
these choices that created a 3 × 3 GLCM matrix for the 4 × 4
array that was used at the beginning of this section (see page 8)
to explain the basic idea of how one constructs a GLCM matrix.
18
Texture and Color An RVL Tutorial by Avi Kak
Back to TOC
Typically, one does NOT construct a GLCM matrix for the full
range of grayscale values in an image. For 8-bit grayscale
images with its 256 shades of gray (that is, the value of the
brightness at each pixel is an integer between 0 and 255, both
ends inclusive), you are likely to create a GLCM matrix of size
of just 16 × 16 that corresponds to a re-quantization of the gray
levels to just 4 bits (for the purpose of texture characterization).
19
Texture and Color An RVL Tutorial by Avi Kak
Cityblock distance, also known as the Manhattan or the Taxicab distance. The Cityblock Distance between
any two points is the sum of the absolute differences of the cooordinates of the two points. More formally, the
Cityblock distance is referred to as the L1 norm. The 8 immediate neighbors of a pixel as shown at left below
are at a Chessboard Distance of 1 from the pixel. However, as shown at right below, only the two column-wise
closest neighbors and the two row-wise closest neighbors of a pixel are at a Cityblock distance of 1:
X X X X
X P X X P X
X X X X
In and of itself, the GLCM characterization does not care what distance metric you use for the displacement
vector d. ]
20
Texture and Color An RVL Tutorial by Avi Kak
Back to TOC
grayscale values in the image are identical — that is, when there is no texture in the image.]
15 X15
X 1
Entropy = − log2 2−8
i=0 j=0
256
15 X15
X 1
= − (−8)
i=0 j=0
256
15 X15
X 1
= 8.
i=0 j=0
256
= 8 bits (3)
Now consider the opposite case, that is, when an image has the
same grayscale value at all the pixels. In this case, only one cell
of the GLCM matrix would be populated — the cell at the [0, 0]
element of the matrix. By using the property that x · log x goes
to zero as x approaches 0, it is easy to show that the Entropy
becomes zero for this case.
The main rule to remember here is that the smaller the value of
the entropy the more nonuniform a GLCM matrix.
Here are some other textures attributes that are derived from a
GLCM Matrix:
23
Texture and Color An RVL Tutorial by Avi Kak
M −1 M −1
P [i, j]2
X X
Energy =
i=0 j=0
M
X −1 M
X −1
Contrast = (i − j)2.P [i, j]
i=0 j=0
M −1 M −1
X X P [i, j]
Homogeneity =
i=0 j=0
1 + |i − j|
(4)
25
Texture and Color An RVL Tutorial by Avi Kak
Back to TOC
Shown below is the code that was used to generate the GLCM
results you saw previously on pages 11 through 15 of this tutorial.
#!/usr/bin/env python
## GLCM.py
## Author: Avi Kak ([email protected])
## Date: September 26, 2016
## This script was written as a teaching aid for the lecture on "Textures
## and Color" as a part of my class on Computer Vision at Purdue. This
## Python script demonstrates how the Gray Level Co-occurrence Matrix can
## be used for characterizing image textures.
26
Texture and Color An RVL Tutorial by Avi Kak
##
## Note that if uncomment line (A6), that sets the image size to 4
## and the number of gray levels of 3 regardless of the choices you
## make in lines (A7) and (A8)
##
## 2. Set the image size in line (A7)
##
## 3. Set the number of gray levels in line (A8)
##
## 4. Set the displacement vector by uncommenting one of the lines (A9),
# (A10), or (A11). However, note that the "low_contrast" choice for
## the contrast type in line (A5) is low contrast only when the displacement
## vector is set as in line (A9).
import random
import math
import functools
IMAGE_SIZE = 8 #(A7)
GRAY_LEVELS = 6 #(A8)
displacement = [1,1] #(A9)
#displacement = [1,0] #(A10)
#displacement = [0,1] #(A11)
27
Texture and Color An RVL Tutorial by Avi Kak
28
Texture and Color An RVL Tutorial by Avi Kak
29
Texture and Color An RVL Tutorial by Avi Kak
Back to TOC
aspects of LBP:
31
Texture and Color An RVL Tutorial by Avi Kak
Back to TOC
Fig. A
32
Texture and Color An RVL Tutorial by Avi Kak
------------------------------------------------
| | | | | |
| | | | | |
| | | | | |
------------------------------------------------
| | | p=4 | | |
| | p=5 | x | p=3 | |
| | x | | x | |
------------------------------------------------
| | | | | |
| | x | X | x | |
| | p=6 | | p=2 | |
------------------------------------------------
| | x | | x | |
| | p=7 | x | p=1 | |
| | | p=0 | | |
------------------------------------------------
| | | | | |
| | | | | |
| | | | | |
------------------------------------------------
Fig. B
33
Texture and Color An RVL Tutorial by Avi Kak
\Del l
--------->
.
A . B
--------------
| | . |
\Del k | | . |
| | . |
V | . |
.........|....... x |
| |
--------------
C D
34
Texture and Color An RVL Tutorial by Avi Kak
Fig. C
35
Texture and Color An RVL Tutorial by Avi Kak
Fig. D
37
Texture and Color An RVL Tutorial by Avi Kak
Back to TOC
38
Texture and Color An RVL Tutorial by Avi Kak
I’ll next show the minIntVal versions of the binary patterns for
the image represented by the randomly generated 8x8 array of
numbers shown below. The binary patterns will be for the
parameter values R = 1 and P = 8. Here is the image array:
The image:
[5, 4, 2, 4, 2, 2, 4, 0]
[4, 2, 1, 2, 1, 0, 0, 2]
[2, 4, 4, 0, 4, 0, 2, 4]
[4, 1, 5, 0, 4, 0, 5, 5]
[0, 4, 4, 5, 0, 0, 3, 2]
[2, 0, 4, 3, 0, 3, 1, 2]
[5, 1, 0, 0, 5, 4, 2, 3]
[1, 0, 0, 4, 5, 5, 0, 1]
39
Texture and Color An RVL Tutorial by Avi Kak
pixel at (1,1):
pattern: [1, 1, 0, 1, 1, 1, 1, 1]
minbv: 01111111
pixel at (1,2):
pattern: [1, 1, 1, 1, 1, 1, 1, 1]
minbv: 11111111
pixel at (1,3):
pattern: [0, 1, 0, 1, 1, 1, 0, 1]
minbv: 01010111
pixel at (1,4):
pattern: [1, 0, 0, 1, 1, 1, 1, 1]
minbv: 00111111
pixel at (1,5):
pattern: [1, 1, 1, 1, 1, 1, 1, 1]
minbv: 11111111
pixel at (1,6):
pattern: [1, 1, 1, 1, 1, 1, 1, 1]
minbv: 11111111
...
...
40
Texture and Color An RVL Tutorial by Avi Kak
Back to TOC
42
Texture and Color An RVL Tutorial by Avi Kak
43
Texture and Color An RVL Tutorial by Avi Kak
else: #(C28)
pattern.append(0) #(C29)
print("pattern: %s" % pattern) #(C30)
bv = BitVector.BitVector( bitlist = pattern ) #(C31)
intvals_for_circular_shifts = [int(bv << 1) for _ in range(P)] #(C32)
minbv = BitVector.BitVector( intVal = \
min(intvals_for_circular_shifts), size = P ) #(C33)
print("minbv: %s" % minbv) #(C34)
bvruns = minbv.runs() #(C35)
encoding = None
if len(bvruns) > 2: #(C36)
lbp_hist[P+1] += 1 #(C37)
encoding = P+1 #(C38)
elif len(bvruns) == 1 and bvruns[0][0] == ’1’: #(C39)
lbp_hist[P] += 1 #(C40)
encoding = P #(C41)
elif len(bvruns) == 1 and bvruns[0][0] == ’0’: #(C42)
lbp_hist[0] += 1 #(C43)
encoding = 0 #(C44)
else: #(C45)
lbp_hist[len(bvruns[1])] += 1 #(C46)
encoding = len(bvruns[1]) #(C47)
print("encoding: %s" % encoding) #(C48)
print("\nLBP Histogram: %s" % lbp_hist) #(C49)
Shown below are the encodings for the local binary patterns for
each of the inner 6x6 section of the 8x8 image array presented
towards the end of the previous subsection:
pixel at (1,1):
pattern: [1, 1, 0, 1, 1, 1, 1, 1]
minbv: 01111111
encoding: 7
pixel at (1,2):
pattern: [1, 1, 1, 1, 1, 1, 1, 1]
minbv: 11111111
encoding: 8
pixel at (1,3):
pattern: [0, 1, 0, 1, 1, 1, 0, 1]
minbv: 01010111
encoding: 9
pixel at (1,4):
pattern: [1, 0, 0, 1, 1, 1, 1, 1]
minbv: 00111111
encoding: 6
44
Texture and Color An RVL Tutorial by Avi Kak
pixel at (1,5):
pattern: [1, 1, 1, 1, 1, 1, 1, 1]
minbv: 11111111
encoding: 8
pixel at (1,6):
pattern: [1, 1, 1, 1, 1, 1, 1, 1]
minbv: 11111111
encoding: 8
pixel at (2,1):
pattern: [0, 0, 1, 0, 0, 0, 0, 0]
minbv: 00000001
encoding: 1
pixel at (2,2):
pattern: [1, 0, 0, 0, 0, 0, 1, 0]
minbv: 00000101
encoding: 9
pixel at (2,3):
pattern: [1, 1, 1, 1, 1, 1, 1, 1]
minbv: 11111111
encoding: 8
pixel at (2,4):
pattern: [1, 0, 0, 0, 0, 0, 0, 0]
minbv: 00000001
encoding: 1
pixel at (2,5):
pattern: [1, 1, 1, 1, 1, 1, 1, 1]
minbv: 11111111
encoding: 8
pixel at (2,6):
pattern: [1, 1, 1, 1, 0, 0, 0, 0]
minbv: 00001111
encoding: 4
pixel at (3,1):
pattern: [1, 1, 1, 1, 1, 1, 1, 1]
minbv: 11111111
encoding: 8
pixel at (3,2):
pattern: [0, 0, 0, 0, 0, 0, 0, 0]
minbv: 00000000
encoding: 0
pixel at (3,3):
pattern: [1, 1, 1, 1, 1, 1, 1, 1]
minbv: 11111111
encoding: 8
45
Texture and Color An RVL Tutorial by Avi Kak
pixel at (3,4):
pattern: [0, 0, 0, 0, 1, 0, 0, 0]
minbv: 00000001
encoding: 1
pixel at (3,5):
pattern: [1, 1, 1, 1, 1, 1, 1, 1]
minbv: 11111111
encoding: 8
pixel at (3,6):
pattern: [0, 0, 1, 0, 0, 0, 0, 0]
minbv: 00000001
encoding: 1
pixel at (4,1):
pattern: [0, 0, 1, 0, 0, 0, 0, 0]
minbv: 00000001
encoding: 1
pixel at (4,2):
pattern: [1, 0, 1, 0, 1, 0, 1, 0]
minbv: 01010101
encoding: 9
pixel at (4,3):
pattern: [0, 0, 0, 0, 0, 0, 0, 0]
minbv: 00000000
encoding: 0
pixel at (4,4):
pattern: [1, 1, 1, 1, 1, 1, 1, 1]
minbv: 11111111
encoding: 8
pixel at (4,5):
pattern: [1, 1, 1, 1, 1, 1, 1, 1]
minbv: 11111111
encoding: 8
pixel at (4,6):
pattern: [0, 0, 0, 1, 1, 0, 0, 0]
minbv: 00000011
encoding: 2
pixel at (5,1):
pattern: [1, 1, 1, 1, 1, 1, 1, 1]
minbv: 11111111
encoding: 8
pixel at (5,2):
pattern: [0, 0, 0, 1, 1, 0, 0, 0]
minbv: 00000011
encoding: 2
46
Texture and Color An RVL Tutorial by Avi Kak
pixel at (5,3):
pattern: [0, 0, 0, 0, 1, 1, 1, 0]
minbv: 00000111
encoding: 3
pixel at (5,4):
pattern: [1, 1, 1, 1, 1, 1, 1, 1]
minbv: 11111111
encoding: 8
pixel at (5,5):
pattern: [1, 0, 0, 0, 0, 0, 0, 1]
minbv: 00000011
encoding: 2
pixel at (5,6):
pattern: [1, 1, 1, 1, 1, 1, 1, 1]
minbv: 11111111
encoding: 8
pixel at (6,1):
pattern: [0, 0, 0, 1, 0, 1, 1, 1]
minbv: 00010111
encoding: 9
pixel at (6,2):
pattern: [1, 1, 1, 1, 1, 1, 1, 1]
minbv: 11111111
encoding: 8
pixel at (6,3):
pattern: [1, 1, 1, 1, 1, 1, 1, 1]
minbv: 11111111
encoding: 8
pixel at (6,4):
pattern: [1, 0, 0, 0, 0, 0, 0, 0]
minbv: 00000001
encoding: 1
pixel at (6,5):
pattern: [1, 0, 0, 0, 0, 0, 1, 1]
minbv: 00000111
encoding: 3
pixel at (6,6):
pattern: [0, 0, 1, 1, 0, 1, 1, 1]
minbv: 00110111
encoding: 9
47
Texture and Color An RVL Tutorial by Avi Kak
Shown below are the LBP histograms for the other cases of
textures you can produce with the LBP.py script that is
presented in the next subsection.
Texture type: vertical
[5, 0, 5, 0, 5, 0, 5, 0]
[5, 0, 5, 0, 5, 0, 5, 0]
[5, 0, 5, 0, 5, 0, 5, 0]
[5, 0, 5, 0, 5, 0, 5, 0]
[5, 0, 5, 0, 5, 0, 5, 0]
[5, 0, 5, 0, 5, 0, 5, 0]
[5, 0, 5, 0, 5, 0, 5, 0]
[5, 0, 5, 0, 5, 0, 5, 0]
[5, 5, 5, 5, 5, 5, 5, 5]
[0, 0, 0, 0, 0, 0, 0, 0]
[5, 5, 5, 5, 5, 5, 5, 5]
[0, 0, 0, 0, 0, 0, 0, 0]
[5, 5, 5, 5, 5, 5, 5, 5]
[0, 0, 0, 0, 0, 0, 0, 0]
[5, 5, 5, 5, 5, 5, 5, 5]
[0, 0, 0, 0, 0, 0, 0, 0]
48
Texture and Color An RVL Tutorial by Avi Kak
[0, 5, 0, 5, 0, 5, 0, 5]
[5, 0, 5, 0, 5, 0, 5, 0]
[0, 5, 0, 5, 0, 5, 0, 5]
[5, 0, 5, 0, 5, 0, 5, 0]
[0, 5, 0, 5, 0, 5, 0, 5]
[5, 0, 5, 0, 5, 0, 5, 0]
[0, 5, 0, 5, 0, 5, 0, 5]
[5, 0, 5, 0, 5, 0, 5, 0]
49
Texture and Color An RVL Tutorial by Avi Kak
Back to TOC
All of the LBP based results shown earlier were produced by the
Python script shown below.
The script generates elementary textures for you and applies the
LBP algorithm to the textures. You can specify the texture you
want by uncommenting one of lines in (A1) through (A5).
The script allows you to set any arbitrary size for the image array
and the number of gray levels you want in the image.
The script also allows you to give whatever values you wish to the
P and R parameters needed by the LBP algorithm.
#!/usr/bin/env python
## LBP.py
## Author: Avi Kak ([email protected])
## Date: November 1, 2016
## This script was written as a teaching aid for the lecture on "Textures and Color"
## as a part of my class on Computer Vision at Purdue.
##
## This Python script demonstrates how Local Binary Patterns can be used for
## characterizing image textures.
## For educational purposes, this script generates five different types of textures
## -- you make the choice by uncommenting one of the statements in lines (A1)
## through (A5). You can also set the size of the image array and number of gray
## levels to use.
50
Texture and Color An RVL Tutorial by Avi Kak
##
## 1. Specify the texture type you want by uncommenting one of the lines (A1)
## through (A5)
##
## 2. Set the image size in line (A6)
##
## 3. Set the number of gray levels in line (A7)
##
## 4. Choose a value for the circle radius R in line (A8)
#3
## 5. Choose a value for the number of sampling points on the circle in line (A9).
import random
import math
import BitVector
IMAGE_SIZE = 8 #(A6)
#IMAGE_SIZE = 4 #(A6)
GRAY_LEVELS = 6 #(A7)
R = 1 # the parameter R is radius of the circular pattern #(A8)
P = 8 # the number of points to sample on the circle #(A9)
51
Texture and Color An RVL Tutorial by Avi Kak
52
Texture and Color An RVL Tutorial by Avi Kak
encoding = 0 #(C44)
else: #(C45)
lbp_hist[len(bvruns[1])] += 1 #(C46)
encoding = len(bvruns[1]) #(C47)
print("encoding: %s" % encoding) #(C48)
print("\nLBP Histogram: %s" % lbp_hist) #(C49)
53
Texture and Color An RVL Tutorial by Avi Kak
Back to TOC
54
Texture and Color An RVL Tutorial by Avi Kak
image resolution to be made available in a video service. MPEG-1, which is the oldest such standard,
specifies an image resolution of 352 × 240 at 30 fps (frames per second). On the other hand, MPEG-2
specifies two image resolutions, 720 × 480 and 1280 × 720, both at 60 fps. The acronym MPEG stands for
“Moving Picture Experts Group”. MPEG is a part of ISO (International Organization for Standardization). ]
Figure 1: Defining the Gabor texture channels for the MPEG-7 stan-
dard by dividing the spatial frequencies in polar coordinates. The Ga-
bor filter bank in this case consists of Gabor convolutional operators
at 6 orientations and 5 spatial frequencies.
55
Texture and Color An RVL Tutorial by Avi Kak
f0
fs = s = 0, 1, 2, 3, 4 (8)
2s
where s = 0 obviously corresponds to the outer boundary of the
outermost band. Note that, except for the lowest band, each
band represents an octave (since the upper limit of frequencies
in each band is twice the lower limit). Here are the frequency
bandwidths for the five different bands:
56
Texture and Color An RVL Tutorial by Avi Kak
!
f0
band1 = , f0
2
!
f0 f0
band2 = ,
4 2
!
f0 f0
band3 = ,
8 4
!
f0 f0
band4 = ,
16 8
!
f0
band5 = 0,
16
57
Texture and Color An RVL Tutorial by Avi Kak
Back to TOC
58
Texture and Color An RVL Tutorial by Avi Kak
The real part (and also the imaginary part) of the expression on
the right above is a two-dimensional sinusoidal wave (think of an
ocean wave) in the (x, y) plane that cuts the x-axis at u-cycles
per unit length and the y-axis at v cycles per unit length.
θ = arctan(v/u) (14)
that is subtended with the x-axis. If we wish, we can express
the frequency components u and v in polar coordinates:
u = f ∗ cos(θ)
v = f ∗ sin(θ) (15)
59
Texture and Color An RVL Tutorial by Avi Kak
Back to TOC
60
Texture and Color An RVL Tutorial by Avi Kak
What that implies is that the best way to interpret the Gabor
operator is to think of it as a localized Fourier transform of an
image where, for the calculation at each locale in the image, you
weight the pixels away from the locale according to the
exponential decay factor. Obviously, the decay rate with respect
to the pixel coordinates is controlled by the ”standard
deviation” σ
62
Texture and Color An RVL Tutorial by Avi Kak
!
1 − 12 i2 +j22
hre (i, j; f, θ) = √ e 2σ cos 2πf (i cos(θ) + j sin(θ))
πσ
!
1 2
1 i +j 2
him (i, j; f, θ) = √ e− 2 2σ2 sin 2πf (i cos(θ) + j sin(θ))
πσ
(23)
where i represents the row index and the j the column index.
Note that we commonly think of the x-axis as being along the
horizontal, going from left to right, and the y-axis as being
along the vertical, going from bottom to top. However, in the
discrete version, we think of the i index as representing the rows
(that is, i is along the vertical, going from top to bottom, and j
is along the horizontal, going from left to right. The origin in
the continuous case is usually at the center of the (x, y) plane.
The origin in the discrete case is at the upper left corner.
An important issue for the discrete case is how far one should
go from the origin for a given value of σ. Obviously, the smaller
the value of σ, the faster the drop-off in terms of the weighting
given to the pixels in the image, and, therefore, the smaller the
effective footprint of the operator on the (x, y) plane. A
commonly used rule of thumb for the discrete case is to let i and
j cover a bounding box that goes from −int(3σ) to int(3σ)
along the x and the y axes.
63
Texture and Color An RVL Tutorial by Avi Kak
1
√ (24)
πσ
its purpose is to ensure that the “energy” of the operator is
unity. For the continuous case, the energy constraint is defined
as
Z ∞ Z ∞
|h(x, y)|2 = 1 (25)
x=−∞ y=−∞
int(3σ) int(3σ)
X X
|h(i, j)|2 ≈ 1 (26)
i=−int(3σ) j=−int(3σ)
64
Texture and Color An RVL Tutorial by Avi Kak
The Gabor filter bank shown in the figure was used to measure
the texture in the following 32 × 32 image array:
[5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0]
[5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0]
[5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0]
[5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0]
[5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0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0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0]
[5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0]
[5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0]
[5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0]
[5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0]
[5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0]
[5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0]
67
Texture and Color An RVL Tutorial by Avi Kak
68
Texture and Color An RVL Tutorial by Avi Kak
The fact that the third column has the highest values is
consistent with the orientation of the texture in the image.
[5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
In the output shown above, recall again that the first column for
the orientation of 0 radians with respect to the vertical, the
second for 45 degrees, the third for 90 degrees, and the last for
135 degrees, all with respect to the vertical and going
counter-clockwise. We see that the highest spectral energy is
now at 0 degrees which is consistent with the new orientation of
the texture.
With regard to the rows shown above, the first row is for 2
cycles per window width, the second for 4 cycles per window
width, and the third for 6 cycles per window width. Since the
pattern of the alternations in the texture is the same as before
(albeit its new rotation), we again expect the maximum spectral
energy to be roughly around 6 cycles per window width. This is
borne out by the output shown above.
70
Texture and Color An RVL Tutorial by Avi Kak
71
Texture and Color An RVL Tutorial by Avi Kak
72
Texture and Color An RVL Tutorial by Avi Kak
Back to TOC
All of the Gabor based results shown earlier were produced by the
Python script shown below.
The script generates elementary textures for you and applies Gabor
filter bank to the image array. You can specify the texture you
want by uncommenting one of lines in (A4) through (A8).
The script allows you to set any arbitrary size for the image array
and the number of grayscale values you want in the image.
Note that you must also set the size of the Gabor σ and the size of
the convolutional operator in lines (A29) and (A30).
#!/usr/bin/env python
## Gabor.py
## Author: Avi Kak ([email protected])
## Date: October 15, 2016
## This script was written as a teaching aid for the lecture on "Textures and Color" as
## a part of my class on Computer Vision at Purdue.
##
## This Python script demonstrates how Gabor Filtering can be used for characterizing
## image textures.
## Just for the sake of playing with the code, this script generates five different types
73
Texture and Color An RVL Tutorial by Avi Kak
## of "textures". You make the choice by uncommenting one of the statements in lines
## (A4) through (A8). You can also set the size of the image array and number of grayscale
## values to use in lines in lines (A9) and (A10)
import random
import math
import sys, glob, os
if sys.version_info[0] == 3:
import tkinter as Tkinter
from tkinter.constants import *
else:
import Tkinter
from Tkconstants import *
from PIL import Image
from PIL import ImageDraw
from PIL import ImageTk
import functools
74
Texture and Color An RVL Tutorial by Avi Kak
75
Texture and Color An RVL Tutorial by Avi Kak
op_imag[-(gabor_size//2)+k][-(gabor_size//2)+l] #(A61)
filter_outputs[freq][direc] += \
math.sqrt( real_part**2 + imag_part**2 ) #(A62)
filter_outputs = list(map(lambda x: x / (1.0*(rowmax-rowmin)*(colmax-colmin)),
filter_outputs[freq]) for freq in range(1,how_many_frequencies)) #(A63)
print("\nGabor filter output:\n") #(A64)
for freq in range(len(filter_outputs)): print(list(filter_outputs[freq])) #(A65)
76
Texture and Color An RVL Tutorial by Avi Kak
main() #(G1)
77
Texture and Color An RVL Tutorial by Avi Kak
Back to TOC
algorithm to each color channel separately? If you do, how would you combine the three
characterizations? Yes, one could possibly “concatenate” the three characterizations. But might there
be other ways to represent color images so that such a concatenation would become unnecessary? ]
]
brightness (I/V/L), where I stands for intensity, V for value, and L for lightness.
Spaces” in Section 6.3 of this tutorial for what is meant by “red-green” and “yellow-blue” values. The
L*a*b* space has become very important for the purpose of data visualization. It is now well
known that the changes in data are best visualized if they correspond to proportional changes in the L* value
]
in the color graphic used for visualization.
79
Texture and Color An RVL Tutorial by Avi Kak
Figure 3: The goal here is to segment out the fruitlets from the im-
age on the left. The segmentation shown at right is based on just
hue filtering. We accept all pixels whose hue values are between 0◦
and 30◦. The image on the left was supplied by Dr. Peter Hirst,
Professor of Horticulture, Purdue University. And the segmentation
of the fruitlets on the right was carried out by invoking the function
apply color filter hsv() of my Python based Watershed module,
Version 2.0.4.
80
Texture and Color An RVL Tutorial by Avi Kak
Back to TOC
81
Texture and Color An RVL Tutorial by Avi Kak
human observer would perceive this combined light as magenta. On the other hand, if we could produce a
spectrally pure light at a wavelength halfway between blue and red, that light would not be perceived as
magenta. In other words, both the average wavelength and the shape of the spectral distribution are
also be conveyed by its projection on either the XY, or the YZ, or the XZ plane. Such a projection is referred
Last but not the least is the great difficulty of making reliable
83
Texture and Color An RVL Tutorial by Avi Kak
85
Texture and Color An RVL Tutorial by Avi Kak
Back to TOC
86
Texture and Color An RVL Tutorial by Avi Kak
photo receptors are sensitive mostly to monochromatic blue. In the past, L, M, and S
photo receptors have been loosely associated with R (for red), G (for green), and B
(for blue).]
The three spectral responses shown in the figure have
commonly been denoted hR (λ), hG(λ), and hB (λ) in the
literature.
Figure 4: The retina of the human eye contains three different types
of photo receptors for color vision. The spectral responses of the three
types are shown here. This figure is from the Wikipedia page on “Color
Vision”.
87
Texture and Color An RVL Tutorial by Avi Kak
Z
R = fill (λ)fref (λ)hR (λ)dλ
Z
G = fill (λ)fref (λ)hG (λ)dλ
Z
B = fill (λ)fref (λ)hB (λ)dλ (27)
processing of color in the visual cortex of the brain remains largely a mystery. ]
88
Texture and Color An RVL Tutorial by Avi Kak
through light at or in close vicinity to the 700 nm wavelength, the wavelength of the pure red hue.
Similarly, the G filter passes through light at or in close vicinity to the 545 nm wavelength, the
wavelength of the pure green hue. And, the B filter passes through light at or in close vicinity to the
] Mathematically, this
480 nm wavelength, the wavelength of the pure blue hue.
89
Texture and Color An RVL Tutorial by Avi Kak
90
Texture and Color An RVL Tutorial by Avi Kak
Figure 5: This figure shows how much red, how much green, and how
much blue is needed at each wavelength to match the spectral color
at that wavelength. Obviously, the wavelengths where the curve goes
negative means that it is not possible to match those spectral colors
with a combination of R, G, and B. This figure is from the Wikipedia
page on “CIE 1931 color space”.
91
Texture and Color An RVL Tutorial by Avi Kak
Back to TOC
In the rest of this section, I’ll first explain the idea of the
Chromaticity Space. Subsequently, I’ll discuss the relationship
between different types of 3D color spaces.
92
Texture and Color An RVL Tutorial by Avi Kak
~ +YY
C = XX ~ + ZZ
~ (28)
Z
X = k P (λ)x̄(λ)dλ
Z
Y = k P (λ)ȳ(λ)dλ
Z
Z = k P (λ)z̄(λ)dλ (29)
X
C = Y (30)
Z
X
x =
X +Y +Z
Y
y =
X +Y +Z
Z
z =
X +Y +Z
(31)
94
Texture and Color An RVL Tutorial by Avi Kak
x+y+z =1 (32)
mapped points. ]
96
Texture and Color An RVL Tutorial by Avi Kak
97
Texture and Color An RVL Tutorial by Avi Kak
the periphery of the large rounded cone are the pure spectral
colors. So if you could tune the wavelength of a laser, you would
move around the rounded perimeter shown in the diagram.
98
Texture and Color An RVL Tutorial by Avi Kak
99
Texture and Color An RVL Tutorial by Avi Kak
Figure 11: Chromaticity Plane that shows the locations of red, green,
and blue on the plane.
100
Texture and Color An RVL Tutorial by Avi Kak
RGB Space:
“quadrant” of the RGB space is a subset of the positive quadrant of the XYZ space. ]
101
Texture and Color An RVL Tutorial by Avi Kak
words, the chromaticity coordinates only give us the relative proportions of the
primaries in a mixture but not their true values. So, it may well be that to produce
the color white we may have to be at the point W ′ , but, as far the chromaticity
depiction is concerned, the color white will be represented by the point W .]
X 0.618 0.177 0.205 R
Y = 0.299 0.587 0.114 G (34)
Z 0.000 0.056 0.944 B
102
Texture and Color An RVL Tutorial by Avi Kak
HSI Space:
103
Texture and Color An RVL Tutorial by Avi Kak
White, I=1.0
Intensity
Green Hue
120 degrees
Black
I=0.0 θ for hue
saturation
104
Texture and Color An RVL Tutorial by Avi Kak
R C
B C
w G
I
v
S
H C
v
C
u
Figure 14: The RGB cube is tilted and rotated in the manner shown
here for deriving a relationship between RGB and HSI coordinates.
The longest diagonal of the cube becomes aligned with the vertical In-
tensity (I) axis in HSI. The Hue (H) coordinates of HSI is measured
counterclockwise from the projection of the RGB cube’s red corner on
the horizontal plane. Saturation (S) is the radial distance from the
vertical axis.
106
Texture and Color An RVL Tutorial by Avi Kak
Cv
H = tan−1
Cu
p
S = Cu2 + Cv2
I = Cw (35)
r
2R − G − B
Cu =
6
r
G−B
Cv =
2
R+G+B
Cw = (36)
3
107
Texture and Color An RVL Tutorial by Avi Kak
!
Cu
H = cos−1 p if G >= B
Cu2 + Cv2
!
Cu
= 360 − cos−1 p if G < B
Cu2 + Cv2
(37)
108
Texture and Color An RVL Tutorial by Avi Kak
The goal of what I have described so far was to give the reader
a sense how one can set up a connection between the RGB and
the HSI color spaces. Using this reasoning, along with some
refinements related to explicitly factoring in the piecewise
continuous nature of the boundary of the RGB cube, I’ll show
below the transformation equations that have actually been
implemented in several software libraries: [These and other transformations
like the one shown below are taken from http://dystopiancode.blogspot.com with some
]
modifications by me to reflect how they are actually implemented in code.
109
Texture and Color An RVL Tutorial by Avi Kak
M = max(R, G, B)
m = min(R, G, B)
c = M −m
R+G+B
I =
3
G−B
60 c
mod 6 if M = R, c 6= 0
60 B−R +2 if M = G, c 6= 0
H = c
60 R−G +4 if M = B, c 6= 0
c
0.0 if C = 0
0 if c = 0
S =
1 − ml if c =6 0
(40)
Shown below are the formulas that take you from HSI to RGB.
In these formulas, we represent a point in the HSI space by the
triplet (H, S, I). The variable hr stands for the value of H in
110
Texture and Color An RVL Tutorial by Avi Kak
] The input to
calculation for z requires that you first have calculated the values for x and y.
hr = 0.0174532925 × H
hr if 0 ≤ hr ≤ 23 π
2
hk = hr − π
3
if 23 π < hr ≤ 34 π
hr − 43 π if 43 π < hr < 2π
!
1 − S 1 + S cos(hk )
(x, y, z) = , , 1 − (x + y)
3 3 cos( π3 − hk )
(x′ , y ′, z ′ ) = 3 × I × x, 3 × I × y, 3 × I × z
(I, I, I) if S = 0
(y ′ , z ′ , x′ ) if 0 ≤ hr ≤ 2 π
(R, G, B) = ′ ′ ′
(x , y , z ) if 2 π < hr ≤ 4 π
3
3 3
(z ′ , y ′ , x′ ) if 43 π < hr < 2π
(41)
111
Texture and Color An RVL Tutorial by Avi Kak
assume that there is no blue or green in the image. By setting I = R/3 in this case, the HSI formula would
discount the “energy” coming off the pixels by two-thirds — which really does not make a whole lot of sense.
The fact that HSI mutes the I values unless it sees strength in all three color channels can be highly
problematic for computer vision algorithms. For extracting the shape information, many computer
S values span [0.0, 1.0]; and both V and L values span [0.0, 1.0]. ]
112
Texture and Color An RVL Tutorial by Avi Kak
max(R,G,B)−min(R,G,B)
The S in HSV is calculated as V
. And the
max(R,G,B)−min(R,G,B)
same in HSL is calculated as 1−abs(2L−1)
.
Here are the formulas that take you from RGB to HSV. The
input to the transformation shown below consists of a triplet of
values (R, G, B) with the individual color components
normalized to the [0, 1] range.
113
Texture and Color An RVL Tutorial by Avi Kak
Figure 15: The left image in the topmost row is the original image.
The right image in the same row is the depiction of I values in the
HSI model. In the second row, the left image shows the V in the HSV
representation of the colors and the right image the L in the HSL
representation. The image shown in the last row is the L∗ component
in the CIE L*a*b* representation of color. All these images are from
the Wikipedia page on “HSL and HSV”.
114
Texture and Color An RVL Tutorial by Avi Kak
M = max(R, G, B)
m = min(R, G, B)
c = M −m
V = M
G−B
60 c
mod 6 if M = R, c 6= 0
60 B−R + 2 if M = G, c 6= 0
H = c
R−G
60 c + 4 if M = B, c 6= 0
0 if c = 0
c
S = (42)
V
115
Texture and Color An RVL Tutorial by Avi Kak
c = V ×S
m = V −c
H
x = c 1− mod 2 − 1
60
(c + m, x + m, m) if 0 ≤ H ≤ 60
(x + m, c + m, m) if 60 < H ≤ 120
(m, c + m, x + m) if 120 < H ≤ 180
(R, G, B) =
(m, x + m, c + m) if 180 < H ≤ 240
(x + m, m, c + m) if 240 < H ≤ 300
(c + m, m, x + m) if 300 < H ≤ 360
(43)
opposed to white. The human brain wants to receive a single signal for each of the three opposites. ]
That is, as shown in Figure 16, the visual cortex in the brain
gets three independent signals from the retina: one regarding
how much of the red-green dimension exists at a point in the
scene; a second regarding how much of the blue-yellow
116
Texture and Color An RVL Tutorial by Avi Kak
reddish-green or yellowish-blue supports the opponent color model. Since the two color components in a
reddish-green mixture are along the same dimension, the red-green receptor complex is unable to discern
Figure 16: This figure depicts how the output produced by the L, M,
and S cone cells (whose spectral responses were shown previously in
Figure 4) are combined to produce the three opponent color signals for
the visual cortex in the brain. This figure is from the Wikipedia page
on “Opponent Processes”.
117
Texture and Color An RVL Tutorial by Avi Kak
118
Texture and Color An RVL Tutorial by Avi Kak
CMY Space:
together the cyan and magenta pigments, we obtain the blue pigment. ]
119
Texture and Color An RVL Tutorial by Avi Kak
Figure 17: The color wheel for pigment hues. The colors that
are diametrically opposites are subtractive vis-a-vis each other. The
opposite color pairs are considered to be complementary. The two
colors that are immediately adjacent to each color are considered
to be analogous. Finally, the three colors at the vertices of an
equilateral inside the wheel are considered to form a triad. From
http: // www. realcolorwheel. com/ newcolorwheel. htm
120
Texture and Color An RVL Tutorial by Avi Kak
light the hue at the opposite point on the color circle. Assume
that a surface is coated with the cyan pigment. Now assume
that a beam of white light rich in all hues is incident on the
surface. The pigment will absorb the red hue and the
distribution of hues in the reflected light will carry all hues
except red. Since the reflected light must again be thought of as
emitted light, we must add the hues on the color wheel to
determine the color of the reflected beam. The hues that are
opposite on the color wheel add up to white. Therefore, the
reflected light will bear the color of cyan (since its opposite, red,
would be absorbed by the pigment), in addition to containing a
significant extent of white light. In other words, the reflected
light will be an unsaturated version of cyan. [This explanation
should also point to the fact that the colors seen in the white light that is
reflected off colored object surfaces can never appear to be intense since
much of the white light remains in what is reflected. In other words, you
have to use emitted light for constructing brilliant displays — as you might
have noticed from the more modern billboards along highways these days.]
121
Texture and Color An RVL Tutorial by Avi Kak
possess the right color. However, when the same pixel is sent for
display to a hardcopy printer, the values sent to the cyan,
magenta, and yellow pens must be
C = 0.2 = 1 − R
M = 0.7 = 1 − G
Y = 0.6 = 1 − B
(44)
With the CMY values set as shown above, C being 0.2 will
cause the deposited cyan pigment to absorb 0.2 fraction of the
red hue in the supposedly white illumination of the hardcopy
surface, releasing 0.8 of the red hue in the direction of the
observer. Similarly, with M and Y for the generation of the
correct values of G and B for an observer.
C 1 R
M = 1 − G (45)
Y 1 B
122
Texture and Color An RVL Tutorial by Avi Kak
Back to TOC
– The color of the illumination. The color of the light coming off a
surface depends significantly on the color of the illumination.
123
Texture and Color An RVL Tutorial by Avi Kak
That the color of the light coming off an object surface depends
significantly on the color composition of the illumination is
illustrated by Figure 18. As you can see in the figure, a yellow
ball looks green under mostly blue illumination. This figure also
shows how one can algorithmically correct for illumination
induced distortion of the color of a surface. The result shown in
(c) of the figure was obtained through the use of an
illumination-adaptive color space that was introduced in 2005
by Park and Kak in the report “A New Color Representation
for Non-White Illumination Conditions,” that was cited at
the end of Section 6.1.
Figure 18: (a) A green ball and a yellow ball imaged under ambient
white-light illumination. (b) The yellow ball imaged under blue illu-
mination. It now looks quite a bit like the green ball. (c) The image of
(b) after its processing by the algorithm in the 2005 Park and Kak re-
port titled “A New Color Representation for Non-White Illumination
Conditions” that was cited at the end of Section 6.1.
The second row demonstrates the extent to which the true color
of the toy was restored in each case by the illumination-adaptive
color representation of Park and Kak mentioned in the previous
bullet.
125
Texture and Color An RVL Tutorial by Avi Kak
Figure 19: The photos in the top row are of the same plastic toy
and were recorded by the same digital camera using four different il-
lumination sources: (1) florescent, (2) blue incandescent, (3) green
incandescent, and (4) tungston. The second row demonstrates the ex-
tent to which the true color of the toy was restored in each case by the
illumination-adaptive color representation method of Park and Kak.
126
Texture and Color An RVL Tutorial by Avi Kak
separately. An incoming light ray is deflected to each of three cells in a grouping and light for each cell passed
through a color-sensitive filter specific to that cell. An alternative consists of using three separate sensor
chips, one for each color component, and a set of prisms that are shaped to deflect a particular color
light are also known as the surface component and the body component, respectively. Light reflected by
homogeneous materials such as metals is dominated by the surface components, whereas the light reflected by
127
Texture and Color An RVL Tutorial by Avi Kak
Figure 21: The yellow and the white objects shown in this pile were
used for demonstrating the dependence of the measured color on the
orientation of a surface.
128
Texture and Color An RVL Tutorial by Avi Kak
129
Texture and Color An RVL Tutorial by Avi Kak
when the surface is at slant of 15◦ , the camera registers the largest specular component. This is explained
approximately by the law of reflection, which says that the angle of reflection subtended by the specular
component of light with respect to the surface normal must equal the angle of incidence of the illumination
light with respect to the normal. More accurately, this phenomenon is explained by the Torrance-Sparrow
]
model of reflection.
In the results shown in Figure 22, when the slant angle of the
surface becomes large, the reflected light consists mostly of the
body component. But the intensity of this light diminishes
rapidly as the surface slant angle approaches 90◦. This explains
the “darkness” of the result shown for the surface tilt angle of
75◦. This diminishing effect is a result of the fact that if we
assume a perfectly Lambertian surface, the magnitude of the
body component varies as a cosine of the angle of incidence.
130