-
Notifications
You must be signed in to change notification settings - Fork 0
/
dewarp_build_maps.py
141 lines (127 loc) · 4.02 KB
/
dewarp_build_maps.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
from SimpleCV import Camera, VideoStream, Color, Display, Image, VirtualCamera
import cv2
import numpy as np
import time
import re
# deprecated, checks if point in the sphere is in our output
def isInROI(x,y,R1,R2,Cx,Cy):
isInOuter = False
isInInner = False
xv = x-Cx
yv = y-Cy
rt = (xv*xv)+(yv*yv)
if( rt < R2*R2 ):
isInOuter = True
if( rt < R1*R1 ):
isInInner = True
return isInOuter and not isInInner
# build the mapping
def buildMap(Ws,Hs,Wd,Hd,R1,R2,Cx,Cy):
map_x = np.zeros((Hd,Wd),np.float32)
map_y = np.zeros((Hd,Wd),np.float32)
rMap = np.linspace(R1, R1 + (R2 - R1), Hd)
thetaMap = np.linspace(0, 0 + float(Wd) * 2.0 * np.pi, Wd)
sinMap = np.sin(thetaMap)
cosMap = np.cos(thetaMap)
# sinMap = Cx+rMap*np.sin(thetaMap)
# cosMap = Cy+rMap*np.cos(thetaMap)
# for y in range(0,int(Hd-1)):
# for x in range(0,int(Wd-1)):
# map_x.itemset((y,x),int(sinMap))
# map_y.itemset((y,x),int(cosMap))
for y in xrange(0, int(Hd-1)):
map_x[y] = Cx + rMap[y] * sinMap
map_y[y] = Cy + rMap[y] * cosMap
#(map1, map2) = cv2.convertMaps(map_x, map_y, cv2.CV_16SC2)
return map_x, map_y
#return map1, map2
# do the unwarping
def unwarp(img,xmap,ymap):
output = cv2.remap(img.getNumpyCv2(),xmap,ymap,cv2.INTER_LINEAR)
result = Image(output, cv2image=True)
# return result
return result
#disp = Display((800,600))
disp = Display((1296,972))
vals = []
last = (0,0)
# Load the video from the rpi
#vc = VirtualCamera("/var/www/html/vid_files/video.h264","video")
# Sometimes there is crud at the begining, buffer it out
#for i in range(0,10):
# img = vc.getImage()
img = Image("pi3.jpg")
img.save(disp)
# Show the user a frame let them left click the center
# of the "donut" and the right inner and outer edge
# in that order. Press esc to exit the display
while not disp.isDone():
test = disp.leftButtonDownPosition()
if( test != last and test is not None):
last = test
vals.append(test)
# 0 = xc yc
# 1 = r1
# 2 = r2
# center of the "donut"
Cx = vals[0][0]
Cy = vals[0][1]
#print str(Cx) + " " + str(Cy)
# Inner donut radius
R1x = vals[1][0]
R1y = vals[1][1]
R1 = R1x-Cx
#print str(R1)
# outer donut radius
R2x = vals[2][0]
R2y = vals[2][1]
R2 = R2x-Cx
#print str(R2)
# our input and output image siZes
Wd = round(float(max(R1, R2)) * 2.0 * np.pi)
#Wd = 2.0*((R2+R1)/2)*np.pi
#Hd = (2.0*((R2+R1)/2)*np.pi) * (90/360)
Hd = (R2-R1)
Ws = img.width
Hs = img.height
# build the pixel map, this could be sped up
print "BUILDING MAP!"
#print "HD value" + str(Hd)
#print "WD value" + str(Wd)
xmap,ymap = buildMap(Ws,Hs,Wd,Hd,R1,R2,Cx,Cy)
#np.savetxt('xmap.txt', xmap.flatten(), header=str(xmap.shape))
#np.savetxt('ymap.txt', ymap.flatten(), header=str(ymap.shape))
print "MAP DONE!"
# do an unwarping and show it to us
result = unwarp(img,xmap,ymap)
#result = result.adaptiveScale(resolution=(640,480))
#result = result.adaptiveScale(resolution=(1024,768))
result.save("pi_unwarped3.png")
# SimpleCV/OpenCV video out was giving problems
# decided to output frames and convert using
# avconv / ffmpeg. Comment out the block below
# to save to video
#ofname = 'OUT.AVI'
#vs = VideoStream(fps=20,filename=ofname,framefill=False)
#vs.initializeWriter((640,480))
# I used these params for converting the raw frames to video
# avconv -f image2 -r 30 -v:b 1024K -i samples/lapinsnipermin/%03d.jpeg output.mpeg
# i = 0
# while img is not None:
# print img.width,img.height
# result = unwarp(img,xmap,ymap)
# #derp = result.adaptiveScale(resolution=(640,480))
# #result = result.resize(w=img.width)
# # Once we get an image overlay it on the source
# #derp = img.blit(result,(0,img.height-result.height))
# #derp = derp.applyLayers()
# #derp = derp.resize(640,480)
# #result = result.adaptiveScale(resolution=(960,480))
# result.save(disp)
# # Save to file
# fname = "/var/www/html/vid_files/FRAME{num:05d}.png".format(num=i)
# result.save(fname)
# #vs.writeFrame(derp)
# # get the next frame
# img = vc.getImage()
# i = i + 1