Skip to content

Commit a3d8197

Browse files
committed
Added opencv demos
1 parent 9590a94 commit a3d8197

8 files changed

Lines changed: 1691 additions & 3 deletions

opencv/data/art.png

12.5 KB
Loading

opencv/gen_sine_table.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#! /usr/bin/env python3
2+
3+
# Copyright (c) 2017 Dave McCoy ([email protected])
4+
#
5+
# NAME is free software; you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation; either version 3 of the License, or
8+
# any later version.
9+
#
10+
# NAME is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with NAME; If not, see <http://www.gnu.org/licenses/>.
17+
18+
19+
import sys
20+
import os
21+
import argparse
22+
import numpy as np
23+
24+
#sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)))
25+
26+
NAME = os.path.basename(os.path.realpath(__file__))
27+
28+
DESCRIPTION = "\n" \
29+
"\n" \
30+
"usage: %s [options]\n" % NAME
31+
32+
EPILOG = "\n" \
33+
"\n" \
34+
"Examples:\n" \
35+
"\tSomething\n" \
36+
"\n"
37+
38+
def main(argv):
39+
#Parse out the commandline arguments
40+
parser = argparse.ArgumentParser(
41+
formatter_class=argparse.RawDescriptionHelpFormatter,
42+
description=DESCRIPTION,
43+
epilog=EPILOG
44+
)
45+
46+
parser.add_argument("-t", "--test",
47+
nargs=1,
48+
default=["something"])
49+
50+
parser.add_argument("-d", "--debug",
51+
action="store_true",
52+
help="Enable Debug Messages")
53+
54+
args = parser.parse_args()
55+
print ("Running Script: %s" % NAME)
56+
57+
58+
if args.debug:
59+
print ("test: %s" % str(args.test[0]))
60+
61+
sine_table = []
62+
for i in range(0, 180):
63+
j = i / 2
64+
#print ("%f" % (j))
65+
k = np.deg2rad(j)
66+
sine_table.append(np.sin(k))
67+
68+
with open("sine_table_float.txt", 'w') as f:
69+
for d in sine_table:
70+
f.write("%f\n" % d)
71+
72+
73+
if __name__ == "__main__":
74+
main(sys.argv)
75+
76+

opencv/play_video.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#! /usr/bin/env python
1+
#! /usr/bin/env python3
22

33
# Copyright (c) 2017 Dave McCoy ([email protected])
44
#
@@ -70,12 +70,12 @@ def main(argv):
7070
help="Enable Debug Messages")
7171

7272
args = parser.parse_args()
73-
print "Running Script: %s" % NAME
73+
print ("Running Script: %s" % NAME)
7474

7575
path = args.path[0]
7676

7777
if args.debug:
78-
print "path: %s" % path
78+
print ("path: %s" % path)
7979

8080
play_video(path)
8181

