Docsity

Computer Vision Assignment: Hybrid Images and Corner Detection Algorithms, Exercises of Computer science

Toggle sidebar
bg1
Computer Vision Assignment – 1
Sudheera Sivani Billakurti - S20160010014
Subhadeep Dash - S20160010021
V. Tejkiran - S20160020155
Question-1: Hybrid images
1. Initialization of libraries:
1. import numpy as np
2. from matplotlib import pyplot as plt
3. from matplotlib import image as mpimg
The above snippet of code is for importing the libraries numpy for vectorization and matplotlib for
reading and displaying images.
2. Hybrid Image conversion function:
1. def hybridImages(image1, image2, alpha, beta):
2. def highpass(values, alpha):
3. (M, N) = (values.shape[0], values.shape[1])
4. for i in range(M):
5. for j in range(N):
6. if(np.linalg.norm((i, j)) < alpha):
7. values[i, j] = 0
8. return values
9. def lowpass(values, beta):
10. (M, N) = (values.shape[0], values.shape[1])
11. for i in range(M):
12. for j in range(N):
13. if(np.linalg.norm((i, j)) > beta):
14. values[i, j] = 0
15. return values
16. image1 = np.array(mpimg.imread(image1))
17. image2 = np.array(mpimg.imread(image2))
18. if(image1.shape != image2.shape):
19. if(image1.shape > image2.shape):
20. image1 = image1[:image2.shape[0], :image2.shape[1]]
21. else:
22. image2 = image2[:image1.shape[0], :image1.shape[1]]
23. red1 = image1[:, :, 0]
24. green1 = image1[:, :, 1]
25. blue1 = image1[:, :, 2]
26. red2 = image2[:, :, 0]
27. green2 = image2[:, :, 1]
28. blue2 = image2[:, :, 2]
29. plt.figure();
30. plt.imshow(image1)
31. plt.show();
32. plt.figure();
33. plt.imshow(image2);
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

Download Computer Vision Assignment: Hybrid Images and Corner Detection Algorithms and more Exercises Computer science in PDF only on Docsity!

Computer Vision Assignment – 1

Sudheera Sivani Billakurti - S

Subhadeep Dash - S

V. Tejkiran - S

Question-1: Hybrid images

1. Initialization of libraries:

  1. import numpy as np
  2. from matplotlib import pyplot as plt
  3. from matplotlib import image as mpimg

The above snippet of code is for importing the libraries numpy for vectorization and matplotlib for

reading and displaying images.

2. Hybrid Image conversion function:

  1. def hybridImages(image1, image2, alpha, beta):
  2. def highpass(values, alpha):
  3. (M, N) = (values.shape[ 0 ], values.shape[ 1 ])
  4. for i in range(M):
  5. for j in range(N):
  6. if(np.linalg.norm((i, j)) < alpha):
  7. values[i, j] = 0
  8. return values
  9. def lowpass(values, beta):
  10. (M, N) = (values.shape[ 0 ], values.shape[ 1 ])
  11. for i in range(M):
  12. for j in range(N):
  13. if(np.linalg.norm((i, j)) > beta):
  14. values[i, j] = 0
  15. return values
  16. image1 = np.array(mpimg.imread(image1))
  17. image2 = np.array(mpimg.imread(image2))
  18. if(image1.shape != image2.shape):
  19. if(image1.shape > image2.shape):
  20. image1 = image1[:image2.shape[ 0 ], :image2.shape[ 1 ]]
  21. else:
  22. image2 = image2[:image1.shape[ 0 ], :image1.shape[ 1 ]]
  23. red1 = image1[:, :, 0 ]
  24. green1 = image1[:, :, 1 ]
  25. blue1 = image1[:, :, 2 ]
  26. red2 = image2[:, :, 0 ]
  27. green2 = image2[:, :, 1 ]
  28. blue2 = image2[:, :, 2 ]
  29. plt.figure();
  30. plt.imshow(image1)
  31. plt.show();
  32. plt.figure();
  33. plt.imshow(image2);
  1. plt.show();
  2. freq_image1_red = np.fft.fft2(red1)
  3. freq_image1_green = np.fft.fft2(green1)
  4. freq_image1_blue = np.fft.fft2(blue1)
  5. freq_image2_red = np.fft.fft2(red2)
  6. freq_image2_green = np.fft.fft2(green2)
  7. freq_image2_blue = np.fft.fft2(blue2)
  8. freq_image1_red = highpass(freq_image1_red, alpha)
  9. freq_image1_green = highpass(freq_image1_green, alpha)
  10. freq_image1_blue = highpass(freq_image1_blue, alpha)
  11. freq_image2_red = lowpass(freq_image2_red, beta)
  12. freq_image2_green = lowpass(freq_image2_green, beta)
  13. freq_image2_blue = lowpass(freq_image2_blue, beta)
  14. final_image_red = freq_image1_red + freq_image2_red
  15. final_image_green = freq_image1_green + freq_image2_green
  16. final_image_blue = freq_image1_blue + freq_image2_blue
  17. final_image_red = np.fft.ifft2(final_image_red)
  18. final_image_blue = np.fft.ifft2(final_image_blue)
  19. final_image_green = np.fft.ifft2(final_image_green)
  20. final_image = np.zeros(image1.shape)
  21. final_image[:,:, 0 ] = np.absolute(final_image_red)
  22. final_image[:,:, 1 ] = np.absolute(final_image_green)
  23. final_image[:,:, 2 ] = np.absolute(final_image_blue)
  24. return np.absolute(final_image)/np.max(np.absolute(final_image))

The above function is used to apply low pass and high pass filters in the frequency domain of

the images and further merging them in frequency domain to give a single hybrid image. The lines 2- 8

define the high pass filter which allows the frequencies greater than a certain value 𝜶 to pass and

suppresses other frequencies whereas the lines 9- 15 define the low pass filter which only allow

frequencies less than 𝜷 to pass.

In the lines 16-17, the images are read using imread function and from the lines 18 to 22, both

the images are made to the same length and width, if they aren’t of same shape. In the lines 23-28,

the channels are separated in three channels for each image and then in the next 6 lines, we view the

images. In the lines 35-46, the data in the channels are converted to frequency domain individually

and high pass function is applied on the first image whereas low pass function is applied on the

second image. In the next three lines, the result frequency intensities are merged into one and are

converted to spatial domain in the further 3 lines. Then, we define the final image and assign the

resultant channels to it and return the absolute normalized value of the resultant image as the final

output.

INPUTS:

  1. plt.imshow(hybridImages("marilyn.bmp", "einstein.bmp", 25 , 1000 ))
  2. plt.show()
  3. plt.imshow(hybridImages("Afghan_girl_before.jpg","Afghan_girl_after.jpg ", 25 , 750 ))
  4. plt.show()

Question-2: Corner Detection

1. Initialization of libraries:

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from matplotlib import image as mpimg

The above snippet of code is for importing the libraries numpy for vectorization and matplotlib for

reading and displaying images.

2. Shi-Tomasi Corner detection algorithm:

  1. def shi_tomasi(org_image, threshold, window_size):
  2. org_image = np.array(mpimg.imread(org_image))
  3. plt.figure()
  4. plt.imshow(org_image)
  5. plt.show()
  6. if(len(org_image.shape) == 3 ):
  7. image = np.dot(org_image[...,: 3 ], [0.299, 0.587, 0.114])
  1. left_shifted_image = np.concatenate((image[:, 1 :], image[:,- 1 :]), axis= 1 )
  2. up_shifted_image = np.concatenate((image[ 1 :, :], image[- 1 :,:]), axis= 0 )
  3. x_change = left_shifted_image-image
  4. y_change = up_shifted_image-image
  5. new_image = np.zeros(image.shape)
  6. m = image.shape[ 0 ]
  7. n = image.shape[ 1 ]
  8. for i in range(window_size//2, m-window_size//2):
  9. for j in range(window_size//2, n-window_size//2):
  10. A = np.square(x_change[i-window_size//2:i+window_size//2, j-window_size//2:j+window_size//2]).sum()
  11. B = np.multiply((x_change[i- window_size//2:i+window_size//2, j- window_size//2:j+window_size//2]),(y_change[i- window_size//2:i+window_size//2, j- window_size//2:j+window_size//2])).sum()
  12. C = np.square(y_change[i-window_size//2:i+window_size//2, j-window_size//2:j+window_size//2]).sum()
  13. H = np.array([[A, B], [B, C]])
  14. f = np.linalg.det(H) - k*np.square(np.trace(H))
  15. if(f > threshold):
  16. if(len(org_image.shape) == 3 ):
  17. org_image[i][j] = [ 255 , 0 , 0 ]
  18. else:
  19. org_image[i][j] = 255
  20. new_image[i][j] = 255
  21. return org_image, new_image

In the Harris corner detection function, he second moment matrix is computed similar as in

previous case. The only difference is the condition based on which it’s decided if it’s a corner or not. A

point is considered a corner if the following condition satisfies:

𝒅𝒆𝒕 ([

∑ 𝑰𝒙^ 𝟐^ ∑ 𝑰𝒙𝑰𝒚

∑ 𝑰𝒙𝑰𝒚 ∑ 𝑰𝒚^ 𝟐^

]) − 𝒌 ∗

𝒕𝒓𝒂𝒄𝒆 ([

∑ 𝑰𝒙^ 𝟐^ ∑ 𝑰𝒙𝑰𝒚

∑ 𝑰𝒙𝑰𝒚 ∑ 𝑰𝒚^ 𝟐^

])

Where k is a constant.

INPUTS:

1) Shi-Tomasi Corner Detection Algorithm:

  1. corners_placed, corners = shi_tomasi("chess.jpg", 1000 , 7 )
  2. plt.figure()
  3. plt.imshow(corners_placed)
  4. plt.show()
  5. plt.figure()
  6. plt.imshow(corners, cmap="gray")
  7. plt.show()

We have tested the corner detection image on an additional chess board image apart from

the data provided.

  1. corners_placed, corners = shi_tomasi("Image1.jpg", 100 , 3 )
  2. plt.figure()
  3. plt.imshow(corners_placed)
  4. plt.show()
  5. plt.figure()
  6. plt.imshow(corners, cmap="gray")
  7. plt.show()
  8. corners_placed, corners = shi_tomasi("Image2.jpg", 100 , 3 )
  9. plt.figure()
  10. plt.imshow(corners_placed)
  11. plt.show()
  12. plt.figure()
  13. plt.imshow(corners, cmap="gray")
  14. plt.show()
  15. corners_placed, corners = shi_tomasi("Image3.jpg", 100 , 3 )
  16. plt.figure()
  17. plt.imshow(corners_placed)
  18. plt.show()
  19. plt.figure()
  20. plt.imshow(corners, cmap="gray")
  21. plt.show()

2) Harris Corner Detection Algorithm:

  1. corners_placed, corners = harris_corners("chess.jpg", 2000 , 9 , 0.24)
  2. plt.figure()
  3. plt.imshow(corners_placed)
  4. plt.show()
  5. plt.figure()
  6. plt.imshow(corners, cmap="gray")
  7. plt.show()
  8. corners_placed, corners = harris_corners("Image1.jpg", 20000 , 3 , 0.10)
  9. plt.figure()
  10. plt.imshow(corners_placed)
  11. plt.show()
  12. plt.figure()
  13. plt.imshow(corners, cmap="gray")
  14. plt.show()
  15. corners_placed, corners = harris_corners("Image2.jpg", 10000 , 3 , 0.1)
  16. plt.figure()
  17. plt.imshow(corners_placed)
  18. plt.show()
  19. plt.figure()
  20. plt.imshow(corners, cmap="gray")
  21. plt.show()
  1. def max_filter(img, K, sigma):
  2. M, N = img.shape
  3. final_image = np.zeros(img.shape)
  4. plt.figure()
  5. fig, ax = plt.subplots()
  6. for i in range(M-K):
  7. for j in range(N-K):
  8. if(img[i+K//2, j+K//2] == np.max(img[i:i+K, j:j+K])):
  9. final_image[i+K//2, j+K//2] = np.sqrt(2)*sigma
  10. ax.add_artist(plt.Circle((j+K//2, i+K//2), np.sqrt(2)*sigma, color='r', fill = False, linewidth=3))
  11. plt.imshow(final_image, cmap="gray")
  12. plt.show()
  13. return final_image

Implementation of various functions such as Laplacian of Gaussian (in lines 1-5), Gaussian (in

lines 7-12) and Difference of Gaussian (in lines 14-17), Padding (in lines 19-22), Convolution (in lines

24 - 31) and Non-max Suppression (in lines 33-45).

  1. start = time.time()
  2. n = 10
  3. path = "butterfly.jpg"
  4. org_sigma=3.
  5. factor = 1.
  6. filter_type = 1
  7. org_image = mpimg.imread(path)
  8. if(len(org_image.shape) == 3 ):
  9. image = np.dot(org_image[...,: 3 ], [0.299, 0.587, 0.114])
  10. else:
  11. image = org_image
  12. layers = np.zeros((n, image.shape[ 0 ], image.shape[ 1 ]))
  13. for i in range(n):
  14. sigma = org_sigma*np.power(factor, i)
  15. window_size = int(np.ceil( 6 *sigma))
  16. if(window_size% 2 == 0 ):
  17. window_size += 1
  18. if(filter_type == 1 ):
  19. LoG = Laplacian_of_Gaussian(window_size, sigma)
  20. else:
  21. LoG=dog(np.power(factor, i+ 1 ),window_size,org_sigma)
  22. plt.figure()
  23. layers[i] = apply_filter(padding(image, window_size), LoG)[window_size//2:-window_size//2+1, window_size//2:- window_size//2+1]
  24. plt.imshow(layers[i], cmap = "gray")
  25. centres = np.zeros(layers.shape)
  26. for i in range(n):
  27. sigma = org_sigma*np.power(factor, i)
  28. window_size = int(np.ceil( 6 *sigma))
  29. if(window_size% 2 == 0 ):
  30. window_size += 1
  31. centres[i] = max_filter(layers[i], window_size, sigma)
  32. plt.figure()
  33. fig, ax = plt.subplots()
  34. for i in range(centres.shape[ 0 ]):
  35. sigma = org_sigma*np.power(factor, i)
  36. for j in range(centres.shape[ 1 ]):
  37. for k in range(centres.shape[ 2 ]):
  1. if(centres[i][j][k]!= 0 and centres[i, j, k] == np.max(centres[:, int(j-np.sqrt( 2 )sigma):int(j+np.sqrt( 2 )sigma), k- int(np.sqrt( 2 )sigma):k+int(np.sqrt( 2 )sigma)])):
  2. ax.add_artist(plt.Circle((k, j), np.sqrt( 2 )*sigma, color='r', fill = False, linewidth= 1 ))
  3. plt.imshow(org_image)
  4. plt.show()
  5. end = time.time()
  6. elapsed = end - start
  7. print(elapsed)

In this snippet, the variables n, org_sigma, factor, path are used to represent the number of

iterations, the initial standard deviation, the factor by which standard deviation is scaled every

iteration, the path of the image file respectively and the filter_type 1 corresponds to usage of

Laplacian of Gaussian filter whereas filter_type 2 refers to the usage of Difference of Gaussian filter.

The line 7 reads the image and immediate after we convert the 3D RGB image to grayscale

image. From the lines 12-24, the filter is applied with the scaled variance and then after applying non-

max suppression the resultant information is stored in the layers list. Then the lines (25-32) perform

the non-max suppression on the image giving centers to the circles of radius √𝟐𝝈. These centers are

stored in center list on which again Non-max Suppression is applied over the stack of layers and only

then the circles are plotted with their respective radii.

INPUTS:

The following parameters are send for detecting blobs:

  1. n = 10
  2. path = "butterfly.jpg"
  3. org_sigma=3.
  4. factor = 1.
  5. filter_type = 1
  6. n = 10
  7. path = "butterfly.jpg"
  8. org_sigma=3.
  9. factor = 1.
  10. filter_type = 2
  11. n = 10
  12. path = "einstein.jpg"
  13. org_sigma=3.
  14. factor = 1.
  15. filter_type = 1
  16. n = 10
  17. path = "einstein.jpg"
  18. org_sigma=3.
  19. factor = 1.
  20. filter_type = 2
  1. n = 10
  2. path = "rods.jpeg"
  3. org_sigma=3.
  4. factor = 1.
  5. filter_type = 2
  6. n = 10
  7. path = "bubbles.jpeg"
  8. org_sigma=3.
  9. factor = 1.
  10. filter_type = 1
  11. n = 10
  12. path = "bubbles.jpeg"
  13. org_sigma=3.
  14. factor = 1.
  15. filter_type = 2

RESULTS:

The outputs of Laplacian of Gaussian are on left where as the ones of Difference of Gaussian are on

right.