Slmaan

Task 3: High Boost filtering

Apr 18th, 2023 (edited)
815
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.28 KB | None | 0 0
  1. import numpy as np
  2. import cv2 as cv
  3. from matplotlib import pyplot as plt
  4.  
  5. def apply_high_boost_filter(image, mask, A):
  6.     kernel = np.array(mask, dtype=np.float32)
  7.     kernel = kernel + A
  8.     filtered_image = cv.filter2D(image, -1, kernel)
  9.     return filtered_image
  10.  
  11. def display_images(images, titles):
  12.     plt.figure(figsize=(12, 6))
  13.     for i, (image, title) in enumerate(zip(images, titles), start=1):
  14.         plt.subplot(1, len(images), i)
  15.         plt.imshow(cv.cvtColor(image, cv.COLOR_BGR2RGB))
  16.         plt.title(title)
  17.         plt.xticks([])
  18.         plt.yticks([])
  19.     plt.show()
  20.  
  21. def main():
  22.     image = cv.imread("Electron_Microscope.jpg", cv.IMREAD_COLOR)
  23.  
  24.     mask1 = [[0, -1, 0], [-1, 4, -1], [0, -1, 0]]
  25.     mask2 = [[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]]
  26.  
  27.     A_values = [0, 1]
  28.  
  29.     for A in A_values:
  30.         filtered_image1 = apply_high_boost_filter(image, mask1, A)
  31.         filtered_image2 = apply_high_boost_filter(image, mask2, A)
  32.  
  33.         display_images(
  34.             [image, filtered_image1, filtered_image2],
  35.             [
  36.                 f"Original Image",
  37.                 f"High Boost Filtered Image (A={A}, Mask1)",
  38.                 f"High Boost Filtered Image (A={A}, Mask2)",
  39.             ],
  40.         )
  41.  
  42. if __name__ == "__main__":
  43.     main()
Advertisement
Add Comment
Please, Sign In to add comment