# Image Sharpening by High Pass Filter using Python and OpenCV

Image enhancement plays an important role in the world of image processing from photoshopping an image to uploading cool filtered images to your Instagram account, image enhancement is everywhere.

Image sharpening is one of the enhancements which can be done on to an image to enhance the detailing of the image, one of the methods which can be used is High Pass Filter.

Advertisement: Buy OpenCV Computer Vision Book from Amazon High Pass Filter is a filter that examines the region of an image and boosts the intensity of certain pixels based on the difference in the intensity with the surrounding pixels. The basic principle of HPF is that whenever a pixel with a high difference is found in relation with its neighbouring pixels the intensity of those pixels are boosted.

This is method is particularly effective in edge detection, where a common form of HPF called High Boost Filter is used. You can read more about HPF from the following links

Now we have enough idea about HPF, so let’s dive straight into the code.

#### Importing the packages

``Import numpy as np Import cv2 From scipy import ndimage ``

Now let’s write a class called HPF which will take two parameters at the initialisation first will be the kernel and second will be the image in form of a numpy array.

``class HPF(object):   def __init__(self, kernel, image):       self.kernel = np.array(kernel)       self.image = image    def process(self):       return ndimage.convolve(self.image, self.kernel) ``

In the above class, we defined a function called process which will simply return the convolution between the kernel and the image.

In this code we are going to demonstrate HPF using a 3 x 3 kernel and a 5 x 5 kernel, so let’s define them.

``kernel3x3 = [[-1,-1,-1],[-1,8,-1],[-1,-1,-1]]  kernel5x5 = [[-1, -1, -1, -1, -1], [-1, 1, 2, 1, -1], [-1, 2, 4, 2, -1], [-1, 1, 2, 1, -1], [-1, -1, -1, -1, -1]] ``

We now have the ammo(kernels) as well as the Gun(HPF class) now let’s fire the shot(write the driver function) .

``if __name__ == "__main__":   #you can add ur own image path   image = cv2.imread("images/test2.jpg", 0)    kernel3x3 = [[-1,-1,-1],[-1,8,-1],[-1,-1,-1]]   kernel5x5 = [[-1, -1, -1, -1, -1],   [-1, 1, 2, 1, -1],   [-1, 2, 4, 2, -1],   [-1, 1, 2, 1, -1],   [-1, -1, -1, -1, -1]]    hpf1 = HPF(kernel3x3, image)   hpfimage1 = hpf1.process()   hpf2 = HPF(kernel5x5, image)   hpfimage2 = hpf2.process()   cv2.imshow("3x3",hpfimage1)   cv2.imshow("5x5",hpfimage2)   cv2.waitKey()   cv2.destroyAllWindows() ``

#### The complete code

``import numpy as np import cv2 from scipy import ndimage  class HPF(object):   def __init__(self, kernel, image):       self.kernel = np.array(kernel)       self.image = image    def process(self):       return ndimage.convolve(self.image, self.kernel)   if __name__ == "__main__":   image = cv2.imread("images/test2.jpg", 0)   kernel3x3 = [[-1,-1,-1],[-1,8,-1],[-1,-1,-1]]   kernel5x5 = [[-1, -1, -1, -1, -1],   [-1, 1, 2, 1, -1],   [-1, 2, 4, 2, -1],   [-1, 1, 2, 1, -1],   [-1, -1, -1, -1, -1]]    hpf1 = HPF(kernel3x3, image)   hpfimage1 = hpf1.process()   hpf2 = HPF(kernel5x5, image)   hpfimage2 = hpf2.process()   cv2.imshow("3x3",hpfimage1)   cv2.imshow("5x5",hpfimage2)   cv2.waitKey()   cv2.destroyAllWindows() ``

#### Output Preorder and Postorder Traversal of binary tree in Python
02 September 2018

Binary Tree in Python
02 September 2018

Explaining Register variables in C with examples
17 August 2018

Data Autosave System using PHP, MySQL and AJAX
06 July 2018

Понравилась статья? Поделиться с друзьями: