Skip to content

Commit 4cccbf5

Browse files
committed
Merge branch 'release/0.1.0'
2 parents 05c54de + 1a947bb commit 4cccbf5

File tree

9 files changed

+259
-0
lines changed

9 files changed

+259
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#This will include files and rules I want ignored in my repository

.idea/misc.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/sobel-filter-tutorial.iml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 138 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Images/original_image.PNG

395 KB
Loading

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The purpose of this repository is to create a tutorial for the application of a Sobel filter.
2+
This repository is currently under construction.

sobel_main.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
"""
2+
There is a lot of good information out there related to this topic. The image used in this example was extracted from:
3+
https://en.wikipedia.org/wiki/Sobel_operator
4+
5+
The same concepts explained in this code can be used for other types of filters.
6+
7+
I'll not go into much detail in how to transform a colored image (an array of dimension N x M x 3 where N (height) and M
8+
(width) defines the frame of the image while the 3 dimensions gives us the colors (RedGreenBlue)) but the formulas to do
9+
such transformation can be found easily in Google.
10+
11+
There are lots of information about colored to grayscale image transformation out there on the web. I use one that takes
12+
the RGB values of the image and weighs them based on predefined parameters (https://en.wikipedia.org/wiki/Grayscale).
13+
"""
14+
15+
from matplotlib.image import imread
16+
import matplotlib.pyplot as plt
17+
import numpy as np
18+
19+
# #------------------------------------------------------------------------------
20+
# PART I - Transforming a color image into grayscale
21+
# #------------------------------------------------------------------------------
22+
23+
# Here we import the image file as a tensor of shape (nx, ny, nz)
24+
photo_file = 'Images/original_image.PNG'
25+
input_image = imread(photo_file)
26+
[nx, ny, nz] = np.shape(input_image) # nx: height, ny: width, nz: colors (RGB)
27+
28+
# Extracting each one of the RGB components
29+
# r_img, g_img, b_img = input_image[:, :, 0], input_image[:, :, 1], input_image[:, :, 2]
30+
r_img, g_img, b_img = input_image[:, :, 0] / 255, input_image[:, :, 1] / 255, input_image[:, :, 2] / 255
31+
32+
# To make it grayscale we can use the following constants to weight
33+
# r_const, g_const, b_const = 0.2989, 0.5870, 0.1140
34+
r_const, g_const, b_const = 0.2126, 0.7152, 0.0722
35+
36+
# A weighted average of each color component will give us the grayscale image
37+
# grayscale = r_const*r_img + g_const*g_img + b_const*b_img
38+
gamma = 1.0
39+
grayscale = (r_const*r_img**gamma + g_const*g_img**gamma + b_const*b_img**gamma)*255
40+
41+
# This command will display the image on the screen
42+
# plt.imshow(grayscale, cmap=plt.get_cmap('gray'))
43+
# plt.show()
44+
45+
# #------------------------------------------------------------------------------
46+
# PART II - Applying the kernel Gx and Gy to image
47+
# #------------------------------------------------------------------------------
48+
49+
"""
50+
The kernels Gx and Gy can be thought of as a differential operation in the input_image of the input image
51+
in the directions x and y respectively. These kernels are given by:
52+
_ _ _ _
53+
| | | |
54+
| 1.0 0.0 -1.0 | | 1.0 2.0 1.0 |
55+
Gx = | 2.0 0.0 -2.0 | and Gy = | 0.0 0.0 0.0 |
56+
| 1.0 0.0 -1.0 | | -1.0 -2.0 -1.0 |
57+
|_ _| |_ _|
58+
"""
59+
60+
# This chunk of code defines the kernel operations and outputs image
61+
Gx = np.array([[1.0, 0.0, -1.0], [2.0, 0.0, -2.0], [1.0, 0.0, -1.0]])
62+
Gy = np.array([[1.0, 2.0, 1.0], [0.0, 0.0, 0.0], [-1.0, -2.0, -1.0]])
63+
[rows, columns] = np.shape(grayscale)
64+
sobel_filtered_image = np.zeros(shape=(rows, columns))
65+
for i in range(rows - 2):
66+
for j in range(columns - 2):
67+
s1 = np.sum(np.multiply(Gx, grayscale[i:i+3, j:j+3]))
68+
s2 = np.sum(np.multiply(Gy, grayscale[i:i+3, j:j+3]))
69+
sobel_filtered_image[i + 1, j + 1] = np.sqrt(s1 ** 2 + s2 ** 2)
70+
71+
72+
# Change the data type for the images we want to display
73+
sobel_filtered_image = sobel_filtered_image.astype('int32')
74+
input_image = input_image.astype('int32')
75+
76+
# Display the images
77+
78+
fig = plt.figure()
79+
# plt.gray() # show the filtered result in grayscale
80+
ax1 = fig.add_subplot(121) # left side
81+
ax2 = fig.add_subplot(122) # right side
82+
83+
ax1.imshow(input_image)
84+
ax2.imshow(sobel_filtered_image, cmap=plt.get_cmap('gray'))
85+
plt.show()
86+
87+
# Save the filtered image in destination path
88+
plt.imsave('test_sobel_filtered_image.png', sobel_filtered_image, cmap=plt.get_cmap('gray'))

0 commit comments

Comments
 (0)