def Run(self, img_path, guide_image_path='', objective=0):
"""Run deep dream"""
self.guide_path = guide_image_path
if self.guide_path != '':
self.Get_guide()
self.net.blobs.keys()
if img_path != '':
frame = PIL.Image.open(img_path)
frame = imresize(frame)
frame = np.float32(frame)
else:
frame = self.GenerateInputImage()
frame_i = 0
h, w = frame.shape[:2]
#s = 0.05 # scale coefficient
for i in xrange(self.epoch):
start = time.time()
frame = self.Deepdream(frame)
PIL.Image.fromarray(np.uint8(frame)).save("frames/%04d.jpg"%frame_i)
#frame = nd.affine_transform(frame, [1-s,1-s,1], [h*s/2,w*s/2,0], order=1)
frame_i += 1
stop = time.time()
print "Time cost for {:d}th image: {:.3f} s".format(i,stop-start)
python类affine_transform()的实例源码
def Run(self, img_path, guide_image_path='', objective=0):
"""Run deep dream"""
self.guide_path = guide_image_path
if self.guide_path != '':
self.Get_guide()
self.net.blobs.keys()
if img_path != '':
frame = PIL.Image.open(img_path)
frame = imresize(frame)
frame = np.float32(frame)
else:
frame = self.GenerateInputImage()
frame_i = 0
h, w = frame.shape[:2]
#s = 0.05 # scale coefficient
for i in xrange(self.epoch):
start = time.time()
frame = self.Deepdream(frame)
PIL.Image.fromarray(np.uint8(frame)).save("frames/%04d.jpg"%frame_i)
#frame = nd.affine_transform(frame, [1-s,1-s,1], [h*s/2,w*s/2,0], order=1)
frame_i += 1
stop = time.time()
print "Time cost for {:d}th image: {:.3f} s".format(i,stop-start)
def image_in_image(im1,im2,tp):
m, n = im1.shape[:2]
fp = np.array([[0,m,m,0],[0,0,n,n],[1,1,1,1]])
# compute affine transform and apply
H = homography.Haffine_from_points(tp,fp)
im1_t = ndimage.affine_transform(im1,H[:2,:2],
(H[0,2],H[1,2]),im2.shape[:2])
alpha = (im1_t > 0)
return (1-alpha)*im2 + alpha*im1_t
def run(self, ips, snap, img, para = None):
if para == None: para = self.para
a = para['ang']/180.0*np.pi
o = np.array(ips.size)*0.5
if ips.roi!=None:
box = ips.roi.get_box()
o = np.array([box[1]+box[3],box[0]+box[2]])*0.5
trans = np.array([[np.cos(a),-np.sin(a)],[np.sin(a),np.cos(a)]])
offset = o-trans.dot(o)
nimg.affine_transform(snap, trans, output=img, offset=offset)
def run(self, ips, snap, img, para = None):
if para == None: para = self.para
k = 1/para['zoom']
o = np.array(ips.size)*0.5
if ips.roi!=None:
box = ips.roi.get_box()
o = np.array([box[1]+box[3],box[0]+box[2]])*0.5
trans = np.array([[k,0],[0,k]])
offset = o-trans.dot(o)
nimg.affine_transform(snap, trans, output=img, offset=offset)
def run(self, ips, img, buf, para = None):
if para == None: para = self.para
a = para['ang']/180.0*np.pi
trans = np.array([[np.cos(a),-np.sin(a)],[np.sin(a),np.cos(a)]])
o = np.array([self.para['oy'], self.para['ox']])
offset = o-trans.dot(o)
if self.para['img']:
nimg.affine_transform(img, trans, output=buf, offset=offset)
if self.para['msk'] and self.bufroi!=None:
ips.roi = self.bufroi.affine(trans, o[::-1]-trans.dot(o[::-1]))
def run(self, ips, img, buf, para = None):
if para == None: para = self.para
self.count(False)
trans = np.array([[1/self.para['ky'],0],[0,1/self.para['kx']]])
o = np.array([self.para['oy'], self.para['ox']])
offset = self.orio[::-1]-trans.dot(o)
if self.para['img']:
nimg.affine_transform(img, trans, output=buf, offset=offset)
trans = np.array([[self.para['kx'],0],[0, self.para['ky']]])
offset = o[::-1]-trans.dot(self.orio)
if self.para['msk'] and self.bufroi!=None:ips.roi = self.bufroi.affine(trans, offset)
if self.para['img'] and not ips.get_msk('out') is None:
buf[ips.get_msk('out')] = img[ips.get_msk('out')]
ips.update = True
def random_affine(transpose_scale, affine_scale, fill=0):
def transform(glyph):
# random transpose: +-transpose_max
transpose = np.random.normal(loc=0, scale=transpose_scale, size=2)
# identity matrix (no transform)
affine = np.asarray([[1, 0], [0, 1]], dtype='float32')
# add randomness: +-affine_max
affine += np.random.normal(loc=0, scale=affine_scale, size=(2,2))
return ndimage.affine_transform(glyph, matrix=affine, offset=transpose, cval=fill)
return transform