Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. High-pass filter:
- ```
- def sharpen_image(img):
- # applying high pass filter
- kernel = [[-1,-1,-1], [-1,9,-1], [-1,-1,-1]]
- sharp_img = img.filter(ImageFilter.Kernel((3,3), kernel))
- return sharp_img
- ```
- 2. Laplacian filter:
- ```
- def sharpen_image(img):
- # applying Laplacian filter
- kernel = [[0, -1, 0], [-1, 4, -1], [0, -1, 0]]
- sharp_img = img.filter(ImageFilter.Kernel((3,3), kernel))
- return sharp_img
- ```
- 3. Sobel operator:
- ```
- def sharpen_image(img):
- # applying sobel operator
- kernel_x = [[-1,0,1], [-2,0,2], [-1,0,1]]
- kernel_y = [[-1,-2,-1], [0,0,0], [1,2,1]]
- img_x = img.filter(ImageFilter.Kernel((3,3), kernel_x))
- img_y = img.filter(ImageFilter.Kernel((3,3), kernel_y))
- sharp_img = ImageChops.add(img_x, img_y)
- return sharp_img
- ```
- 4. Unsharp mask filter:
- ```
- def sharpen_image(img):
- # applying unsharp mask filter
- blurred = img.filter(ImageFilter.BLUR)
- sharp = ImageEnhance.Sharpness(img).enhance(2.0)
- sharp_img = ImageChops.subtract(sharp, blurred)
- return sharp_img
- ```
- 5. Roberts operator:
- ```
- def sharpen_image(img):
- # applying roberts operator
- kernel_x = [[1,0], [0,-1]]
- kernel_y = [[0,1], [-1,0]]
- img_x = img.filter(ImageFilter.Kernel((2,2), kernel_x))
- img_y = img.filter(ImageFilter.Kernel((2,2), kernel_y))
- sharp_img = ImageChops.add(img_x, img_y)
- return sharp_img
- ```
- 6. Prewitt operator:
- ```
- def sharpen_image(img):
- # applying prewitt operator
- kernel_x = [[-1,-1,-1], [0,0,0], [1,1,1]]
- kernel_y = [[-1,0,1], [-1,0,1], [-1,0,1]]
- img_x = img.filter(ImageFilter.Kernel((3,3), kernel_x))
- img_y = img.filter(ImageFilter.Kernel((3,3), kernel_y))
- sharp_img = ImageChops.add(img_x, img_y)
- return sharp_img
- ```
- 7. Scharr operator:
- ```
- def sharpen_image(img):
- # applying scharr operator
- kernel_x = [[3,0,-3], [10,0,-10], [3,0,-3]]
- kernel_y = [[3,10,3], [0,0,0], [-3,-10,-3]]
- img_x = img.filter(ImageFilter.Kernel((3,3), kernel_x))
- img_y = img.filter(ImageFilter.Kernel((3,3), kernel_y))
- sharp_img = ImageChops.add(img_x, img_y)
- return sharp_img
- ```
- 8. Canny edge detection filter:
- ```
- def sharpen_image(img):
- # applying canny edge detection filter
- edges = img.filter(ImageFilter.FIND_EDGES)
- sharp_img = ImageChops.subtract(img, edges)
- return sharp_img
- ```
- 9. Median filter:
- ```
- def sharpen_image(img):
- # applying median filter
- sharp_img = img.filter(ImageFilter.MedianFilter(size=3))
- return sharp_img
- ```
- 10. Gaussian weighted average:
- ```
- def sharpen_image(img):
- # applying gaussian weighted average
- kernel = [[0.04, 0.12, 0.04], [0.12, 0.36, 0.12], [0.04, 0.12, 0.04]]
- sharp_img = img.filter(ImageFilter.Kernel((3,3), kernel))
- return sharp_img
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement