-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsobel_from_scratch.py
More file actions
84 lines (67 loc) · 4.14 KB
/
sobel_from_scratch.py
File metadata and controls
84 lines (67 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""
There is a lot of good information out there related to this topic. The image used in this example was extracted from:
https://en.wikipedia.org/wiki/Sobel_operator
The same concepts explained in this code can be used for other types of filters.
A color image is an array of dimension N x M x 3 where N is the height (number of rows), M is the width (number of
columns) and 3 is related to the colors red, green, blue composing the image.
A grayscale_image image is an array of dimension N x M.
There are lots of good information about color to grayscale_image image transformations out there on the web.
The following link has a very interesting discussion of how to properly do it:
https://stackoverflow.com/questions/687261/converting-rgb-to-grayscale-intensity
"""
from matplotlib.image import imread
import matplotlib.pyplot as plt
import numpy as np
# #---------------------------------------------------------------------------------------------------------------------
# PART I - Transforming an image from color to grayscale
# #---------------------------------------------------------------------------------------------------------------------
# Here we import the image file as an array of shape (nx, ny, nz)
image_file = 'Images/original_image.PNG'
input_image = imread(image_file) # this is the array representation of the input image
[nx, ny, nz] = np.shape(input_image) # nx: height, ny: width, nz: colors (RGB)
# Extracting each one of the RGB components
r_img, g_img, b_img = input_image[:, :, 0], input_image[:, :, 1], input_image[:, :, 2]
# The following operation will take weights and parameters to convert the color image to grayscale
gamma = 1.400 # a parameter
r_const, g_const, b_const = 0.2126, 0.7152, 0.0722 # weights for the RGB components respectively
grayscale_image = r_const * r_img ** gamma + g_const * g_img ** gamma + b_const * b_img ** gamma
# This command will display the grayscale image alongside the original image
fig1 = plt.figure(1)
ax1, ax2 = fig1.add_subplot(121), fig1.add_subplot(122)
ax1.imshow(input_image)
ax2.imshow(grayscale_image, cmap=plt.get_cmap('gray'))
fig1.show()
# #---------------------------------------------------------------------------------------------------------------------
# PART II - Applying the Sobel operator
# #---------------------------------------------------------------------------------------------------------------------
"""
The kernels Gx and Gy can be thought of as a differential operation in the "input_image" array in the directions x and y
respectively. These kernels are represented by the following matrices:
_ _ _ _
| | | |
| 1.0 0.0 -1.0 | | 1.0 2.0 1.0 |
Gx = | 2.0 0.0 -2.0 | and Gy = | 0.0 0.0 0.0 |
| 1.0 0.0 -1.0 | | -1.0 -2.0 -1.0 |
|_ _| |_ _|
"""
# Here we define the matrices associated with the Sobel filter
Gx = np.array([[1.0, 0.0, -1.0], [2.0, 0.0, -2.0], [1.0, 0.0, -1.0]])
Gy = np.array([[1.0, 2.0, 1.0], [0.0, 0.0, 0.0], [-1.0, -2.0, -1.0]])
[rows, columns] = np.shape(grayscale_image) # we need to know the shape of the input grayscale image
sobel_filtered_image = np.zeros(shape=(rows, columns)) # initialization of the output image array (all elements are 0)
# Now we "sweep" the image in both x and y directions and compute the output
for i in range(rows - 2):
for j in range(columns - 2):
gx = np.sum(np.multiply(Gx, grayscale_image[i:i + 3, j:j + 3])) # x direction
gy = np.sum(np.multiply(Gy, grayscale_image[i:i + 3, j:j + 3])) # y direction
sobel_filtered_image[i + 1, j + 1] = np.sqrt(gx ** 2 + gy ** 2) # calculate the "hypotenuse"
# Display the original image and the Sobel filtered image
fig2 = plt.figure(2)
ax1, ax2 = fig2.add_subplot(121), fig2.add_subplot(122)
ax1.imshow(input_image)
ax2.imshow(sobel_filtered_image, cmap=plt.get_cmap('gray'))
fig2.show()
# Show both images
# plt.show()
# Save the filtered image in destination path
# plt.imsave('sobel_filtered_image.png', sobel_filtered_image, cmap=plt.get_cmap('gray'))