Repository navigation
Expand file tree
/
Copy pathGradientArt.py
More file actions
118 lines (93 loc) · 3.67 KB
/
Copy pathGradientArt.py
File metadata and controls
118 lines (93 loc) · 3.67 KB
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
import cv2
import numpy as np
from MainWindow import MainWindow
def load_image_grey(image_name):
return cv2.imread(image_name, cv2.IMREAD_GRAYSCALE)
def load_image(image_name):
return cv2.imread(image_name)
def load_image_colour(image_name, colour):
c = []
image = cv2.imread(image_name)
for x in image:
line = []
for y in x:
line.append(y[colour])
c.append(line)
return c
def create_vector_array(gradient_x, gradient_y):
vector_array = []
for x in range(0, len(gradient_x)):
line = []
for y in range(0, len(gradient_x[x])):
entry = [gradient_x[x][y], gradient_y[x][y]]
line.append(entry)
vector_array.append(line)
return vector_array
# reducing the vector array by 2 (call this more times if you want to reduce it more.
def reduce_vector_array(array):
vector_array = []
for x in range(0, len(array), 2):
line = []
for y in range(0, len(array[x]), 2):
total = [0, 0]
total[0] += array[x][y][0]
total[0] += array[x+1][y][0]
total[0] += array[x][y+1][0]
total[0] += array[x+1][y+1][0]
total[1] += array[x][y][1]
total[1] += array[x+1][y][1]
total[1] += array[x][y+1][1]
total[1] += array[x+1][y+1][1]
line.append(total)
vector_array.append(line)
return vector_array
if __name__ == "__main__":
# First we will test it with the grayscale.
image_name = 'frog2.png'
img = load_image(image_name)
img_grey = load_image_grey(image_name)
img_red = load_image_colour(image_name, 0)
img_green = load_image_colour(image_name, 1)
img_blue = load_image_colour(image_name, 2)
rows, cols = img_grey.shape
print("rows: " + str(rows) + " columns: " + str(cols))
image = np.sqrt(img_grey)
image_red = np.sqrt(img_red)
image_green = np.sqrt(img_green)
image_blue = np.sqrt(img_blue)
gx = cv2.Sobel(np.float32(image), cv2.CV_32F, 1, 0, ksize=1)
gy = cv2.Sobel(np.float32(image), cv2.CV_32F, 0, 1, ksize=1)
red_x = cv2.Sobel(np.float32(image_red), cv2.CV_32F, 1, 0, ksize=1)
red_y = cv2.Sobel(np.float32(image_red), cv2.CV_32F, 0, 1, ksize=1)
green_x = cv2.Sobel(np.float32(image_green), cv2.CV_32F, 1, 0, ksize=1)
green_y = cv2.Sobel(np.float32(image_green), cv2.CV_32F, 0, 1, ksize=1)
blue_x = cv2.Sobel(np.float32(image_blue), cv2.CV_32F, 1, 0, ksize=1)
blue_y = cv2.Sobel(np.float32(image_blue), cv2.CV_32F, 0, 1, ksize=1)
red = create_vector_array(red_x, red_y)
green = create_vector_array(green_x, green_y)
blue = create_vector_array(blue_x, blue_y)
red = reduce_vector_array(red)
red = reduce_vector_array(red)
red = reduce_vector_array(red)
# red = reduce_vector_array(red)
green = reduce_vector_array(green)
green = reduce_vector_array(green)
green = reduce_vector_array(green)
# green = reduce_vector_array(green)
blue = reduce_vector_array(blue)
blue = reduce_vector_array(blue)
blue = reduce_vector_array(blue)
# blue = reduce_vector_array(blue)
# cv2.imshow('Original', img)
# cv2.imshow('Sobel vertical', gx)
# cv2.imshow('Sobel horizontal', gy)
# cv2.imshow('Sobel vertical red', red_x)
# cv2.imshow('Sobel horizontal red', red_y)
# cv2.imshow('Sobel vertical green', green_x)
# cv2.imshow('Sobel horizontal green', green_y)
# cv2.imshow('Sobel vertical blue', blue_x)
# cv2.imshow('Sobel horizontal blue', blue_y)
# mag, ang = cv2.cartToPolar(gx, gy)
# cv2.waitKey(0)
window = MainWindow(cols, rows, 1024, 1024, red, green, blue, "Gradient art project")
window.main_loop()