Python skimage.transform 模块,rescale() 实例源码

我们从Python开源项目中,提取了以下23个代码示例,用于说明如何使用skimage.transform.rescale()

项目:chainer-spatial-transformer-networks    作者:hvy    | 项目源码 | 文件源码
def transform_mnist_rts(in_data):
    img, label = in_data
    img = img[0]  # Remove channel axis for skimage manipulation

    # Rotate
    img = transform.rotate(img, angle=np.random.uniform(-45, 45),
                           resize=True, mode='constant')
    #  Scale
    img = transform.rescale(img, scale=np.random.uniform(0.7, 1.2),
                            mode='constant')

    # Translate
    h, w = img.shape
    if h >= img_size[0] or w >= img_size[1]:
        img = transform.resize(img, output_shape=img_size, mode='constant')
        img = img.astype(np.float32)
    else:
        img_canvas = np.zeros(img_size, dtype=np.float32)
        ymin = np.random.randint(0, img_size[0] - h)
        xmin = np.random.randint(0, img_size[1] - w)
        img_canvas[ymin:ymin+h, xmin:xmin+w] = img
        img = img_canvas

    img = img[np.newaxis, :]  # Add the bach channel back
    return img, label
项目:SLAM    作者:sanjeevkumar42    | 项目源码 | 文件源码
def read_rgb_image(filepath):
    rgb_img = ndimage.imread(filepath)
    width = height = 224
    img_width = rgb_img.shape[1]
    img_height = rgb_img.shape[0]

    # scale such that smaller dimension is 256
    if img_width < img_height:
        factor = 256.0 / img_width
    else:
        factor = 256.0 / img_height
    rgb_img = transform.rescale(rgb_img, factor, preserve_range=True)

    # crop randomly
    width_start = np.random.randint(0, rgb_img.shape[1] - width)
    height_start = np.random.randint(0, rgb_img.shape[0] - height)

    rgb_img = rgb_img[height_start:height_start + height, width_start:width_start + width]
    return rgb_img
项目:segmentation    作者:zengyu714    | 项目源码 | 文件源码
def _rotate_and_rescale(xs, ys):
    """Rotate images and labels and scale image and labels by a certain factor.
    Both need to swap axis from [depth, height, width] to [height, width, depth]
    required by skimage.transform library.
    """

    degree = np.int(np.random.uniform(low=-3, high=5))
    factor = np.random.uniform(low=0.9, high=1.1)
    # swap axis
    HWC_xs, HWC_ys = [np.transpose(item, [1, 2, 0]) for item in [xs, ys]]
    # rotate and rescale
    HWC_xs, HWC_ys = [rotate(item, degree, mode='symmetric', preserve_range=True) for item in [HWC_xs, HWC_ys]]
    HWC_xs, HWC_ys = [rescale(item, factor, mode='symmetric', preserve_range=True) for item in [HWC_xs, HWC_ys]]
    # swap back
    xs, ys = [np.transpose(item, [2, 0, 1]) for item in [HWC_xs, HWC_ys]]
    return xs, ys
项目:Semantic-Segmentation-using-Adversarial-Networks    作者:oyam    | 项目源码 | 文件源码
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
# -----------------------------------------------------------------------------
项目:saliency-bms    作者:fzliu    | 项目源码 | 文件源码
def main(args):
    """
        Entry point.
    """

    # load the image
    img = imread(args.input)
    if img.ndim == 2:
        img = gray2rgb(img)
    elif img.shape[2] == 4:
        img = img[:, :, :3]
    upper_dim = max(img.shape[:2])
    if upper_dim > args.max_dim:
        img = rescale(img, args.max_dim/float(upper_dim), order=3) 

    # compute saliency
    start = timeit.default_timer()
    img_sal = compute_saliency(img)
    runtime = timeit.default_timer() - start
    print("Took {0} seconds.".format(runtime))

    # save image
    (fname, ext) = os.path.splitext(args.input)
    out_path = fname + "_saliency" + ext
    imsave(out_path, img_sal)
项目:ternarynet    作者:czhu95    | 项目源码 | 文件源码
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)
项目:tanda    作者:HazyResearch    | 项目源码 | 文件源码
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)
项目:facade-segmentation    作者:jfemiani    | 项目源码 | 文件源码
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]
项目:Nature-Conservancy-Fish-Image-Prediction    作者:Brok-Bucholtz    | 项目源码 | 文件源码
def rand_scale(image):
    scale = choice(np.arange(0.5, 0.9, 0.1))
    return rescale(image, scale, preserve_range=True).astype(np.uint8)
项目:modl    作者:arthurmensch    | 项目源码 | 文件源码
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':
        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')
项目:imgProcessor    作者:radjkarl    | 项目源码 | 文件源码
def _firstImg(self, img):

        if self.scale_factor is None:
            # determine so that smaller image size has 50 px
            self.scale_factor = 100 / min(img.shape)
        img = rescale(img, self.scale_factor)

        self._m = MaskedMovingAverage(shape=img.shape)
        if self.ksize is None:
            self.ksize = max(3, int(min(img.shape) / 10))
        self._first = False
        return img
项目:DQN    作者:pekaalto    | 项目源码 | 文件源码
def process_image(img):
        return 2 * color.rgb2gray(transform.rescale(img[34:194], 0.5)) - 1
项目:deepdream-neural-style-transfer    作者:rdcolema    | 项目源码 | 文件源码
def _rescale_net(self, img):
        """
            Rescales the network to fit a particular image.
        """

        # get new dimensions and rescale net + transformer
        new_dims = (1, img.shape[2]) + img.shape[:2]
        self.net.blobs["data"].reshape(*new_dims)
        self.transformer.inputs["data"] = new_dims
