Python skimage.transform.rescale() Examples

The following are 30 code examples of skimage.transform.rescale(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module skimage.transform , or try the search function .
Example #1
Source File: task_launcher_faceswap.py    From DepthNets with MIT License 6 votes vote down vote up
def image_dump_handler(out_folder, scale_factor=1.):
    def _fn(losses, inputs, outputs, kwargs):
        if kwargs['iter'] != 1:
            return
        A_real = inputs[0].data.cpu().numpy()
        B_real = inputs[1].data.cpu().numpy()
        atob, atob_btoa, btoa, btoa_atob = \
            [elem.data.cpu().numpy() for elem in outputs.values()]
        outs_np = [A_real, atob, atob_btoa, B_real, btoa, btoa_atob]
        # determine # of channels
        n_channels = outs_np[0].shape[1]
        w, h = outs_np[0].shape[-1], outs_np[0].shape[-2]
        # possible that A_real.bs != B_real.bs
        bs = np.min([outs_np[0].shape[0], outs_np[3].shape[0]])
        grid = np.zeros((h*bs, w*6, 3))
        for j in range(bs):
            for i in range(6):
                n_channels = outs_np[i][j].shape[0]
                img_to_write = convert_to_rgb(outs_np[i][j], is_grayscale=False)
                grid[j*h:(j+1)*h, i*w:(i+1)*w, :] = img_to_write
        imsave(arr=rescale(grid, scale=scale_factor),
               fname="%s/%i_%s.png" % (out_folder, kwargs['epoch'], kwargs['mode']))
    return _fn 
Example #2
Source File: enhancer_gan.py    From ImageEnhancer with MIT License 6 votes vote down vote up
def _corrupt(self, source):
        """ corrupt the input with specific corruption method
            :param source: original data set
            :return: corrupted data set, if noise type is not defined, the original data set will return
        """
        if self.corrupt_type is None:
            return source
        noised = np.zeros((np.shape(source)[0],) + self.shape['in'])
        for i, raw in enumerate(source):
            if self.corrupt_type == 'GSN':
                noised[i] = random_noise(raw, 'gaussian', var=self.corrupt_ratio)
            elif self.corrupt_type == 'MSN':
                noised[i] = random_noise(raw, 'pepper', amount=self.corrupt_ratio)
            elif self.corrupt_type == 'SPN':
                noised[i] = random_noise(raw, 's&p', amount=self.corrupt_ratio)
            elif self.corrupt_type == 'GSB':
                noised[i] = gaussian(raw, sigma=self.corrupt_ratio, multichannel=True)
            elif self.corrupt_type == 'GRY':
                noised[i] = gray2rgb(rgb2gray(raw))
            elif self.corrupt_type == 'BLK':
                rad = int(self.corrupt_ratio)
                row = random.randint(rad, self.shape['in'][0] - rad - 1)
                col = random.randint(rad, self.shape['in'][1] - rad - 1)
                noised[i] = np.copy(raw)
                noised[i][circle(row, col, self.corrupt_ratio)] = 0
            elif self.corrupt_type == 'ZIP':
                noised[i] = rescale(raw, 0.5, mode='constant')
        return noised 
Example #3
Source File: image_tfs.py    From tanda with MIT License 6 votes vote down vote up
def TF_zoom(x, scale=1.0, target=None):
    assert len(x.shape) == 3
    h, w, nc = x.shape
    assert h == w

    # Zoom
    xc   = rescale(x, scale)
    diff = h - xc.shape[0]
    d    = int(np.floor(diff / 2.0))
    if d >= 0:
        padding = ((d, d),(d, d),(0,0))
        if diff % 2 != 0:
            padding = ((d,d+1), (d,d + 1),(0,0))
        return np.pad(xc, padding, mode='edge')
    else:
        return xc[-d:h-d, -d:w-d].reshape(h, w, nc) 
Example #4
Source File: pool.py    From ternarynet with Apache License 2.0 6 votes vote down vote up
def test_upsample(self):
        h, w = 5, 5
        scale = 2

        mat = np.random.rand(h, w).astype('float32')
        inp = self.make_variable(mat)
        inp = tf.reshape(inp, [1, h, w, 1])

        output = BilinearUpSample('upsample', inp, scale)
        res = self.run_variable(output)[0,:,:,0]

        from skimage.transform import rescale
        res2 = rescale(mat, scale)

        diff = np.abs(res2 - res)

        # not equivalent to rescale on edge?
        diff[0,:] = 0
        diff[:,0] = 0
        if not diff.max() < 1e-4:
            import IPython;
            IPython.embed(config=IPython.terminal.ipapp.load_default_config())
        self.assertTrue(diff.max() < 1e-4) 
Example #5
Source File: pool.py    From Distributed-BA3C with Apache License 2.0 6 votes vote down vote up
def test_upsample(self):
        h, w = 5, 5
        scale = 2

        mat = np.random.rand(h, w).astype('float32')
        inp = self.make_variable(mat)
        inp = tf.reshape(inp, [1, h, w, 1])

        output = BilinearUpSample('upsample', inp, scale)
        res = self.run_variable(output)

        from skimage.transform import rescale
        res2 = rescale(mat, scale)

        diff = np.abs(res2 - res[0,:,:,0])

        # not equivalent to rescale on edge
        diff[0,:] = 0
        diff[:,0] = 0
        if not diff.max() < 1e-4:
            import IPython;
            IPython.embed(config=IPython.terminal.ipapp.load_default_config())
        self.assertTrue(diff.max() < 1e-4) 
Example #6
Source File: pool.py    From petridishnn with MIT License 6 votes vote down vote up
def test_BilinearUpSample(self):
        h, w = 12, 12
        scale = 2

        mat = np.random.rand(h, w).astype('float32')
        inp = self.make_variable(mat)
        inp = tf.reshape(inp, [1, h, w, 1])

        output = BilinearUpSample(inp, scale)
        res = self.run_variable(output)[0, :, :, 0]

        from skimage.transform import rescale
        res2 = rescale(mat, scale, mode='edge')

        diff = np.abs(res2 - res)

        # if not diff.max() < 1e-4:
        #     import IPython
        #     IPython.embed(config=IPython.terminal.ipapp.load_default_config())
        self.assertTrue(diff.max() < 1e-4, diff.max()) 
Example #7
Source File: pool.py    From VDAIC2017 with MIT License 6 votes vote down vote up
def test_upsample(self):
        h, w = 5, 5
        scale = 2

        mat = np.random.rand(h, w).astype('float32')
        inp = self.make_variable(mat)
        inp = tf.reshape(inp, [1, h, w, 1])

        output = BilinearUpSample('upsample', inp, scale)
        res = self.run_variable(output)

        from skimage.transform import rescale
        res2 = rescale(mat, scale)

        diff = np.abs(res2 - res[0,:,:,0])

        # not equivalent to rescale on edge
        diff[0,:] = 0
        diff[:,0] = 0
        if not diff.max() < 1e-4:
            import IPython;
            IPython.embed(config=IPython.terminal.ipapp.load_default_config())
        self.assertTrue(diff.max() < 1e-4) 
Example #8
Source File: crappifiers.py    From PSSR with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def fluo_SP_AG_D_sameas_preprint_rescale(x, scale=4, upsample=False):
    xn = np.array(x)
    xorig_max = xn.max()
    xn = xn.astype(np.float32)
    xn /= float(np.iinfo(np.uint8).max)
    xn = random_noise(xn, mode='salt', amount=0.005)
    xn = random_noise(xn, mode='pepper', amount=0.005)
    lvar = filters.gaussian(xn, sigma=5) + 1e-10
    xn = random_noise(xn, mode='localvar', local_vars=lvar*0.5)
    new_max = xn.max()
    x = xn
    if new_max > 0:
        xn /= new_max
    xn *= xorig_max
    multichannel = len(x.shape) > 2
    x_down = rescale(x, scale=1/scale, order=1, multichannel=multichannel)
    return PIL.Image.fromarray(x_down.astype(np.uint8)) 
Example #9
Source File: crappifiers.py    From PSSR with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def new_crap(x, scale=4, upsample=False):
    xn = np.array(x)
    xorig_max = xn.max()
    xn = xn.astype(np.float32)
    xn /= float(np.iinfo(np.uint8).max)

    xn = random_noise(xn, mode='salt', amount=0.005)
    xn = random_noise(xn, mode='pepper', amount=0.005)
    lvar = filters.gaussian(xn, sigma=5) + 1e-10
    xn = random_noise(xn, mode='localvar', local_vars=lvar*0.5)
    new_max = xn.max()
    x = xn
    if new_max > 0:
        xn /= new_max
    xn *= xorig_max
    multichannel = len(x.shape) > 2
    x = rescale(x, scale=1/scale, order=1, multichannel=multichannel)
    return PIL.Image.fromarray(x.astype(np.uint8)) 
Example #10
Source File: crappifiers.py    From PSSR with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def new_crap_AG_SP(x, scale=4, upsample=False):
    xn = np.array(x)
    xorig_max = xn.max()
    xn = xn.astype(np.float32)
    xn /= float(np.iinfo(np.uint8).max)

    lvar = filters.gaussian(xn, sigma=5) + 1e-10
    xn = random_noise(xn, mode='localvar', local_vars=lvar*0.5)

    xn = random_noise(xn, mode='salt', amount=0.005)
    xn = random_noise(xn, mode='pepper', amount=0.005)

    new_max = xn.max()
    x = xn
    if new_max > 0:
        xn /= new_max
    xn *= xorig_max
    multichannel = len(x.shape) > 2

    xn = rescale(xn, scale=1/scale, order=1, multichannel=multichannel)
    return PIL.Image.fromarray(xn.astype(np.uint8)) 
Example #11
Source File: object_detection_client.py    From lambda-deep-learning-demo with Apache License 2.0 6 votes vote down vote up
def main():
  parser = argparse.ArgumentParser(
    formatter_class=argparse.ArgumentDefaultsHelpFormatter)

  parser.add_argument("--image_path",
                      help="path for image to run inference",
                      default="~/demo/data/mscoco_fns/val2014/COCO_val2014_000000301397.jpg")

  args = parser.parse_args()

  args.image_path = os.path.expanduser(args.image_path)

  # Read the image
  image = skimage.io.imread(args.image_path, plugin='imageio')
  image = rescale(image, 2.0, anti_aliasing=False)
  image = img_as_ubyte(image)

  data = json.dumps({"signature_name": "predict", "instances": image.tolist()})
  headers = {"content-type": "application/json"}

  response = requests.post(SERVER_URL, data=data, headers=headers)
  response.raise_for_status()
  predictions = response.json()["predictions"]

  display_ori(image, predictions[0]) 
Example #12
Source File: utils.py    From Semantic-Segmentation-using-Adversarial-Networks with MIT License 6 votes vote down vote up
def resize_img_with_max_size(img, max_size=500*500):
    """Resize image with max size (height x width)"""
    from skimage.transform import rescale
    height, width = img.shape[:2]
    scale = max_size / (height * width)
    resizing_scale = 1
    if scale < 1:
        resizing_scale = np.sqrt(scale)
        img = rescale(img, resizing_scale, preserve_range=True)
        img = img.astype(np.uint8)
    return img, resizing_scale


# -----------------------------------------------------------------------------
# Chainer Util
# ----------------------------------------------------------------------------- 
Example #13
Source File: pool.py    From DDRL with Apache License 2.0 6 votes vote down vote up
def test_upsample(self):
        h, w = 5, 5
        scale = 2

        mat = np.random.rand(h, w).astype('float32')
        inp = self.make_variable(mat)
        inp = tf.reshape(inp, [1, h, w, 1])

        output = BilinearUpSample('upsample', inp, scale)
        res = self.run_variable(output)

        from skimage.transform import rescale
        res2 = rescale(mat, scale)

        diff = np.abs(res2 - res[0,:,:,0])

        # not equivalent to rescale on edge
        diff[0,:] = 0
        diff[:,0] = 0
        if not diff.max() < 1e-4:
            import IPython;
            IPython.embed(config=IPython.terminal.ipapp.load_default_config())
        self.assertTrue(diff.max() < 1e-4) 
Example #14
Source File: image.py    From modl with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def load_image(source,
               scale=1,
               gray=False,
               memory=Memory(cachedir=None)):
    data_dir = get_data_dirs()[0]
    if source == 'face':
        image = face(gray=gray)
        image = image.astype(np.float32) / 255
        if image.ndim == 2:
            image = image[..., np.newaxis]
        if scale != 1:
            image = memory.cache(rescale)(image, scale=scale)
        return image
    elif source == 'lisboa':
        image = imread(join(data_dir, 'images', 'lisboa.jpg'), as_grey=gray)
        image = image.astype(np.float32) / 255
        if image.ndim == 2:
            image = image[..., np.newaxis]
        if scale != 1:
            image = memory.cache(rescale)(image, scale=scale)
        return image
    elif source == 'aviris':
        from spectral import open_image

        image = open_image(
            join(data_dir,
                 'aviris',
                 'f100826t01p00r05rdn_b/'
                 'f100826t01p00r05rdn_b_sc01_ort_img.hdr'))
        image = np.array(image.open_memmap(), dtype=np.float32)
        good_bands = list(range(image.shape[2]))
        good_bands.remove(110)
        image = image[:, :, good_bands]
        indices = image == -50
        image[indices] = -1
        image[~indices] -= np.min(image[~indices])
        image[~indices] /= np.max(image[~indices])
        return image
    else:
        raise ValueError('Data source is not known') 
Example #15
Source File: cos_mx.py    From advhat with MIT License 5 votes vote down vote up
def main(args):
        print(args)
        
        # Embedding model
        sym, arg_params, aux_params = mx.model.load_checkpoint(args.model, 0)
        sym = sym.get_internals()['fc1_output']
        model = mx.mod.Module(symbol=sym, context=mx.gpu(0), label_names = None)
        model.bind(data_shapes=[('data', (1, 3, 112, 112))])
        model.set_params(arg_params, aux_params)
        
        # Embedding calculation
        im1 = (prep(rescale(io.imread(args.face1)/255.,112./600.,order=5))*255.).astype(np.uint8)
        im2 = (prep(rescale(io.imread(args.face2)/255.,112./600.,order=5))*255.).astype(np.uint8)
        
        batch = mx.io.DataBatch(data=[nd.array(im1)])
        model.forward(batch, is_train=False)
        emb1 = model.get_outputs()[0].asnumpy()[0]
        batch = mx.io.DataBatch(data=[nd.array(im2)])
        model.forward(batch, is_train=False)
        emb2 = model.get_outputs()[0].asnumpy()[0]

        # Normalization
        emb1 /= LA.norm(emb1)
        emb2 /= LA.norm(emb2)
        cos_sim = np.sum(emb1 * emb2)

        # Result
        print('Cos_sim(face1, face2) =', cos_sim) 
Example #16
Source File: cos_tf.py    From advhat with MIT License 5 votes vote down vote up
def main(args):
        print(args)
        
        sess = tf.Session()
        
        # Embedding model
        with tf.gfile.GFile(args.model, "rb") as f:
                graph_def = tf.GraphDef()
                graph_def.ParseFromString(f.read())
        tf.import_graph_def(graph_def,
                                          input_map=None,
                                          return_elements=None,
                                          name="")
        image_input = tf.get_default_graph().get_tensor_by_name('image_input:0')
        keep_prob = tf.get_default_graph().get_tensor_by_name('keep_prob:0')
        is_train = tf.get_default_graph().get_tensor_by_name('training_mode:0')
        embedding = tf.get_default_graph().get_tensor_by_name('embedding:0')

        tfdict = {keep_prob:1.0, is_train:False}
        
        # Embedding calculation
        im1 = prep(rescale(io.imread(args.face1)/255.,112./600.,order=5))
        im2 = prep(rescale(io.imread(args.face2)/255.,112./600.,order=5))
        tfdict[image_input] = im1
        emb1 = sess.run(embedding,feed_dict=tfdict)
        tfdict[image_input] = im2
        emb2 = sess.run(embedding,feed_dict=tfdict)

        # Result
        cos_sim = np.sum(emb1 * emb2)
        print('Cos_sim(face1, face2) =', cos_sim) 
Example #17
Source File: enhancer.py    From ImageEnhancer with MIT License 5 votes vote down vote up
def _corrupt(self, source):
        """ corrupt the input with specific corruption method
            :param source: original data set
            :return: corrupted data set, if noise type is not defined, the original data set will return
        """
        if self.corrupt_type is None:
            return source
        noised = np.zeros((np.shape(source)[0],) + self.shape['in'])
        for i, raw in enumerate(source):
            if self.corrupt_type == 'GSN':
                noised[i] = random_noise(raw, 'gaussian', var=self.corrupt_ratio)
            elif self.corrupt_type == 'MSN':
                noised[i] = random_noise(raw, 'pepper', amount=self.corrupt_ratio)
            elif self.corrupt_type == 'SPN':
                noised[i] = random_noise(raw, 's&p', amount=self.corrupt_ratio)
            elif self.corrupt_type == 'GSB':
                noised[i] = gaussian(raw, sigma=self.corrupt_ratio, multichannel=True)
            elif self.corrupt_type == 'GRY':
                noised[i] = gray2rgb(rgb2gray(raw))
            elif self.corrupt_type == 'BLK':
                rad = int(self.corrupt_ratio)
                row = random.randint(rad, self.shape['in'][0] - rad - 1)
                col = random.randint(rad, self.shape['in'][1] - rad - 1)
                noised[i] = np.copy(raw)
                noised[i][circle(row, col, self.corrupt_ratio)] = 0
            elif self.corrupt_type == 'ZIP':
                noised[i] = rescale(raw, 0.5, mode='constant')
        return noised 
Example #18
Source File: style_transfer_client.py    From lambda-deep-learning-demo with Apache License 2.0 5 votes vote down vote up
def main():

  parser = argparse.ArgumentParser(
    formatter_class=argparse.ArgumentDefaultsHelpFormatter)

  parser.add_argument("--image_path",
                      help="path for image to run inference",
                      default="~/demo/data/mscoco_fns/val2014/COCO_val2014_000000301397.jpg")

  args = parser.parse_args()

  args.image_path = os.path.expanduser(args.image_path)

  # Read the image
  image = skimage.io.imread(args.image_path, plugin='imageio')
  image = rescale(image, 2.0, anti_aliasing=False)
  image = img_as_ubyte(image)

  data = json.dumps({"signature_name": "predict", "instances": image.tolist()})
  headers = {"content-type": "application/json"}

  response = requests.post(SERVER_URL, data=data, headers=headers)
  response.raise_for_status()

  predictions = np.squeeze(
    np.array(response.json()["predictions"]), axis=0)
  
  render_image = Image.fromarray(img_as_ubyte(predictions / 255.0), 'RGB')
  plt.imshow(render_image)
  plt.show() 
Example #19
Source File: utils.py    From DeepExplain with MIT License 5 votes vote down vote up
def plot(data, xi=None, cmap='RdBu_r', axis=plt, percentile=100, dilation=3.0, alpha=0.8):
    dx, dy = 0.05, 0.05
    xx = np.arange(0.0, data.shape[1], dx)
    yy = np.arange(0.0, data.shape[0], dy)
    xmin, xmax, ymin, ymax = np.amin(xx), np.amax(xx), np.amin(yy), np.amax(yy)
    extent = xmin, xmax, ymin, ymax
    cmap_xi = plt.get_cmap('Greys_r')
    cmap_xi.set_bad(alpha=0)
    overlay = None
    if xi is not None:
        # Compute edges (to overlay to heatmaps later)
        xi_greyscale = xi if len(xi.shape) == 2 else np.mean(xi, axis=-1)
        in_image_upscaled = transform.rescale(xi_greyscale, dilation, mode='constant')
        edges = feature.canny(in_image_upscaled).astype(float)
        edges[edges < 0.5] = np.nan
        edges[:5, :] = np.nan
        edges[-5:, :] = np.nan
        edges[:, :5] = np.nan
        edges[:, -5:] = np.nan
        overlay = edges

    abs_max = np.percentile(np.abs(data), percentile)
    abs_min = abs_max

    if len(data.shape) == 3:
        data = np.mean(data, 2)
    axis.imshow(data, extent=extent, interpolation='none', cmap=cmap, vmin=-abs_min, vmax=abs_max)
    if overlay is not None:
        axis.imshow(overlay, extent=extent, interpolation='none', cmap=cmap_xi, alpha=alpha)
    axis.axis('off')
    return axis 
Example #20
Source File: util.py    From SpaceNet_Off_Nadir_Solutions with Apache License 2.0 5 votes vote down vote up
def pansharpen(m, pan):
    #reference https://www.kaggle.com/resolut/panchromatic-sharpening
    # get m_bands
    rgbn = np.empty((m.shape[1], m.shape[2], 3))
    rgbn[:, :, 0] = m[7, :, :]  # ir2
    rgbn[:, :, 1] = m[6, :, :]  # ir1
    rgbn[:, :, 2] = m[4, :, :]  # red
    # scaled them
    rgbn_scaled = np.empty((m.shape[1] * 4, m.shape[2] * 4, 4))
    for i in range(3):
        img = rgbn[:, :, i]
        scaled = rescale(img, (4, 4))
        rgbn_scaled[:, :, i] = scaled
    # check size and crop for pan band
    if pan.shape[0] < rgbn_scaled.shape[0]:
        rgbn_scaled = rgbn_scaled[:pan.shape[0], :, :]
    else:
        pan = pan[:rgbn_scaled.shape[0], :]

    if pan.shape[1] < rgbn_scaled.shape[1]:
        rgbn_scaled = rgbn_scaled[:, :pan.shape[1], :]
    else:
        pan = pan[:, :rgbn_scaled.shape[1]]
    R = rgbn_scaled[:, :, 0]
    G = rgbn_scaled[:, :, 1]
    B = rgbn_scaled[:, :, 2]
    image = None
    all_in = R + G + B
    prod = np.multiply(all_in, pan)
    r = np.multiply(R, pan / all_in)[:, :, np.newaxis]
    g = np.multiply(G, pan / all_in)[:, :, np.newaxis]
    b = np.multiply(B, pan / all_in)[:, :, np.newaxis]
    image = np.concatenate([r, g, b], axis=2)
    return image

#bounding box strip for spacenet style output 
Example #21
Source File: transform.py    From brain-segmentation-pytorch with MIT License 5 votes vote down vote up
def __call__(self, sample):
        image, mask = sample

        img_size = image.shape[0]

        scale = np.random.uniform(low=1.0 - self.scale, high=1.0 + self.scale)

        image = rescale(
            image,
            (scale, scale),
            multichannel=True,
            preserve_range=True,
            mode="constant",
            anti_aliasing=False,
        )
        mask = rescale(
            mask,
            (scale, scale),
            order=0,
            multichannel=True,
            preserve_range=True,
            mode="constant",
            anti_aliasing=False,
        )

        if scale < 1.0:
            diff = (img_size - image.shape[0]) / 2.0
            padding = ((int(np.floor(diff)), int(np.ceil(diff))),) * 2 + ((0, 0),)
            image = np.pad(image, padding, mode="constant", constant_values=0)
            mask = np.pad(mask, padding, mode="constant", constant_values=0)
        else:
            x_min = (image.shape[0] - img_size) // 2
            x_max = x_min + img_size
            image = image[x_min:x_max, x_min:x_max, ...]
            mask = mask[x_min:x_max, x_min:x_max, ...]

        return image, mask 
Example #22
Source File: line_generator.py    From calamari with Apache License 2.0 5 votes vote down vote up
def draw(self, text, scale=1.0, spacing=0):
        if len(text) == 0:
            return np.zeros((0, 0), dtype=np.uint8)
        try:
            spacing = max(0, spacing)
            image = Image.new('L', tuple(np.add(self.font.getsize(text), [int(self.char_width * spacing * len(text) * 2), 0])), 255)
            draw = ImageDraw.Draw(image)
            if spacing == 0 or len(text.strip()) == 0:
                draw.text((0, 0), text, font=self.font)
                image = np.array(image)[self.offset:, :]

            else:
                x = 0
                for i, c in enumerate(text):
                    draw.text((x, 0), c, font=self.font)
                    w, h = self.font.getsize(c)
                    x += int(spacing * self.char_width + w)
                image = np.array(image)[self.offset:, :]

                sums = np.mean(image, axis=0)
                if np.mean(sums) >= 254:
                    # empty image
                    return np.zeros((0, 0), dtype=np.uint8)

                end = len(sums)
                while sums[end - 1] >= 254:
                    end -= 1
                image = image[:, :end]

            if scale != 1:
                image = rescale(image, float(scale), preserve_range=True)

            return image
        except Exception as e:
            print(e)
            print(text, spacing, scale, len(text.strip()))
            raise e 
Example #23
Source File: RT.py    From DEEPSEC with MIT License 5 votes vote down vote up
def randomization_transformation(self, samples=None, original_size=None, final_size=None):
        """

        :param samples:
        :param original_size:
        :param final_size:
        :return:
        """
        # Convert torch Tensor to numpy array
        if torch.is_tensor(samples) is True:
            samples = samples.cpu().numpy()
        # convert the channel of images
        samples = np.transpose(samples, (0, 2, 3, 1))
        assert samples.shape[-1] == 1 or samples.shape[-1] == 3, 'in the randomization transform function, channel must be placed in the last'

        transformed_samples = []
        # print ('transforming the images (size: {}) ...'.format(samples.shape))
        for image in samples:
            # Step 1: Random Resizing Layer
            # specify the random size which the image will be rescaled to
            rnd = np.random.randint(original_size, final_size)
            scale = (rnd * 1.0) / original_size
            rescaled_image = rescale(image=image, scale=scale, multichannel=True, preserve_range=True, mode='constant', anti_aliasing=False)

            # Step 2: Random Padding Layer
            h_rem = final_size - rnd
            w_rem = final_size - rnd
            pad_left = np.random.randint(0, w_rem)
            pad_right = w_rem - pad_left
            pad_top = np.random.randint(0, h_rem)
            pad_bottom = h_rem - pad_top
            # padding the image to the new size using gray pixels
            padded_image = np.pad(rescaled_image, ((pad_top, pad_bottom), (pad_left, pad_right), (0, 0)), 'constant', constant_values=0.5)
            transformed_samples.append(padded_image)

        # reset the channel location back and convert numpy back as the Tensor
        transformed_samples = np.array(transformed_samples)
        transformed_samples = torch.from_numpy(np.transpose(transformed_samples, (0, 3, 1, 2))).float().to(self.device)
        return transformed_samples 
Example #24
Source File: eimage.py    From gcr-catalogs with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_data(self, rebinning=None):
        data = FitsFile(self.path).data
        if rebinning is None:
            rebinning = self.default_rebinning
        if rebinning != 1:
            data = rescale(data, 1 / rebinning, mode='constant', preserve_range=True, multichannel=False, anti_aliasing=True)
        return data 
Example #25
Source File: image.py    From PynPoint with GNU General Public License v3.0 5 votes vote down vote up
def scale_image(image: np.ndarray,
                scaling_y: float,
                scaling_x: float) -> np.ndarray:
    """
    Function to spatially scale an image.

    Parameters
    ----------
    image : numpy.ndarray
        Input image (2D).
    scaling_y : float
        Scaling factor y.
    scaling_x : float
        Scaling factor x.

    Returns
    -------
    numpy.ndarray
        Shifted image (2D).
    """

    sum_before = np.sum(image)

    im_scale = rescale(image=np.asarray(image, dtype=np.float64),
                       scale=(scaling_y, scaling_x),
                       order=5,
                       mode='reflect',
                       anti_aliasing=True,
                       multichannel=False)

    sum_after = np.sum(im_scale)

    return im_scale * (sum_before / sum_after) 
Example #26
Source File: crappifiers.py    From PSSR with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def no_crap(img, scale=4, upsample=False):
    from skimage.transform import rescale
    x = np.array(img)
    multichannel = len(x.shape) > 2
    x = rescale(x, scale=1/scale, order=1, multichannel=multichannel)
    x *= np.iinfo(np.uint8).max
    return PIL.Image.fromarray(x.astype(np.uint8)) 
Example #27
Source File: image.py    From PassportEye with MIT License 5 votes vote down vote up
def __call__(self, img):
        scale_factor = self.max_width / float(img.shape[1])
        if scale_factor <= 1:
            img_small = transform.rescale(img, scale_factor, mode='constant', multichannel=False, anti_aliasing=True)
        else:
            scale_factor = 1.0
            img_small = img
        return img_small, scale_factor 
Example #28
Source File: image.py    From PassportEye with MIT License 5 votes vote down vote up
def _try_larger_image(self, roi, cur_text, cur_mrz, filter_order=3):
        """Attempts to improve the OCR result by scaling the image. If the new mrz is better, returns it, otherwise returns
        the old mrz."""
        if roi.shape[1] <= 700:
            scale_by = int(1050.0 / roi.shape[1] + 0.5)
            roi_lg = transform.rescale(roi, scale_by, order=filter_order, mode='constant', multichannel=False,
                                       anti_aliasing=True)
            new_text = ocr(roi_lg, extra_cmdline_params=self.extra_cmdline_params)
            new_mrz = MRZ.from_ocr(new_text)
            new_mrz.aux['method'] = 'rescaled(%d)' % filter_order
            if new_mrz.valid_score > cur_mrz.valid_score:
                cur_mrz = new_mrz
                cur_text = new_text
        return cur_text, cur_mrz 
Example #29
Source File: process_strip.py    From facade-segmentation with MIT License 5 votes vote down vote up
def split_tiles(image, shape, overlap=16):
    """ Rescale and split the input images to get several overlapping images of a given shape.

    *** The inpput must be CHANNELS FIRST ***

    The input image is rescaled so that height matches the output height.
    It is split into possibly overlapping tiles, each sized to match the output shape
    """
    # image_channels = image.shape[0]
    image_height = image.shape[-2]
    # image_width = image.shape[-1]
    output_height = shape[-2]
    output_width = shape[-1]

    # Rescale to match vertical size
    scale = output_height / float(image_height)
    scaled_image = rescale(image.transpose(1, 2, 0), (scale, scale), order=0, preserve_range=True).transpose(2, 0, 1)

    scaled_width = scaled_image.shape[-1]

    if scaled_width < output_width:
        padding = output_width - scaled_width

        if len(scaled_image.shape) == 3:
            scaled_image = np.pad(scaled_image, ((0, 0), (0, 0), (padding / 2, padding - padding / 2)), mode='constant')
        else:
            scaled_image = np.pad(scaled_image, ((0, 0), (padding / 2, padding - padding / 2)), mode='constant')

    # Since the input is not a multiple of the output width, we will evenly divide the image
    # to produce overlapping tiles. Work it out.
    #   -- The last tile always fits, and does not overlap with the _next_ tile (there is none)
    #   -- The remaining tiles each overlap with the following tile. The width of uncovered portion
    #      evenly divides the rest of the strip
    #   -- I need an integer number of tiles to cover the remaining strip (so I use a ceil)
    num_tiles = 1 + int(np.ceil(max(0, (scaled_width - output_width)) / float(output_width - overlap)))
    for x in np.linspace(0, scaled_width - output_width, num_tiles):
        yield scaled_image[:, :, int(x):int(x) + output_width] 
Example #30
Source File: visualize.py    From MOTSFusion with MIT License 5 votes vote down vote up
def visualize_sequence_3D(config, tracks, point_imgs, raw_imgs, ids=None):
    global_scene_visualization = {'points': [],
                                  'colors': []}
    colors = generate_colors()
    for step in range(len(raw_imgs)):
        if step % 2 == 0 or step == 0:
            point_img = rescale(point_imgs[step], 1 / 4, anti_aliasing=False)
            raw_img = rescale(raw_imgs[step], 1 / 4, anti_aliasing=True, preserve_range=True).astype(np.float)
            shape = (point_img.shape[0] * point_img.shape[1], point_img.shape[2])
            height_mask = np.where(point_img.reshape(shape)[:, 1] >= -2.5)
            global_scene_visualization['points'].extend(point_img.reshape(shape)[height_mask])
            global_scene_visualization['colors'].extend(raw_img.reshape(shape)[height_mask])

        for ref_id in tracks.get_active_tracks(step):
            if ids is not None:
                if not ref_id in ids:
                    continue

            if tracks.get_attribute(step, ref_id, 'global_3D_bbox') is not None:
                global_scene_visualization['points'].extend(
                    get_bbox_points_dense(config,
                                          tracks.get_attribute(step, ref_id, 'global_3D_bbox'),
                                          tracks.get_detection(step, ref_id)['class'])
                )
                color = np.asarray(colors[ref_id % len(colors)]) * 255
                global_scene_visualization['colors'].extend(np.tile(color, (1208, 1)))

    global_scene_visualization['points'] = np.asarray(global_scene_visualization['points'])
    global_scene_visualization['colors'] = np.asarray(global_scene_visualization['colors'])

    visualize(global_scene_visualization['points'], global_scene_visualization['colors'])