Ejemplo n.º 1
0
def resize(image,
           output_shape,
           order=1,
           mode='constant',
           cval=0,
           clip=True,
           preserve_range=False,
           anti_aliasing=False,
           anti_aliasing_sigma=None):
    '''A wrapper for Scikit-Image resize().
    Scikit-Image generates warnings on every call to resize() if it doesn't
    receive the right parameters. The right parameters depend on the version
    of skimage. This solves the problem by using different parameters per
    version. And it provides a central place to control resizing defaults.
    '''
    if LooseVersion(skimage.__version__) >= LooseVersion('0.14'):
        # New in 0.14: anti_aliasing. Default it to False for backward
        # compatibility with skimage 0.13.
        return skresize(image,
                        output_shape,
                        order=order,
                        mode=mode,
                        cval=cval,
                        clip=clip,
                        preserve_range=preserve_range,
                        anti_aliasing=anti_aliasing,
                        anti_aliasing_sigma=anti_aliasing_sigma)
    else:
        return skresize(image,
                        output_shape,
                        order=order,
                        mode=mode,
                        cval=cval,
                        clip=clip,
                        preserve_range=preserve_range)
Ejemplo n.º 2
0
def just_resize(im_fname, out_fname):
    """
    Since Lihui now gave me the correct stimuli, I'm just gonna resize the stimuli to 104x104
    """
    im = sio.imread(im_fname)
    res = skresize(im, (104, 104))
    sio.imsave(out_fname, res)
Ejemplo n.º 3
0
    def __call__(self, input_, clinic_, index=None, mode="gunho"):
        output = self.model(input_.to(self.device), clinic_.to(self.device))
        _, pred_idx = output.max(dim=0)
        self.backward(output)

        # idx : [-1] : last output, [0] : remove batch dim
        feature_map = self.feature[-1]
        grad = self.grad[-1]
        if mode == "gunho":
            gcam = self.gunho(grad, feature_map)
        elif mode == "gdcam":
            gcam = self.gdcam(grad, feature_map)
        elif mode == "gdcampp":
            gcam = self.gdcampp(grad, feature_map)
        elif mode == "respond":
            gcam = self.respondcam(grad, feature_map)

        gcam -= gcam.min()
        gcam /= gcam.max()
        resize_shape = np.array([1, input_.shape[-2], input_.shape[-1]])
        gcam = skresize(gcam, resize_shape, mode="constant")
        gcam = gcam - np.min(gcam)
        gcam = gcam / np.max(gcam)
        gcam = np.float32(gcam)
        gcam = np.squeeze(gcam)
        return gcam, pred_idx
Ejemplo n.º 4
0
    def _np_resize_image(image, size, dtype='int'):
        """Resize image for np.array

        NOTE:
            * Resized result from cv2 and skimage is different. Just use a workaround to resize image in floating type.

        Args:
            image (np.array): image array
            size (tuple): input size of model
            dtype (str, optional): data type of image. Defaults to 'int'.

        Raises:
            NotImplementedError: dtype is allowed 'int' or 'float' only.

        Returns:
            np.array: resized image
        """
        if dtype == 'int':
            _size = (size[1], size[0])  # (H,W) to (W,H)
            return cv2.resize(image.astype('uint8'),
                              _size,
                              interpolation=cv2.INTER_LINEAR)
        elif dtype == 'float':
            return skresize(image,
                            size,
                            order=0,
                            mode='constant',
                            preserve_range=True)
        else:
            raise NotImplementedError(f"'{dtype}' is not a valid dtype.")
Ejemplo n.º 5
0
 def _resize(gcam):
     gcam -= gcam.min()
     gcam /= gcam.max()
     gcam = skresize(gcam, input_.shape[-3:], mode="constant")
     gcam -= gcam.min()
     gcam /= gcam.max()
     return gcam
Ejemplo n.º 6
0
    def __init__(self, arg):
        super(Posterize, self).__init__()
        self.inp_img = arg.inp_img
        self.img = imio.imread(self.inp_img).astype(np.float32)
        if len(self.img.shape) == 3:
            self.img = rgb2gray(self.img)
        self.alpha = 0.1
        self.till_conv = arg.till_conv
        self.max_iter = arg.max_iter
        self.gauss_filt = arg.gauss_filt
        self.sigma = arg.sigma

        self.a0 = 512
        self.a1 = int((512. * self.img.shape[1]) / float(self.img.shape[0]))
        self.img = skresize(self.img, (self.a0, self.a1),
                            mode='constant',
                            anti_aliasing=True)

        # colours lighter to stronger
        self.c1 = (245, 255, 201)  # light colour
        self.c2 = (86, 151, 163)  # medium colour
        self.c3 = (161, 30, 34)  # dark colour
        # modify the above tuples using an RGB chart to get your custom colours

        self.th = np.array([100., 200.])
        self.inc = np.array([[0., -1.], [0., 0.], [0., 1.], [-1., -1.],
                             [-1., 0.], [-1, 1.], [1., -1], [1., 0.], [1.,
                                                                       1.]])
Ejemplo n.º 7
0
 def get_np_array(self, frame):
     if self.numpy_size and (self.numpy_size[0] != self.capture_size[1]
                             or self.numpy_size[1] != self.capture_size[0]):
         # HxWxD
         return skresize(frame,
                         self.numpy_size,
                         mode='reflect',
                         anti_aliasing=False)
     return frame
Ejemplo n.º 8
0
def _resize_image(image: np.ndarray,
                  x_side_size: float = None,
                  y_side_size: float = None):
    if image.shape[0] != x_side_size or image.shape[1] != y_side_size:
        return skresize(image, (x_side_size, y_side_size),
                        order=RESIZE_ORDER,
                        mode=RESIZE_MODE,
                        preserve_range=PRESERVE_RANGE)
    else:
        return image
Ejemplo n.º 9
0
def get_LaplaceDetail(laplace_pyr, detail=2, dim=[480, 640]):
    """
     Provide Laplacian Pyramid of image to extract details from. 
     dim; shape of the image size you wish. (typically the 
          shape of the image you'll be using it in.)
     Using skimage for resizing. 
     detail = which detail space. 
     # assuming skimage - resize. 
     """
    return skresize(laplace_pyr[detail], dim), laplace_pyr[detail].shape
Ejemplo n.º 10
0
 def get_jpeg(self, frame):
     if frame is not None:
         if self.jpeg_size and self.jpeg_size != self.capture_size:
             frame = skresize(frame, (self[1], self.jpeg_size[0]),
                              mode='reflect',
                              anti_aliasing=False)
         tmpfile = BytesIO()
         img = self.Image.fromarray(frame)
         img.save(tmpfile, format='jpeg')
         return tmpfile.getvalue()
Ejemplo n.º 11
0
    def classify_cell(self, fluor, optional, microscope):

        fluor_img = skresize(self.preprocess_image(fluor, microscope),
                             (100, 100),
                             order=0,
                             preserve_range=True,
                             anti_aliasing=False,
                             anti_aliasing_sigma=None)

        optional_img = skresize(self.preprocess_image(optional, microscope),
                                (100, 100),
                                order=0,
                                preserve_range=True,
                                anti_aliasing=False,
                                anti_aliasing_sigma=None)

        pred = self.model.predict_classes(np.concatenate((fluor_img, optional_img), axis=1).reshape(-1, 100, 200, 1))

        return pred[0] + 1
Ejemplo n.º 12
0
    def multi_scaled_imgs(self, imgs, flip_size, ch, cw, scales):
        len_scales = len(scales)

        ms_mask = np.zeros((flip_size, self.n_class, ch, cw))
        for scale in range(0, len_scales):
            scaling = nn.Upsample(scale_factor=scales[scale], mode='nearest')
            scaled_imgs = scaling(imgs)  # 8, 3, 1024,1024
            out = self.net.forward(
                scaled_imgs).cpu().data.numpy()  # [8, C, 1024, 1024]
            # downsampling
            scaled_size = [self.n_class, ch, cw]

            for fs in range(flip_size):
                scaled_mask = skresize(out[fs], scaled_size)
                scaled_mask = np.divide(scaled_mask, 255)
                ms_mask[fs] = ms_mask[fs] + scaled_mask

        return ms_mask  # [8, C, 1024, 1024]
Ejemplo n.º 13
0
def overfeat_preprocess(img, resize):
    # Ensure single-precision
    img = img
    # Crop and resize image
    h0, w0 = img.shape[:2]
    # Compute crop indices
    d0 = min(h0, w0)
    hc = round((h0 - d0) / 2.)
    wc = round((w0 - d0) / 2.)
    # Center crop image (ensure 3 channels...)
    img = img[int(hc):int(hc + d0), int(wc):int(wc + d0), :]
    # Resize image
    img = skresize(img, (resize, resize),
                   mode='constant',
                   preserve_range=True,
                   order=1).astype(np.float32)
    # Change channel order: h,w,c -> c,h,w
    img = np.rollaxis(img, 2, 0)
    return img
def width_normalize(dataset, reshape_sizes, output_size):
    dataset = dataset.astype('float32')
    dataset /= 255
    output_dataset = {}
    for this_shape in tqdm(reshape_sizes):
        w = this_shape if this_shape != 0 else dataset.shape[2]
        h = dataset.shape[1]
        new_dataset = []
        for d in tqdm(dataset):
            nd = skresize(d[0], (h, w))
            padding = output_size - w
            if padding > 0:
                left_padding = round(padding / 2)
                right_padding = padding - left_padding
                nd = np.pad(nd, ((0, output_size - dataset.shape[1]),
                                 (left_padding, right_padding)),
                            mode='constant')

            new_dataset.append(nd.reshape(output_size, output_size, 1))
        output_dataset[w] = dataset
    return output_dataset
Ejemplo n.º 15
0
def crop_im(im_fname, out_fname, downsample=True):
    """
    DEPRECATED since Lihui gave me the correct stimuli.
    Load image, remove white margin, and save to file.
    Use .png extension to avoid compression.
    """
    im = sio.imread(im_fname)
    im_first = im[:, :, 0]  # first frame
    wval = im_first[0, 0]  # value of top left pixel (should be white)
    # mask and coordinates of non-white pixel
    immask = im_first != wval
    imcoords = np.argwhere(immask)
    # creat bounding box to crop
    x0, y0 = imcoords.min(axis=0)
    x1, y1 = imcoords.max(axis=0) + 1  # slices are exclusive at the top
    # crop and save
    cropped = im_first[x0:x1, y0:y1]

    if downsample:
        cropped = skresize(cropped, (104, 104))

    sio.imsave(out_fname, cropped)
Ejemplo n.º 16
0
 def get(self, start_id, end_id=None):
   """return 4-D blob of frames
   note: start_id and end_id are effective frame ids"""
   if end_id is None: end_id = start_id + 1
   real_ids = self.valid_fid[start_id:end_id]
   
   if self.fid > real_ids[0]: #read pass this point already, need to reset
     self.setup(self.path)
   imgs =[]
   for i in range(self.fid, real_ids[-1]+1):
     if self.vreader == 0:  # skvideo
       img = next(self.vid)
     elif self.vreader == 1: # opencv
       res, img = self.cap.read()
       assert res, 'Error. OpenCV unable to read frame #%d in %s' % (i, self.path)
       img = img[:,:,::-1]
     if i in real_ids:
       imgs.append(img)
   self.fid = real_ids[-1] + 1
   
   if self.out_shape is not None:
     imgs = [skresize(im, self.out_shape, anti_aliasing=True,mode='reflect') for im in imgs]
   return np.uint8(np.array(imgs)*255)
Ejemplo n.º 17
0
    def to_image(self,
                 shape=None,
                 channels=1,
                 colormap=None,
                 invert=False,
                 return_type="pil"):
        """Create an image from spectrogram (array, tensor, or PIL.Image)

        Linearly rescales values in the spectrogram from
        self.decibel_limits to [0,255] (PIL.Image) or [0,1] (array/tensor)

        Default of self.decibel_limits on load is [-100, -20], so, e.g.,
        -20 db is loudest -> black, -100 db is quietest -> white

        Args:
            shape: tuple of output dimensions as (height, width)
                - if None, retains original shape of self.spectrogram
            channels: eg 3 for rgb, 1 for greyscale
                - must be 3 to use colormap
            colormap:
                if None, greyscale spectrogram is generated
                Can be any matplotlib colormap name such as 'jet'
            return_type: type of returned object
                - 'pil': PIL.Image
                - 'np': numpy.ndarray
                - 'torch': torch.tensor
        Returns:
            Image/array with type depending on `return_type`:
            - PIL.Image with c channels and shape w,h given by `shape`
                and values in [0,255]
            - np.ndarray with shape [c,h,w] and values in [0,1]
            - or torch.tensor with shape [c,h,w] and values in [0,1]
        """
        from skimage.transform import resize as skresize

        assert return_type in [
            "pil",
            "np",
            "torch",
        ], f"Arg `return_type` must be one of 'pil', 'np', 'torch'. Got {return_type}."
        if colormap is not None:
            # it doesn't make sense to use a colormap with #channels != 3
            assert (
                channels == 3
            ), f"Channels must be 3 to use colormap. Specified {channels}"

        # rescale spec_range to [1, 0]
        # note the low values represent silence, so a silent img would be black
        # if plotted directly from these values.
        array = linear_scale(self.spectrogram,
                             in_range=self.decibel_limits,
                             out_range=(0, 1))
        # flip so that frequency increases from bottom to top
        array = array[::-1, :]

        # invert if desired
        if invert:
            array = 1 - array

        # apply colormaps
        if colormap is not None:  # apply a colormap to get RGB channels
            cm = get_cmap(colormap)
            array = cm(array)

        # resize and change channel dims
        if shape is None:
            shape = np.shape(array)
        out_shape = [shape[0], shape[1], channels]
        array = skresize(array, out_shape)

        if return_type == "pil":  # expected shape of input is [h,w,c]
            from PIL import Image

            # use correct type for img, and scale from 0-1 to 0-255
            array = np.uint8(array * 255)
            if array.shape[-1] == 1:
                # PIL doesnt like [x,y,1] shape, wants [x,y] instead
                array = array[:, :, 0]
            image = Image.fromarray(array)

        elif return_type == "np":  # shape should be c,h,w
            image = array.transpose(2, 0, 1)

        elif return_type == "torch":  # shape should be c,h,w
            import torch

            image = torch.Tensor(array.transpose(2, 0, 1))

        return image
Ejemplo n.º 18
0
 def resize(image, shape):
     from skimage.transform import resize as skresize
     return skresize(image, shape, preserve_range=True,
                     mode='constant').astype(np.uint8)
Ejemplo n.º 19
0
def resize(image,scale):
    image = skresize(image, (int(scale*float(image.shape[0])), 
                            int(scale*float(image.shape[1]))))
    return image
Ejemplo n.º 20
0
 def resize_and_scale(img, size, scale):
     img = skresize(img, size)
     return 1 - (np.array(img, "float32") / scale)