-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconv_edge.py
55 lines (40 loc) · 1.7 KB
/
conv_edge.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import cv2
import numpy as np
def load_image(image_path):
"""Load the image"""
image = cv2.imread(image_path)
return image
def extract_edges(image):
"""Extract first and second order edges using convolution"""
# Convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Apply first order edge kernel
first_order_kernel = np.array([[-1, 0, 1],
[-1, 0, 1],
[-1, 0, 1]])
first_order_edges = cv2.filter2D(gray, -1, first_order_kernel)
# Apply second order edge kernel
second_order_kernel = np.array([[1, 2, 1],
[0, 0, 0],
[-1, -2, -1]])
second_order_edges = cv2.filter2D(gray, -1, second_order_kernel)
# Threshold the edges
_, first_order_edges_thresh = cv2.threshold(first_order_edges, 50, 255, cv2.THRESH_BINARY)
_, second_order_edges_thresh = cv2.threshold(second_order_edges, 50, 255, cv2.THRESH_BINARY)
# Overlap the binary edges on the original image
overlapped_image = image.copy()
overlapped_image[first_order_edges_thresh == 255] = [0, 0, 255] # Red for first order edges
overlapped_image[second_order_edges_thresh == 255] = [0, 255, 0] # Green for second order edges
return overlapped_image
def main():
# Image
image_path = 'sample_img.jpg'
image = load_image(image_path)
# Extract first and second order edges
overlapped_edges = extract_edges(image)
#Display the result
cv2.imshow('Edges', overlapped_edges)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == "__main__":
main()