Python cv2.INTER_NEAREST Examples

The following are 30 code examples of cv2.INTER_NEAREST(). 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 cv2 , or try the search function .
Example #1
Source File: video_transforms.py    From DDPAE-video-prediction with MIT License 7 votes vote down vote up
def resize(video, size, interpolation):
  if interpolation == 'bilinear':
    inter = cv2.INTER_LINEAR
  elif interpolation == 'nearest':
    inter = cv2.INTER_NEAREST
  else:
    raise NotImplementedError

  shape = video.shape[:-3]
  video = video.reshape((-1, *video.shape[-3:]))
  resized_video = np.zeros((video.shape[0], size[1], size[0], video.shape[-1]))
  for i in range(video.shape[0]):
    img = cv2.resize(video[i], size, inter)
    if len(img.shape) == 2:
      img = img[:, :, np.newaxis]
    resized_video[i] = img
  return resized_video.reshape((*shape, size[1], size[0], video.shape[-1])) 
Example #2
Source File: resize.py    From chainer-compiler with MIT License 7 votes vote down vote up
def _resize_cv2(img, size, interpolation):
    img = img.transpose((1, 2, 0))
    if interpolation == PIL.Image.NEAREST:
        cv_interpolation = cv2.INTER_NEAREST
    elif interpolation == PIL.Image.BILINEAR:
        cv_interpolation = cv2.INTER_LINEAR
    elif interpolation == PIL.Image.BICUBIC:
        cv_interpolation = cv2.INTER_CUBIC
    elif interpolation == PIL.Image.LANCZOS:
        cv_interpolation = cv2.INTER_LANCZOS4
    H, W = size
    img = cv2.resize(img, dsize=(W, H), interpolation=cv_interpolation)

    # If input is a grayscale image, cv2 returns a two-dimentional array.
    if len(img.shape) == 2:
        img = img[:, :, np.newaxis]
    return img.transpose((2, 0, 1)) 
Example #3
Source File: utils.py    From progressive_growing_of_GANs with MIT License 6 votes vote down vote up
def grid_batch_images(self, images):
        n, h, w, c = images.shape
        a = int(math.floor(np.sqrt(n)))
        # images = (((images - images.min()) * 255) / (images.max() - images.min())).astype(np.uint8)
        images = images.astype(np.uint8)
        images_in_square = np.reshape(images[:a * a], (a, a, h, w, c))
        new_img = np.zeros((h * a, w * a, c), dtype=np.uint8)
        for col_i, col_images in enumerate(images_in_square):
            for row_i, image in enumerate(col_images):
                new_img[col_i * h: (1 + col_i) * h, row_i * w: (1 + row_i) * w] = image
        resolution = self.cfg.resolution
        if self.cfg.resolution != h:
            scale = resolution / h
            new_img = cv2.resize(new_img, None, fx=scale, fy=scale,
                                 interpolation=cv2.INTER_NEAREST)
        return new_img 
Example #4
Source File: h5_test.py    From keras-image-segmentation with MIT License 6 votes vote down vote up
def write_data(h5py_file, mode, x_paths, y_paths):
    num_data = len(x_paths)

    uint8_dt = h5py.special_dtype(vlen=np.uint8)
    string_dt = h5py.special_dtype(vlen=str)

    group = h5py_file.create_group(mode)
    h5_name = group.create_dataset('name', shape=(num_data,), dtype=string_dt)
    h5_image = group.create_dataset('image', shape=(num_data,), dtype=uint8_dt)
    h5_label = group.create_dataset('label', shape=(num_data,), dtype=uint8_dt)

    h5_image.attrs['size'] = [256,512,3]
    h5_label.attrs['size'] = [256,512,1]

    for i in range(num_data):
        x_img = cv2.imread(x_paths[i], 1)
        y_img = cv2.imread(y_paths[i], 0)
        x_img = cv2.resize(x_img, None, fx=0.25, fy=0.25, interpolation=cv2.INTER_LINEAR)
        y_img = cv2.resize(y_img, None, fx=0.25, fy=0.25, interpolation=cv2.INTER_NEAREST)

        h5_image[i] = x_img.flatten()
        h5_label[i] = y_img.flatten()
        h5_name[i] = os.path.basename(x_paths[i])

        # break 