项目:Sign-Language-Recognition    作者:achyudhk    | 项目源码 | 文件源码
def gen_imagescale(image, start_size=90, end_size=140, step_size=10, division_factor=120.0):
    for i in range(start_size,end_size,step_size):
        scale = i/division_factor
        yield rescale(image,(scale,scale)), scale
项目:Sign-Language-Recognition    作者:achyudhk    | 项目源码 | 文件源码
def gen_imagescale(image, start_size=90, end_size=140, step_size=10, division_factor=120.0):
    for i in range(start_size,end_size,step_size):
        scale = i/division_factor
        yield rescale(image,(scale,scale)),scale
项目:PyDLSSVM    作者:djosix    | 项目源码 | 文件源码
def rescale_frame(frame, scale):
    if scale == 1:
        return frame
    return rescale(frame, scale)
项目:PyDLSSVM    作者:djosix    | 项目源码 | 文件源码
def __init__(self, frames, rect, config=None):
        """Track a target form frames."""
        self.frame_id = 0
        self.frames = frames
        self.config = config or DEFAULT_CONFIG
        self.scale = self.config["rescale"]
        self.target = rescale_rect(rect, self.scale)
项目:PyDLSSVM    作者:djosix    | 项目源码 | 文件源码
def __init__(self, config):
        self.config = config or DEFAULT_CONFIG
        self.scale = self.config["rescale"]
项目:PassportEye    作者:konstantint    | 项目源码 | 文件源码
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')
        else:
            scale_factor = 1.0
            img_small = img
        return img_small, scale_factor
项目:PassportEye    作者:konstantint    | 项目源码 | 文件源码
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')
            new_text = ocr(roi_lg)
            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
项目:opensat    作者:yarynam    | 项目源码 | 文件源码
def _rescale(self, bands):
        """ Rescale bands """
        # self.output("Rescaling", normal=True, arrow=True)

        for key, band in enumerate(bands):
            print "processing"
            # self.output("band %s" % self.bands[key], normal=True, color='green', indent=1)
            bands[key] = sktransform.rescale(band, 2)
            bands[key] = (bands[key] * 65535).astype('uint16')

        return bands
项目:imgProcessor    作者:radjkarl    | 项目源码 | 文件源码
def addImg(self, i):
        img = self._read(i)

        if self._first:
            img = self._firstImg(img)
        elif self.scale_factor != 1:
            img = rescale(img, self.scale_factor)
        try:
            f = FitHistogramPeaks(img)
        except AssertionError:
            return
        #sp = getSignalPeak(f.fitParams)
        mn = getSignalMinimum(f.fitParams)
        # non-backround indices:
        ind = img > mn  # sp[1] - self.nstd * sp[2]
        # blur:
        # blurred = minimum_filter(img, 3)#remove artefacts
        #blurred = maximum_filter(blurred, self.ksize)
#         blurred = img
#         gblurred = gaussian_filter(img, self.ksize)
#         ind = minimum_filter(ind, self.ksize)
        nind = np.logical_not(ind)
        gblurred = maskedFilter(img, nind, ksize=2 * self.ksize,
                                fill_mask=False,
                                fn="mean")

        #blurred[ind] = gblurred[ind]
        # scale [0-1]:
        mn = img[nind].mean()
        if np.isnan(mn):
            mn = 0
        mx = gblurred[ind].max()
        gblurred -= mn
        gblurred /= (mx - mn)
#         img -= mn
#         img /= (mx - mn)
#         ind = np.logical_and(ind, img > self._m.avg)

        self._m.update(gblurred, ind)
        self.bglevel.append(mn)
        self._mx += mx

        self._n += 1

#         import pylab as plt
#         plt.imshow(self._m.avg)
#         plt.show()
项目:kaggle-satellite-imagery-feature-detection    作者:toshi-k    | 项目源码 | 文件源码
def img_to_wkt(img, scale_rate, target_name, W, H, Xmax, Ymin, t_value, cla):

    W_dash = W * W / (W + 1)
    H_dash = H * H / (H + 1)

    if scale_rate < 0.99:
        img_tiny = rescale(img, scale_rate)
    else:
        img_tiny = img

    bmp_image_path = '_temp/' + target_name + '.bmp'
    target_json_path = '_temp/' + target_name + '.json'

    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        imsave(bmp_image_path, img_tiny)

    os.system('potrace -a 2 -t ' + str(t_value) + ' -b geojson -i ' + bmp_image_path + ' -o ' + target_json_path)

    f = open(target_json_path)
    data = json.load(f)
    f.close()

    os.remove(target_json_path)
    os.remove(bmp_image_path)

    # type of 'data' is feature collection 
    # we only need focus on features
    features = data['features']

    list_polygons = list()

    for i in range(len(features)):

        shapely_polygon = shape(geojson.loads(json.dumps(features[i]['geometry'])))

        if scale_rate < 0.99:
            shapely_polygon = scale(shapely_polygon, 1/scale_rate, 1/scale_rate, origin=(0, 0))

        list_polygons.append(shapely_polygon.buffer(0.0))

    multi = MultiPolygon(list_polygons)

    multi = scale(multi, 1, -1, 1, origin=(float(W)/2, float(H)/2))
    multi = scale(multi, Xmax / W_dash, Ymin / H_dash, origin=(0, 0))

    if cla != 6:
        multi = multi.simplify(1e-6, preserve_topology=True)
    else:
        multi = multi.simplify(1e-5, preserve_topology=True)

    multi = multi.buffer(0)

    if multi.type == 'Polygon':
        multi = MultiPolygon([multi])

    return multi

# tpex's evaluation code can validate topology more strictly
# https://github.com/cxz/tpex