def __getitem__(self, index): img_path = self.images_path[index] target_ = cv2.imread(img_path) input_ = imresize(target_, scalar_scale=1 / self.sr_factor) subim_in, subim_tar = get_patch(input_, target_, self.patch_size, self.sr_factor) if not self.rgb: subim_in = utils.rgb2ycbcr(subim_in) subim_tar = utils.rgb2ycbcr(subim_tar) subim_in = np.expand_dims(subim_in[:, :, 0], 2) subim_tar = np.expand_dims(subim_tar[:, :, 0], 2) if self.input_up: subim_bic = imresize(subim_in, scalar_scale=self.sr_factor) subim_in = utils.np2tensor(subim_in, self.rgb_range) subim_tar = utils.np2tensor(subim_tar, self.rgb_range) subim_bic = utils.np2tensor(subim_bic, self.rgb_range) return { 'input': subim_in, 'target': subim_tar, 'input_up': subim_bic } subim_in = utils.np2tensor(subim_in, self.rgb_range) subim_tar = utils.np2tensor(subim_tar, self.rgb_range) return {'input': subim_in, 'target': subim_tar}
def __getitem__(self, index): path = self.test_path[index] images = {} images.update({'name': path['save']}) input_ = cv2.imread(path['input']) input_ = cv2.cvtColor(input_, cv2.COLOR_BGR2RGB) target_ = cv2.imread(path['target']) target_ = utils.modcrop(target_, self.sr_factor) target_ = cv2.cvtColor(target_, cv2.COLOR_BGR2RGB) if not self.rgb: input_out = np.copy(input_) input_out = utils.np2tensor(input_out, self.rgb_range) # print(input_out) input_ = utils.rgb2ycbcr(input_) input_cbcr = input_[:, :, 1:] input_ = np.expand_dims(input_[:, :, 0], 2) input_cbcr = utils.np2tensor(input_cbcr, self.rgb_range) images.update({'input_cbcr': input_cbcr, 'input_rgb': input_out}) if self.target_down: target_down = imresize(target_, scalar_scale=1 / self.sr_factor) target_down = utils.np2tensor(target_down, self.rgb_range) images.update({'target_down': target_down}) input_ = utils.np2tensor(input_, self.rgb_range) target_ = utils.np2tensor(target_, self.rgb_range) images.update({'input': input_, 'target': target_}) return images
def read_avgblur(self, filename, skip=True): print('Average Blurring') cap = cv2.VideoCapture(filename) nframe = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) print(nframe) if skip: if nframe // self.depth >= 2: skip_frames = nframe % self.depth + self.depth else: skip_frames = nframe % self.depth else: skip_frames = nframe % self.depth start = skip_frames//2 end = nframe-skip_frames//2 if skip_frames % 2 == 1: end -= 1 framelst = [] for i in range(start, end): cap.set(cv2.CAP_PROP_POS_FRAMES, i) ret, frame = cap.read() frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) frame = self.dsampleWithAvg(frame, self.ksize) frame = imresize(frame, output_shape=(120, 160)) framelst.append(frame) cap.release() print(np.array(framelst).shape) print(nframe-skip_frames) return np.split(np.array(framelst), (nframe-skip_frames) // self.depth, axis=0)
def get_subimages(self, frame): subimage_lst = [] for x in range(self.ksize): for y in range(self.ksize): mask = np.fromfunction(lambda i, j, k: (i%self.ksize==x) & (j%self.ksize==y), frame.shape, dtype=int) subimage = frame[mask].reshape(frame.shape[0]//self.ksize, frame.shape[1]//self.ksize, -1) subimage = imresize(subimage, output_shape=(120, 160)) subimage_lst.append(subimage) return subimage_lst
def __getitem__(self, index): img_path = self.images_path[index] target_ = cv2.imread(img_path) input_ = imresize(target_, scalar_scale=1 / self.sr_factor) if not self.rgb: input_ = utils.rgb2ycbcr(input_) target_ = utils.rgb2ycbcr(target_) if self.input_up: input_bic = imresize(input_, scalar_scale=self.sr_factor).round() input_ = utils.np2tensor(input_, self.rgb_range) target_ = utils.np2tensor(target_, self.rgb_range) input_bic_ = utils.np2tensor(input_bic, self.rgb_range) return {'input': input_, 'target': target_, 'input_up': input_bic_} input_ = utils.np2tensor(input_, self.rgb_range) target_ = utils.np2tensor(target_, self.rgb_range) return {'input': input_, 'target': target_}
def __getitem__(self, index): path = self.train_path[index] images = {} if self.npy_reader: input_ = np.load(path['input'], allow_pickle=False) target_ = np.load(path['target'], allow_pickle=False) target_ = utils.modcrop(target_, self.sr_factor) else: input_ = cv2.imread(path['input']) input_ = cv2.cvtColor(input_, cv2.COLOR_BGR2RGB) target_ = cv2.imread(path['target']) target_ = utils.modcrop(target_, self.sr_factor) target_ = cv2.cvtColor(target_, cv2.COLOR_BGR2RGB) # for i in range(10): # subim_in, subim_tar = get_patch(input_, target_, self.patch_size, self.sr_factor) # win_mean = ndimage.uniform_filter(subim_in[:, :, 0], (5, 5)) # win_sqr_mean = ndimage.uniform_filter(subim_in[:, :, 0]**2, (5, 5)) # win_var = win_sqr_mean - win_mean**2 # # if np.sum(win_var) / (win_var.shape[0]*win_var.shape[1]) > 30: # break subim_in, subim_tar = get_patch(input_, target_, self.patch_size, self.sr_factor) if not self.rgb: subim_in = utils.rgb2ycbcr(subim_in) subim_tar = utils.rgb2ycbcr(subim_tar) subim_in = np.expand_dims(subim_in[:, :, 0], 2) subim_tar = np.expand_dims(subim_tar[:, :, 0], 2) if self.target_down: subim_target_down = imresize(subim_tar, scalar_scale=1 / self.sr_factor) subim_target_down = utils.np2tensor(subim_target_down, self.rgb_range) images.update({'target_down': subim_target_down}) subim_in = utils.np2tensor(subim_in, self.rgb_range) subim_tar = utils.np2tensor(subim_tar, self.rgb_range) images.update({'input': subim_in, 'target': subim_tar}) return images
def __getitem__(self, index): input_ = np.load(self.input_path[index]) target_ = np.load(self.target_path[index]) if not self.rgb: input_ = utils.rgb2ycbcr(input_) target_ = utils.rgb2ycbcr(target_) if self.input_up: input_bic = imresize(input_, scalar_scale=self.sr_factor).round() input_ = utils.np2tensor(input_, self.rgb_range) target_ = utils.np2tensor(target_, self.rgb_range) input_bic_ = utils.np2tensor(input_bic, self.rgb_range) return {'input': input_, 'target': target_, 'input_up': input_bic_} input_ = utils.np2tensor(input_, self.rgb_range) target_ = utils.np2tensor(target_, self.rgb_range) return {'input': input_, 'target': target_}
def preprocess_py(path, scale=4): """ Preprocess single image file (1) Read original image as YCbCr format (and grayscale as default) (2) Normalize (3) Apply image file with bicubic interpolation Args: path: file path of desired file input_: image applied bicubic interpolation (low-resolution) label_: image with original resolution (high-resolution) """ im_uint8 = imread(path) im_double = im_uint8 / 255. input_ = modcrop(im_double, scale) label_ = imresize(input_, scalar_scale=1. / scale) return label_
def read(self, filename): print('No Blurring') cap = cv2.VideoCapture(filename) nframe = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) if nframe > self.depth: indices = self.indices_augment(nframe, self.depth, 20) else: indices = [x for x in range(nframe) ] + [nframe - 1] * (self.depth - nframe) framelst = [] for i in indices: cap.set(cv2.CAP_PROP_POS_FRAMES, i) ret, frame = cap.read() print(frame.shape) #print(frame[:,:,0]) frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) frame = imresize(frame, output_shape=(120, 160)) framelst.append(frame) cap.release() clip_lst = np.split(np.array(framelst), len(framelst) // self.depth, axis=0) return clip_lst
def save_image2npy(path, save_path, sr_fator): images = make_dataset(path) input_save_path = os.path.join(save_path, 'x%d' % (sr_fator)) target_save_path = os.path.join(save_path, 'origin') mkdir(input_save_path) mkdir(target_save_path) for img_name in images: print(img_name) _, name = os.path.split(img_name) name, _ = name.split('.') target_name = '%s.npy' % name input_name = '%s.npy' % name input_save_name = os.path.join(input_save_path, input_name) target_save_name = os.path.join(target_save_path, target_name) img = cv2.imread(img_name) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img_down = imresize(img, scalar_scale=1/sr_fator) print(img.shape, img_down.shape) np.save(target_save_name, img) np.save(input_save_name, img_down)
import cv2 import os from matlab_imresize import imresize from utils import modcrop, mkdir origin_path = '/media/luo/data/data/super-resolution/VISTA/origin' path = '/media/luo/data/data/super-resolution/VISTA' # Q_list = [5, 10, 20, 30, 40, 50] sr_factor = [2, 3, 4] for root, _, names in os.walk(origin_path): for name in names: target_name = os.path.join(origin_path, name) img_tar = cv2.imread(target_name) # img_tar = cv2.cvtColor(img_tar, cv2.cvtColor()) for i in sr_factor: img_tar_ = modcrop(img_tar, i) sr_path = os.path.join(path, 'x{}'.format(i)) img_down = imresize(img_tar_, scalar_scale=1 / i) for Q in Q_list: webp_path = os.path.join(sr_path, 'webp{}'.format(Q)) if not os.path.exists(webp_path): mkdir(webp_path) save_name = name[:-4] + '.webp' save = os.path.join(webp_path, save_name) cv2.imwrite(save, img_down, [cv2.IMWRITE_WEBP_QUALITY, Q])
def __call__(self, img): dsize = (int(np.shape(img)[1] / self.scale_factor), int(np.shape(img)[0] / self.scale_factor)) return matlab_imresize.imresize(img, output_shape=dsize)