Python cv2.IMREAD_GRAYSCALE Examples

The following are 30 code examples of cv2.IMREAD_GRAYSCALE(). 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: captcha_generator.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 9 votes vote down vote up
def image(self, captcha_str):
        """
        Generate a greyscale captcha image representing number string

        Parameters
        ----------
        captcha_str: str
            string a characters for captcha image

        Returns
        -------
        numpy.ndarray
            Generated greyscale image in np.ndarray float type with values normalized to [0, 1]
        """
        img = self.captcha.generate(captcha_str)
        img = np.fromstring(img.getvalue(), dtype='uint8')
        img = cv2.imdecode(img, cv2.IMREAD_GRAYSCALE)
        img = cv2.resize(img, (self.h, self.w))
        img = img.transpose(1, 0)
        img = np.multiply(img, 1 / 255.0)
        return img 
Example #2
Source File: predict_folds.py    From argus-tgs-salt with MIT License 7 votes vote down vote up
def pred_test_fold(model_path, fold):
    predictor = Predictor(model_path)
    prob_df = pd.read_csv(config.SAMPLE_SUBM_PATH)
    prob_df.rename(columns={'rle_mask': 'prob'}, inplace=True)

    fold_prediction_dir = join(PREDICTION_DIR, f'fold_{fold}', 'test')
    make_dir(fold_prediction_dir)

    for i, row in prob_df.iterrows():
        image_path = join(config.TEST_DIR, 'images'+IMAGES_NAME, row.id + '.png')
        image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
        segm, prob = predictor(image)
        row.prob = prob
        segm_save_path = join(fold_prediction_dir, row.id + '.png')
        cv2.imwrite(segm_save_path, segm)

    prob_df.to_csv(join(fold_prediction_dir, 'probs.csv'), index=False) 
Example #3
Source File: test_matting.py    From closed-form-matting with MIT License 7 votes vote down vote up
def test_solution_close_to_original_implementation(self):
        image = cv2.imread('testdata/source.png', cv2.IMREAD_COLOR) / 255.0
        scribles = cv2.imread('testdata/scribbles.png', cv2.IMREAD_COLOR) / 255.0

        alpha = closed_form_matting.closed_form_matting_with_scribbles(image, scribles)
        foreground, background = solve_foreground_background(image, alpha)

        matlab_alpha = cv2.imread('testdata/matlab_alpha.png', cv2.IMREAD_GRAYSCALE) / 255.0
        matlab_foreground = cv2.imread('testdata/matlab_foreground.png', cv2.IMREAD_COLOR) / 255.0
        matlab_background = cv2.imread('testdata/matlab_background.png', cv2.IMREAD_COLOR) / 255.0

        sad_alpha = np.mean(np.abs(alpha - matlab_alpha))
        sad_foreground = np.mean(np.abs(foreground - matlab_foreground))
        sad_background = np.mean(np.abs(background - matlab_background))

        self.assertLess(sad_alpha, 1e-2)
        self.assertLess(sad_foreground, 1e-2)
        self.assertLess(sad_background, 1e-2) 
Example #4
Source File: utils_image.py    From KAIR with MIT License 7 votes vote down vote up
def imread_uint(path, n_channels=3):
    #  input: path
    # output: HxWx3(RGB or GGG), or HxWx1 (G)
    if n_channels == 1:
        img = cv2.imread(path, 0)  # cv2.IMREAD_GRAYSCALE
        img = np.expand_dims(img, axis=2)  # HxWx1
    elif n_channels == 3:
        img = cv2.imread(path, cv2.IMREAD_UNCHANGED)  # BGR or G
        if img.ndim == 2:
            img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)  # GGG
        else:
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)  # RGB
    return img


