def plot_regions(self, fill=True, bgimage=None, alpha=0.5):
import pylab as pl
ax = pl.gca()
assert isinstance(ax, pl.Axes)
colors = i12.JET_12
self._plot_background(bgimage)
for label in self.regions:
color = colors[i12.LABELS.index(label)] / 255.
for region in self.regions[label]:
t = region['top']
l = self.facade_left + region['left']
b = region['bottom']
r = self.facade_left + region['right']
patch = pl.Rectangle((l, t), r - l, b - t, color=color, fill=fill, alpha=alpha)
ax.add_patch(patch)
python类Rectangle()的实例源码
def plot(self):
import pylab as pl
ax = pl.gca()
pl.hlines(self.tops, self.left, self.right, linestyles='dashed', colors='blue')
pl.hlines(self.tops + self.heights, self.left, self.right, linestyles='dashed', colors='green')
pl.vlines(self.lefts, self.top, self.bottom, linestyles='dashed', colors='blue')
pl.vlines(self.lefts + self.widths, self.top, self.bottom, linestyles='dashed', colors='green')
for box in self.rectangles:
t, l, b, r = box
patch = pl.Rectangle((l, t), r - l, b - t, color='blue', fill=True, alpha=0.5)
ax.add_patch(patch)
pass
def plot(self, bgimage=None):
import pylab as pl
self._plot_background(bgimage)
ax = pl.gca()
y0, y1 = pl.ylim()
# r is the width of the thick line we use to show the facade colors
r = 5
patch = pl.Rectangle((self.facade_left + r, self.sky_line + r),
self.width - 2 * r,
self.door_line - self.sky_line - 2 * r,
color=self.color, fill=False, lw=2 * r)
ax.add_patch(patch)
pl.text((self.facade_right + self.facade_left) / 2.,
(self.door_line + self.sky_line) / 2.,
'$\sigma^2={:0.2f}$'.format(self.uncertainty_for_windows()))
patch = pl.Rectangle((self.facade_left + r, self.door_line + r),
self.width - 2 * r,
y0 - self.door_line - 2 * r,
color=self.mezzanine_color, fill=False, lw=2 * r)
ax.add_patch(patch)
# Plot the left and right edges in yellow
pl.vlines([self.facade_left, self.facade_right], self.sky_line, y0, colors='yellow')
# Plot the door line and the roof line
pl.hlines([self.door_line, self.sky_line], self.facade_left, self.facade_right, linestyles='dashed',
colors='yellow')
self.window_grid.plot()
def save_maxima(image, new_x, new_y, dist, outpath, nameCondition):
pylab.close('all')
pylab.imshow(image)
pylab.axis('off')
pylab.autoscale(False)
for i in xrange(len(new_x)):
points = pylab.plot(new_x[i], new_y[i], 'wo')
squares = pylab.gca().add_patch(pylab.Rectangle((new_x[i] - dist/2, new_y[i] - dist/2), dist, dist, edgecolor = 'w', alpha = 0.3, lw = 3))
pylab.savefig("{0}/found_maxima_n{1}_{2}.png".format(outpath, len(new_x), nameCondition))
pylab.close()
def main(argv):
if ((len(argv) < 5) or (len(argv) % 2 != 1)):
print("Usage: python -m rect_pack.rect_pack binWidth binHeight w_0 h_0 w_1 h_1 w_2 h_2 ... w_n h_n\n")
print(" where binWidth and binHeight define the size of the bin.\n")
print(" w_i is the width of the i'th rectangle to pack, and h_i the height.\n")
print("Example: python -m rect_pack.rect_pack 256 256 30 20 50 20 10 80 90 20\n")
argNums = [float(arg) for arg in argv[1:]]
binSize = argNums[0:2]
print("Initializing bin to size {}x{}".format(*binSize))
rectBin = RectPack(*binSize)
widthByHeights = [(rectWidth, rectHeight,) for rectWidth, rectHeight in pairwiseIter(argNums[2:])]
# widthByHeights = list(sorted(widthByHeights, key= lambda wByH: -wByH[0] * wByH[1]))
# inRects = [Rect(0, 0, width, height) for width, height in widthByHeights]
inRects = [Rect(0, 0, width, height, data="{},{},{}".format(num, width, height)) for num, (width, height) in enumerate(widthByHeights)]
rectBin.pack(inRects)
failedCount = 0
for num, (packedRect, occupancy) in enumerate(zip(rectBin.packedRectList, rectBin.occupancyHistory)):
rectWidth, rectHeight = packedRect.width, packedRect.height
print("# {} Packing rectangle of size {}x{}: ".format(num, rectWidth, rectHeight), end="")
# Test success or failure.
if packedRect.height > 0:
print("Packed to (x,y)=({},{}), (w,h)=({},{}). Free space left: {:.2f}%".format(
packedRect.x, packedRect.y, packedRect.width, packedRect.height,
100.0 - occupancy*100.0))
else:
print("Failed! Could not find a proper position to pack this rectangle into. Skipping this one.")
failedCount += 1
if not failedCount:
print("Done. All rectangles packed.")
else:
print("Done. {} rectangles failed packing.".format(failedCount))
## let's plot it
import pylab as pl
import matplotlib.colorbar as cbar
colors = pl.cm.jet(np.asarray(range(len(widthByHeights))) / len(widthByHeights))
ax = pl.subplot(111)
for packedRect in rectBin.packedRectList:
# Test success or failure.
if packedRect.height > 0:
import random
color = pl.cm.jet(random.random())
rectPatch = pl.Rectangle(
(packedRect.x, packedRect.y), packedRect.width, packedRect.height,
# linewidth=0,
# color=color)
facecolor=color)
ax.add_patch(rectPatch)
cax, _ = cbar.make_axes(ax)
cb2 = cbar.ColorbarBase(cax, cmap=pl.cm.jet)
# for packedRect in rectBin.packedRectList:
# print(packedRect)
ax.set_xlim(0, binSize[0])
ax.set_ylim(0, binSize[1])
pl.show()
###
return 0
def image_loop(self, decay, display_mode):
import pylab
fig = pylab.figure()
pylab.ion()
img = pylab.imshow(self.image, vmax=1, vmin=-1,
interpolation='none', cmap='binary')
pylab.xlim(0, 127)
pylab.ylim(127, 0)
regions = {}
if self.count_spike_regions is not None:
for k, v in self.count_spike_regions.items():
minx, miny, maxx, maxy = v
rect = pylab.Rectangle((minx - 0.5, miny - 0.5),
maxx - minx,
maxy - miny,
facecolor='yellow', alpha=0.2)
pylab.gca().add_patch(rect)
regions[k] = rect
if self.track_periods is not None:
colors = ([(0,0,1), (0,1,0), (1,0,0), (1,1,0), (1,0,1)] * 10)[:len(self.p_y)]
scatter = pylab.scatter(self.p_x, self.p_y, s=50, c=colors)
else:
scatter = None
while True:
img.set_data(self.image)
for k, rect in regions.items():
alpha = self.get_spike_rate(k) * 0.5
alpha = min(alpha, 0.5)
rect.set_alpha(0.05 + alpha)
if scatter is not None:
scatter.set_offsets(np.array([self.p_x, self.p_y]).T)
c = [(r,g,b,min(self.track_certainty[i],1)) for i,(r,g,b) in enumerate(colors)]
scatter.set_color(c)
if display_mode == 'quick':
# this is much faster, but doesn't work on all systems
fig.canvas.draw()
fig.canvas.flush_events()
else:
# this works on all systems, but is kinda slow
pylab.pause(0.001)
self.image *= decay
def images_loop(self, decays, display_mode):
import pylab
fig = pylab.figure()
num_images = len(decays)
pylab.ion()
imgs = []
for i in range(len(decays)):
fig.add_subplot(1, num_images, i+1)
imgs.append( pylab.imshow(self.images[i], vmax=1, vmin=-1,
interpolation='none', cmap='binary') )
pylab.xlim(0, 127)
pylab.ylim(127, 0)
regions = {}
if self.count_spike_regions is not None:
for k, v in self.count_spike_regions.items():
minx, miny, maxx, maxy = v
rect = pylab.Rectangle((minx - 0.5, miny - 0.5),
maxx - minx,
maxy - miny,
facecolor='yellow', alpha=0.2)
pylab.gca().add_patch(rect)
regions[k] = rect
if self.track_periods is not None:
colors = ([(0,0,1), (0,1,0), (1,0,0), (1,1,0), (1,0,1)] * 10)[:len(self.p_y)]
scatter = pylab.scatter(self.p_x, self.p_y, s=50, c=colors)
else:
scatter = None
while True:
for i,d in enumerate(decays):
imgs[i].set_data(self.images[i])
for k, rect in regions.items():
alpha = self.get_spike_rate(k) * 0.5
alpha = min(alpha, 0.5)
rect.set_alpha(0.05 + alpha)
if scatter is not None:
scatter.set_offsets(np.array([self.p_x, self.p_y]).T)
c = [(r,g,b,min(self.track_certainty[i],1)) for i,(r,g,b) in enumerate(colors)]
scatter.set_color(c)
if display_mode == 'quick':
# this is much faster, but doesn't work on all systems
fig.canvas.draw()
fig.canvas.flush_events()
else:
# this works on all systems, but is kinda slow
pylab.pause(0.001)
for i,d in enumerate(decays):
self.images[i] *= d
def image_loop(self, decay, display_mode):
import pylab
fig = pylab.figure()
pylab.ion()
img = pylab.imshow(self.image, vmax=1, vmin=-1,
interpolation='none', cmap='binary')
pylab.xlim(0, 127)
pylab.ylim(127, 0)
regions = {}
if self.count_spike_regions is not None:
for k, v in self.count_spike_regions.items():
minx, miny, maxx, maxy = v
rect = pylab.Rectangle((minx - 0.5, miny - 0.5),
maxx - minx,
maxy - miny,
facecolor='yellow', alpha=0.2)
pylab.gca().add_patch(rect)
regions[k] = rect
if self.track_periods is not None:
colors = ([(0,0,1), (0,1,0), (1,0,0), (1,1,0), (1,0,1)] * 10)[:len(self.p_y)]
scatter = pylab.scatter(self.p_x, self.p_y, s=50, c=colors)
else:
scatter = None
while True:
img.set_data(self.image)
for k, rect in regions.items():
alpha = self.get_spike_rate(k) * 0.5
alpha = min(alpha, 0.5)
rect.set_alpha(0.05 + alpha)
if scatter is not None:
scatter.set_offsets(np.array([self.p_x, self.p_y]).T)
c = [(r,g,b,min(self.track_certainty[i],1)) for i,(r,g,b) in enumerate(colors)]
scatter.set_color(c)
if display_mode == 'quick':
# this is much faster, but doesn't work on all systems
fig.canvas.draw()
fig.canvas.flush_events()
else:
# this works on all systems, but is kinda slow
pylab.pause(0.001)
self.image *= decay