Skip to content

Commit b968903

Browse files
authored
Create 2_image_smoothing.py
1 parent 2242e3e commit b968903

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

2_image_smoothing.py

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#******************************************#
2+
'''
3+
This program can be used to apply smoothing (blurring) techniques on brain MRI scan image
4+
All image processing functions are performed on grayscale image (need for opencv tool)
5+
'''
6+
#******************************************#
7+
8+
import cv2
9+
import numpy as np
10+
import matplotlib.pyplot as plt
11+
12+
kernel = np.ones((5,5),np.float32)/25 #for cv2.filter2D()
13+
14+
class smoothing(object):
15+
16+
def read_image(self,image):
17+
image_read = cv2.imread(image)
18+
image_gray = cv2.cvtColor(image_read,cv2.COLOR_BGR2GRAY)
19+
20+
return image_gray
21+
'''
22+
cv2.filter2D() - to convolve the kernel with an image
23+
'''
24+
def filter2D(self,image_gray):
25+
image_result = cv2.filter2D(image_gray,-1,kernel)
26+
return image_result
27+
'''
28+
Blurring - Low pass filter kernel
29+
- removal of noise
30+
- removes high frequency content (noise, edges)
31+
- 4 types - averaging, median, gaussian, bilateral
32+
'''
33+
'''
34+
cv2.blur() or cv2.boxFilter() - convolving image with normalized box filter
35+
- takes avg of pixels under kernel area and replaces central element with this average
36+
'''
37+
def averaging(self,image_gray):
38+
image_result = cv2.blur(image_gray,(5,5))
39+
return image_result
40+
'''
41+
cv2.GaussianBlur() - remove gaussian noise from the noise
42+
Instead of box filter with equal coefficients, we specify the width and height of kernel (positive and odd)
43+
SigmaX Gaussian kernel standard deviation in X direction and SigmaY in Y-direction
44+
Gaussian filter takes the a neighborhood around the pixel and finds its Gaussian weighted average
45+
'''
46+
def gaussian(self,image_gray):
47+
image_result = cv2.GaussianBlur(image_gray,(5,5),sigmaX=0)
48+
return image_result
49+
'''
50+
cv2.meadianBlur() - computes median of all pixels under the kernel and the central pixel
51+
is replaced with this median value.
52+
Effective in removing salt and pepper noise - reduces noise effectively.
53+
'''
54+
def median(self,image_gray):
55+
image_result = cv2.medianBlur(image_gray,5)
56+
return image_result
57+
58+
'''
59+
cv2.bilateralFilter() - noise removal while preserving edges
60+
bilateral filter = Gaussian filter + multiplicative gaussian filter component which is a function of pixel intensity differences.
61+
- sptail neigbors + intensity domain
62+
'''
63+
def bilateral(self,image_gray):
64+
image_result = cv2.bilateralFilter(image_gray,9,75,75)
65+
return image_result
66+
67+
_inst = smoothing()
68+
read_image = _inst.read_image
69+
filter2D = _inst.filter2D
70+
averaging = _inst.averaging
71+
gaussian = _inst.gaussian
72+
median = _inst.median
73+
bilateral = _inst.bilateral
74+
75+
image_path = 'image_tumor.jpg'
76+
tumor = read_image(image_path)
77+
78+
filter2D = filter2D(tumor)
79+
averaging = averaging(tumor)
80+
gaussian = gaussian(tumor)
81+
median = median(tumor)
82+
bilateral = bilateral(tumor)
83+
84+
#####################################
85+
## Plotting all stages of image blurring ##
86+
#####################################
87+
fig, axs = plt.subplots(2,3, figsize=(15, 9), facecolor='w', edgecolor='k')
88+
fig.subplots_adjust(hspace = .25, wspace=.5)
89+
90+
axs = axs.ravel()
91+
92+
axs[0].imshow(tumor,'gray')
93+
axs[0].set_title('1. Orginial Gray Image')
94+
axs[1].imshow(filter2D,'gray')
95+
axs[1].set_title('2. Using cv2.filter2D()')
96+
axs[2].imshow(averaging,'gray')
97+
axs[2].set_title('3. Using cv2.blur()')
98+
axs[3].imshow(gaussian,'gray')
99+
axs[3].set_title('4. Using cv2.GaussianBlur()')
100+
axs[4].imshow(median,'gray')
101+
axs[4].set_title('5. Using cv2.medianBlur()')
102+
axs[5].imshow(bilateral,'gray')
103+
axs[5].set_title('6. Using cv2.bilateralFilter()')
104+
105+
plt.show()
106+
107+
108+

0 commit comments

Comments
 (0)