# --------------------------------------------
# matlab's imwrite
# -------------------------------------------- 
Example #5
Source File: predict_folds.py    From argus-tgs-salt with MIT License 7 votes vote down vote up
def pred_val_fold(model_path, fold):
    predictor = Predictor(model_path)
    folds_df = pd.read_csv(TRAIN_FOLDS_PATH)
    fold_df = folds_df[folds_df.fold == fold]
    fold_prediction_dir = join(PREDICTION_DIR, f'fold_{fold}', 'val')
    make_dir(fold_prediction_dir)

    prob_dict = {'id': [], 'prob': []}
    for i, row in fold_df.iterrows():
        image = cv2.imread(row.image_path, cv2.IMREAD_GRAYSCALE)
        segm, prob = predictor(image)
        segm_save_path = join(fold_prediction_dir, row.id + '.png')
        cv2.imwrite(segm_save_path, segm)

        prob_dict['id'].append(row.id)
        prob_dict['prob'].append(prob)

    prob_df = pd.DataFrame(prob_dict)
    prob_df.to_csv(join(fold_prediction_dir, 'probs.csv'), index=False) 
Example #6
Source File: predict_folds.py    From argus-tgs-salt with MIT License 7 votes vote down vote up
def pred_test_fold(model_path, fold):
    predictor = Predictor(model_path)
    prob_df = pd.read_csv(config.SAMPLE_SUBM_PATH)
    prob_df.rename(columns={'rle_mask': 'prob'}, inplace=True)

    fold_prediction_dir = join(PREDICTION_DIR, f'fold_{fold}', 'test')
    make_dir(fold_prediction_dir)

    for i, row in prob_df.iterrows():
        image_path = join(config.TEST_DIR, 'images'+IMAGES_NAME, row.id + '.png')
        image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
        segm, prob = predictor(image)
        row.prob = prob
        segm_save_path = join(fold_prediction_dir, row.id + '.png')
        cv2.imwrite(segm_save_path, segm)

    prob_df.to_csv(join(fold_prediction_dir, 'probs.csv'), index=False) 
Example #7
Source File: predict_folds.py    From argus-tgs-salt with MIT License 7 votes vote down vote up
def pred_val_fold(model_path, fold):
    predictor = Predictor(model_path)
    folds_df = pd.read_csv(TRAIN_FOLDS_PATH)
    fold_df = folds_df[folds_df.fold == fold]
    fold_prediction_dir = join(PREDICTION_DIR, f'fold_{fold}', 'val')
    make_dir(fold_prediction_dir)

    prob_dict = {'id': [], 'prob': []}
    for i, row in fold_df.iterrows():
        image = cv2.imread(row.image_path, cv2.IMREAD_GRAYSCALE)
        segm, prob = predictor(image)
        segm_save_path = join(fold_prediction_dir, row.id + '.png')
        cv2.imwrite(segm_save_path, segm)

        prob_dict['id'].append(row.id)
        prob_dict['prob'].append(prob)

    prob_df = pd.DataFrame(prob_dict)
    prob_df.to_csv(join(fold_prediction_dir, 'probs.csv'), index=False) 
Example #8
Source File: predict_folds.py    From argus-tgs-salt with MIT License 7 votes vote down vote up
def pred_test_fold(model_path, fold):
    predictor = Predictor(model_path)
    prob_df = pd.read_csv(config.SAMPLE_SUBM_PATH)
    prob_df.rename(columns={'rle_mask': 'prob'}, inplace=True)

    fold_prediction_dir = join(PREDICTION_DIR, f'fold_{fold}', 'test')
    make_dir(fold_prediction_dir)

    for i, row in prob_df.iterrows():
        image_path = join(config.TEST_DIR, 'images'+IMAGES_NAME, row.id + '.png')
        image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
        segm, prob = predictor(image)
        row.prob = prob
        segm_save_path = join(fold_prediction_dir, row.id + '.png')
        cv2.imwrite(segm_save_path, segm)

    prob_df.to_csv(join(fold_prediction_dir, 'probs.csv'), index=False) 