Example #5
Source File: h5_test.py    From keras-image-segmentation with MIT License 6 votes vote down vote up
def image_copy_to_dir(mode, x_paths, y_paths):
    target_path = '/run/media/tkwoo/myWorkspace/workspace/01.dataset/03.Mask_data/cityscape'
    target_path = os.path.join(target_path, mode)

    for idx in trange(len(x_paths)):
        image = cv2.imread(x_paths[idx], 1)
        mask = cv2.imread(y_paths[idx], 0)

        image = cv2.resize(image, None, fx=0.25, fy=0.25, interpolation=cv2.INTER_LINEAR)
        mask = cv2.resize(mask, None, fx=0.25, fy=0.25, interpolation=cv2.INTER_NEAREST)

        cv2.imwrite(os.path.join(target_path, 'image', os.path.basename(x_paths[idx])), image)
        cv2.imwrite(os.path.join(target_path, 'mask', os.path.basename(y_paths[idx])), mask)

        # show = image.copy()
        # mask = (mask.astype(np.float32)*255/33).astype(np.uint8)
        # mask_color = cv2.applyColorMap(mask, cv2.COLORMAP_JET)
        # show = cv2.addWeighted(show, 0.5, mask_color, 0.5, 0.0)
        # cv2.imshow('show', show)
        # key = cv2.waitKey(1)
        # if key == 27:
        #     return 
Example #6
Source File: dataloader_thinning.py    From Pytorch-Instance-Lane-Segmentation with MIT License 6 votes vote down vote up
def __getitem__(self, index):
        path, label, h_label = self.imgs[index]
        path = os.path.join(self.dir_path, path)
        img = cv2.imread(path).astype(np.float32)
        img = img[:,:,:3]
        img = cv2.resize(img, (self.width, self.height))
        img -= [104, 117, 123]
        img = img.transpose(2, 0, 1)
        gt = cv2.imread(label,-1)
        gt = cv2.resize(gt, (self.label_width, self.label_height), interpolation = cv2.INTER_NEAREST)  
        if len(gt.shape) == 3:
            gt = gt[:,:,0]
        thining_gt = cv2.imread(h_label,-1)
        gt_num_list = list(np.unique(gt))
        gt_num_list.remove(0)
        target_ins = np.zeros((4, gt.shape[0],gt.shape[1])).astype('uint8')
        for index, ins in enumerate(gt_num_list):
            target_ins[index,:,:] += (gt==ins)
        return img, target_ins, len(gt_num_list), thining_gt 
Example #7
Source File: custom_transforms.py    From MaskTrack with MIT License 6 votes vote down vote up
def __call__(self, sample):

        # Fixed range of scales
        sc = self.scales[random.randint(0, len(self.scales) - 1)]

        for elem in sample.keys():
            if 'fname' in elem:
                continue
            tmp = sample[elem]

            if tmp.ndim == 2:
                flagval = cv2.INTER_NEAREST
            else:
                flagval = cv2.INTER_CUBIC

            tmp = cv2.resize(tmp, None, fx=sc, fy=sc, interpolation=flagval)

            sample[elem] = tmp

        return sample 
Example #8
Source File: dataloader_parallel.py    From Pytorch-Instance-Lane-Segmentation with MIT License 6 votes vote down vote up
def __getitem__(self, index):
        path, label= self.imgs[index]
        path = os.path.join(self.dir_path, path)
        img = cv2.imread(path).astype(np.float32)
        img = img[:,:,:3]
        img = cv2.resize(img, (self.width, self.height))
        img -= [104, 117, 123]
        img = img.transpose(2, 0, 1)
        gt = cv2.imread(label,-1)
        gt = cv2.resize(gt, (self.label_width, self.label_height), interpolation = cv2.INTER_NEAREST)  
        if len(gt.shape) == 3:
            gt = gt[:,:,0]
        gt_num_list = list(np.unique(gt))
        gt_num_list.remove(0)
        target_ins = np.zeros((4, gt.shape[0],gt.shape[1])).astype('uint8')
        for index, ins in enumerate(gt_num_list):
            target_ins[index,:,:] += (gt==ins)
        return img, target_ins, len(gt_num_list), gt 
