German University in Cairo
Faculty of Media Engineering and Technology
Winter 2024
DMET 901 – Computer Vision
Problem Set #1 Solution
Problem 1
Implement a Python function that converts a colored image to gray scale using the weighted-
summation equation given in the lecture.
import [Link] as plt
from PIL import Image
im = Image. open(<What ever the image is>)
gray_r = gray_im.copy()
for i in range (0,[Link][0]):
for j in range (0,[Link][1]):
r, g, b = [Link]((i, j))
gray_r.putpixel((i,j),
(int(float(0.3*r)+float(0.59*g)+float(0.11*b))))
[Link](gray_r, cmap="gray")
Problem 2
Compute the co-occurrence matrix of each of the images given below for the North-South pixels
relationship. Quantify the contrast in both images using the contrast measure given in class:
m n
C r (i, j) f r (i) − f c ( j)2
i =1 j =1
German University in Cairo
Faculty of Media Engineering and Technology
Winter 2024
DMET 901 – Computer Vision
Problem Set #1 Solution
1 2 3 4 10 20 30 40
2 3 4 5 20 30 40 50
3 4 5 6 30 40 50 60
4 5 6 7 40 50 60 70
Image 1 Image 2
Comment on the results obtained and whether or not the obtained numbers represent a good
quantification of contrast.
Image 1
North\South 1 2 3 4 5 6 7
1 0 1 0 0 0 0 0
2 0 0 2 0 0 0 0
3 0 0 0 3 0 0 0
4 0 0 0 0 3 0 0
5 0 0 0 0 0 2 0
6 0 0 0 0 0 0 1
7 0 0 0 0 0 0 0
𝐶𝑜𝑛𝑡𝑟𝑎𝑠𝑡 = 1 ∗ (2 − 1)2 + 2 ∗ (3 − 2)2 + 3 ∗ (4 − 3)2 + 3 ∗ (5 − 4)2 + 2 ∗ (6 − 5)2
+ 1 ∗ (7 − 6)2 = 12
Image 2
North\South 10 20 30 40 50 60 70
10 0 1 0 0 0 0 0
20 0 0 2 0 0 0 0
30 0 0 0 3 0 0 0
German University in Cairo
Faculty of Media Engineering and Technology
Winter 2024
DMET 901 – Computer Vision
Problem Set #1 Solution
40 0 0 0 0 3 0 0
50 0 0 0 0 0 2 0
60 0 0 0 0 0 0 1
70 0 0 0 0 0 0 0
𝐶𝑜𝑛𝑡𝑟𝑎𝑠𝑡 = 1 ∗ (20 − 10)2 + 2 ∗ (30 − 20)2 + 3 ∗ (40 − 30)2 + 3 ∗ (50 − 40)2 + 2
∗ (60 − 50)2 + 1 ∗ (70 − 60)2 = 1200
As per the calculated contrasts, Image 2 is with higher contrast than image 1. This was caused by the
difference in intensity of neighboring pixels in Image 2 (10 intensity steps) being bigger than the
difference in intensity of neighboring pixels in image 1 (1 intensity step).
Problem 3
Implement a Python function to compute the co-occurrence matrix for:
a) Each pixel and its south
b) Each pixel and its east
The function should output the contrast estimate using the contrast measure given in class for both
relationships.
1. Each pixel and its south
a. Getting the Co-occurrence Matrix:
i. After taking the image, create an empty array of size (256,256) and call it Cr (Co-
occurrence Matrix)
1. Now we have indexes (0 - 255)
ii. Loop over (i,j) in the original image:
1. Get the value of the pixel in (i,j) and the value in pixel (i,j+1) and save it in
variables (for example m and n)
German University in Cairo
Faculty of Media Engineering and Technology
Winter 2024
DMET 901 – Computer Vision
Problem Set #1 Solution
2. Increment the value in position (m,n) in Cr array, as it responds to co-
occurrence of pixel color of value (m) having to its south color of pixel
value (n)
b. Getting the Contrast:
i. Loop over the Cr array and for each value do the following:
1. Calculate the difference squared between i and j and multiply it with
(Cr[i][j]).
c. Print the resulted Contrast
-----------------------------------------------------------------------------------------------
2. Each pixel and its east
a. Getting the Co-occurrence Matrix:
i. After taking the image, create an empty array of size (256,256) and call it Cr (Co-
occurrence Matrix)
1. Now we have indexes (0 - 255)
ii. Loop over (i,j) in the original image:
1. Get the value of the pixel in (i,j) and the value in pixel (i+1,j) and save it in
variables (for example m and n)
2. Increment the value in position (m,n) in Cr array, as it responds to co-
occurrence of pixel color of value (m) having to its east color of pixel value
(n)
b. Getting the Contrast:
i. Loop over the Cr array and for each value do the following:
1. Calculate the difference squared between i and j and multiply it with
(Cr[i][j]).
c. Print the resulted Contrast
German University in Cairo
Faculty of Media Engineering and Technology
Winter 2024
DMET 901 – Computer Vision
Problem Set #1 Solution
Problem 4
Compute the integral image of the image given below which corresponds to a bright rectangular
object in the center:
1 1 2 2
2 150 200 3
2 250 175 2
1 100 120 1
1 2 1 3
Use the obtained integral image to compute the average intensity of the rectangular object.
𝑠(𝑖, 𝑗) = 𝑠(𝑖, 𝑗 − 1) + 𝑓(𝑖, 𝑗) 𝑖𝑖(𝑖, 𝑗) = 𝑖𝑖(𝑖 − 1, 𝑗) + 𝑠(𝑖, 𝑗)
1 2 4 6 1 2 4 6
2 152 352 355 3 154 356 361
2 252 427 429 5 406 783 790
1 101 221 222 6 507 1004 1012
1 3 4 7 7 510 1008 1019
𝑠(𝑖, 𝑗) 𝑖𝑖(𝑖, 𝑗)
3 2
𝑓(𝑖, 𝑗) = 𝒊𝒊(𝟑, 𝟐) − 𝒊𝒊(𝟎, 𝟐) − 𝒊𝒊(𝟑, 𝟎) + 𝒊𝒊(𝟎, 𝟎)
𝑖=1 𝑗=1
German University in Cairo
Faculty of Media Engineering and Technology
Winter 2024
DMET 901 – Computer Vision
Problem Set #1 Solution
Problem 5
Using the integral image method, compute the variance of the 2 x 2 region indicated with (*). Show
the computations of each of the cumulative row sum matrix s and the final integral image. The
variance of N values can be estimated as
2
1 N 1 N
Var = xi2 − xi
N i =1 N i =1
1 1 5 2
1 10* 5* 2
5 9* 5* 10
Image
Image
1 1 5 2
1 10* 5* 2
5 9* 5* 10
𝑠(𝑖, 𝑗) = 𝑠(𝑖, 𝑗 − 1) + 𝑓(𝑖, 𝑗) 𝑖𝑖(𝑖, 𝑗) = 𝑖𝑖(𝑖 − 1, 𝑗) + 𝑠(𝑖, 𝑗)
1 2 7 9 1 2 7 9
1 11* 16* 18 2 13* 23* 27
5 14* 19* 29 7 27* 42* 56
(2,2)
𝑓(𝑖, 𝑗) = 𝑖𝑖(2,2) + 𝑖𝑖(0,0) − 𝑖𝑖(2,0) − 𝑖𝑖(0,2) = 42 + 1 − 7 − 7 = 29
(𝑖,𝑗)=(1,1)
German University in Cairo
Faculty of Media Engineering and Technology
Winter 2024
DMET 901 – Computer Vision
Problem Set #1 Solution
Image2
1 1 25 4
1 100* 25* 4
25 81* 25* 100
𝑠(𝑖, 𝑗) = 𝑠(𝑖, 𝑗 − 1) + 𝑓(𝑖, 𝑗) 𝑖𝑖(𝑖, 𝑗) = 𝑖𝑖(𝑖 − 1, 𝑗) + 𝑠(𝑖, 𝑗)
1 2 27 31 1 2 27 31
1 101* 126* 130 2 103* 153* 161
25 106* 131* 231 27 209* 284* 392
(2,2)
𝑓 2 (𝑖, 𝑗) = 𝑖𝑖(2,2) + 𝑖𝑖(0,0) − 𝑖𝑖(2,0) − 𝑖𝑖(0,2) = 284 + 1 − 27 − 27 = 231
(𝑖,𝑗)=(1,1)
(2,2) (2,2) 2
2
1 1 1 1
𝑉𝑎𝑟 = ( ∗ 𝑓 2 (𝑖, 𝑗)) − ( ∗ 𝑓(𝑖, 𝑗)) = ( ∗ 231) − ( ∗ 29) = 5.1875
4 4 4 4
(𝑖,𝑗)=(1,1) (𝑖,𝑗)=(1,1)
German University in Cairo
Faculty of Media Engineering and Technology
Winter 2024
DMET 901 – Computer Vision
Problem Set #1 Solution
Problem 6
Find the Run-length (RL) code for the binary image given below (coding for white pixels). The RL code is
defined as: (row, {first element in run, last element in run}*)*
0 0 1 1 1 1
0 0 0 0 0 0
0 1 0 1 0 0
1 1 1 0 1 0
0 0 0 0 0 0
RL code: (025)(21133)(30244)
Problem 7
Find the region adjacency graph corresponding to the image with 7 regions given below
0
1 5
3 2
4 6
German University in Cairo
Faculty of Media Engineering and Technology
Winter 2024
DMET 901 – Computer Vision
Problem Set #1 Solution
1 5
2 6
3 4