-
Notifications
You must be signed in to change notification settings - Fork 0
/
exp3.py
89 lines (84 loc) · 2.63 KB
/
exp3.py
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
85
86
87
88
89
'''
============================
To smoothen the input raw satellite image or image with general formats using various
smoothing filters such as average filter, Weighted average filter, Gaussian smoothing, etc.
============================
created: 17/08/2015
*auhor: sujitdeokar30@gmail.com*
'''
from scipy import misc, signal
import numpy as np
x = misc.lena()
x1 = misc.lena().astype(np.float32)
w = signal.gaussian(50, 5.0)
laplacian = np.array([[0,1,0], [1,-4,1], [0,1,0]], dtype=np.float32)
derfilt = np.array([1.0, -2, 1.0], dtype=np.float32)
ck = signal.cspline2d(x1, 8.0)
deriv = (signal.sepfir2d(ck, derfilt, [1]) + signal.sepfir2d(ck, [1], derfilt))
im1 = signal.convolve2d(x1,laplacian,mode='same',boundary='symm')
im2 = signal.sepfir2d(x, w, w)
plt.figure()
plt.subplot(221)
plt.imshow(x)
plt.title('original image')
plt.subplot(222)
plt.imshow(im1)
plt.title('laplacian filter image')
plt.subplot(223)
plt.imshow(im2)
plt.title('gaussian blurred iamge')
plt.subplot(224)
plt.imshow(deriv)
plt.gray()
plt.title('spline edge filter iamge')
plt.show()
'''
2D convolution is used for Image filtering in OpenCV.
As for one-dimensional signals, images also can be filtered with various low-pass filters (LPF), high-pass filters (HPF),
etc. A LPF helps in removing noise, or blurring the image. A HPF filters helps in finding edges in an image.
OpenCV provides a function, cv2.filter2D(), to convolve a kernel with an image. As an example, we will try
anaveraging filter on an image. A 5x5 averaging filter kernel can be defined as follows:
| 1 1 1 1 1 |
1 | 1 1 1 1 1 |
K = - | 1 1 1 1 1 |
25 | 1 1 1 1 1 |
| 1 1 1 1 1 |
'''
import cv2
import numpy as np
import pylab as plt
from scipy import misc
x = misc.lena()
img = x.astype(np.uint8)
kernel = np.ones((5,5),np.float32)/25
dst = cv2.filter2D(img,-1,kernel)
plt.figure()
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(dst),plt.title('Averaging')
plt.xticks([]), plt.yticks([])
plt.show()
'''
Image blurring can also be achieved using OpenCV function of *cv2.blur*. Here Kernel element is taken as
input and taken care inside the function itself.
'''
import cv2
import numpy as np
import pylab as plt
from scipy import misc
x = misc.lena()
x = x.astype(np.uint8)
blur = cv2.GaussianBlur(x,(9,9),50)
blur1 = cv2.blur(x,(9,9))
plt.figure()
plt.subplot(1,3,1)
plt.imshow(x)
plt.title('Original Image')
plt.subplot(1,3,2)
plt.imshow(blur)
plt.title('Image blurring using *cv2.blur* function in an Image')
plt.subplot(1,3,3)
plt.imshow(blur1)
plt.title('Image blurring using *cv2.GaussianBlur* function in an Image')
plt.gray()
plt.show()