opencv/rotate_example.py

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
#! /usr/bin/env python3
2+
3+
# Copyright (c) 2017 Dave McCoy ([email protected])
4+
#
5+
# NAME is free software; you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation; either version 3 of the License, or
8+
# any later version.
9+
#
10+
# NAME is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with NAME; If not, see <http://www.gnu.org/licenses/>.
17+
18+
19+
import sys
20+
import os
21+
import argparse
22+
import cv2
23+
import numpy as np
24+
25+
#sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)))
26+
27+
NAME = os.path.basename(os.path.realpath(__file__))
28+
IMAGE_PATH = "data/art.png"
29+
30+
DESCRIPTION = "\n" \
31+
"\n" \
32+
"usage: %s [options]\n" % NAME
33+
34+
EPILOG = "\n" \
35+
"\n" \
36+
"Examples:\n" \
37+
"\tSomething\n" \
38+
"\n"
39+
40+
DEFAULT_ROTATION = 275.0
41+
DEFAULT_SCALE = 1.0
42+
43+
def rotate_sub_image(image, sub_start_x, sub_start_y, sub_width, sub_height, angle, scale, debug=False):
44+
sub_center = (sub_width / 2, sub_height / 2)
45+
rot_mat = gen_rot_mat(sub_center, angle, scale)
46+
# generate map
47+
image_map = np.zeros((2, sub_height, sub_width))
48+
for y in range(sub_height):
49+
for x in range(sub_width):
50+
image_map[0][y][x], image_map[1][y][x] = calc_movement(image, start_pos_x + x, start_pos_y + y)
51+
52+
print ("Image Map: %s" % str(image_map))
53+
54+
def calc_movement(m, start_pos_x, start_pos_y):
55+
#print ("Start Position: %d, %d" % (start_pos_x, start_pos_y))
56+
print ("Start Position: %d, %d" % (start_pos_x, start_pos_y))
57+
new_pos_x = (start_pos_x * m[0][0] + start_pos_y * m[0][1]) + m[0][2]
58+
new_pos_y = (start_pos_x * m[1][0] + start_pos_y * m[1][1]) + m[1][2]
59+
new_pos_x -= start_pos_x
60+
new_pos_y -= start_pos_y
61+
print ("%f, %f -> %f, %f" % (start_pos_x, start_pos_y, new_pos_x, new_pos_y))
62+
return (new_pos_x, new_pos_y)
63+
64+
def gen_rot_mat(center, angle, scale):
65+
alpha = scale * np.cos(angle * np.pi / 180)
66+
beta = scale * np.sin(angle * np.pi / 180)
67+
cx = (1 - alpha) * center[0] - beta * center[1]
68+
cy = beta * center[0] + (1 - alpha) * center[1]
69+
rot_mat = [ [ alpha, beta, cx],
70+
[-beta, alpha, cy]]
71+
return rot_mat
72+
73+
74+
75+
def rotate_image(image, angle, scale = 1.0, debug=False):
76+
image_center = tuple(np.array(image.shape[1::-1]) / 2)
77+
#print ("Image Center: %s" % str(image_center))
78+
rot_mat = cv2.getRotationMatrix2D(image_center, angle, scale)
79+
print ("CV Rotation Matrix: \n%s\n" % str(rot_mat))
80+
rm = gen_rot_mat(image_center, angle, scale)
81+
print ("MY Rotation Matrix: \n%s\n" % str(rm))
82+
83+
print ("Image Center: %d x %d" % (image_center[0], image_center[1]))
84+
print ("Rotation \n%s" % (str(rot_mat)))
85+
print ("Image Shape: %d x %d" % (image.shape[1], image.shape[0]))
86+
#start_pos_x = -1 * image.shape[1] // 2
87+
#start_pos_y = -1 * image.shape[0] // 2
88+
start_pos_x = 0
89+
start_pos_y = 0
90+
new_pos_x, new_pos_y = calc_movement(rot_mat, start_pos_x, start_pos_y)
91+
92+
93+
start_pos_x = 0
94+
start_pos_y = 480
95+
calc_movement(rot_mat, start_pos_x, start_pos_y)
96+
image = cv2.warpAffine(image, rot_mat, image.shape[1::-1], flags=cv2.INTER_LINEAR)
97+
98+
# Vertical Middle of the image in red
99+
height, width, channels = image.shape
100+
101+
image = cv2.line(image, (width // 2, 0), (width // 2 , height), (255, 0, 0), 1, 1)
102+
image = cv2.line(image, (0, height // 2), (width, height // 2), (255, 0, 0), 1, 1)
103+
image = cv2.circle(image, (width // 2, height // 2), radius=0, color=(0, 0, 255), thickness = 4)
104+
offset = 40
105+
image = cv2.putText(image,
106+
"%d, %d" % (width // 2, height // 2),
107+
((width // 2) + offset, (height // 2) + offset),
108+
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255),
109+
2,
110+
cv2.LINE_AA)
111+
112+
image = cv2.line( image,
113+
(int(new_pos_x), 0), (int(new_pos_x), height),
114+
(0, 0, 255),
115+
1,
116+
1)
117+
118+
image = cv2.line( image,
119+
(0, int(new_pos_y)), (width, int(new_pos_y)),
120+
(0, 0, 255),
121+
1,
122+
1)
123+
124+
return image
125+
126+
def add_grid_to_image(image, grid_x_size, grid_y_size):
127+
height, width, channels = image.shape
128+
for x in range (0, width - 1, grid_x_size):
129+
image = cv2.line(image, (x, 0), (x, height), (0, 255, 0), 1, 1)
130+
131+
for y in range (0, height - 1, grid_y_size):
132+
image = cv2.line(image, (0, y), (width, y), (0, 255, 0), 1, 1)
133+
134+
return image
135+
136+
def main(argv):
137+
#Parse out the commandline arguments
138+
parser = argparse.ArgumentParser(
139+
formatter_class=argparse.RawDescriptionHelpFormatter,
140+
description=DESCRIPTION,
141+
epilog=EPILOG
142+
)
143+
144+
parser.add_argument("-r", "--rotation",
145+
nargs=1,
146+
default=["%s" % DEFAULT_ROTATION])
147+
148+
parser.add_argument("-s", "--scale",
149+
nargs=1,
150+
default=["%s" % DEFAULT_SCALE])
151+
152+
153+
parser.add_argument("-d", "--debug",
154+
action="store_true",
155+
help="Enable Debug Messages")
156+
157+
args = parser.parse_args()
158+
print ("Running Script: %s" % NAME)
159+
160+
rotation = float(args.rotation[0])
161+
scale = float(args.scale[0])
162+
print ("Rotation: %f, Scale: %f" % (rotation, scale))
163+
164+
165+
if args.debug:
166+
print ("test: %s" % str(args.test[0]))
167+
168+
src = cv2.imread(IMAGE_PATH)
169+
window_name = "Image"
170+
#image = cv2.rotate(src, cv2.ROTATE_90_CLOCKWISE)
171+
image = rotate_image(src, rotation, scale, debug = args.debug)
172+
#image = add_grid_to_image(image, 20, 20)
173+
174+
cv2.imshow(window_name, image)
175+
cv2.waitKey(0)
176+
177+
if __name__ == "__main__":
178+
main(sys.argv)
179+
180+

0 commit comments

Comments
 (0)