@@ -36,70 +36,14 @@ Experimental results (figures and tables on this page):
3636<br />
3737
3838### <a name =" Examples of Simple Filtering " >Examples of Simple Filtering</a >
39+ Taking RGB image as input, converting it to GrayScale.
40+ <br />Consider following part of the code:
3941
4042``` py
41- # Input RGB image and implementing simple filtering
42-
43- # Importing needed libraries
44- import numpy as np
45- from PIL import Image
46- import matplotlib.pyplot as plt
47-
4843# Creating an array from image data
4944input_image = Image.open(" images/eagle.jpeg" )
5045image_np = np.array(input_image)
5146
52- # Checking the type of the array
53- print (type (image_np)) # <class 'numpy.ndarray'>
54- # Checking the shape of the array
55- print (image_np.shape) # (270, 480, 3)
56-
57- # Showing image with every channel separately
58- channel_0 = image_np[:, :, 0 ]
59- channel_1 = image_np[:, :, 1 ]
60- channel_2 = image_np[:, :, 2 ]
61-
62- # Checking if all channels are different
63- print (np.array_equal(channel_0, channel_1)) # False
64- print (np.array_equal(channel_1, channel_2)) # False
65-
66- # Creating a figure with subplots
67- f, ax = plt.subplots(nrows = 2 , ncols = 2 )
68- # ax is (2, 2) np array and to make it easier to read we use 'flatten' function
69- # Or we can call each time ax[0, 0]
70- ax0, ax1, ax2, ax3 = ax.flatten()
71-
72- # Adjusting first subplot
73- ax0.imshow(channel_0, cmap = plt.get_cmap(' Reds' ))
74- ax0.set_xlabel(' ' )
75- ax0.set_ylabel(' ' )
76- ax0.set_title(' First channel' )
77-
78- # Adjusting second subplot
79- ax1.imshow(channel_1, cmap = plt.get_cmap(' Greens' ))
80- ax1.set_xlabel(' ' )
81- ax1.set_ylabel(' ' )
82- ax1.set_title(' Second channel' )
83-
84- # Adjusting third subplot
85- ax2.imshow(channel_2, cmap = plt.get_cmap(' Blues' ))
86- ax2.set_xlabel(' ' )
87- ax2.set_ylabel(' ' )
88- ax2.set_title(' Third channel' )
89-
90- # Adjusting fourth subplot
91- ax3.imshow(image_np)
92- ax3.set_xlabel(' ' )
93- ax3.set_ylabel(' ' )
94- ax3.set_title(' Original image' )
95-
96- # Function to make distance between figures
97- plt.tight_layout()
98- # Giving the name to the window with figure
99- f.canvas.set_window_title(' GreyScaled image with three identical channels' )
100- # Showing the plots
101- plt.show()
102-
10347# Preparing image for Edge detection
10448# Converting RGB image into GrayScale image
10549# Using formula:
@@ -114,7 +58,12 @@ plt.figure('GrayScale image from RGB')
11458# Showing the image by using obtained array
11559plt.imshow(image_GrayScale, cmap = plt.get_cmap(' gray' ))
11660plt.show()
61+ ```
62+
63+ Setting Hyperparameters and applying Pad frame for input image.
64+ <br />Consider following part of the code:
11765
66+ ``` py
11867# Applying to the GrayScale image Pad frame with zero values
11968# Using NumPy method 'pad'
12069GrayScale_image_with_pad = np.pad(image_GrayScale, (1 , 1 ), mode = ' constant' , constant_values = 0 )
@@ -130,23 +79,25 @@ print(GrayScale_image_with_pad.shape) # (272, 482)
13079# Width_Out = (Width_In - K_size + 2*Pad)/Step + 1
13180# Imagine, that input image is 5x5 spatial size (width and height), then output image:
13281# Width_Out = (5 - 3 + 2*1)/1 + 1 = 5, and this is equal to input image
82+ ```
13383
134- # Preparing zero valued output arrays for filtered images (convolved images)
135- # The shape is the same with input image according to the chosen Hyperparameters
136- # For three filters for Edge detection and implementing it only for one GrayScale channel
137- output_image_1 = np.zeros(image_GrayScale.shape)
138- output_image_2 = np.zeros(image_GrayScale.shape)
139- output_image_3 = np.zeros(image_GrayScale.shape)
84+ Declaring filters for ** Edge Detection** .
85+ <br />Consider following part of the code:
14086
87+ ``` py
14188# Declaring standard filters (kernel) with size 3x3 for edge detection
14289filter_1 = np.array([[1 , 0 , - 1 ], [0 , 0 , 0 ], [- 1 , 0 , 1 ]])
14390filter_2 = np.array([[0 , 1 , 0 ], [1 , - 4 , 1 ], [0 , 1 , 0 ]])
14491filter_3 = np.array([[- 1 , - 1 , - 1 ], [- 1 , 8 , - 1 ], [- 1 , - 1 , - 1 ]])
14592# Checking the shape
14693print (filter_1.shape, filter_2.shape, filter_3.shape)
14794# ((3, 3) (3, 3) (3, 3)
95+ ```
14896
97+ Creating function to delete negative values from resulted image.
98+ <br />Consider following part of the code:
14999
100+ ``` py
150101# In order to prevent appearing values that are less than -1
151102# Following function is declared
152103def relu (array ):
@@ -156,8 +107,12 @@ def relu(array):
156107 result = np.where(array > r, array, r)
157108 # Returning resulted array
158109 return result
110+ ```
159111
112+ Creating function to delete values that are more than 255.
113+ <br />Consider following part of the code:
160114
115+ ``` py
161116# In order to prevent appearing values that are more than 255
162117# The following function is declared
163118def image_pixels (array ):
@@ -170,8 +125,12 @@ def image_pixels(array):
170125 result = np.where(array < r, array, r)
171126 # Returning resulted array
172127 return result
128+ ```
173129
130+ Implementing filtering for ** Edge Detection** also known as ** Convolution Operation** .
131+ <br />Consider following part of the code:
174132
133+ ``` py
175134# Implementing convolution operation for Edge detection for GrayScale image
176135# Going through all input image with pad frame
177136for i in range (GrayScale_image_with_pad.shape[0 ] - 2 ):
@@ -185,42 +144,10 @@ for i in range(GrayScale_image_with_pad.shape[0] - 2):
185144 output_image_2[i, j] = np.sum(patch_from_input_image * filter_2)
186145 # With filter_3
187146 output_image_3[i, j] = np.sum(patch_from_input_image * filter_3)
188-
189- # Applying 'relu' and 'image_pixels' function to get rid of negative values and that ones that more than 255
190- output_image_1 = image_pixels(relu(output_image_1))
191- output_image_2 = image_pixels(relu(output_image_2))
192- output_image_3 = image_pixels(relu(output_image_3))
193-
194- # Showing results on the appropriate figures
195- figure_1, ax = plt.subplots(nrows = 3 , ncols = 1 )
196-
197- # Adjusting first subplot
198- ax[0 ].imshow(output_image_1, cmap = plt.get_cmap(' gray' ))
199- ax[0 ].set_xlabel(' ' )
200- ax[0 ].set_ylabel(' ' )
201- ax[0 ].set_title(' Edge #1' )
202-
203- # Adjusting second subplot
204- ax[1 ].imshow(output_image_2, cmap = plt.get_cmap(' gray' ))
205- ax[1 ].set_xlabel(' ' )
206- ax[1 ].set_ylabel(' ' )
207- ax[1 ].set_title(' Edge #2' )
208-
209- # Adjusting third subplot
210- ax[2 ].imshow(output_image_3, cmap = plt.get_cmap(' gray' ))
211- ax[2 ].set_xlabel(' ' )
212- ax[2 ].set_ylabel(' ' )
213- ax[2 ].set_title(' Edge #3' )
214-
215- # Function to make distance between figures
216- plt.tight_layout()
217- # Giving the name to the window with figure
218- figure_1.canvas.set_window_title(' Convolution with filters (simple filtering)' )
219- # Showing the plots
220- plt.show()
221-
222147```
223148
149+ Showing resulted images on the figure.
150+
224151![ Simple_filtering_with_convolution] ( images/Simple_filtering_with_convolution.png )
225152
226153Full code is available here: [ Simple_Filtering.py] ( https://github.com/sichkar-valentyn/Image_processing_in_Python/tree/master/Codes/Simple_Filtering.py )
0 commit comments