Example #9
Source File: predict_folds.py    From argus-tgs-salt with MIT License 7 votes vote down vote up
def pred_test_fold(model_path, fold):
    predictor = Predictor(model_path)
    prob_df = pd.read_csv(config.SAMPLE_SUBM_PATH)
    prob_df.rename(columns={'rle_mask': 'prob'}, inplace=True)

    fold_prediction_dir = join(PREDICTION_DIR, f'fold_{fold}', 'test')
    make_dir(fold_prediction_dir)

    for i, row in prob_df.iterrows():
        image_path = join(config.TEST_DIR, 'images'+IMAGES_NAME, row.id + '.png')
        image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
        segm, prob = predictor(image)
        row.prob = prob
        segm_save_path = join(fold_prediction_dir, row.id + '.png')
        cv2.imwrite(segm_save_path, segm)

    prob_df.to_csv(join(fold_prediction_dir, 'probs.csv'), index=False) 
Example #10
Source File: dataset.py    From argus-tgs-salt with MIT License 7 votes vote down vote up
def get_samples(train_folds_path, folds):
    images_lst = []
    target_lst = []
    depth_lst = []

    train_folds_df = pd.read_csv(train_folds_path)

    for i, row in train_folds_df.iterrows():
        if row.fold not in folds:
            continue

        image = cv2.imread(row.image_path, cv2.IMREAD_GRAYSCALE)
        if image is None:
            raise FileNotFoundError(f"Image not found {row.image_path}")
        mask = cv2.imread(row.mask_path, cv2.IMREAD_GRAYSCALE)
        if mask is None:
            raise FileNotFoundError(f"Mask not found {row.mask_path}")
        images_lst.append(image)
        target_lst.append(mask)
        depth_lst.append(row.z)

    return images_lst, target_lst, depth_lst 
Example #11
Source File: test.py    From DeepDeblur with MIT License 7 votes vote down vote up
def main():

    input_dim = (None, None, num_of_dim)

    #DeblurNet = ShortCutNet().DeblurResidualNet(input_dim, 6)
    DeblurNet = ShortCutNet().DeblurSHCNet(input_dim, 15)
    DeblurNet.summary()

    input_blur = Input(shape=(input_dim))
    out_deblur = DeblurNet(input_blur)

    # Model
    model = Model(inputs = input_blur, outputs = out_deblur)
    model.summary()
    model.load_weights(path_weights, by_name=True)

    # test
    x = cv2.imread(path_test + name_read, cv2.IMREAD_GRAYSCALE) # Read as gray image
    x = x.reshape(x.shape[0], x.shape[1], num_of_dim) / 255.0

    pred = model.predict(np.expand_dims(x, axis=0))

    pred = pred.reshape(x.shape[0] - kernel_crop, x.shape[1] - kernel_crop, num_of_dim)
    cv2.imwrite(path_test + name_save, pred * 255.0) 
Example #12
Source File: DataGen.py    From DeepDeblur with MIT License 7 votes vote down vote up
def _GenerateBatch(self, tmp, path_sharp, path_blur):
        '''Generates data of batch_size samples
        '''
        # Initialization
        x_blur = np.zeros([self.batch_size, self.img_rows, self.img_cols, self.num_of_dim], dtype = np.float64)
        y_sharp = np.zeros([self.batch_size, self.label_rows, self.label_cols, self.num_of_dim], dtype = np.float64)
        y_fake = np.zeros([self.batch_size], dtype = int)

        # Generate data
        for count_i, name_i in enumerate(tmp):

            # Read blurry input images
            x = cv2.imread(path_blur + name_i, cv2.IMREAD_GRAYSCALE)
            x = x.reshape(self.img_rows, self.img_cols, self.num_of_dim)
            x_blur[count_i, :] = x/255.0

            # Read sharp labels
            x = cv2.imread(path_sharp + name_i, cv2.IMREAD_GRAYSCALE)
            x = x.reshape(self.img_rows, self.img_cols, self.num_of_dim)
            x = x[self.kernel_crop:(self.img_rows - self.kernel_crop), \
                    self.kernel_crop:(self.img_cols - self.kernel_crop)]
            y_sharp[count_i, :] = x/255.0
            

        return [x_blur, y_sharp], y_fake 