Example #9
Source File: lucidDream.py    From pyLucid with MIT License 6 votes vote down vote up
def spline_transform_multi(img, mask):
    bimask=mask>0
    M,N=np.where(bimask)
    w=np.ptp(N)+1
    h=np.ptp(M)+1
    kernel=cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))
    bound=cv2.dilate(bimask.astype('uint8'),kernel)-bimask
    y,x=np.where(bound>0)

    if x.size>4:
        newxy=thin_plate_transform(x,y,w,h,mask.shape[:2],num_points=5)

        new_img=cv2.remap(img,newxy,None,cv2.INTER_LINEAR)
        new_msk=cv2.remap(mask,newxy,None,cv2.INTER_NEAREST)
    elif x.size>0:
        new_img=img
        new_msk=mask
    return new_img,new_msk 
Example #10
Source File: sal_data_layer.py    From DSS with MIT License 6 votes vote down vote up
def load_label(self, idx):
        """
        Load label image as 1 x height x width integer array of label indices.
        The leading singleton dimension is required by the loss.
        """
        im = Image.open(self.data_root + self.label_lst[idx])
        label = np.array(im) / 255#cv2.imread(self.data_root + self.label_lst[idx], 0) / 255
        #if self.scales != None:
        #    label = cv2.resize(label, None, None, fx=self.scales[self.scale_ind], fy=self.scales[self.scale_ind], \
        #            interpolation=cv2.INTER_NEAREST)
        #height, width = label.shape[:2]
        #h_off = self.crop_size - height
        #w_off = self.crop_size - width
        #label = cv2.copyMakeBorder(label, 0, max(0, h_off), 0, max(0, w_off), cv2.BORDER_CONSTANT, value=[-1,])
        #label = label[self.h_off:self.h_off+self.height, self.w_off:self.w_off+self.width]
        label = label[np.newaxis, ...]
        if self.flip == 1:
            label = label[:,:,::-1]
        return label 
Example #11
Source File: sal_data_layer.py    From DSS with MIT License 6 votes vote down vote up
def load_region(self, idx):
        """
        Load label image as 1 x height x width integer array of label indices.
        The leading singleton dimension is required by the loss.
        """
        im = Image.open(self.data_root + self.region_lst[idx])
        region = np.array(im, dtype=np.float32) / 15.0
        #print np.unique(region)
        #if self.scales != None:
        #    label = cv2.resize(label, None, None, fx=self.scales[self.scale_ind], fy=self.scales[self.scale_ind], \
        #            interpolation=cv2.INTER_NEAREST)
        #height, width = label.shape[:2]
        #h_off = self.crop_size - height
        #w_off = self.crop_size - width
        #label = cv2.copyMakeBorder(label, 0, max(0, h_off), 0, max(0, w_off), cv2.BORDER_CONSTANT, value=[-1,])
        region = region[np.newaxis, ...]
        if self.flip == 1:
            region = region[:,:,::-1]
        return region 
Example #12
Source File: Resampling.py    From ClearMap with GNU General Public License v3.0 6 votes vote down vote up
def fixInterpolation(interpolation):
    """Converts interpolation given as string to cv2 interpolation object
    
    Arguments:
        interpolation (str or object): interpolation string or cv2 object
    
    Returns:
        object: cv2 interpolation type
    """
    
    if interpolation == 'nn' or interpolation is None or interpolation == cv2.INTER_NEAREST:
        interpolation = cv2.INTER_NEAREST;
    else:
        interpolation = cv2.INTER_LINEAR;
        
    return interpolation; 
Example #13
Source File: camvid.py    From Fast_Seg with Apache License 2.0 6 votes vote down vote up
def __getitem__(self, index):
        datafiles = self.files[index]
        image = cv2.imread(datafiles["img"], cv2.IMREAD_COLOR)
        label = cv2.imread(datafiles["label"], cv2.IMREAD_GRAYSCALE)
        size = image.shape
        name = datafiles["name"]
        if self.f_scale != 1:
            image = cv2.resize(image, None, fx=self.f_scale, fy=self.f_scale, interpolation=cv2.INTER_LINEAR)
            label = cv2.resize(label, None, fx=self.f_scale, fy=self.f_scale, interpolation = cv2.INTER_NEAREST)

        label[label == 11] = self.ignore_label

        image = np.asarray(image, np.float32)

        if self.rgb:
            image = image[:, :, ::-1]  ## BGR -> RGB
            image /= 255  ## using pytorch pretrained models

        image -= self.mean
        image /= self.vars

        image = image.transpose((2, 0, 1))  # HWC -> CHW

        # print('image.shape:',image.shape)
        return image.copy(), label.copy(), np.array(size), name 
Example #14
Source File: helpers.py    From DEXTR-KerasTensorflow with GNU General Public License v3.0 6 votes vote down vote up
def fixed_resize(sample, resolution, flagval=None):

    if flagval is None:
        if ((sample == 0) | (sample == 1)).all():
            flagval = cv2.INTER_NEAREST
        else:
            flagval = cv2.INTER_CUBIC

    if isinstance(resolution, int):
        tmp = [resolution, resolution]
        tmp[np.argmax(sample.shape[:2])] = int(round(float(resolution)/np.min(sample.shape[:2])*np.max(sample.shape[:2])))
        resolution = tuple(tmp)

    if sample.ndim == 2 or (sample.ndim == 3 and sample.shape[2] == 3):
        sample = cv2.resize(sample, resolution[::-1], interpolation=flagval)
    else:
        tmp = sample
        sample = np.zeros(np.append(resolution, tmp.shape[2]), dtype=np.float32)
        for ii in range(sample.shape[2]):
            sample[:, :, ii] = cv2.resize(tmp[:, :, ii], resolution[::-1], interpolation=flagval)
    return sample 
Example #15
Source File: parsing.py    From Parsing-R-CNN with MIT License 6 votes vote down vote up
def parsing_on_boxes(parsing, rois, heatmap_size):
    device = rois.device
    rois = rois.to(torch.device("cpu"))
    parsing_list = []
    for i in range(rois.shape[0]):
        parsing_ins = parsing[i].cpu().numpy()
        xmin, ymin, xmax, ymax = torch.round(rois[i]).int()
        cropped_parsing = parsing_ins[ymin:ymax, xmin:xmax]
        resized_parsing = cv2.resize(
            cropped_parsing,
            (heatmap_size[1], heatmap_size[0]),
            interpolation=cv2.INTER_NEAREST
        )
        parsing_list.append(torch.from_numpy(resized_parsing))

    if len(parsing_list) == 0:
        return torch.empty(0, dtype=torch.int64, device=device)
    return torch.stack(parsing_list, dim=0).to(device, dtype=torch.int64) 
Example #16
Source File: loss.py    From Parsing-R-CNN with MIT License 6 votes vote down vote up
def parsing_on_boxes(parsing, rois, heatmap_size):
    device = rois.device
    rois = rois.to(torch.device("cpu"))
    parsing_list = []
    for i in range(rois.shape[0]):
        parsing_ins = parsing[i].cpu().numpy()
        xmin, ymin, xmax, ymax = torch.round(rois[i]).int()
        cropped_parsing = parsing_ins[max(0, ymin):ymax, max(0, xmin):xmax]
        resized_parsing = cv2.resize(
            cropped_parsing, (heatmap_size[1], heatmap_size[0]), interpolation=cv2.INTER_NEAREST
        )
        parsing_list.append(torch.from_numpy(resized_parsing))

    if len(parsing_list) == 0:
        return torch.empty(0, dtype=torch.int64, device=device)
    return torch.stack(parsing_list, dim=0).to(device, dtype=torch.int64) 
