-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimutils.py
108 lines (90 loc) · 3.36 KB
/
imutils.py
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
# Import the necessary packages
import numpy as np
import cv2
import math
import Preprocess as pp
import Main
import os
import DetectChars
import DetectPlates
import PossiblePlate
def translate(image, x, y):
# Define the translation matrix and perform the translation
M = np.float32([[1, 0, x], [0, 1, y]])
shifted = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))
# Return the translated image
return shifted
def rotate(image, angle, center = None, scale = 1.0):
# Grab the dimensions of the image
(h, w) = image.shape[:2]
# If the center is None, initialize it as the center of
# the image
if center is None:
center = (w / 2, h / 2)
# Perform the rotation
M = cv2.getRotationMatrix2D(center, angle, scale)
rotated = cv2.warpAffine(image, M, (w, h))
# Return the rotated image
return rotated
def resize(image, width = None, height = None, inter = cv2.INTER_AREA):
# initialize the dimensions of the image to be resized and
# grab the image size
dim = None
(h, w) = image.shape[:2]
# if both the width and height are None, then return the
# original image
if width is None and height is None:
return image
# check to see if the width is None
if width is None:
# calculate the ratio of the height and construct the
# dimensions
r = height / float(h)
dim = (int(w * r), height)
# otherwise, the height is None
else:
# calculate the ratio of the width and construct the
# dimensions
r = width / float(w)
dim = (width, int(h * r))
# resize the image
resized = cv2.resize(image, dim, interpolation = inter)
# return the resized image
return resized
def transform (image):
CAL_VAL = np.loadtxt("calibrated_value.txt")
imheight = np.size(image, 0)
imwidth = np.size(image, 1)
M = getTransform (imwidth, imheight, CAL_VAL[2], CAL_VAL[3], CAL_VAL[4], CAL_VAL[5], CAL_VAL[6], CAL_VAL[7], CAL_VAL[8])
transformed = cv2.warpPerspective(image, M, (imwidth,imheight),cv2.INTER_CUBIC or cv2.WARP_INVERSE_MAP)
return transformed
def detransform(image):
CAL_VAL = np.loadtxt("calibrated_value.txt")
imheight = np.size(image, 0)
imwidth = np.size(image, 1)
M = getTransform (imwidth, imheight, (0-CAL_VAL[2]), (0-CAL_VAL[3]), (0-CAL_VAL[4]), (0-CAL_VAL[5]), (0-CAL_VAL[6]), (1-CAL_VAL[7]), (1-CAL_VAL[8]))
#M = getTransform (imwidth, imheight, 0.0, 0.0, 0.0, 0, 0, 1.0,1.0)
detransformed = cv2.warpPerspective(image, M, (imwidth,imheight),cv2.INTER_CUBIC or cv2.WARP_INVERSE_MAP)
return detransformed
def getTransform (w, h, rotationx, rotationy, rotationz, panX, panY, stretchX, dist):
alpha = rotationx;
beta = rotationy;
gamma = rotationz;
f = 1.0;
A1 = np.matrix([[1, 0, -w/2], [0, 1, -h/2],[0, 0,0],[0, 0,1]])
#print(A1)
A2 = np.matrix([[f, 0, w/2, 0], [0, f, h/2, 0],[0, 0, 1, 0]])
#print(A2)
Rx = np.matrix([[1, 0, 0, 0],[0, math.cos(alpha), -(math.sin(alpha)), 0],[0, math.sin(alpha), math.cos(alpha), 0],[0, 0, 0, 1]])
#print(Rx)
Ry = np.matrix([[math.cos(beta), 0, math.sin(beta), 0],[0, 1, 0, 0],[-(math.sin(beta)), 0, math.cos(beta), 0],[0, 0, 0, 1]])
#print(Ry)
Rz = np.matrix([[math.cos(gamma), -(math.sin(gamma)), 0, 0],[math.sin(gamma), math.cos(gamma), 0, 0],[0, 0, 1, 0],[0, 0, 0, 1]])
#print(Rz)
R = Rx*Ry*Rz
#print(R)
T = np.matrix([[stretchX, 0, 0, panX],[0, 1, 0, panY],[0, 0, 1, dist],[0, 0, 0, 1]])
#print(T)
M = A2*(T*(R*A1))
#print(M)
return M