To implement filters for smoothing and sharpening the images in the spatial domain.
Anaconda - Python 3.7
Install the open cv by using pip install opencv-python
Import cv2 for reading and changing it smooth and sharpen image
import numpy for give the width and size of a image
import matplotlib.pyplot for display the image.
i) Using Averaging Filter
import cv2
import numpy as np
import matplotlib.pyplot as plt
image1=cv2.imread('test_image.jpeg')
image2=cv2.cvtColor(image1,cv2.COLOR_BGR2RGB)
kernel=np.ones((11,11),np.float32)/121
plt.figure(figsize=(9,9))
plt.subplot(1,2,1)
plt.imshow(image2)
plt.title('ORIGINAL')
plt.axis('off')
plt.subplot(1,2,2)
plt.imshow(image3)
plt.title('FILTERED')
plt.axis('off')
ii) Using Weighted Averaging Filter
import cv2
import numpy as np
import matplotlib.pyplot as plt
image1=cv2.imread('test_image.jpeg')
image2=cv2.cvtColor(image1,cv2.COLOR_BGR2RGB)
kernel=np.ones((11,11),np.float32)/121
kernal2=np.array([[1,2,1],[2,4,2],[1,2,1]])/16
image3=cv2.filter2D(image2,-1,kernal2)
plt.figure(figsize=(9,9))
plt.subplot(1,2,1)
plt.imshow(image2)
plt.title('ORIGINAL')
plt.axis('off')
plt.subplot(1,2,2)
plt.imshow(image3)
plt.title('FILTERED')
plt.axis('off')
iii) Using Gaussian Filter
import cv2
import numpy as np
import matplotlib.pyplot as plt
image1=cv2.imread('test_image.jpeg')
image2=cv2.cvtColor(image1,cv2.COLOR_BGR2RGB)
kernel=np.ones((11,11),np.float32)/121
image3=cv2.GaussianBlur(src=image2,ksize=(11,11),sigmaX=0,sigmaY=0)
plt.figure(figsize=(9,9))
plt.subplot(1,2,1)
plt.imshow(image2)
plt.title('ORIGINAL')
plt.axis('off')
plt.subplot(1,2,2)
plt.imshow(image3)
plt.title(' Gaussian Filter')
plt.axis('off')
iv) Using Median Filter
import cv2
import numpy as np
import matplotlib.pyplot as plt
image1=cv2.imread('test_image.jpeg')
image2=cv2.cvtColor(image1,cv2.COLOR_BGR2RGB)
kernel=np.ones((11,11),np.float32)/121
image3=cv2.medianBlur(src=image2,ksize=11)
plt.figure(figsize=(9,9))
plt.subplot(1,2,1)
plt.imshow(image2)
plt.title('ORIGINAL')
plt.axis('off')
plt.subplot(1,2,2)
plt.imshow(image3)
plt.title('Median Blur')
plt.axis('off')
i) Using Laplacian Kernal
import cv2
import numpy as np
import matplotlib.pyplot as plt
image1=cv2.imread('test_image.jpeg')
image2=cv2.cvtColor(image1,cv2.COLOR_BGR2RGB)
kernel=np.ones((11,11),np.float32)/121
kernal2=np.array([[0,1,0],[1,-4,1],[0,1,0]])
image3=cv2.filter2D(image2,-1,kernal2)
plt.figure(figsize=(9,9))
plt.subplot(1,2,1)
plt.imshow(image2)
plt.title('ORIGINAL')
plt.axis('off')
plt.subplot(1,2,2)
plt.imshow(image3)
plt.title('Sharpening Filters')
plt.axis('off')
ii) Using Laplacian Operator
import cv2
import numpy as np
import matplotlib.pyplot as plt
image1=cv2.imread('test_image.jpeg')
image2=cv2.cvtColor(image1,cv2.COLOR_BGR2RGB)
kernel=np.ones((11,11),np.float32)/121
image3=cv2.Laplacian(image2,cv2.CV_64F)
laplacian_image = np.uint8(np.absolute(image3))
plt.figure(figsize=(9,9))
plt.subplot(1,2,1)
plt.imshow(image2)
plt.title('ORIGINAL')
plt.axis('off')
plt.subplot(1,2,2)
plt.imshow(laplacian_image)
plt.title('Sharpening Filters')
plt.axis('off')
i) Using Averaging Filter
ii) Using Weighted Averaging Filter
iii) Using Gaussian Filter
iv) Using Median Filter
i) Using Laplacian Kernal
ii) Using Laplacian Operator
Thus the filters are designed for smoothing and sharpening the images in the spatial domain.