def test_values(self): image = cvu.load_grayscale('files/uniform.png') stdevs = [2.5, 5, 7.5, 10] for stdev in stdevs: noisy = add_uniform_noise(image, stdev) actual = np.std(noisy) assert actual == pytest.approx(stdev, rel=0.05)
def test_dct_blocks(): image = cvu.load_grayscale('files/building_1.png') exp_blocks, exp_means = _slow_dct_blocks(image) act_blocks, act_means = compute_dct_blocks(image) assert np.all(np.isclose(exp_means, act_means)) assert np.all(np.isclose(exp_blocks, act_blocks, atol=255 * 1e-5))
def test_signal_dependent_noise(): names = glob.glob('files/*.png') for n in names: image = cvu.load_grayscale(n) noisy = add_signal_dependent_noise(image, 0.3, 5) out = estimate_signal_dependent_noise(noisy, 7) c = n.split('/')[-1].split('.')[0] np.savetxt('out/signal_dependent/%s.txt' % c, out)
def test_patch_average(): image = cvu.load_grayscale('files/flowers_2.png') expected = cv2.blur(image, (5, 5)) d_image = cd.to_device(image) d_out = cd.device_array(image.shape, image.dtype) cuda_patch_average(d_image, d_out, 2) actual = d_out.copy_to_host() # Except for the borders, we expect the same results assert np.all(np.isclose(actual[2:-2, 2:-2], expected[2:-2, 2:-2]))
def test_uniform_noise_with_quantization(): names = glob.glob('files/*.png') for n in names: image = cvu.load_grayscale(n) stdevs = [2.5 * i for i in range(0, 20, 3)] estimations = [] for stdev in stdevs: print("Estimating %s at %s" % (n, stdev)) noisy = add_uniform_noise(image, stdev, quantization=True) estimations.append(estimate_uniform_noise(noisy)) c = n.split('/')[-1].split('.')[0] out = np.array([stdevs, estimations]) np.savetxt('out/uniform_noise_quantization/%s.txt' % c, out)
g_means[i, j] = mean / 64 for k in range(8): for l in range(8): b = block[k, l] g_blocks[i, j, k, l] = b * b def compute_dct_blocks(image): threadsperblock = (16, 16) blockspergrid_x = math.ceil(image.shape[0] / threadsperblock[0]) blockspergrid_y = math.ceil(image.shape[1] / threadsperblock[1]) blockspergrid = (blockspergrid_x, blockspergrid_y) rows, cols = image.shape new_shape = (rows - 7, cols - 7) d_image = cd.to_device(image) d_means = cd.device_array(new_shape, np.float32) d_blocks = cd.device_array((*new_shape, 8, 8), np.float32) _kernel_compute_dct_blocks[blockspergrid, threadsperblock](d_image, d_blocks, d_means) return d_blocks.copy_to_host(), d_means.copy_to_host() if __name__ == '__main__': image = cvu.load_grayscale('files/flowers_1.png') compute_dct_blocks(image)
def test_zero_stdev(self): image = cvu.load_grayscale('files/woman.png') noisy = add_uniform_noise(image, 0) assert np.all(image == noisy)