|
| 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