Example #17
Source File: data_augmentor.py    From Baidu_Lane_Segmentation with MIT License 5 votes vote down vote up
def rotate(self,image, angle, center=None, scale=1.0):
        (h, w) = image.shape[:2]
        if center is None:
            center = (w / 2, h / 2)
        M = cv2.getRotationMatrix2D(center, angle, scale)
        rotated = cv2.warpAffine(image, M, (w, h),flags=cv2.INTER_NEAREST)
        return rotated 
Example #18
Source File: datasets.py    From CCNet with MIT License 5 votes vote down vote up
def generate_scale_label(self, image, label):
        f_scale = 0.7 + random.randint(0, 14) / 10.0
        image = cv2.resize(image, None, fx=f_scale, fy=f_scale, interpolation = cv2.INTER_LINEAR)
        label = cv2.resize(label, None, fx=f_scale, fy=f_scale, interpolation = cv2.INTER_NEAREST)
        return image, label 
Example #19
Source File: train.py    From Baidu_Lane_Segmentation with MIT License 5 votes vote down vote up
def saveImage(img,path):

    label_rgb = np.zeros((img.shape[0], img.shape[1], 3), dtype=np.uint8)
    label_rgb[img == 0] =  [  0,   0,   0]
    label_rgb[img == 1] =  [ 70, 130, 180]
    label_rgb[img == 2] =  [119,  11,  32]
    label_rgb[img == 3] =  [220, 220,   0]
    label_rgb[img == 4] =  [102, 102, 156]
    label_rgb[img == 5] =  [128,  64, 128]
    label_rgb[img == 6] =  [190, 153, 153]
    label_rgb[img == 7] =  [128, 128,   0]
    label_rgb[img == 8] =  [255, 128,   0]
    img = cv2.resize(label_rgb, (4000,4000), interpolation=cv2.INTER_NEAREST)
    cv2.imwrite(path, label_rgb) 
Example #20
Source File: predict.py    From Baidu_Lane_Segmentation with MIT License 5 votes vote down vote up
def saveImage(img,path):
    #img = np.zeros((imageShape[0],imageShape[1], 1), dtype=np.uint8)
    for i in range(len(img)):
        for j in range(len(img[0])):
            if img[i][j] == 0:
               img[i][j] = 0
            elif img[i][j] == 1:
                img[i][j] = 200
            elif img[i][j] == 2:
                img[i][j] = 203
            elif img[i][j] == 3:
                img[i][j] = 217
            elif img[i][j] == 4:
                img[i][j] = 218
            elif img[i][j] == 5:
                img[i][j] = 210
            elif img[i][j] == 6:
                img[i][j] = 214
            elif img[i][j] == 7:
                img[i][j] = 220
            elif img[i][j] == 8:
                img[i][j] = 205

    img = cv2.resize(img, (4000,4000), interpolation=cv2.INTER_NEAREST)
    img = cv2.warpPerspective(img, Minv, (3384, 1710),flags=cv2.INTER_NEAREST) 
    #img = cv2.resize(img, (3384, 1710), interpolation=cv2.INTER_NEAREST)
    cv2.imwrite(path, img) 
Example #21
Source File: transforms.py    From argus-tgs-salt with MIT License 5 votes vote down vote up
def __init__(self, max_scale, interpolation=cv2.INTER_NEAREST):
        assert max_scale >= 1.0, "RandomHorizontalScale works as upscale only"
        self.max_scale = max_scale
        self.interpolation = interpolation 
Example #22
Source File: train.py    From SPNet with MIT License 5 votes vote down vote up
def resize_target(target, size):
    new_target = np.zeros((target.shape[0], size, size), np.int32)
    for i, t in enumerate(target.numpy()):
        new_target[i, ...] = cv2.resize(t, (size,) * 2, interpolation=cv2.INTER_NEAREST)
    return torch.from_numpy(new_target).long() 
