-
Notifications
You must be signed in to change notification settings - Fork 0
/
LiveRunningOverhead.py
157 lines (122 loc) · 5.16 KB
/
LiveRunningOverhead.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
'''
In this Program, we are going to obtain transformation from pre-saved matrixes.
There are two camera input used in this code.
To calculate the transformation we need the points which is mapped to the points in the overhead image
Minimum four points are required to calculate the transformation matrix (Homography).
'''
import cv2
import sys
import numpy as np
from PIL import Image
def inputImage(): # for taking input
first_image = 'input/Camera1/Lpicture070.jpg'
second_image = 'input/Camera2/Rpicture069.jpg'
'''
first_image = 'C:\Users\hp pc\Documents\pythonFiles\open\Amol\img1.jpg'
second_image = 'C:\Users\hp pc\Documents\pythonFiles\open\Amol\img2.jpg'
'''
return first_image, second_image
def endProg():
cv2.waitKey(0)
cv2.destroyAllWindows()
sys.exit(0)
def d(string): # debugging
print string
def print_coordinate(event,x,y,flags,param):
font = cv2.FONT_HERSHEY_SIMPLEX
points = np.float32()
if event == cv2.EVENT_FLAG_LBUTTON:
cv2.putText(dst,str(x) + ' ' + str(y),(x-20,y-20),font,0.3,(0,0,255),1,cv2.LINE_AA)
def store_point_coordinate(event,x,y,flags,param): # stores values in point
font = cv2.FONT_HERSHEY_SIMPLEX
if event == cv2.EVENT_FLAG_LBUTTON and len(pts1) < 4:
cv2.circle(img,(x,y), 2, (255,255,255), -1)
cv2.putText(img,str(x) + ' ' + str(y),(x-20,y-20),font,0.3,(255,255,255),1,cv2.LINE_AA)
pts1.append([x,y])
def store_point_coordinate2(event,x,y,flags,param):
font = cv2.FONT_HERSHEY_SIMPLEX
if event == cv2.EVENT_FLAG_LBUTTON and len(pts2) < 4:
cv2.circle(img2,(x,y), 2, (255,255,255), -1)
cv2.putText(img2,str(x) + ' ' + str(y),(x-20,y-20),font,0.3,(255,255,255),1,cv2.LINE_AA)
pts2.append([x,y])
def resize(img, factor): # resizing the image
rows,cols,ch = img.shape
trans = np.float32([[factor*1,0,0],[0,factor*1,0]])
foreground = cv2.warpAffine(img,trans,(factor*cols,factor*rows))
return foreground
def resizeOnCenter(img, factor): # resizing image keeping it in center
rows,cols,ch = img.shape
tx = (factor-1)*rows*0.5
ty = (factor-1)*cols*0.5
trans = np.float32([[1,0,ty],[0,1,tx]])
foreground = cv2.warpAffine(img,trans,(factor*cols,factor*rows))
return foreground
def modifyValue(vec,img,factor,flag): # modify coordinate wrt resized image
rows,cols,ch = img.shape
if flag == 1:
for x in range(0, 4):
vec[x][0] *= factor
vec[x][1] *= factor
return vec
else:
for x in range(0, 4):
vec[x][0] += (factor-1)*cols*0.5
vec[x][1] += (factor-1)*rows*0.5
return vec
def plotPointOnImage(img,vec,size):
font = cv2.FONT_HERSHEY_SIMPLEX
for x in range(0, size):
cx = int(vec[x][0])
cy = int(vec[x][1])
cv2.circle(img,(cx,cy), 2, (255,255,255), -1)
cv2.putText(img,str(cx) + ' ' + str(cy),(cx-20,cy-20),font,0.3,(255,255,255),1,cv2.LINE_AA)
return img
def cropImage(img): # cropping image
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray,1,255,cv2.THRESH_BINARY)
_,contours,hierarchy = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnt = contours[0]
x,y,w,h = cv2.boundingRect(cnt)
crop = img[y:y+h,x:x+w]
return crop
def main():
output_img = "./output/output33.jpg" # storing output images
first_image, second_image = inputImage() # get input
img = cv2.imread(first_image)
print img.shape
img = cv2.resize(img, (0,0), fx = 0.5, fy = 0.5)
img2 = cv2.imread(second_image)
print img2.shape
img2 = cv2.resize(img2, (0, 0), fx=0.5, fy=0.5)
rows,cols,ch = img.shape
print img.shape
print img2.shape
img = resizeOnCenter(img,4) # resize image to 4 times
rows,cols,ch = img.shape
M = np.load('./output/calibData/perspective_Second_First.npy') # using already saved transformation matrix
dst = cv2.warpPerspective(img2,M,(cols,rows))
background = dst
foreground = img
alpha = 0.5
r = rows
c = cols
size = r,c, 3
dist = np.zeros(size, dtype=np.uint8)
# d('yy')
mm = np.float32([[1,0,0],[0,1,0]])
foreground = cv2.warpAffine(foreground,mm,(c,r)) # affine transformation matrix for scaling, translating, etc
beta = 1.0 - alpha # giving weights to both images
cv2.addWeighted(background,alpha,foreground, beta, 0.0, dist) # Stitching image
#cv2.imshow("Final",dist) # in case of debugging with output
cv2.imwrite("./output/FinalImage4.jpg",dist)
h = np.load('./output/calibData/perspective_overhead.npy') # using final overhead transformation matrix
out = np.zeros((2000,2000,3),np.uint8)
out = cv2.warpPerspective(dist,h,(2*cols,2*rows))
cv2.namedWindow("FinalImage", cv2.WINDOW_AUTOSIZE)
#out = resize(out,0.5)
out = cropImage(out) # cropping and resizing image
cv2.imshow("FinalImage",out)
cv2.imwrite(output_img,out)
cv2.waitKey(0)
cv2.destroyAllWindows()
main()