0% found this document useful (0 votes)
18 views7 pages

Question

The document discusses applying different filters to an image in Python including Canny edge detection, Sobel filtering, Laplacian filtering, average filtering, and weighted average filtering. It provides code definitions for each filter type as well as a main program to display a menu allowing a user to select and apply a filter to an input image and display the output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views7 pages

Question

The document discusses applying different filters to an image in Python including Canny edge detection, Sobel filtering, Laplacian filtering, average filtering, and weighted average filtering. It provides code definitions for each filter type as well as a main program to display a menu allowing a user to select and apply a filter to an input image and display the output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

PATTERN RECOGINTION

Asssignment no 02

SINAN AHMED IHSAN


SP21-BCS-019
QUESTION:

DIFFERENT FILTERS ON AN IMAGE IN PYTHON

• CANNY EDGE DETECTION

• SOBAL FILTER

• LAPLACIAN FILTER

• WEIGHTED AVERAGE

• AVERAGE FILTER

SOLUTION

CODE

import cv2
import numpy as np

def edge_detection_canny(image):
return cv2.Canny(image, 50, 150)

def edge_detection_sobel(image):
blurred = cv2.GaussianBlur(image, (5, 5), 0)
sobel_x = cv2.Sobel(blurred, cv2.CV_64F, 1, 0, ksize=3)
sobel_y = cv2.Sobel(blurred, cv2.CV_64F, 0, 1, ksize=3)
magnitude = np.sqrt(sobel_x**2 + sobel_y**2)
magnitude = np.uint8(magnitude)
return magnitude

def edge_detection_laplacian(image):
blurred = cv2.GaussianBlur(image, (5, 5), 0)
laplacian = cv2.Laplacian(blurred, cv2.CV_64F)
laplacian = np.uint8(np.absolute(laplacian))
return laplacian

def edge_detection_average(image):
blurred = cv2.blur(image, (5, 5))
edges = cv2.subtract(image, blurred)
return edges

def edge_detection_weighted_average(image):
blurred = cv2.GaussianBlur(image, (5, 5), 0)
edges = cv2.subtract(image, blurred)
return edges

def geometric_mean_filter(image):
image = image.astype(np.float32)
blurred = cv2.blur(image, (3, 3))
output_image = np.exp(np.mean(np.log(image + 1))) * np.ones_like(image)
return (output_image - 1).astype(np.uint8)

def display_menu():
print("Menu:")
print("1. Canny Edge Detection")
print("2. Sobel Edge Detection")
print("3. Laplacian Edge Detection")
print("4. Average Filter")
print("5. Weighted Average (Gaussian) Filter")
print("7. Exit")

def apply_filter(image, option):


if option == 1:
return edge_detection_canny(image)
elif option == 2:
return edge_detection_sobel(image)
elif option == 3:
return edge_detection_laplacian(image)
elif option == 4:
return edge_detection_average(image)
elif option == 5:
return edge_detection_weighted_average(image)
elif option == 6:
return geometric_mean_filter(image)
elif option == 7:
return None
else:
print("Invalid option!")
return None

# Path to the input image


image_path = "download.png"
input_image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)

while True:
display_menu()
option = int(input("Enter your choice (1-7): "))
if option == 7:
break
filtered_image = apply_filter(input_image, option)
if filtered_image is not None:
cv2.imshow("Filtered Image", filtered_image)
cv2.waitKey(0)

cv2.destroyAllWindows()
OUTPUT:

CANNY EDGE DETECTION

SOBEL EDGE DETECTION


LAPLACIAN FILTER

AVERAGE FILTER
WEIGHTED AVERAGE FILTER

You might also like