def __call__(self, img): """ Args: img (PIL Image): Image to be flipped. Returns: PIL Image: Randomly flipped image. """ def dct2(img): return dct( dct( img, axis=0, norm='ortho' ), axis=1, norm='ortho' ) def idct2(img): return idct( idct( img, axis=0, norm='ortho'), axis=1 , norm='ortho') img_ycbcr = rgb2ycbcr(img) # convert to ycbcr color space img_dct = np.zeros(img_ycbcr.shape) # create dct vector for c in range(img_ycbcr.shape[-1]): img_dct[:,:,c] = dct2(img_ycbcr[:,:,c]) # Dct coefficients t = np.random.randint(0, 51) # Random threshold [0,50] img_dct[abs(img_dct) < t] = 0 # img_ycbcr[:,:,c] = idct2(img_dct[:,:,c]) # Inverse dct img = torch.tensor(ycbcr2rgb(img_ycbcr)) # convert to rgb return img
def test_yuv_roundtrip(self): img_rgb = img_as_float(self.img_rgb)[::16, ::16] assert_array_almost_equal(yuv2rgb(rgb2yuv(img_rgb)), img_rgb) assert_array_almost_equal(yiq2rgb(rgb2yiq(img_rgb)), img_rgb) assert_array_almost_equal(ypbpr2rgb(rgb2ypbpr(img_rgb)), img_rgb) assert_array_almost_equal(ycbcr2rgb(rgb2ycbcr(img_rgb)), img_rgb) assert_array_almost_equal(ydbdr2rgb(rgb2ydbdr(img_rgb)), img_rgb)
def dct_t(images, threshold=None): def dct2(img): return dct( dct( img, axis=0, norm='ortho' ), axis=1, norm='ortho' ) def idct2(img): return idct( idct( img, axis=0, norm='ortho'), axis=1 , norm='ortho') for i, (img_rgb) in enumerate(images): img_ycbcr = rgb2ycbcr(img_rgb) # convert to ycbcr color space img_dct = np.zeros(img_ycbcr.shape) # create dct vector for c in range(img_ycbcr.shape[-1]): img_dct[:,:,c] = dct2(img_ycbcr[:,:,c]) # Dct coefficients if threshold: t = threshold else: t = np.random.randint(0, 51) # Random threshold [0,50] img_dct[abs(img_dct) < t] = 0 # img_ycbcr[:,:,c] = idct2(img_dct[:,:,c]) # Inverse dct images[i] = torch.tensor(ycbcr2rgb(img_ycbcr)) # convert to rgb return images
def save_images(self, filename, save_list, epoch): if self.args.task == 'VideoBDE': f = filename.split('.') dirname = '{}/result/{}/{}'.format(self.dir, self.args.data_test, f[0]) if not os.path.exists(dirname): os.mkdir(dirname) filename = '{}/{}'.format(dirname, f[1]) postfix = ['gt', 'lbd', 'res_iter1', 'res_iter2'] elif self.args.task == 'OpticalFlow': f = filename.split('.') dirname = '{}/result/{}/{}'.format(self.dir, self.args.data_test, f[0]) if not os.path.exists(dirname): os.mkdir(dirname) filename = '{}/{}'.format(dirname, f[1]) postfix = ['gt', 'warped'] else: raise NotImplementedError('Task [{:s}] is not found'.format( self.args.task)) for img, post in zip(save_list, postfix): img = img[0].data # img = np.transpose(img.cpu().numpy(), (1, 2, 0)).astype(np.uint8) img = np.transpose(img.cpu().numpy(), (1, 2, 0)).astype(np.uint16) if img.shape[2] == 1: img = img.squeeze(axis=2) elif img.shape[2] == 3 and self.args.n_colors == 1: img = sc.ycbcr2rgb(img.astype('float')).clip(0, 1) img = (255 * img).round().astype('uint8') # imageio.imwrite('{}_{}.png'.format(filename, post), img) cv2.imwrite('{}_{}.png'.format(filename, post), img)
def coloredHistoEqual(img): img_cvt = sc.rgb2ycbcr(img) # img_cvt = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb) img_cvt[:, :, 0] = se.equalize_hist(img_cvt[:, :, 0]) # img_cvt[:, :, 0] = img_cvt.equalizeHist(img_cvt[:, :, 0]) # img_histogram = cv2.cvtColor(img_cvt, cv2.COLOR_YCrCb2BGR) img_histogram = sc.ycbcr2rgb(img_cvt) return img_histogram
def _save_single_lf_image(save_path, img, mode): c, vh, vw, h, w = list(img.shape) img = np.transpose(img, [1, 3, 2, 4, 0]) img = img.reshape([vh * h, vw * w, c]) if np.max(img) < 1.1: img *= 255 if mode == 'ycbcr': img = color.ycbcr2rgb(img) imageio.imwrite(save_path, img)
def prepare_sm(img_str): img = io.imread(img_str) img_gts.append(img) h, w = img.shape[:2] #img_sm = cv2.resize(img, (w/2,h/2), interpolation=cv2.INTER_CUBIC) img = color.rgb2ycbcr(img).astype(np.float) / 255.0 img_sm = cv2.resize(img, (w / 2, h / 2)) #img_pad = cv2.resize(img_sm, (w,h), interpolation=cv2.INTER_CUBIC) img_pad = cv2.resize(img_sm, (w, h)) img_pads.append(color.ycbcr2rgb(img_pad.astype(np.float) * 255.0)) #img_ycbcrs.append(color.rgb2ycbcr(img_pad).astype(np.float)) img_ycbcrs.append(img_pad)
def local_contrast_weight(img): local_con_wt = np.zeros(img.shape) Ycbcr = rgb2ycbcr(img) Y_factor = Ycbcr[:, :, 0] #print(max(np.ravel(Ycbcr))) laplacian = cv2.Laplacian(Y_factor, cv2.CV_64F) Y_new = np.abs(Y_factor + laplacian) local_con_wt[:, :, 0] = Y_new local_con_wt[:, :, 1] = img[:, :, 1] local_con_wt[:, :, 2] = img[:, :, 2] local_new = ycbcr2rgb(local_con_wt) return local_new
def predict(images, session=None, network=None, targets=None, border=0): session_passed = session is not None if not session_passed: session = tf.Session() if network is None: network = load_model(session) predictions = [] if targets is not None: psnr = [] for i in range(len(images)): image = images[i] if len(image.shape) == 3: image_ycbcr = color.rgb2ycbcr(image) image_y = image_ycbcr[:, :, 0] else: image_y = image.copy() image_y = image_y.astype(np.float) / 255 reshaped_image_y = np.array([np.expand_dims(image_y, axis=2)]) prediction = network.output.eval(feed_dict={network.input: reshaped_image_y}, session=session)[0] prediction *= 255 if targets is not None: if len(targets[i].shape) == 3: target_y = color.rgb2ycbcr(targets[i])[:, :, 0] else: target_y = targets[i].copy() psnr.append(utils.psnr(prediction[border:-border, border:-border, 0], target_y[border:-border, border:-border], maximum=255.0)) if len(image.shape) == 3: prediction = color.ycbcr2rgb(np.concatenate((prediction, image_ycbcr[:, :, 1:3]), axis=2)) * 255 else: prediction = prediction[:, :, 0] prediction = np.clip(prediction, 0, 255).astype(np.uint8) predictions.append(prediction) if not session_passed: session.close() if targets is not None: return predictions, psnr else: return predictions
def GetSRColorImage(img_sr, img_ds): nrow1 = np.shape(img_sr)[0] ncol1 = np.shape(img_sr)[1] # alguma coisa img_us = resize(img_ds, (nrow1, ncol1)) img_us = rgb2ycbcr(img_us) img_sr_min = min(img_sr[:]) img_sr_max = max(img_sr[:]) img_sr = (img_sr - img_sr_min) * img_sr_max / (img_sr_max - img_sr_min) img_us[:, :, 1] = int(img_sr) return ycbcr2rgb(img_us)
def save_images(self, filename, save_list, testset): if self.args.model == 'DBVSR': f = filename.split('.') dirname = '{}/result/{}/{}/{}'.format(self.dir, self.args.data_test, testset, f[0]) #if not os.path.exists(dirname): #os.makedirs(dirname) filename = '{}/{}'.format(dirname, f[1]) postfix = ['dbvsr'] elif self.args.model == 'baseline_lr': f = filename.split('.') dirname = '{}/result/{}/{}/{}'.format(self.dir, self.args.data_test, testset, f[0]) if not os.path.exists(dirname): os.makedirs(dirname) filename = '{}/{}'.format(dirname, f[1]) postfix = ['bl'] elif self.args.model == 'baseline_hr': f = filename.split('.') dirname = '{}/result/{}/{}/{}'.format(self.dir, self.args.data_test, testset, f[0]) if not os.path.exists(dirname): os.makedirs(dirname) filename = '{}/{}'.format(dirname, f[1]) postfix = ['bh'] else: raise NotImplementedError('Model [{:s}] is not found'.format( self.args.model)) for img, post in zip(save_list, postfix): img = img[0].data img = np.transpose(img.cpu().numpy(), (1, 2, 0)).astype(np.uint8) if img.shape[2] == 1: img = img.squeeze(axis=2) elif img.shape[2] == 3 and self.args.n_colors == 1: img = sc.ycbcr2rgb(img.astype('float')).clip(0, 1) img = (255 * img).round().astype('uint8') words = list(filename.split('//')) last = words[-1] from pathlib import Path words = list(last.split('/')) direct = "" for word in words[:-1]: direct += "/" + word Path(direct).mkdir(parents=True, exist_ok=True) imageio.imwrite(f"/{last}.png", img)
def val(net1, epoch, i): with torch.no_grad(): psnr_ac = 0 ssim_ac = 0 for j in range(5): # RGB array:[3, H, W] label = io.imread('./data/test_data/bicubic/X%d/'%opt.scaling_factor + 'img_00' + str(j + 1) + '_SRF_%d_HR'%opt.scaling_factor + '.png') test = io.imread('./data/test_data/bicubic/X%d/'%opt.scaling_factor + 'img_00' + str(j + 1) + '_SRF_%d_LR'%opt.scaling_factor + '.png') label_ycbcr = color.rgb2ycbcr(label) test_ycbcr = color.rgb2ycbcr(test) label_y = label_ycbcr[:, :, 0] / 255 test_y = test_ycbcr[:, :, 0] / 255 label_cb = label_ycbcr[:, :, 1] label_cr = label_ycbcr[:, :, 2] label = torch.FloatTensor(label_y).unsqueeze(0).unsqueeze(0).cuda() test = torch.FloatTensor(test_y).unsqueeze(0).unsqueeze(0).cuda() output = net1(test) output = torch.clamp(output, 0.0, 1.0) loss = (output*255 - label*255).pow(2).sum() / (output.shape[2]*output.shape[3]) psnr = 10*np.log10(255*255 / loss.item()) output = output.squeeze(0).squeeze(0).cpu() label = label.squeeze(0).squeeze(0).cpu() output_array = np.array(output * 255).astype(np.float32) label_array = np.array(label * 255).astype(np.float32) ssim = measure.compare_ssim(output_array, label_array, data_range=255) psnr_ac += psnr ssim_ac += ssim # every 500 batches save test output if i%500 == 0: # synthesize SR image SR_image = np.zeros([*label_array.shape, 3]) SR_image[:, :, 0] = output_array SR_image[:, :, 1] = label_cb SR_image[:, :, 2] = label_cr # SR_image = SR_image.astype(np.uint8) save_index = str(int(epoch*(opt.num_data/opt.batch_size/500) + (i+1)/500)) SR_image = color.ycbcr2rgb(SR_image)*255 SR_image = np.clip(SR_image, a_min=0., a_max=255.) SR_image = SR_image.astype(np.uint8) io.imsave('./data/test_data/bicubic/X%d/test_output/'%opt.scaling_factor + save_index + '.png', SR_image) return loss, psnr_ac/5, ssim_ac/5
def __init__(self, benchmark, scaling_factors=(2, 3, 4)): self.benchmark = benchmark self.scaling_factors = scaling_factors self.images_completed = 0 self.root_path = os.path.join(DATA_PATH, 'test', self.benchmark) self.file_names = os.listdir(self.root_path) self.images = [] self.targets = [] if not os.path.exists(self.root_path): download() for file_name in os.listdir(self.root_path): image = misc.imread(os.path.join(self.root_path, file_name)) width, height = image.shape[0], image.shape[1] width = width - width % 12 height = height - height % 12 image = image[:width, :height] if len(image.shape) == 3: ycbcr = color.rgb2ycbcr(image) y = ycbcr[:, :, 0].astype(np.uint8) else: y = image for scaling_factor in self.scaling_factors: downscaled = misc.imresize(y, 1 / scaling_factor, 'bicubic', mode='L') rescaled = misc.imresize(downscaled, float(scaling_factor), 'bicubic', mode='L') if len(image.shape) == 3: low_res_image = ycbcr low_res_image[:, :, 0] = rescaled low_res_image = color.ycbcr2rgb(low_res_image) low_res_image = (np.clip(low_res_image, 0.0, 1.0) * 255).astype(np.uint8) else: low_res_image = rescaled self.images.append(low_res_image) self.targets.append(image) self.length = len(self.images)
def color_image_save(src_image, im_cb_out, im_cr_out): """ 生成彩色图,并进行像素防溢出警告处理 """ back2src = cv2.merge([src_image * 255, im_cb_out * 255, im_cr_out * 255]) pic = skco.ycbcr2rgb(back2src) # pic = skco.convert_colorspace(back2src, 'YCbCr', 'RGB') # 去掉超出0-255的像素 pic = pic * 255 pic *= (pic > 0) pic = pic * (pic <= 255) + 255 * (pic > 255) pic = pic.astype(np.uint8) dst_image = pic return dst_image
def __init__(self, benchmark, scaling_factors=(2, 4, 8)): self.benchmark = benchmark self.scaling_factors = scaling_factors self.images_completed = 0 self.root_path = os.path.join(DATA_PATH, self.benchmark) self.file_names = os.listdir(self.root_path) self.images = [] self.targets = [] for file_name in tqdm(os.listdir(self.root_path)): image = misc.imread(os.path.join(self.root_path, file_name)) for scaling_factor in self.scaling_factors: if len(image.shape) == 3: ycbcr = color.rgb2ycbcr(image) downscaled = ycbcr[:, :, 0].astype(np.uint8) d_b = ycbcr[:, :, 1].astype(np.uint8) d_r = ycbcr[:, :, 2].astype(np.uint8) else: y = image rescaled = misc.imresize(downscaled, float(scaling_factor), 'bicubic', mode='L') r_b = misc.imresize(d_b, float(scaling_factor), 'bicubic', mode='L') d_r = misc.imresize(d_r, float(scaling_factor), 'bicubic', mode='L') if len(image.shape) == 3: low_res_image = np.stack([rescaled, r_b, d_r], axis=2) low_res_image = low_res_image.astype(np.float64) low_res_image = color.ycbcr2rgb(low_res_image) low_res_image = (np.clip(low_res_image, 0.0, 1.0) * 255).astype(np.uint8) else: low_res_image = rescaled self.images.append(low_res_image) self.targets.append(image) self.length = len(self.images)
def save_images(self, filename, save_list, scale): f = filename.split('.') filename = '{}/result/{}/{}/{}_'.format(self.dir, self.args.data_test, f[0], f[1].zfill(8)) if not os.path.exists(os.path.dirname(filename)): os.makedirs(os.path.dirname(filename)) postfix = ['SR'] for img, post in zip(save_list, postfix): img = img[0].data img = np.transpose(img.cpu().numpy(), (1, 2, 0)) if img.shape[2] == 1: img = img.squeeze(axis=2) elif img.shape[2] == 3 and self.args.n_colors == 1: img = sc.ycbcr2rgb(img.astype('float')).clip(0, 1) img = (255 * img).round().astype('uint8') imageio.imsave('{}{}.png'.format(filename, post), img_as_ubyte(img))
def colorize_ycbcr(y0, y1, y2): img = np.zeros((y0.shape[0], y0.shape[1], 3), np.float) img[:,:,0] = y0 img[:,:,1] = y1 img[:,:,2] = y2 img = color.ycbcr2rgb(img.astype(np.float)*255.0) ha, wa, ca = np.shape(img) for c in range(ca): for w in range(wa): for h in range(ha): if img[h,w,c] > 1.0: img[h,w,c] = 1.0 elif img[h,w,c] < -1.0: img[h,w,c] = -1.0 return img
def waveletDenoiseColor(input_image, shrinkage_parameter): if not is_input_valid_color(input_image): return # Do denoising in ycbcr for aesthetic reasons ycbcr_image = color.rgb2ycbcr(input_image) output_image = np.zeros_like(ycbcr_image) # We multiply the thresholds by the new maxima for the channels output_image[:, :, 0] = waveletDenoise(ycbcr_image[:, :, 0], 235. * shrinkage_parameter) output_image[:, :, 1] = waveletDenoise(ycbcr_image[:, :, 1], 230. * shrinkage_parameter) output_image[:, :, 2] = waveletDenoise(ycbcr_image[:, :, 2], 240. * shrinkage_parameter) output_image = color.ycbcr2rgb(output_image) return output_image
def test_yuv_roundtrip(self, channel_axis): img_rgb = img_as_float(self.img_rgb)[::16, ::16] img_rgb = np.moveaxis(img_rgb, source=-1, destination=channel_axis) assert_array_almost_equal( yuv2rgb(rgb2yuv(img_rgb, channel_axis=channel_axis), channel_axis=channel_axis), img_rgb) assert_array_almost_equal( yiq2rgb(rgb2yiq(img_rgb, channel_axis=channel_axis), channel_axis=channel_axis), img_rgb) assert_array_almost_equal( ypbpr2rgb(rgb2ypbpr(img_rgb, channel_axis=channel_axis), channel_axis=channel_axis), img_rgb) assert_array_almost_equal( ycbcr2rgb(rgb2ycbcr(img_rgb, channel_axis=channel_axis), channel_axis=channel_axis), img_rgb) assert_array_almost_equal( ydbdr2rgb(rgb2ydbdr(img_rgb, channel_axis=channel_axis), channel_axis=channel_axis), img_rgb)
def save_images(self, filename, save_list, testset): if self.args.model == 'DBVSR': f = filename.split('.') dirname = '{}/result/{}/{}/{}'.format(self.dir, self.args.data_test, testset, f[0]) if not os.path.exists(dirname): os.makedirs(dirname) filename = '{}/{}'.format(dirname, f[1]) postfix = ['dbvsr'] elif self.args.model == 'baseline_lr': f = filename.split('.') dirname = '{}/result/{}/{}/{}'.format(self.dir, self.args.data_test, testset, f[0]) if not os.path.exists(dirname): os.makedirs(dirname) filename = '{}/{}'.format(dirname, f[1]) postfix = ['bl'] elif self.args.model == 'baseline_hr': f = filename.split('.') dirname = '{}/result/{}/{}/{}'.format(self.dir, self.args.data_test, testset, f[0]) if not os.path.exists(dirname): os.makedirs(dirname) filename = '{}/{}'.format(dirname, f[1]) postfix = ['bh'] else: raise NotImplementedError('Model [{:s}] is not found'.format( self.args.model)) for img, post in zip(save_list, postfix): img = img[0].data img = np.transpose(img.cpu().numpy(), (1, 2, 0)).astype(np.uint8) if img.shape[2] == 1: img = img.squeeze(axis=2) elif img.shape[2] == 3 and self.args.n_colors == 1: img = sc.ycbcr2rgb(img.astype('float')).clip(0, 1) img = (255 * img).round().astype('uint8') imageio.imwrite('{}_{}.png'.format(filename, post), img)
def save_train_images(self, save_list, epoch, batch): if self.args.model == 'DBVSR': dirname = '{}/result/{}/train'.format(self.dir, self.args.data_test) if not os.path.exists(dirname): os.mkdir(dirname) filename = '{}/{}epoch_{}iters'.format(dirname, batch, epoch) postfix = ['train_map'] else: raise NotImplementedError('Model [{:s}] is not found'.format( self.args.model)) for img, post in zip(save_list, postfix): img = img[0].data img = np.transpose(img.cpu().numpy(), (1, 2, 0)).astype(np.uint8) if img.shape[2] == 1: img = img.squeeze(axis=2) elif img.shape[2] == 3 and self.args.n_colors == 1: img = sc.ycbcr2rgb(img.astype('float')).clip(0, 1) img = (255 * img).round().astype('uint8') imageio.imwrite('{}_{}.png'.format(filename, post), img)
def denoise_wavelet(image, num_coeffs=None, mode='hard', wavelet_levels=None, multichannel=False, to_ycbcr=False): image = img_as_float(image) if multichannel: if to_ycbcr: out = color.rgb2ycbcr(image.transpose(1, 2, 0)) for i in range(3): min, max = out[..., i].min(), out[..., i].max() channel = out[..., i] - min channel /= max - min out[..., i] = denoise_wavelet(channel, num_coeffs=num_coeffs, mode=mode, wavelet_levels=wavelet_levels) out[..., i] = out[..., i] * (max - min) out[..., i] += min out = color.ycbcr2rgb(out) out = out.transpose(2, 0, 1) else: out = np.empty_like(image) for c in range(image.shape[-1]): out[..., c] = wavelet_recover(image[..., c], num_coeffs=num_coeffs, mode=mode, wavelet_levels=wavelet_levels) else: out = wavelet_recover(image, mode=mode, num_coeffs=num_coeffs, wavelet_levels=wavelet_levels) if (image.min() < 0): reconstruct_image_np = np.clip(out, *(-1, 1)) else: reconstruct_image_np = np.clip(out, *(0, 1)) return reconstruct_image_np
def merge_test_RGB(images, size): loc = size[2] #读取其他CbCr通道的数据将Images变为[?,21,21,3] with h5py.File(os.getcwd() + "/checkpoint/CbANDCrlabel.h5", 'r') as hf: dataCbANDCrimage = np.array(hf.get("CbANDCrlabel")) for i in range(0, len(loc)): x = loc[i] x = [x[0] - 6, x[1] - 6] loc[i] = x h, w = loc[len(loc) - 1][0] + 21, loc[len(loc) - 1][1] + 21 img = np.zeros((h, w, 3)) for i, image in enumerate(images): x, y = loc[i] img[x:x + 21, y:y + 21, 0:1] = image img[x:x + 21, y:y + 21, 1:] = dataCbANDCrimage[i] #将YCbCR变为RGB img = img * 255 img = ycbcr2rgb(img) return img
def reassemble_rgb(recovered_luminance, ycbcr): if not isinstance(recovered_luminance, np.ndarray): recovered_luminance = recovered_luminance.clone().numpy() if not isinstance(ycbcr, np.ndarray): ycbcr = ycbcr.clone().numpy() while recovered_luminance.ndim != 2: recovered_luminance = recovered_luminance[0, ...] while ycbcr.ndim != 3: ycbcr = ycbcr[0, ...] recovered = np.zeros(ycbcr.shape) recovered[..., 0] = recovered_luminance recovered[..., 1] = normalize_img(ycbcr[..., 1]) recovered[..., 2] = normalize_img(ycbcr[..., 2]) recovered_rgb = ycbcr2rgb(recovered * 255).clip(0, 1) return img_as_float(recovered_rgb)
def lower_resolution(image, scaling_factor, max_value=1.0): assert max_value in [1.0, 255] if max_value == 1.0: image = (image * 255).astype(np.uint8) else: assert image.dtype == 'uint8' width = image.shape[0] height = image.shape[1] if len(image.shape) == 3: image_ycbcr = color.rgb2ycbcr(image) image_y = image_ycbcr[:, :, 0].astype(np.uint8) else: image_y = image.copy() downscaled = misc.imresize(image_y, 1 / float(scaling_factor), 'bicubic', mode='L') rescaled = misc.imresize(downscaled, (width, height), 'bicubic', mode='L').astype(np.float32) if len(image.shape) == 3: low_res_image = image_ycbcr low_res_image[:, :, 0] = rescaled low_res_image = color.ycbcr2rgb(low_res_image) low_res_image = (np.clip(low_res_image, 0.0, 1.0) * 255).astype( np.uint8) else: low_res_image = rescaled.astype(np.uint8) if max_value == 1.0: return low_res_image / 255 else: return low_res_image
def save_images(self, filename, save_list, scale): if self.args.task == 'Image': filename = '{}/result/{}/{}_x{}_'.format(self.dir, self.args.data_test, filename, scale) postfix = ['LR', 'HR', 'SR'] elif self.args.task == 'MC': f = filename.split('.') filename = '{}/result/{}/{}/{}_'.format(self.dir, self.args.data_test, f[0], f[1]) if not os.path.exists(os.path.dirname(filename)): os.makedirs(os.path.dirname(filename)) postfix = ['f1', 'f2', 'f2c'] elif self.args.task == 'Video': f = filename.split('.') filename = '{}/result/{}/{}/{}_'.format(self.dir, self.args.data_test, f[0], f[1]) if not os.path.exists(os.path.dirname(filename)): os.makedirs(os.path.dirname(filename)) postfix = ['LR', 'HR', 'SR'] for img, post in zip(save_list, postfix): img = img[0].data.mul(255 / self.args.rgb_range) img = np.transpose(img.cpu().numpy(), (1, 2, 0)) if img.shape[2] == 1: img = img.squeeze(axis=2) elif img.shape[2] == 3 and self.args.n_colors == 1: img = sc.ycbcr2rgb(img.astype('float')).clip(0, 1) img = (255 * img).round().astype('uint8') #img = img[:,:,0].round().astype('uint8') if post == 'LR': img = misc.imresize(img, size=self.args.scale * 100, interp='bicubic') imageio.imwrite('{}{}.png'.format(filename, post), img)
def makeRGB(): dir = os.getcwd() + "\Test\Set5" data = glob.glob(os.path.join(dir, "*.bmp")) result_dir = os.getcwd() + "/RGBtest/" # 以YCbCr色彩空间打开,分别存储,通道 Image = imread(data[2], flatten=False, mode="YCbCr").astype(np.float) Yimage = Image[:, :, 0] CbANDCrimage = Image[:, :, 1:] imsave(result_dir + "Yimage.bmp", Yimage) #存储Y通道 #存储其他两个通道 with h5py.File(os.getcwd() + "/RGBtest/CbANDCrimage.h5", 'w') as hf: hf.create_dataset("CbANDCrimage", data=CbANDCrimage) #打开两个其他通道 with h5py.File(os.getcwd() + "/RGBtest/CbANDCrimage.h5", 'r') as hf: dataCbANDCrimage = np.array(hf.get("CbANDCrimage")) #组合在一起 coimage = np.zeros([Yimage.shape[0], Yimage.shape[1], 3]) Yimage = Yimage.reshape([Yimage.shape[0], Yimage.shape[0], 1]) coimage[:, :, 0:1] = Yimage coimage[:, :, 1:] = dataCbANDCrimage imsave(result_dir + "YCbCr组合结果.bmp", coimage) imsave(result_dir + "RGB组合结果.bmp", ycbcr2rgb(coimage))
def denoise_wavelet(image, sigma=None, wavelet='db1', mode='soft', wavelet_levels=None, multichannel=False, convert2ycbcr=False, method='BayesShrink'): """Perform wavelet denoising on an image. Parameters ---------- image : ndarray ([M[, N[, ...P]][, C]) of ints, uints or floats Input data to be denoised. `image` can be of any numeric type, but it is cast into an ndarray of floats for the computation of the denoised image. sigma : float or list, optional The noise standard deviation used when computing the wavelet detail coefficient threshold(s). When None (default), the noise standard deviation is estimated via the method in [2]_. wavelet : string, optional The type of wavelet to perform and can be any of the options ``pywt.wavelist`` outputs. The default is `'db1'`. For example, ``wavelet`` can be any of ``{'db2', 'haar', 'sym9'}`` and many more. mode : {'soft', 'hard'}, optional An optional argument to choose the type of denoising performed. It noted that choosing soft thresholding given additive noise finds the best approximation of the original image. wavelet_levels : int or None, optional The number of wavelet decomposition levels to use. The default is three less than the maximum number of possible decomposition levels. multichannel : bool, optional Apply wavelet denoising separately for each channel (where channels correspond to the final axis of the array). convert2ycbcr : bool, optional If True and multichannel True, do the wavelet denoising in the YCbCr colorspace instead of the RGB color space. This typically results in better performance for RGB images. method : {'BayesShrink', 'VisuShrink'}, optional Thresholding method to be used. The currently supported methods are "BayesShrink" [1]_ and "VisuShrink" [2]_. Defaults to "BayesShrink". Returns ------- out : ndarray Denoised image. Notes ----- The wavelet domain is a sparse representation of the image, and can be thought of similarly to the frequency domain of the Fourier transform. Sparse representations have most values zero or near-zero and truly random noise is (usually) represented by many small values in the wavelet domain. Setting all values below some threshold to 0 reduces the noise in the image, but larger thresholds also decrease the detail present in the image. If the input is 3D, this function performs wavelet denoising on each color plane separately. The output image is clipped between either [-1, 1] and [0, 1] depending on the input image range. When YCbCr conversion is done, every color channel is scaled between 0 and 1, and `sigma` values are applied to these scaled color channels. Many wavelet coefficient thresholding approaches have been proposed. By default, ``denoise_wavelet`` applies BayesShrink, which is an adaptive thresholding method that computes separate thresholds for each wavelet sub-band as described in [1]_. If ``method == "VisuShrink"``, a single "universal threshold" is applied to all wavelet detail coefficients as described in [2]_. This threshold is designed to remove all Gaussian noise at a given ``sigma`` with high probability, but tends to produce images that appear overly smooth. References ---------- .. [1] Chang, S. Grace, Bin Yu, and Martin Vetterli. "Adaptive wavelet thresholding for image denoising and compression." Image Processing, IEEE Transactions on 9.9 (2000): 1532-1546. DOI: 10.1109/83.862633 .. [2] D. L. Donoho and I. M. Johnstone. "Ideal spatial adaptation by wavelet shrinkage." Biometrika 81.3 (1994): 425-455. DOI: 10.1093/biomet/81.3.425 Examples -------- >>> from skimage import color, data >>> img = img_as_float(data.astronaut()) >>> img = color.rgb2gray(img) >>> img += 0.1 * np.random.randn(*img.shape) >>> img = np.clip(img, 0, 1) >>> denoised_img = denoise_wavelet(img, sigma=0.1) """ if method not in ["BayesShrink", "VisuShrink"]: raise ValueError( ('Invalid method: {}. The currently supported methods are ' '"BayesShrink" and "VisuShrink"').format(method)) image = img_as_float(image) if multichannel: if isinstance(sigma, numbers.Number) or sigma is None: sigma = [sigma] * image.shape[-1] if multichannel: if convert2ycbcr: out = color.rgb2ycbcr(image) for i in range(3): # renormalizing this color channel to live in [0, 1] min, max = out[..., i].min(), out[..., i].max() channel = out[..., i] - min channel /= max - min out[..., i] = denoise_wavelet(channel, wavelet=wavelet, method=method, sigma=sigma[i], mode=mode, wavelet_levels=wavelet_levels) out[..., i] = out[..., i] * (max - min) out[..., i] += min out = color.ycbcr2rgb(out) else: out = np.empty_like(image) for c in range(image.shape[-1]): out[..., c] = _wavelet_threshold(image[..., c], wavelet=wavelet, method=method, sigma=sigma[c], mode=mode, wavelet_levels=wavelet_levels) else: out = _wavelet_threshold(image, wavelet=wavelet, method=method, sigma=sigma, mode=mode, wavelet_levels=wavelet_levels) clip_range = (-1, 1) if image.min() < 0 else (0, 1) return np.clip(out, *clip_range)
def denoise_wavelet(image, sigma=None, wavelet='db1', mode='soft', wavelet_levels=None, multichannel=False, convert2ycbcr=False, method='BayesShrink'): """Perform wavelet denoising on an image. Parameters ---------- image : ndarray ([M[, N[, ...P]][, C]) of ints, uints or floats Input data to be denoised. `image` can be of any numeric type, but it is cast into an ndarray of floats for the computation of the denoised image. sigma : float or list, optional The noise standard deviation used when computing the wavelet detail coefficient threshold(s). When None (default), the noise standard deviation is estimated via the method in [2]_. wavelet : string, optional The type of wavelet to perform and can be any of the options ``pywt.wavelist`` outputs. The default is `'db1'`. For example, ``wavelet`` can be any of ``{'db2', 'haar', 'sym9'}`` and many more. mode : {'soft', 'hard'}, optional An optional argument to choose the type of denoising performed. It noted that choosing soft thresholding given additive noise finds the best approximation of the original image. wavelet_levels : int or None, optional The number of wavelet decomposition levels to use. The default is three less than the maximum number of possible decomposition levels. multichannel : bool, optional Apply wavelet denoising separately for each channel (where channels correspond to the final axis of the array). convert2ycbcr : bool, optional If True and multichannel True, do the wavelet denoising in the YCbCr colorspace instead of the RGB color space. This typically results in better performance for RGB images. method : {'BayesShrink', 'VisuShrink'}, optional Thresholding method to be used. The currently supported methods are "BayesShrink" [1]_ and "VisuShrink" [2]_. Defaults to "BayesShrink". Returns ------- out : ndarray Denoised image. Notes ----- The wavelet domain is a sparse representation of the image, and can be thought of similarly to the frequency domain of the Fourier transform. Sparse representations have most values zero or near-zero and truly random noise is (usually) represented by many small values in the wavelet domain. Setting all values below some threshold to 0 reduces the noise in the image, but larger thresholds also decrease the detail present in the image. If the input is 3D, this function performs wavelet denoising on each color plane separately. The output image is clipped between either [-1, 1] and [0, 1] depending on the input image range. When YCbCr conversion is done, every color channel is scaled between 0 and 1, and `sigma` values are applied to these scaled color channels. Many wavelet coefficient thresholding approaches have been proposed. By default, ``denoise_wavelet`` applies BayesShrink, which is an adaptive thresholding method that computes separate thresholds for each wavelet sub-band as described in [1]_. If ``method == "VisuShrink"``, a single "universal threshold" is applied to all wavelet detail coefficients as described in [2]_. This threshold is designed to remove all Gaussian noise at a given ``sigma`` with high probability, but tends to produce images that appear overly smooth. Although any of the wavelets from ``PyWavelets`` can be selected, the thresholding methods assume an orthogonal wavelet transform and may not choose the threshold appropriately for biorthogonal wavelets. Orthogonal wavelets are desirable because white noise in the input remains white noise in the subbands. Biorthogonal wavelets lead to colored noise in the subbands. Additionally, the orthogonal wavelets in PyWavelets are orthonormal so that noise variance in the subbands remains identical to the noise variance of the input. Example orthogonal wavelets are the Daubechies (e.g. 'db2') or symmlet (e.g. 'sym2') families. References ---------- .. [1] Chang, S. Grace, Bin Yu, and Martin Vetterli. "Adaptive wavelet thresholding for image denoising and compression." Image Processing, IEEE Transactions on 9.9 (2000): 1532-1546. :DOI:`10.1109/83.862633` .. [2] D. L. Donoho and I. M. Johnstone. "Ideal spatial adaptation by wavelet shrinkage." Biometrika 81.3 (1994): 425-455. :DOI:`10.1093/biomet/81.3.425` Examples -------- >>> from skimage import color, data >>> img = img_as_float(data.astronaut()) >>> img = color.rgb2gray(img) >>> img += 0.1 * np.random.randn(*img.shape) >>> img = np.clip(img, 0, 1) >>> denoised_img = denoise_wavelet(img, sigma=0.1) """ if method not in ["BayesShrink", "VisuShrink"]: raise ValueError( ('Invalid method: {}. The currently supported methods are ' '"BayesShrink" and "VisuShrink"').format(method)) image = img_as_float(image) if multichannel: if isinstance(sigma, numbers.Number) or sigma is None: sigma = [sigma] * image.shape[-1] if multichannel: if convert2ycbcr: out = color.rgb2ycbcr(image) for i in range(3): # renormalizing this color channel to live in [0, 1] min, max = out[..., i].min(), out[..., i].max() channel = out[..., i] - min channel /= max - min out[..., i] = denoise_wavelet(channel, wavelet=wavelet, method=method, sigma=sigma[i], mode=mode, wavelet_levels=wavelet_levels) out[..., i] = out[..., i] * (max - min) out[..., i] += min out = color.ycbcr2rgb(out) else: out = np.empty_like(image) for c in range(image.shape[-1]): out[..., c] = _wavelet_threshold(image[..., c], wavelet=wavelet, method=method, sigma=sigma[c], mode=mode, wavelet_levels=wavelet_levels) else: out = _wavelet_threshold(image, wavelet=wavelet, method=method, sigma=sigma, mode=mode, wavelet_levels=wavelet_levels) clip_range = (-1, 1) if image.min() < 0 else (0, 1) return np.clip(out, *clip_range)
def denoise_wavelet(image, sigma=None, wavelet='db1', mode='soft', wavelet_levels=None, multichannel=False, convert2ycbcr=False, method='BayesShrink', rescale_sigma=True): """Perform wavelet denoising on an image. Parameters ---------- image : ndarray ([M[, N[, ...P]][, C]) of ints, uints or floats Input data to be denoised. `image` can be of any numeric type, but it is cast into an ndarray of floats for the computation of the denoised image. sigma : float or list, optional The noise standard deviation used when computing the wavelet detail coefficient threshold(s). When None (default), the noise standard deviation is estimated via the method in [2]_. wavelet : string, optional The type of wavelet to perform and can be any of the options ``pywt.wavelist`` outputs. The default is `'db1'`. For example, ``wavelet`` can be any of ``{'db2', 'haar', 'sym9'}`` and many more. mode : {'soft', 'hard'}, optional An optional argument to choose the type of denoising performed. It noted that choosing soft thresholding given additive noise finds the best approximation of the original image. wavelet_levels : int or None, optional The number of wavelet decomposition levels to use. The default is three less than the maximum number of possible decomposition levels. multichannel : bool, optional Apply wavelet denoising separately for each channel (where channels correspond to the final axis of the array). convert2ycbcr : bool, optional If True and multichannel True, do the wavelet denoising in the YCbCr colorspace instead of the RGB color space. This typically results in better performance for RGB images. method : {'BayesShrink', 'VisuShrink'}, optional Thresholding method to be used. The currently supported methods are "BayesShrink" [1]_ and "VisuShrink" [2]_. Defaults to "BayesShrink". rescale_sigma : bool, optional If False, no rescaling of the user-provided ``sigma`` will be performed. The default of ``True`` rescales sigma appropriately if the image is rescaled internally. .. versionadded:: 0.16 ``rescale_sigma`` was introduced in 0.16 Returns ------- out : ndarray Denoised image. Notes ----- The wavelet domain is a sparse representation of the image, and can be thought of similarly to the frequency domain of the Fourier transform. Sparse representations have most values zero or near-zero and truly random noise is (usually) represented by many small values in the wavelet domain. Setting all values below some threshold to 0 reduces the noise in the image, but larger thresholds also decrease the detail present in the image. If the input is 3D, this function performs wavelet denoising on each color plane separately. .. versionchanged:: 0.16 For floating point inputs, the original input range is maintained and there is no clipping applied to the output. Other input types will be converted to a floating point value in the range [-1, 1] or [0, 1] depending on the input image range. Unless ``rescale_sigma = False``, any internal rescaling applied to the ``image`` will also be applied to ``sigma`` to maintain the same relative amplitude. Many wavelet coefficient thresholding approaches have been proposed. By default, ``denoise_wavelet`` applies BayesShrink, which is an adaptive thresholding method that computes separate thresholds for each wavelet sub-band as described in [1]_. If ``method == "VisuShrink"``, a single "universal threshold" is applied to all wavelet detail coefficients as described in [2]_. This threshold is designed to remove all Gaussian noise at a given ``sigma`` with high probability, but tends to produce images that appear overly smooth. Although any of the wavelets from ``PyWavelets`` can be selected, the thresholding methods assume an orthogonal wavelet transform and may not choose the threshold appropriately for biorthogonal wavelets. Orthogonal wavelets are desirable because white noise in the input remains white noise in the subbands. Biorthogonal wavelets lead to colored noise in the subbands. Additionally, the orthogonal wavelets in PyWavelets are orthonormal so that noise variance in the subbands remains identical to the noise variance of the input. Example orthogonal wavelets are the Daubechies (e.g. 'db2') or symmlet (e.g. 'sym2') families. References ---------- .. [1] Chang, S. Grace, Bin Yu, and Martin Vetterli. "Adaptive wavelet thresholding for image denoising and compression." Image Processing, IEEE Transactions on 9.9 (2000): 1532-1546. :DOI:`10.1109/83.862633` .. [2] D. L. Donoho and I. M. Johnstone. "Ideal spatial adaptation by wavelet shrinkage." Biometrika 81.3 (1994): 425-455. :DOI:`10.1093/biomet/81.3.425` Examples -------- >>> from skimage import color, data >>> img = img_as_float(data.astronaut()) >>> img = color.rgb2gray(img) >>> img += 0.1 * np.random.randn(*img.shape) >>> img = np.clip(img, 0, 1) >>> denoised_img = denoise_wavelet(img, sigma=0.1, rescale_sigma=True) """ if method not in ["BayesShrink", "VisuShrink"]: raise ValueError( ('Invalid method: {}. The currently supported methods are ' '"BayesShrink" and "VisuShrink"').format(method)) # floating-point inputs are not rescaled, so don't clip their output. clip_output = image.dtype.kind != 'f' if convert2ycbcr and not multichannel: raise ValueError("convert2ycbcr requires multichannel == True") image, sigma = _scale_sigma_and_image_consistently(image, sigma, multichannel, rescale_sigma) if multichannel: if convert2ycbcr: out = color.rgb2ycbcr(image) # convert user-supplied sigmas to the new colorspace as well if rescale_sigma: sigma = _rescale_sigma_rgb2ycbcr(sigma) for i in range(3): # renormalizing this color channel to live in [0, 1] _min, _max = out[..., i].min(), out[..., i].max() scale_factor = _max - _min if scale_factor == 0: # skip any channel containing only zeros! continue channel = out[..., i] - _min channel /= scale_factor sigma_channel = sigma[i] if sigma_channel is not None: sigma_channel /= scale_factor out[..., i] = denoise_wavelet(channel, wavelet=wavelet, method=method, sigma=sigma_channel, mode=mode, wavelet_levels=wavelet_levels, rescale_sigma=rescale_sigma) out[..., i] = out[..., i] * scale_factor out[..., i] += _min out = color.ycbcr2rgb(out) else: out = np.empty_like(image) for c in range(image.shape[-1]): out[..., c] = _wavelet_threshold(image[..., c], wavelet=wavelet, method=method, sigma=sigma[c], mode=mode, wavelet_levels=wavelet_levels) else: out = _wavelet_threshold(image, wavelet=wavelet, method=method, sigma=sigma, mode=mode, wavelet_levels=wavelet_levels) if clip_output: clip_range = (-1, 1) if image.min() < 0 else (0, 1) out = np.clip(out, *clip_range, out=out) return out
def Upsample4D(path=None, save_path=None, ext='png', angular_size=-1, crop_length=7, factor=3, adjust_tone=0.0, save_results=False): options = locals().copy() if path is not None: log.info('=' * 40) if not os.path.exists(path): raise IOError('No such folder: {}'.format(path)) # if save_path is None: # save_path = path+'_up4d/' # if not os.path.exists(save_path): # log.warning('No such path for saving Our results, creating dir {}' # .format(save_path)) # os.mkdir(save_path) save_path = path + '_up4d' os.makedirs(save_path, exist_ok=True) sceneNameTuple = getSceneNameFromPath(path, ext, angular_size) if len(sceneNameTuple) == 0: raise IOError('Not any .%s file found in %s' % (ext, path)) else: scene_num = len(sceneNameTuple) else: raise NameError('No folder given.') log_file = os.path.join( save_path, 'test_%s.log' % datetime.now().strftime("%Y%m%d%H%M")) fh = logging.FileHandler(log_file) log.addHandler(fh) total_PSNR = [] total_SSIM = [] total_Elapsedtime = [] performacne_index_file = os.path.join(save_path, 'performance_stat.mat') # log.info('-'*40) # log.info('-'*40) # log.info('...Loading Pre-trained Model'+model_path) # model = p4dcnn_model(lr_angular_size=crop_length//factor + 1, # height=HEIGHT, # width=WIDTH, # channel=1, # hr_angular_size=crop_length, # dilation_rate=dilation_rate) # model.load_weights(model_path) # log.info('Model Successfully Loaded in.') # log.info('-'*40) # log.info('-'*40) for indx, scene in enumerate(sceneNameTuple): log.info('=' * 20 + '[' + str(indx + 1) + '/' + str(len(sceneNameTuple)) + '] ' + str(scene) + '=' * 20) if save_results: our_save_path = os.path.join(save_path, scene + '_U4D') # GT_save_path = os.path.join(save_path, scene + '_GT') if os.path.isdir(our_save_path): log.info('-' * 20) del_files(our_save_path, 'png') log.warning('Ours Save Path %s exists, delete all .png files' % our_save_path) else: os.mkdir(our_save_path) # if os.path.isdir(GT_save_path): # del_files(GT_save_path, 'png') # log.info('GT path %s exists, delete all .png files' % GT_save_path) # else: # os.mkdir(GT_save_path) tic = time.time() if angular_size > 1: img_filename = os.path.join(path, scene + '.' + ext) lf = ImgTo4DLF(filename=img_filename, vnum=angular_size, unum=angular_size, length=crop_length, adjust_tone=adjust_tone, save_sub_flag=False) else: scene_folder = os.path.join(path, scene) lf = FolderTo4DLF(scene_folder, ext, crop_length) (g_u, g_v, g_h, g_w, g_c) = lf.shape gt_lf = lf # if g_h != HEIGHT or g_w != WIDTH: # log.warning('!!! Input LF h-w [%d %d] not required [%d %d] padding zeros to border !!!' % # (lf.shape[2], lf.shape[3], HEIGHT, WIDTH)) # log.info('='*40) # gt_lf = pad_lf(lf) # else: # gt_lf = lf input_sparse_lf = gt_lf[0:crop_length:factor, 0:crop_length:factor, :, :, 0] (s_u, s_v, s_h, s_w) = input_sparse_lf.shape log.info("Elapsed time: %.2f sec" % (time.time() - tic)) log.info('-' * 40) log.info('...Upsampling 4D Light fields') tic = time.time() intermediate_lf = np.zeros((g_u, s_v, g_h, s_w), dtype=np.float32) out_dense_lf = np.zeros((g_u, g_v, g_h, g_w), dtype=np.float32) log.info('First Along x-s Epi') for v in range(s_v): for w in range(s_w): uh_epi = input_sparse_lf[:, v, :, w] uh_epi = cv2.resize(uh_epi, (g_h, g_u), interpolation=cv2.INTER_LINEAR) intermediate_lf[:, v, :, w] = uh_epi log.info('Then Along y-t Epi') for u in range(g_u): for h in range(g_h): vw_epi = intermediate_lf[u, :, h, :] vw_epi = cv2.resize(vw_epi, (g_w, g_v), interpolation=cv2.INTER_LINEAR) out_dense_lf[u, :, h, :] = vw_epi s_res = out_dense_lf.shape[0] t_res = out_dense_lf.shape[1] x_res = out_dense_lf.shape[2] y_res = out_dense_lf.shape[3] # channel = out_dense_lf.shape[4] processed_time = time.time() - tic log.info("Elapsed time: %.2f sec" % processed_time) PSNR = [] SSIM = [] log.info('-' * 40) log.info('Evaluation......') for s_n in range(s_res): for t_n in range(t_res): gt_img = lf[s_n, t_n, :, :, 0] # print('GT range: %.2f-%.2f' %(gt_img.min(),gt_img.max())) view_img = np.clip(out_dense_lf[s_n, t_n, :, :], gt_img.min(), gt_img.max()) # if not(u % factor == 0 and v % factor == 0): # this_test_loss = np.sqrt(mse(view_img,gt_img)) # this_PSNR = 20*math.log(1.0/this_test_loss,10) if s_n % factor == 0 and t_n % factor == 0: log.info('[o] View %.2d_%.2d: Original view not included' % (s_n + 1, t_n + 1)) else: this_PSNR = psnr(view_img, gt_img) this_SSIM = ssim(view_img, gt_img) PSNR.append(this_PSNR) SSIM.append(this_SSIM) log.info('[n] View %.2d_%.2d: PSNR: %.2fdB SSIM: %.4f' % (s_n + 1, t_n + 1, this_PSNR, this_SSIM)) if save_results: filename = os.path.join( our_save_path, 'View_' + str(s_n + 1) + '_' + str(t_n + 1) + '.png') # GTname = os.path.join(GT_save_path, 'View_'+str(s_n+1)+'_'+str(t_n+1)+'.png') out_img = np.zeros((x_res, y_res, 3)) # gt_out_img = np.zeros((x_res, y_res, 3)) out_img[:, :, 0] = np.clip(view_img * 255.0, 16.0, 235.0) # gt_out_img[:, :, 0] = np.clip(gt_img*255.0, 16.0, 235.0) # print('Max: %.2f Min: %.2f' %(out_img[:,:,0].max(),out_img[:,:,0].min())) out_img[:, :, 1:3] = lf[s_n, t_n, :, :, 1:3] # gt_out_img[:, :, 1:3] = lf[s_n, t_n, :, :, 1:3] # print('Max: %.2f Min: %.2f' %(out_img[:,:,1].max(),out_img[:,:,1].min())) out_img = color.ycbcr2rgb(out_img) out_img = np.clip(out_img, 0.0, 1.0) out_img = np.uint8(out_img * 255.0) # gt_out_img = color.ycbcr2rgb(gt_out_img) # gt_out_img = np.clip(gt_out_img, 0.0, 1.0) # gt_out_img = np.uint8(gt_out_img*255.0) io.imsave(filename, out_img) # io.imsave(GTname, gt_out_img) log.info('=' * 40) total_PSNR.append(np.mean(np.array(PSNR))) total_SSIM.append(np.mean(np.array(SSIM))) total_Elapsedtime.append(processed_time) log.info( "Average PSNR: %.2f dB\nSSIM: %.4f\nElapsed time: %.2f sec" % (np.mean(np.array(PSNR)), np.mean(np.array(SSIM)), processed_time)) gc.collect() log.info('=' * 40) log.info('=' * 6 + 'Average Performance on %d scenes' % scene_num + '=' * 6) log.info('PSNR: %.2f dB' % np.mean(np.array(total_PSNR))) log.info('SSIM: %.4f' % np.mean(np.array(total_SSIM))) log.info('Elapsed Time: %.2f sec' % np.mean(np.array(total_Elapsedtime))) log.info('=' * 40) embeded = dict(NAME=sceneNameTuple, PSNR=np.array(total_PSNR), SSIM=np.array(total_SSIM), TIME=np.array(total_Elapsedtime)) sio.savemat(performacne_index_file, embeded)
def denoise_wavelet(img, sigma=None, wavelet='db1', mode='soft', wavelet_levels=None, multichannel=False, convert2ycbcr=False): """Perform wavelet denoising on an image. Parameters ---------- img : ndarray ([M[, N[, ...P]][, C]) of ints, uints or floats Input data to be denoised. `img` can be of any numeric type, but it is cast into an ndarray of floats for the computation of the denoised image. sigma : float or list, optional The noise standard deviation used when computing the threshold adaptively as described in [1]_ for each color channel. When None (default), the noise standard deviation is estimated via the method in [2]_. wavelet : string, optional The type of wavelet to perform and can be any of the options ``pywt.wavelist`` outputs. The default is `'db1'`. For example, ``wavelet`` can be any of ``{'db2', 'haar', 'sym9'}`` and many more. mode : {'soft', 'hard'}, optional An optional argument to choose the type of denoising performed. It noted that choosing soft thresholding given additive noise finds the best approximation of the original image. wavelet_levels : int or None, optional The number of wavelet decomposition levels to use. The default is three less than the maximum number of possible decomposition levels. multichannel : bool, optional Apply wavelet denoising separately for each channel (where channels correspond to the final axis of the array). convert2ycbcr : bool, optional If True and multichannel True, do the wavelet denoising in the YCbCr colorspace instead of the RGB color space. This typically results in better performance for RGB images. Returns ------- out : ndarray Denoised image. Notes ----- The wavelet domain is a sparse representation of the image, and can be thought of similarly to the frequency domain of the Fourier transform. Sparse representations have most values zero or near-zero and truly random noise is (usually) represented by many small values in the wavelet domain. Setting all values below some threshold to 0 reduces the noise in the image, but larger thresholds also decrease the detail present in the image. If the input is 3D, this function performs wavelet denoising on each color plane separately. The output image is clipped between either [-1, 1] and [0, 1] depending on the input image range. When YCbCr conversion is done, every color channel is scaled between 0 and 1, and `sigma` values are applied to these scaled color channels. References ---------- .. [1] Chang, S. Grace, Bin Yu, and Martin Vetterli. "Adaptive wavelet thresholding for image denoising and compression." Image Processing, IEEE Transactions on 9.9 (2000): 1532-1546. DOI: 10.1109/83.862633 .. [2] D. L. Donoho and I. M. Johnstone. "Ideal spatial adaptation by wavelet shrinkage." Biometrika 81.3 (1994): 425-455. DOI: 10.1093/biomet/81.3.425 Examples -------- >>> from skimage import color, data >>> img = img_as_float(data.astronaut()) >>> img = color.rgb2gray(img) >>> img += 0.1 * np.random.randn(*img.shape) >>> img = np.clip(img, 0, 1) >>> denoised_img = denoise_wavelet(img, sigma=0.1) """ img = img_as_float(img) if multichannel: if isinstance(sigma, numbers.Number) or sigma is None: sigma = [sigma] * img.shape[-1] if multichannel: if convert2ycbcr: out = color.rgb2ycbcr(img) for i in range(3): # renormalizing this color channel to live in [0, 1] min, max = out[..., i].min(), out[..., i].max() channel = out[..., i] - min channel /= max - min out[..., i] = denoise_wavelet(channel, sigma=sigma[i], wavelet=wavelet, mode=mode) out[..., i] = out[..., i] * (max - min) out[..., i] += min out = color.ycbcr2rgb(out) else: out = np.empty_like(img) for c in range(img.shape[-1]): out[..., c] = _wavelet_threshold(img[..., c], wavelet=wavelet, mode=mode, sigma=sigma[c], wavelet_levels=wavelet_levels) else: out = _wavelet_threshold(img, wavelet=wavelet, mode=mode, sigma=sigma, wavelet_levels=wavelet_levels) clip_range = (-1, 1) if img.min() < 0 else (0, 1) return np.clip(out, *clip_range)