def merge_ims(srcs, pts_or_rects, bg, opt = None): """ Makes a new image where each image in patches is copied at a corresponding pixel location. Overlapping images are averaged together. """ dst = bg.copy() layer = np.zeros(dst.shape) counts = np.zeros(dst.shape[:2], 'l') for src, r in itl.izip(srcs, pts_or_rects): r = ut.int_tuple(r) x, y = r[:2] # rescale if we're given a rectangle, and it has a different size if len(r) > 2: assert len(r) == 4 assert opt != 'center' if src.shape[:2] != (r[3], r[2]): src = resize(src, (r[3], r[2])) elif opt == 'center': x -= src.shape[1]/2 y -= src.shape[0]/2 # crop intersecting dx, dy, dw, dh = ut.crop_rect_to_img((x, y, src.shape[1], src.shape[0]), dst) sx = dx - x sy = dy - y layer[dy : dy + dh, dx : dx + dw] += src[sy : sy + dh, sx : sx + dw] counts[dy : dy + dh, dx : dx + dw] += 1 dst[counts > 0] = layer[counts > 0] / counts[counts > 0][:, np.newaxis] return dst
def merge_ims(srcs, pts_or_rects, bg, opt=None): """ Makes a new image where each image in patches is copied at a corresponding pixel location. Overlapping images are averaged together. """ dst = rgb_from_gray(bg) layer = np.zeros(dst.shape) #counts = np.zeros(dst.shape[:2], 'l') counts = np.zeros(dst.shape[:2], 'd') for src, r in itl.izip(srcs, pts_or_rects): r = ut.int_tuple(r) x, y = r[:2] # rescale if we're given a rectangle, and it has a different size if len(r) > 2: assert len(r) == 4 assert opt != 'center' if src.shape[:2] != (r[3], r[2]): src = resize(src, (r[3], r[2])) elif opt == 'center': x -= src.shape[1] / 2 y -= src.shape[0] / 2 # crop intersecting dx, dy, dw, dh = ut.crop_rect_to_img( (x, y, src.shape[1], src.shape[0]), dst) sx = dx - x sy = dy - y layer[dy:dy + dh, dx:dx + dw] += src[sy:sy + dh, sx:sx + dw, :3] if np.ndim(src) == 3 and src.shape[2] == 4: counts[dy:dy + dh, dx:dx + dw] += np.array(src[sy:sy + dh, sx:sx + dw, 3], 'd') / 255. else: counts[dy:dy + dh, dx:dx + dw] += 1 dst[counts > 0] = layer[counts > 0] / counts[counts > 0][:, np.newaxis] return dst
def weighted_add(src, dst, x, y, src_weight, dst_weight, opt = None): if opt == 'center': x -= src.shape[1]/2 y -= src.shape[0]/2 # crop intersecting dx, dy, dw, dh = ut.crop_rect_to_img((x, y, src.shape[1], src.shape[0]), dst) sx = dx - x sy = dy - y dst[dy : dy + dh, dx : dx + dw] = dst[dy : dy + dh, dx : dx + dw]*dst_weight + src[sy : sy + dh, sx : sx + dw]*src_weight
def weighted_add(src, dst, x, y, src_weight, dst_weight, opt = None): if opt == 'center': x -= src.shape[1]/2 y -= src.shape[0]/2 # crop intersecting dx, dy, dw, dh = ut.crop_rect_to_img((x, y, src.shape[1], src.shape[0]), dst) sx = dx - x sy = dy - y dst[dy : dy + dh, dx : dx + dw] = dst[dy : dy + dh, dx : dx + dw]*dst_weight + src[sy : sy + dh, sx : sx + dw]*src_weight
vals.append(v.reshape(im.shape[:2] + (1,))) return np.concatenate(vals, axis = 2) def stack_meshgrid(xs, ys, dtype = 'l'): x, y = np.meshgrid(xs, ys) return np.array(np.concatenate([x[..., np.newaxis], y[..., np.newaxis]], axis = 2), dtype = dtype) def sub_img_pad(im, (x, y, w, h), oob = 0): if len(im.shape) == 2: dst = np.zeros((h, w)) else: dst = np.zeros((h, w, im.shape[2])) dst[:] = oob sx, sy, sw, sh = ut.crop_rect_to_img((x, y, w, h), im) dst[(sy - y) : (sy - y) + sh, (sx - x) : (sx - x) + sw] = im[sy : sy + sh, sx : sx + sw] return dst def compress(im, format = 'png'): out = StringIO() im = to_pil(im) im.save(out, format = format) c = out.getvalue() out.close() return c def uncompress(s): return from_pil(Image.open(StringIO(s)))
def stack_meshgrid(xs, ys, dtype='l'): x, y = np.meshgrid(xs, ys) return np.array(np.concatenate([x[..., np.newaxis], y[..., np.newaxis]], axis=2), dtype=dtype) def sub_img_pad(im, (x, y, w, h), oob=0): if len(im.shape) == 2: dst = np.zeros((h, w)) else: dst = np.zeros((h, w, im.shape[2])) dst[:] = oob sx, sy, sw, sh = ut.crop_rect_to_img((x, y, w, h), im) dst[(sy - y):(sy - y) + sh, (sx - x):(sx - x) + sw] = im[sy:sy + sh, sx:sx + sw] return dst def sub_img_reflect(im, (x, y, w, h)): x, y, w, h = map(ut.iround, [x, y, w, h]) yy, xx = np.mgrid[y:y + h, x:x + w] vals = np.uint8( lookup_bilinear(im, xx.flatten(), yy.flatten(), order=0, mode='reflect')) return vals.reshape((h, w, im.shape[2]))