Example #23
Source File: data_augment.py    From ASFF with GNU General Public License v3.0 5 votes vote down vote up
def __call__(self, img, res, input_size):

        interp_methods = [cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_NEAREST, cv2.INTER_LANCZOS4]
        interp_method = interp_methods[0]
        img = cv2.resize(np.array(img), input_size,
                        interpolation = interp_method).astype(np.float32)
        img = img[:,:,::-1]
        img /= 255.
        if self.means is not None:
            img -= self.means
        if self.std is not None:
            img /= self.std
        img = img.transpose(self.swap)
        img = np.ascontiguousarray(img, dtype=np.float32)
        return torch.from_numpy(img), torch.zeros(1,5) 
Example #24
Source File: data_augment.py    From ASFF with GNU General Public License v3.0 5 votes vote down vote up
def preproc_for_test(image, input_size, mean, std):
    interp_methods = [cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_NEAREST, cv2.INTER_LANCZOS4]
    interp_method = interp_methods[random.randrange(5)]
    image = cv2.resize(image, input_size,interpolation=interp_method)
    image = image.astype(np.float32)
    image = image[:,:,::-1]
    image /= 255.
    if mean is not None:
        image -= mean
    if std is not None:
        image /= std
    return image.transpose(2, 0, 1) 
Example #25
Source File: opencv_functional.py    From deep-smoke-machine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def affine(img, angle, translate, scale, shear, interpolation=cv2.INTER_LINEAR, mode=cv2.BORDER_CONSTANT, fillcolor=0):
    """Apply affine transformation on the image keeping image center invariant
    Args:
        img (numpy ndarray): numpy ndarray to be transformed.
        angle (float or int): rotation angle in degrees between -180 and 180, clockwise direction.
        translate (list or tuple of integers): horizontal and vertical translations (post-rotation translation)
        scale (float): overall scale
        shear (float): shear angle value in degrees between -180 to 180, clockwise direction.
        interpolation (``cv2.INTER_NEAREST` or ``cv2.INTER_LINEAR`` or ``cv2.INTER_AREA``, ``cv2.INTER_CUBIC``):
            An optional resampling filter.
            See `filters`_ for more information.
            If omitted, it is set to ``cv2.INTER_LINEAR``, for bilinear interpolation.
        mode (``cv2.BORDER_CONSTANT`` or ``cv2.BORDER_REPLICATE`` or ``cv2.BORDER_REFLECT`` or ``cv2.BORDER_REFLECT_101``)
            Method for filling in border regions.
            Defaults to cv2.BORDER_CONSTANT, meaning areas outside the image are filled with a value (val, default 0)
        val (int): Optional fill color for the area outside the transform in the output image. Default: 0
    """
    if not _is_numpy_image(img):
        raise TypeError('img should be numpy Image. Got {}'.format(type(img)))

    assert isinstance(translate, (tuple, list)) and len(translate) == 2, \
        "Argument translate should be a list or tuple of length 2"

    assert scale > 0.0, "Argument scale should be positive"

    output_size = img.shape[0:2]
    center = (img.shape[1] * 0.5 + 0.5, img.shape[0] * 0.5 + 0.5)
    matrix = _get_affine_matrix(center, angle, translate, scale, shear)

    if img.shape[2]==1:
        return cv2.warpAffine(img, matrix, output_size[::-1],interpolation, borderMode=mode, borderValue=fillcolor)[:,:,np.newaxis]
    else:
        return cv2.warpAffine(img, matrix, output_size[::-1],interpolation, borderMode=mode, borderValue=fillcolor) 
