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
| import numpy as np
def scale(img, ratio_h, ratio_w): try: target_img = np.zeros((int(img.shape[1] * ratio_h), int(img.shape[0] * ratio_w), img.shape[2]), dtype=img.dtype) except: target_img = np.zeros((int(img.shape[1] * ratio_h), int(img.shape[0] * ratio_w)), dtype=img.dtype)
convert_matrix = [[ratio_h, 0, 0], [0, ratio_w, 0], [0, 0, 1]] convert_matrix_inv = np.linalg.inv(convert_matrix)
for x in range(int(img.shape[1] * ratio_h)): for y in range(int(img.shape[0] * ratio_w)): target_pos = [x, y, 1] origin_pos = np.dot(convert_matrix_inv, target_pos)
if (img.shape[0] >= origin_pos[0] + 1 and img.shape[1] >= origin_pos[1] + 1) and (origin_pos >= 0).all(): x_low = np.floor(origin_pos[0]) x_up = np.ceil(origin_pos[0]) y_low = np.floor(origin_pos[1]) y_up = np.ceil(origin_pos[1])
s = origin_pos[0] - x_low t = origin_pos[1] - y_low
p1 = img[int(x_low), int(y_low)] p2 = img[int(x_up), int(y_low)] p3 = img[int(x_low), int(y_up)] p4 = img[int(x_up), int(y_up)]
value = (1 - s) * (1 - t) * p1 + (1 - s) * t * p3 + (1 - t) * s * p2 + s * t * p4 target_img[x, y] = value return target_img
def rotate(img, angle): beta = angle / 180 * np.pi new_width = int(abs(img.shape[1] * np.cos(beta)) + abs(img.shape[0] * np.sin(beta))) new_height = int(abs(img.shape[1] * np.sin(beta)) + abs(img.shape[0] * np.cos(beta)))
try: target_img = np.zeros((new_height, new_width, img.shape[2]), dtype=img.dtype) except: target_img = np.zeros((new_height, new_width), dtype=img.dtype)
rotate_matrix = [[np.cos(beta), np.sin(beta), 0], [-np.sin(beta), np.cos(beta), 0], [0, 0, 1]] translation_matrix = [[1, 0, -img.shape[1] / 2], [0, 1, -img.shape[0] / 2], [0, 0, 1]] convert_matrix = np.dot(rotate_matrix, translation_matrix) convert_matrix_inv = np.linalg.inv(convert_matrix)
for x in range(new_width): for y in range(new_height): target_pos = [int(x - new_width / 2), int(y - new_height / 2), 1] origin_pos = np.dot(convert_matrix_inv, target_pos)
if (img.shape[0] > origin_pos[0] + 1 and img.shape[1] > origin_pos[1] + 1) and (origin_pos >= 0).all(): x_low = np.floor(origin_pos[0]) x_up = np.ceil(origin_pos[0]) y_low = np.floor(origin_pos[1]) y_up = np.ceil(origin_pos[1])
s = origin_pos[0] - x_low t = origin_pos[1] - y_low
p1 = img[int(x_low), int(y_low)] p2 = img[int(x_up), int(y_low)] p3 = img[int(x_low), int(y_up)] p4 = img[int(x_up), int(y_up)]
value = (1 - s) * (1 - t) * p1 + (1 - s) * t * p3 + (1 - t) * s * p2 + s * t * p4 target_img[x, y] = value return target_img
def part3_1(): img0 = cv2.imread('source/work2/lena.jpg') img = scale(img0, ratio_h=1.6, ratio_w=2.3) print(img0.shape, img.shape) cv2.imshow("img", img) cv2.waitKey()
def part1(): img = np.array([[1, 4, 7], [2, 5, 8], [3, 6, 9]]) print(scale(img, 2.3, 1.6))
def part2(): img = np.array([[59, 60, 58, 57], [61, 59, 59, 57], [62, 59, 60, 58], [59, 61, 60, 56]]) print(rotate(img, 45))
def part3_1(): img0 = cv2.imread('source/work2/lena.jpg') img = scale(img0, ratio_h=1.6, ratio_w=2.3) print(img0.shape, img.shape) cv2.imshow("img", img) cv2.waitKey()
def part3_2(): img0 = cv2.imread('source/work2/lena.jpg') img = rotate(img0, -45) print(img0.shape, img.shape) cv2.imshow("img", img) cv2.waitKey()
if __name__ == '__main__': part1() part2() part3_1() part3_2()
|