def test1(): from numpy import zeros, ogrid import matplotlib matplotlib.use('Qt4Agg') from matplotlib.animation import FuncAnimation import matplotlib.pyplot as plt class FrameGenerator: def __init__(self): self.width = 300 self.height = 300 self.ox = 150 self.oy = 150 self.radius = 75 self.laser_x = 0 self.laser_y = 0 self.random_walk = False self._cnt = 0 self.time_constant = 0 self.cradius = 0 def __iter__(self): self._cnt = 0 return self def set_pos(self, x, y): self.laser_x = x self.laser_y = y self.ox = x self.oy = y def _calculate_radius(self): f = ((self.laser_x - self.width / 2.)**2 + (self.laser_y - self.height / 2.)**2)**0.5 # g = 50*math.sin(0.1*self._cnt) # g = 1+math.sin(0.1*self._cnt) # print self._cnt, g g = min(1, (1 - (50 - self._cnt) / 50.)) h = 0 + 15 * math.sin(0.1 * self._cnt) if self._cnt > 50 else 0 self.time_constant = h rr = self.radius * g + h self._cnt += 1 r = int(max(1, rr * (150 - f) / 150.)) # +random.randint(0,10) self.cradius = r return r # return self.radius * max(0.001, (1-f/self.radius)) # try: # ff = 5/float(f) # except ZeroDivisionError: # ff = 1 # print f, 1/f # ff = f/self. # return int(self.radius*ff) def next(self): radius = self._calculate_radius() offset = 3 src = zeros((self.width, self.height)) d = (0, 0) if self.random_walk: d = random.uniform(-offset, offset, 2) cx = self.ox + d[0] cy = self.oy + d[1] # if ((cx - 100) ** 2 + (cy - 100) ** 2) ** 0.5 > 50: # dx = offset if cx < 0 else -offset # dy = offset if cy < 0 else -offset # cx += dx # cy += dy # rain_drops['position'][current_index] += dx,dy # self.ox = cx # self.oy = cy y, x = ogrid[-radius:radius, -radius:radius] index = x**2 + y**2 <= radius**2 # src[cy - radius:cy + radius, cx - radius:cx + radius][index] = 255*random.uniform(size=index.shape) src[cy - radius:cy + radius, cx - radius:cx + radius][index] = 255 # xx, yy = mgrid[:200, :200] # circles contains the squared distance to the (100, 100) point # we are just using the circle equation learnt at school # circle = (xx - 100) ** 2 + (yy - 100) ** 2 # print circle.shape # print circle # raise StopIteration return src ld = LumenDetector() ld.hole_radius = 2 fig, (( ax, ax2, ), (ax3, ax4), (ax5, ax6), (ax7, ax8)) = plt.subplots(4, 2, figsize=(7, 7)) f = FrameGenerator() pattern = SeekPattern(base=15, perimeter_radius=100) o = f.next() ax.set_title('Current Frame') img = ax.imshow(o) gen = pattern.point_generator() ax2.set_title('Observed Brightness') img2 = ax2.imshow(o) ax3.set_title('Position') line = ax3.plot([0], [0])[0] r = pattern.perimeter_radius xs = linspace(-r, r) xs2 = xs[::-1] ys = (r**2 - xs**2)**0.5 ys2 = -(r**2 - xs2**2)**0.5 xxx, yyy = hstack((xs, xs2)) + 150, hstack((ys, ys2)) + 150 # print xxx,yyy # print xs+150, ys+150 # ax3.plot(xs+150,ys+150) ax3.plot(xxx, yyy) ax3.set_xlim(50, 250) ax3.set_ylim(50, 250) scatter = ax3.plot([150], [150], '+') xx, yy = 100, 150 scatter2 = ax3.plot([xx], [yy], 'o')[0] cp = ax7.scatter([0], [0], c=[0]) cp.autoscale() ax7.set_xlim(-100, 100) ax7.set_ylim(-100, 100) ax4.set_title('Intensity') tvint = ax4.plot([1], [1])[0] # tvint = ax4.semilogy([1],[1])[0] ax4.set_xlim(0, 50) # ax4.set_ylim(0, 1.1) ax5.set_title('Time Constant') tc = ax5.plot([0], [0])[0] ax5.set_ylim(-20, 20) ax5.set_xlim(0, 50) ax5.set_title('Radius') rs = ax6.plot([0], [0])[0] ax6.set_ylim(0, 100) ax6.set_xlim(0, 50) xs = [] ys = [] ts = [] zs = [] tcs = [] rcs = [] f.set_pos(xx, yy) st = time.time() def update(frame_number): # print frame_number x, y = gen.next() f.set_pos(xx + x, yy + y) # f.set_pos(frame_number,100) src = f.next() img.set_array(src) a, z = ld.get_value(copy(src)) # print x,y, z img2.set_array(a) # print z, x, y xs.append(xx + x) ys.append(yy + y) line.set_data(xs, ys) scatter2.set_data([xx + x], [yy + y]) ts.append(time.time() - st) # print z # z /= 3716625.0 # z += 0.1 * random.random() # print x, y, z pattern.set_point(z, x, y) zs.append(z) # print ts # print zs tvint.set_data(ts, zs) ax4.set_ylim(min(zs) * 0.9, max(zs) * 1.1) tcs.append(f.time_constant) rcs.append(f.cradius) tc.set_data(ts, tcs) rs.set_data(ts, rcs) # >>>>>>>>>>>>>>>>>>>>>>> # add a plot that is the current points of the triangle # >>>>>>>>>>>>>>>>>>>>>>> xy = pattern._tri.xys() x, y, z = zip(*xy) # print x, y # cp.set_data(x, y) cp.set_offsets(zip(x, y)) maz, miz = max(z), min(z) z = [(zi - miz) / (maz - miz) for zi in z] print z cp.set_facecolors([cm.hot(zi) for zi in z]) # cp.set_edgecolors(z) if not pattern._tri.is_equilateral(): print 'not eq' # return mplfig_to_npimage(fig) # raw_input() animation = FuncAnimation(fig, update, interval=1000) plt.show()
def test1(): from numpy import zeros, ogrid import matplotlib matplotlib.use('Qt4Agg') from matplotlib.animation import FuncAnimation import matplotlib.pyplot as plt class FrameGenerator: def __init__(self): self.width = 300 self.height = 300 self.ox = 150 self.oy = 150 self.radius = 75 self.laser_x = 0 self.laser_y = 0 self.random_walk = False self._cnt = 0 self.time_constant = 0 self.cradius = 0 def __iter__(self): self._cnt = 0 return self def set_pos(self, x, y): self.laser_x = x self.laser_y = y self.ox = x self.oy = y def _calculate_radius(self): f = ((self.laser_x - self.width / 2.) ** 2 + (self.laser_y - self.height / 2.) ** 2) ** 0.5 # g = 50*math.sin(0.1*self._cnt) # g = 1+math.sin(0.1*self._cnt) # print self._cnt, g g = min(1, (1 - (50 - self._cnt) / 50.)) h = 0 + 15 * math.sin(0.1 * self._cnt) if self._cnt > 50 else 0 self.time_constant = h rr = self.radius * g + h self._cnt += 1 r = int(max(1, rr * (150 - f) / 150.)) # +random.randint(0,10) self.cradius = r return r # return self.radius * max(0.001, (1-f/self.radius)) # try: # ff = 5/float(f) # except ZeroDivisionError: # ff = 1 # print f, 1/f # ff = f/self. # return int(self.radius*ff) def next(self): radius = self._calculate_radius() offset = 3 src = zeros((self.width, self.height)) d = (0, 0) if self.random_walk: d = random.uniform(-offset, offset, 2) cx = self.ox + d[0] cy = self.oy + d[1] # if ((cx - 100) ** 2 + (cy - 100) ** 2) ** 0.5 > 50: # dx = offset if cx < 0 else -offset # dy = offset if cy < 0 else -offset # cx += dx # cy += dy # rain_drops['position'][current_index] += dx,dy # self.ox = cx # self.oy = cy y, x = ogrid[-radius:radius, -radius:radius] index = x ** 2 + y ** 2 <= radius ** 2 # src[cy - radius:cy + radius, cx - radius:cx + radius][index] = 255*random.uniform(size=index.shape) src[cy - radius:cy + radius, cx - radius:cx + radius][index] = 255 # xx, yy = mgrid[:200, :200] # circles contains the squared distance to the (100, 100) point # we are just using the circle equation learnt at school # circle = (xx - 100) ** 2 + (yy - 100) ** 2 # print circle.shape # print circle # raise StopIteration return src ld = LumenDetector() ld.hole_radius = 2 fig, ((ax, ax2,), (ax3, ax4), (ax5, ax6), (ax7, ax8)) = plt.subplots(4, 2, figsize=(7, 7)) f = FrameGenerator() pattern = SeekPattern(base=15, perimeter_radius=100) o = f.next() ax.set_title('Current Frame') img = ax.imshow(o) gen = pattern.point_generator() ax2.set_title('Observed Brightness') img2 = ax2.imshow(o) ax3.set_title('Position') line = ax3.plot([0], [0])[0] r = pattern.perimeter_radius xs = linspace(-r, r) xs2 = xs[::-1] ys = (r ** 2 - xs ** 2) ** 0.5 ys2 = -(r ** 2 - xs2 ** 2) ** 0.5 xxx, yyy = hstack((xs, xs2)) + 150, hstack((ys, ys2)) + 150 # print xxx,yyy # print xs+150, ys+150 # ax3.plot(xs+150,ys+150) ax3.plot(xxx, yyy) ax3.set_xlim(50, 250) ax3.set_ylim(50, 250) scatter = ax3.plot([150], [150], '+') xx, yy = 100, 150 scatter2 = ax3.plot([xx], [yy], 'o')[0] cp = ax7.scatter([0], [0], c=[0]) cp.autoscale() ax7.set_xlim(-100, 100) ax7.set_ylim(-100, 100) ax4.set_title('Intensity') tvint = ax4.plot([1], [1])[0] # tvint = ax4.semilogy([1],[1])[0] ax4.set_xlim(0, 50) # ax4.set_ylim(0, 1.1) ax5.set_title('Time Constant') tc = ax5.plot([0], [0])[0] ax5.set_ylim(-20, 20) ax5.set_xlim(0, 50) ax5.set_title('Radius') rs = ax6.plot([0], [0])[0] ax6.set_ylim(0, 100) ax6.set_xlim(0, 50) xs = [] ys = [] ts = [] zs = [] tcs = [] rcs = [] f.set_pos(xx, yy) st = time.time() def update(frame_number): # print frame_number x, y = gen.next() f.set_pos(xx + x, yy + y) # f.set_pos(frame_number,100) src = f.next() img.set_array(src) a, z = ld.get_value(copy(src)) # print x,y, z img2.set_array(a) # print z, x, y xs.append(xx + x) ys.append(yy + y) line.set_data(xs, ys) scatter2.set_data([xx + x], [yy + y]) ts.append(time.time() - st) # print z # z /= 3716625.0 # z += 0.1 * random.random() # print x, y, z pattern.set_point(z, x, y) zs.append(z) # print ts # print zs tvint.set_data(ts, zs) ax4.set_ylim(min(zs) * 0.9, max(zs) * 1.1) tcs.append(f.time_constant) rcs.append(f.cradius) tc.set_data(ts, tcs) rs.set_data(ts, rcs) # >>>>>>>>>>>>>>>>>>>>>>> # add a plot that is the current points of the triangle # >>>>>>>>>>>>>>>>>>>>>>> xy = pattern._tri.xys() x, y, z = zip(*xy) # print x, y # cp.set_data(x, y) cp.set_offsets(zip(x, y)) maz, miz = max(z), min(z) z = [(zi - miz) / (maz - miz) for zi in z] print z cp.set_facecolors([cm.hot(zi) for zi in z]) # cp.set_edgecolors(z) if not pattern._tri.is_equilateral(): print 'not eq' # return mplfig_to_npimage(fig) # raw_input() animation = FuncAnimation(fig, update, interval=1000) plt.show()