Example #26
Source File: helpers.py    From DEXTR-KerasTensorflow with GNU General Public License v3.0 5 votes vote down vote up
def crop_from_mask(img, mask, relax=0, zero_pad=False):
    if mask.shape[:2] != img.shape[:2]:
        mask = cv2.resize(mask, dsize=tuple(reversed(img.shape[:2])), interpolation=cv2.INTER_NEAREST)

    assert(mask.shape[:2] == img.shape[:2])

    bbox = get_bbox(mask, pad=relax, zero_pad=zero_pad)

    if bbox is None:
        return None

    crop = crop_from_bbox(img, bbox, zero_pad)

    return crop 
Example #27
Source File: object_detection_2d_geometric_ops.py    From data_generator_object_detection_2d with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self,
                 height,
                 width,
                 interpolation_modes=[cv2.INTER_NEAREST,
                                      cv2.INTER_LINEAR,
                                      cv2.INTER_CUBIC,
                                      cv2.INTER_AREA,
                                      cv2.INTER_LANCZOS4],
                 box_filter=None,
                 labels_format={'class_id': 0, 'xmin': 1, 'ymin': 2, 'xmax': 3, 'ymax': 4}):
        '''
        Arguments:
            height (int): The desired height of the output image in pixels.
            width (int): The desired width of the output image in pixels.
            interpolation_modes (list/tuple, optional): A list/tuple of integers
                that represent valid OpenCV interpolation modes. For example,
                integers 0 through 5 are valid interpolation modes.
            box_filter (BoxFilter, optional): Only relevant if ground truth bounding boxes are given.
                A `BoxFilter` object to filter out bounding boxes that don't meet the given criteria
                after the transformation. Refer to the `BoxFilter` documentation for details. If `None`,
                the validity of the bounding boxes is not checked.
            labels_format (dict, optional): A dictionary that defines which index in the last axis of the labels
                of an image contains which bounding box coordinate. The dictionary maps at least the keywords
                'xmin', 'ymin', 'xmax', and 'ymax' to their respective indices within last axis of the labels array.
        '''
        if not (isinstance(interpolation_modes, (list, tuple))):
            raise ValueError("`interpolation_mode` must be a list or tuple.")
        self.height = height
        self.width = width
        self.interpolation_modes = interpolation_modes
        self.box_filter = box_filter
        self.labels_format = labels_format
        self.resize = Resize(height=self.height,
                             width=self.width,
                             box_filter=self.box_filter,
                             labels_format=self.labels_format) 
Example #28
Source File: CVTransforms.py    From ext_portrait_segmentation with MIT License 5 votes vote down vote up
def __call__(self, image, label):

        if self.scale != 1:
            h, w = label.shape[:2]
            image = cv2.resize(image, (int(w), int(h)))
            label = cv2.resize(label, (int(w/self.scale), int(h/self.scale)), interpolation=cv2.INTER_NEAREST)

        image = image.transpose((2,0,1))

        image_tensor = torch.from_numpy(image).div(255)
        label_tensor = torch.LongTensor(np.array(label, dtype=np.int)).div(255) #torch.from_numpy(label)

        return [image_tensor, label_tensor] 
Example #29
Source File: CVTransforms.py    From ext_portrait_segmentation with MIT License 5 votes vote down vote up
def __call__(self, img, label):
        if random.random() < 0.5:
            h, w = img.shape[:2]
            x1 = random.randint(0, self.ch)
            y1 = random.randint(0, self.cw)

            img_crop = img[y1:h-y1, x1:w-x1]
            label_crop = label[y1:h-y1, x1:w-x1]

            img_crop = cv2.resize(img_crop, (w, h))
            label_crop = cv2.resize(label_crop, (w,h), interpolation=cv2.INTER_NEAREST)
            return img_crop, label_crop
        else:
            return [img, label] 
Example #30
Source File: transforms.py    From argus-tgs-salt with MIT License 5 votes vote down vote up
def __call__(self, image, mask=None):
        resize_image = cv2.resize(image, self.size, interpolation=self.interpolation)
        if mask is None:
            return resize_image
        resize_mask = cv2.resize(mask, self.size, interpolation=cv2.INTER_NEAREST)
        return resize_image, resize_mask