Example #13
Source File: otsu_segmentation.py    From leaf-image-segmentation with MIT License 7 votes vote down vote up
def segment_with_otsu(image_file, background = 0):
    """
    Segment an image file using otsu thresholding
    Args:
        image_file: file path
        background: grayscale value to be set as background

    Returns:
        ret_val:
        segmented_image: in ndarray form
    """
    image = read_image(image_file, cv2.IMREAD_GRAYSCALE)
    
    ret_val, marker = get_marker(image)
    segmented_image = apply_marker(image, marker, background)

    return ret_val, segmented_image 
Example #14
Source File: SudokuExtractor.py    From SolveSudoku with MIT License 7 votes vote down vote up
def parse_grid(path):
    original = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
    processed = pre_process_image(original)
    
#    cv2.namedWindow('processed',cv2.WINDOW_AUTOSIZE)
#    processed_img = cv2.resize(processed, (500, 500))          # Resize image
#    cv2.imshow('processed', processed_img)
    
    corners = find_corners_of_largest_polygon(processed)
    cropped = crop_and_warp(original, corners)
    
#    cv2.namedWindow('cropped',cv2.WINDOW_AUTOSIZE)
#    cropped_img = cv2.resize(cropped, (500, 500))              # Resize image
#    cv2.imshow('cropped', cropped_img)
    
    squares = infer_grid(cropped)
#    print(squares)
    digits = get_digits(cropped, squares, 28)
#    print(digits)
    final_image = show_digits(digits)
    return final_image 
Example #15
Source File: utils.py    From Tensorflow-Cookbook with MIT License 7 votes vote down vote up
def load_test_image(image_path, img_width, img_height, img_channel):

    if img_channel == 1 :
        img = cv2.imread(image_path, flags=cv2.IMREAD_GRAYSCALE)
    else :
        img = cv2.imread(image_path, flags=cv2.IMREAD_COLOR)
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

    img = cv2.resize(img, dsize=(img_width, img_height))

    if img_channel == 1 :
        img = np.expand_dims(img, axis=0)
        img = np.expand_dims(img, axis=-1)
    else :
        img = np.expand_dims(img, axis=0)

    img = img/127.5 - 1

    return img 
Example #16
Source File: image_helper.py    From openseg.pytorch with MIT License 7 votes vote down vote up
def imfrombytes(content, flag='color'):
        """Read an image from bytes.

        Args:
            content (bytes): Image bytes got from files or other streams.
            flag (str): Same as :func:`imread`.

        Returns:
            ndarray: Loaded image array.
        """
        imread_flags = {
            'color': cv2.IMREAD_COLOR,
            'grayscale': cv2.IMREAD_GRAYSCALE,
            'unchanged': cv2.IMREAD_UNCHANGED
        }
        img_np = np.fromstring(content, np.uint8)
        flag = imread_flags[flag] if isinstance(flag, str) else flag
        img = cv2.imdecode(img_np, flag)
        return img 
Example #17
Source File: lip.py    From openseg.pytorch with MIT License 7 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)
        ori_size = image.shape
        image = self.resize_image(image, (self.crop_h, self.crop_w))
         
        name = datafiles["name"]
        image = np.asarray(image, np.float32)
        if self.network == "resnet101":
            mean = (102.9801, 115.9465, 122.7717)
            image = image[:,:,::-1]
            image -= mean
        else: #define other data pre-processing method
            pass
         
        image = image.transpose((2, 0, 1))
        return image, label,  np.array(ori_size), name 
Example #18
Source File: vaegan_mxnet.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 7 votes vote down vote up
def get_data(path, activation):
    '''Get the dataset
    '''
    data = []
    image_names = []
    for filename in os.listdir(path):
        img = cv2.imread(os.path.join(path,filename), cv2.IMREAD_GRAYSCALE)
        image_names.append(filename)
        if img is not None:
            data.append(img)

    data = np.asarray(data)

    if activation == 'sigmoid':
        data = data.astype(np.float32)/(255.0)
    elif activation == 'tanh':
        data = data.astype(np.float32)/(255.0/2) - 1.0

    data = data.reshape((data.shape[0], 1, data.shape[1], data.shape[2]))

    np.random.seed(1234)
    p = np.random.permutation(data.shape[0])
    X = data[p]

    return X, image_names 
