How to do it...

  1. Import the Computer Vision package - cv2:
import cv2 
# Import Numerical Python package - numpy as np 
import numpy as np 
  1. Read the image using the built-in imread function:
image = cv2.imread('image_6.jpg') 
  1. Display the original image using the built-in imshow function:
cv2.imshow("Original", image) 
  1. Wait until any key is pressed:
cv2.waitKey(0) 
  1. Execute the pixel level action with the blurring operation:
# Blurring images: Averaging, cv2.blur built-in function 
# Averaging: Convolving image with normalized box filter 
# Convolution: Mathematical operation on 2 functions which produces third function. 
# Normalized box filter having size 3 x 3 would be: 
# (1/9)  [[1, 1, 1], 
#         [1, 1, 1], 
#         [1, 1, 1]] 
blur = cv2.blur(image,(9,9)) # (9 x 9) filter is used  
  1. Display the blurred image:
cv2.imshow('Blurred', blur) 
  1. Wait until any key is pressed:
cv2.waitKey(0)
  1. Execute the pixel level action with the sharpening operation:
# Sharpening images: Emphasizes edges in an image 
kernel = np.array([[-1,-1,-1],  
                   [-1,9,-1],  
                   [-1,-1,-1]]) 
# If we don't normalize to 1, image would be brighter or darker respectively     
# cv2.filter2D is the built-in function used for sharpening images 
# cv2.filter2D(image, ddepth, kernel) 
# ddepth = -1, sharpened images will have same depth as original image 
sharpened = cv2.filter2D(image, -1, kernel) 
  1. Display the sharpened image:
cv2.imshow('Sharpened', sharpened) 
  1. Wait until any key is pressed:
cv2.waitKey(0) 
  1. Terminate the program execution:
# Close all windows 
cv2.destroyAllWindows() 
  1. The command used to execute the Blurring_Sharpening.py Python program file is shown here:
  1. The input image used to execute the Blurring_Sharpening.py file is shown here:
  1. The blurred image obtained after executing the Blurring_Sharpening.py file is shown here:
  1. The sharpened image obtained after executing the Blurring_Sharpening.py file is shown here:
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.219.217