def test_pyramid_reduce_nd(): for ndim in [1, 2, 3, 4]: img = np.random.randn(*((8, ) * ndim)) out = pyramids.pyramid_reduce(img, downscale=2, multichannel=False) expected_shape = np.asarray(img.shape) / 2 assert_array_equal(out.shape, expected_shape)
def resize_by_factor(im, factor): """ Resizes the image according to a factor. The image is pre-filtered with a Gaussian and then resampled with bilinear interpolation. This function uses scikit-image and essentially combines its `pyramid_reduce` with `pyramid_expand` into one function. Returns the same object if factor is 1, not a copy. Parameters ---------- im : ndarray, ndim=2 or 3 Image. Either 2D or 3D with 3 or 4 channels. factor : float Resize factor, e.g. a factor of 0.5 will halve both sides. """ _import_skimage() from skimage.transform.pyramids import pyramid_reduce, pyramid_expand if factor < 1: return pyramid_reduce(im, downscale=1/factor) elif factor > 1: return pyramid_expand(im, upscale=factor) else: return im
def resize_with_factor_new(im, factor): if factor < 1 - 1e-8: im2 = pyramid_reduce(im, downscale=1/factor) elif factor > 1 + 1e-8: im2 = pyramid_expand(im, upscale=factor) else: im2 = im.copy() return im2
def resize(im, f): if f < 1: im2 = pyramid_reduce(im, downscale=1/f) elif f > 1: im2 = pyramid_expand(im, upscale=f) else: im2 = im return im2
def resize(im, new_size, preserve_aspect_ratio=True, prefilter=True): """ This is a not-very-rigorous function to do image resizing, with pre-filtering. """ factors = [new_size[i] / im.shape[i] for i in range(2)] #assert factors[0] == factors[1], "Must have same factor for now" f = factors[0] if f < 1: im2 = pyramid_reduce(im, downscale=1/f) elif f > 1: im2 = pyramid_expand(im, upscale=f) else: im2 = im assert im2.shape[:2] == tuple(new_size), "{0} != {1} (original size: {2})".format(im2.shape, new_size, im.shape) return im2
def test_pyramid_reduce_rgb(): rows, cols, dim = image.shape out = pyramids.pyramid_reduce(image, downscale=2) assert_array_equal(out.shape, (rows / 2, cols / 2, dim))
def test_pyramid_reduce_gray(): rows, cols = image_gray.shape out = pyramids.pyramid_reduce(image_gray, downscale=2) assert_array_equal(out.shape, (rows / 2, cols / 2))
def test_pyramid_reduce_rgb(): rows, cols, dim = image.shape out = pyramids.pyramid_reduce(image, downscale=2, multichannel=True) assert_array_equal(out.shape, (rows / 2, cols / 2, dim))
singan_ssim = [] singan_ms_ssim = [] singan_vif = [] print(len(dataset)) gen_to_use = 0 lr = opt['resolutions'][gen_to_use] for i in range(len(dataset)): print(i) f = dataset.__getitem__(i).to(opt['device']) #print(TAD(dataset.unscale(f), opt['device']).sum()) f_hr = f.clone()[:, :, ::2, ::2].to(opt['device']) f_lr = pyramid_reduce(f_hr[0].clone().cpu().numpy().swapaxes(0, 2).swapaxes( 0, 1), downscale=f_hr.shape[2] / lr[0], multichannel=True).swapaxes(0, 1).swapaxes(0, 2) f_lr = np2torch(f_lr, opt['device']).unsqueeze(0).to(opt['device']) g_mag = torch.norm(f_hr, dim=1).detach().cpu().numpy() g_vort = to_vort(f_hr, device=opt['device']) print(g_vort.shape) GT_frames.append( toImg(f_hr.clone()[0].detach().cpu().numpy()).swapaxes(0, 2).swapaxes( 0, 1)) gt_mag.append(toImg(g_mag).swapaxes(0, 2).swapaxes(0, 1)) bicub = F.interpolate(f_lr.clone(), size=opt['resolutions'][-1], mode=opt["upsample_mode"]) b_mag = torch.norm(bicub, dim=1).detach().cpu().numpy()
patch_similarity_threshold = [] print("There will be %i patches to check" % ((frame.shape[0]-5)*(frame.shape[1]-5))) count = 0 for i in range(frame.shape[0]-5): for j in range(frame.shape[1]-5): patches.append(frame[i:i+5, j:j+5]) patch_similarity_threshold.append(sim_threshold(frame, i, j)) # 2. Iterate through the downscalings of the initial image for downscale_iter in range(1, 7): factor = 1.25 ** downscale_iter f = pyramid_reduce(frame, downscale = factor).astype(np.float32) patch_counts_this_scale = [] # 3. Iterate through each patch of the downscaled image print("There will be %i patches to check at this scale of %0.02f" % (((f.shape[0]-5)*(f.shape[1]-5)), factor)) # 4. Compare similarity of the patch with all patches of the original image for k in range(len(patches)): sim = cv.matchTemplate(f, patches[k], cv.TM_SQDIFF).astype(np.float32) num_sim = np.where(sim <= patch_similarity_threshold[k], 1, 0).sum().astype(np.float32) patch_counts_this_scale.append(num_sim) patch_counts.append(patch_counts_this_scale) all_hist_counts = [] # 6. Create histograms for counts for k in range(len(patch_counts)): hist_counts = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] for i in range(len(patch_counts[k])):
def test_pyramid_reduce_gray(): rows, cols = image_gray.shape with expected_warnings(['The default multichannel']): out = pyramids.pyramid_reduce(image_gray, downscale=2) assert_array_equal(out.shape, (rows / 2, cols / 2))