Example #19
Source File: davis17_online_data.py    From MaskTrack with MIT License 6 votes vote down vote up
def make_df1_df2(self, idx):
        """
        Make the deformations
        """
        df1 = cv2.imread(os.path.join(self.db_root_dir, self.deformations1[idx]), cv2.IMREAD_GRAYSCALE)
        df2 = cv2.imread(os.path.join(self.db_root_dir, self.deformations2[idx]), cv2.IMREAD_GRAYSCALE)

        if self.inputRes is not None:
            df1 = imresize(df1, self.inputRes, interp='nearest')
            df2 = imresize(df2, self.inputRes, interp='nearest')

        df1 = np.array(df1, dtype=np.float32)
        df1 = df1/np.max([df1.max(), 1e-8])

        df2 = np.array(df2, dtype=np.float32)
        df2 = df2/np.max([df2.max(), 1e-8])

        return df1, df2 
Example #20
Source File: dataset_manager.py    From DexiNed with MIT License 6 votes vote down vote up
def transformer(self, x_path, y_path):
        tmp_x = cv.imread(x_path)
        if y_path is not None:
            tmp_y = cv.imread(y_path,cv.IMREAD_GRAYSCALE)
        else:
            tmp_y=None
        h,w,_ = tmp_x.shape
        if self.args.model_state == "train":
            if self.args.crop_img:
                i_h = random.randint(0,h-self.dim_h)
                i_w = random.randint(0,w-self.dim_w)
                tmp_x = tmp_x[i_h:i_h+self.dim_h,i_w:i_w+self.dim_w,]
                tmp_y = tmp_y[i_h:i_h+self.dim_h,i_w:i_w+self.dim_w,]
            else:
                tmp_x = cv.resize(tmp_x,(self.dim_w,self.dim_h))
                tmp_y = cv.resize(tmp_y,(self.dim_w,self.dim_h))
        else:
            if self.dim_w!=w and self.dim_h!=h:
                tmp_x = cv.resize(tmp_x, (self.dim_w, self.dim_h))
            if self.args.scale is not None:
                scl = self.args.scale
                scl_h = int(self.dim_h * scl) if (self.dim_h * scl) % 16 == 0 else \
                    int(((self.dim_h * scl) // 16 + 1) * 16)
                scl_w = int(self.dim_w * scl) if (self.dim_w * scl) % 16 == 0 else \
                    int(((self.dim_h * scl) // 16 + 1) * 16)
                tmp_x = cv.resize(tmp_x,dsize=(scl_w,scl_h))
            if tmp_y is not None:
                tmp_y = cv.resize(tmp_y, (self.dim_w, self.dim_h))

        if tmp_y is not None:
            tmp_y = np.expand_dims(np.float32(tmp_y)/255.,axis=-1)
        tmp_x = np.float32(tmp_x)
        return tmp_x, tmp_y

    # def __read_h5(self,file_path):
    #
    #     with h5py.File(file_path,'r') as h5f:
    #         # n_var = len(list(h5f.keys()))
    #         data = np.array(h5f.get('data'))
    #     return data 
Example #21
Source File: main.py    From DexiNed with MIT License 6 votes vote down vote up
def __getitem__(self, idx):
        # get data sample
        image_path, label_path = self.data_index[idx]
        
        # load data
        image = cv.imread(image_path, cv.IMREAD_COLOR)
        label = cv.imread(label_path, cv.IMREAD_GRAYSCALE)
        image, label = self.transform(img=image, gt=label)
        return dict(images=image, labels=label) 
Example #22
Source File: exp13_user_dataset_high_API_1.py    From LearningTensorflow with MIT License 6 votes vote down vote up
def __read__(file, label):
    image = cv2.imread(file.decode(), cv2.IMREAD_GRAYSCALE)
    image = np.expand_dims(image, 2)
    image = np.float32(image) / 255.
    return (image, label) 
Example #23
Source File: datasets.py    From pytorch-segmentation-toolbox with MIT License 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)
        label = self.id2trainId(label)
        size = image.shape
        name = datafiles["name"]
        if self.scale:
            image, label = self.generate_scale_label(image, label)
        image = np.asarray(image, np.float32)
        image -= self.mean
        img_h, img_w = label.shape
        pad_h = max(self.crop_h - img_h, 0)
        pad_w = max(self.crop_w - img_w, 0)
        if pad_h > 0 or pad_w > 0:
            img_pad = cv2.copyMakeBorder(image, 0, pad_h, 0, 
                pad_w, cv2.BORDER_CONSTANT, 
                value=(0.0, 0.0, 0.0))
            label_pad = cv2.copyMakeBorder(label, 0, pad_h, 0, 
                pad_w, cv2.BORDER_CONSTANT,
                value=(self.ignore_label,))
        else:
            img_pad, label_pad = image, label

        img_h, img_w = label_pad.shape
        h_off = random.randint(0, img_h - self.crop_h)
        w_off = random.randint(0, img_w - self.crop_w)
        # roi = cv2.Rect(w_off, h_off, self.crop_w, self.crop_h);
        image = np.asarray(img_pad[h_off : h_off+self.crop_h, w_off : w_off+self.crop_w], np.float32)
        label = np.asarray(label_pad[h_off : h_off+self.crop_h, w_off : w_off+self.crop_w], np.float32)
        #image = image[:, :, ::-1]  # change to BGR
        image = image.transpose((2, 0, 1))
        if self.is_mirror:
            flip = np.random.choice(2) * 2 - 1
            image = image[:, :, ::flip]
            label = label[:, ::flip]

        return image.copy(), label.copy(), np.array(size), name 
Example #24
Source File: image.py    From dataflow with Apache License 2.0 6 votes vote down vote up
def __init__(self, files, channel=3, resize=None, shuffle=False):
        """
        Args:
            files (list): list of file paths.
            channel (int): 1 or 3. Will convert grayscale to RGB images if channel==3.
                Will produce (h, w, 1) array if channel==1.
            resize (tuple): int or (h, w) tuple. If given, resize the image.
        """
        assert len(files), "No image files given to ImageFromFile!"
        self.files = files
        self.channel = int(channel)
        assert self.channel in [1, 3], self.channel
        self.imread_mode = cv2.IMREAD_GRAYSCALE if self.channel == 1 else cv2.IMREAD_COLOR
        if resize is not None:
            resize = shape2d(resize)
        self.resize = resize
        self.shuffle = shuffle 
Example #25
Source File: datasets.py    From pytorch-segmentation-toolbox with MIT License 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.scale:
            image, label = self.generate_scale_label(image, label)
        image = np.asarray(image, np.float32)
        image -= self.mean
        img_h, img_w = label.shape
        pad_h = max(self.crop_h - img_h, 0)
        pad_w = max(self.crop_w - img_w, 0)
        if pad_h > 0 or pad_w > 0:
            img_pad = cv2.copyMakeBorder(image, 0, pad_h, 0, 
                pad_w, cv2.BORDER_CONSTANT, 
                value=(0.0, 0.0, 0.0))
            label_pad = cv2.copyMakeBorder(label, 0, pad_h, 0, 
                pad_w, cv2.BORDER_CONSTANT,
                value=(self.ignore_label,))
        else:
            img_pad, label_pad = image, label

        img_h, img_w = label_pad.shape
        h_off = random.randint(0, img_h - self.crop_h)
        w_off = random.randint(0, img_w - self.crop_w)
        # roi = cv2.Rect(w_off, h_off, self.crop_w, self.crop_h);
        image = np.asarray(img_pad[h_off : h_off+self.crop_h, w_off : w_off+self.crop_w], np.float32)
        label = np.asarray(label_pad[h_off : h_off+self.crop_h, w_off : w_off+self.crop_w], np.float32)
        #image = image[:, :, ::-1]  # change to BGR
        image = image.transpose((2, 0, 1))
        if self.is_mirror:
            flip = np.random.choice(2) * 2 - 1
            image = image[:, :, ::flip]
            label = label[:, ::flip]

        return image.copy(), label.copy(), np.array(size), name 
Example #26
Source File: charades.py    From PyTorchConv3D with Apache License 2.0 6 votes vote down vote up
def load_flow_frames(image_dir, vid, start, num):
  frames = []
  for i in range(start, start+num):
    imgx = cv2.imread(os.path.join(image_dir, vid, vid+'-'+str(i).zfill(6)+'x.jpg'), cv2.IMREAD_GRAYSCALE)
    imgy = cv2.imread(os.path.join(image_dir, vid, vid+'-'+str(i).zfill(6)+'y.jpg'), cv2.IMREAD_GRAYSCALE)
    
    w,h = imgx.shape
    if w < 224 or h < 224:
        d = 224.-min(w,h)
        sc = 1+d/min(w,h)
        imgx = cv2.resize(imgx,dsize=(0,0),fx=sc,fy=sc)
        imgy = cv2.resize(imgy,dsize=(0,0),fx=sc,fy=sc)
        
    imgx = (imgx/255.)*2 - 1
    imgy = (imgy/255.)*2 - 1
    img = np.asarray([imgx, imgy]).transpose([1,2,0])
    frames.append(img)
  return np.asarray(frames, dtype=np.float32) 
Example #27
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 #28
Source File: create_dataset.py    From ICDAR-2019-SROIE with MIT License 6 votes vote down vote up
def checkImageIsValid(imageBin):
    if imageBin is None:
        return False
    imageBuf = np.fromstring(imageBin, dtype=np.uint8)
    img = cv2.imdecode(imageBuf, cv2.IMREAD_GRAYSCALE)
    imgH, imgW = img.shape[0], img.shape[1]
    if imgH * imgW == 0:
        return False
    return True 
Example #29
Source File: image_descriptor.py    From netvlad_tf_open with MIT License 6 votes vote down vote up
def describeAllJpegsInPath(self, path, batch_size, verbose=False):
        ''' returns a list of descriptors '''
        jpeg_paths = sorted(glob.glob(os.path.join(path, '*.jpg')))
        descs = []
        for batch_offset in range(0, len(jpeg_paths), batch_size):
            images = []
            for i in range(batch_offset, batch_offset + batch_size):
                if i == len(jpeg_paths):
                    break
                if verbose:
                    print('%d/%d' % (i, len(jpeg_paths)))
                if self.is_grayscale:
                    image = cv2.imread(jpeg_paths[i], cv2.IMREAD_GRAYSCALE)
                    images.append(np.expand_dims(
                            np.expand_dims(image, axis=0), axis=-1))
                else:
                    image = cv2.imread(jpeg_paths[i])
                    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
                    images.append(np.expand_dims(image, axis=0))
            batch = np.concatenate(images, 0)
            descs = descs + list(self.sess.run(
                    self.net_out, feed_dict={self.tf_batch: batch}))
        return descs 
Example #30
Source File: captcha_generator.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 6 votes vote down vote up
def main():
        parser = argparse.ArgumentParser()
        parser.add_argument("font_path", help="Path to ttf font file")
        parser.add_argument("output", help="Output filename including extension (e.g. 'sample.jpg')")
        parser.add_argument("--num", help="Up to 4 digit number [Default: random]")
        args = parser.parse_args()

        captcha = ImageCaptcha(fonts=[args.font_path])
        captcha_str = args.num if args.num else DigitCaptcha.get_rand(3, 4)
        img = captcha.generate(captcha_str)
        img = np.fromstring(img.getvalue(), dtype='uint8')
        img = cv2.imdecode(img, cv2.IMREAD_GRAYSCALE)
        cv2.imwrite(args.output, img)
        print("Captcha image with digits {} written to {}".format([int(c) for c in captcha_str], args.output))