File size: 3,682 Bytes
1b63b70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
from PIL import Image
import cv2
import numpy as np


def pil_to_opencv(image):
    numpy_image = np.array(image)

    opencv_image = cv2.cvtColor(numpy_image, cv2.COLOR_RGB2BGR)

    return opencv_image


def opencv_to_pil(image):
    # Convert OpenCV BGR image to NumPy array
    numpy_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

    # Convert NumPy array to PIL Image
    pil_image = Image.fromarray(numpy_image)

    return pil_image


def generate(image, algorithm_name):
    algorithm_functions = {
        "Sobel Edge Detection": sobel_edge_detection,
        "Canny Edge Detection": canny_edge_detection,
        "Hough Lines": hough_lines,
        "Laplacian Edge Detection": laplacian_edge_detection,
        "Contours Detection": contours_detection,
        "Prewitt Edge Detection": prewitt_edge_detection,
        "Gradient Magnitude": gradient_magnitude,
        "Corner Detection": corner_detection,
    }

    if algorithm_name in algorithm_functions:
        algorithm_function = algorithm_functions[algorithm_name]
        processed_image = algorithm_function(image)
    else:
        processed_image = ()

    return processed_image


def sobel_edge_detection(image):
    gray = pil_to_opencv(image)
    sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=5)
    sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=5)
    magnitude = np.sqrt(sobelx**2 + sobely**2)
    magnitude = np.uint8(magnitude)
    return magnitude


def canny_edge_detection(image):
    gray = pil_to_opencv(image)
    edges = cv2.Canny(gray, 50, 150, apertureSize=3)
    return edges


def hough_lines(image):
    gray = pil_to_opencv(image)
    edges = cv2.Canny(gray, 50, 150)
    lines = cv2.HoughLines(edges, 1, np.pi / 180, threshold=100)
    result = image.copy()
    for line in lines:
        rho, theta = line[0]
        a = np.cos(theta)
        b = np.sin(theta)
        x0 = a * rho
        y0 = b * rho
        x1 = int(x0 + 1000 * (-b))
        y1 = int(y0 + 1000 * (a))
        x2 = int(x0 - 1000 * (-b))
        y2 = int(y0 - 1000 * (a))
        cv2.line(result, (x1, y1), (x2, y2), (0, 0, 255), 2)
    print("passed")

    return result


def laplacian_edge_detection(image):
    gray = pil_to_opencv(image)
    laplacian = cv2.Laplacian(gray, cv2.CV_64F)
    laplacian = np.uint8(np.absolute(laplacian))
    return laplacian


def contours_detection(image):
    gray = pil_to_opencv(image)
    contours, _ = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    result = np.zeros_like(image)

    cv2.drawContours(result, contours, -1, (0, 255, 0), 2)
    print("passed")

    return result


def prewitt_edge_detection(image):
    gray = pil_to_opencv(image)
    prewittx = cv2.filter2D(
        gray, cv2.CV_64F, np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]])
    )
    prewitty = cv2.filter2D(
        gray, cv2.CV_64F, np.array([[-1, -1, -1], [0, 0, 0], [1, 1, 1]])
    )
    magnitude = np.sqrt(prewittx**2 + prewitty**2)
    magnitude = np.uint8(magnitude)
    return magnitude


def gradient_magnitude(image):
    gray = pil_to_opencv(image)
    sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=5)
    sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=5)
    magnitude = np.sqrt(sobelx**2 + sobely**2)
    magnitude = np.uint8(magnitude)
    print("passed")

    return magnitude


def corner_detection(image):
    gray = pil_to_opencv(image)
    corners = cv2.goodFeaturesToTrack(
        gray, maxCorners=100, qualityLevel=0.01, minDistance=10
    )
    result = np.zeros_like(image)
    corners = np.int0(corners)
    for i in corners:
        x, y = i.ravel()
        cv2.circle(result, (x, y), 3, 255, -1)
    print("passed")
    return result