From dfe2540c02429c9fba2c38ae5251f4fd8d1e50e2 Mon Sep 17 00:00:00 2001 From: Benjamin Loison Date: Fri, 29 Mar 2024 00:06:13 +0100 Subject: [PATCH] Apply Context-Adaptive Interpolator --- algorithms/image_utils/image_utils.py | 5 +++- datasets/fake/generate_dataset.py | 5 +--- .../noise_free_test_images/estimate_prnu.py | 29 ++++++++++++++----- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/algorithms/image_utils/image_utils.py b/algorithms/image_utils/image_utils.py index 4316f95..5ffece0 100644 --- a/algorithms/image_utils/image_utils.py +++ b/algorithms/image_utils/image_utils.py @@ -7,4 +7,7 @@ def randomGaussianImage(scale, size): def showImageWithMatplotlib(npArray): plt.imshow(npArray) - plt.show() \ No newline at end of file + plt.show() + +def toPilImage(npArray): + return Image.fromarray(npArray) \ No newline at end of file diff --git a/datasets/fake/generate_dataset.py b/datasets/fake/generate_dataset.py index 008c813..7c52499 100644 --- a/datasets/fake/generate_dataset.py +++ b/datasets/fake/generate_dataset.py @@ -15,7 +15,7 @@ from context_adaptive_interpolator import contextAdaptiveInterpolator sys.path.insert(0, '../../algorithms/image_utils/') -from image_utils import showImageWithMatplotlib, randomGaussianImage +from image_utils import showImageWithMatplotlib, randomGaussianImage, toPilImage from tqdm import tqdm IMAGE_SIZE = 64 @@ -37,9 +37,6 @@ imagesWithPrnu = [[imageWithoutPrnu + prnus[phoneIndex] for imageWithoutPrnu in allImages = np.max([np.max(imagesWithoutPrnu) + np.max(prnus) + np.max(imagesWithPrnu)]) -def toPilImage(npArray): - return Image.fromarray(npArray) - def showImageWithPil(npArray): npArray -= npArray.min() npArray = (npArray / npArray.max()) * 255 diff --git a/datasets/noise_free_test_images/estimate_prnu.py b/datasets/noise_free_test_images/estimate_prnu.py index 407686b..f411977 100644 --- a/datasets/noise_free_test_images/estimate_prnu.py +++ b/datasets/noise_free_test_images/estimate_prnu.py @@ -7,12 +7,16 @@ import sys sys.path.insert(0, '../../algorithms/image_utils/') -from image_utils import showImageWithMatplotlib, randomGaussianImage +from image_utils import showImageWithMatplotlib, randomGaussianImage, toPilImage + +sys.path.insert(0, '../../algorithms/context_adaptive_interpolator/') + +from context_adaptive_interpolator import contextAdaptiveInterpolator datasetPath = 'no_noise_images' # Note that contrarily to `datasets/fake/`, here we do not have images being Gaussian with `scale` `1` but actual images with pixel values between 0 and 255. # In addition to the range difference, note that the distribution in the first set of images was a Gaussian and here is very different and specific. -PRNU_FACTOR = 0.1 +PRNU_FACTOR = 0.15 IMAGE_SIZE_SHAPE = (469, 704) np.random.seed(0) @@ -21,11 +25,22 @@ np.random.seed(0) prnuPil = Image.open('prnu.png').convert('F') prnuNpArray = np.array(prnuPil) * PRNU_FACTOR +def isIn256Range(x): + return 0 <= x and x <= 255 + for imageName in os.listdir(datasetPath): if imageName.endswith('.png'): imagePath = f'{datasetPath}/{imageName}' - imagePil = Image.open(imagePath).convert('F') - imageNpArray = np.array(imagePil) - #showImageWithMatplotlib(imageNpArray) - showImageWithMatplotlib(imageNpArray + prnuNpArray) - break \ No newline at end of file + imageWithoutPrnuPil = Image.open(imagePath).convert('F') + imageWithoutPrnuNpArray = np.array(imageWithoutPrnuPil) + #showImageWithMatplotlib(imageWithoutPrnuNpArray) + imageWithPrnuNpArray = imageWithoutPrnuNpArray + prnuNpArray + showImageWithMatplotlib(imageWithPrnuNpArray) + break + assert all([isIn256Range(extreme) for extreme in [imageWithPrnuNpArray.max(), imageWithPrnuNpArray.min()]]), 'Adding the PRNU resulted in out of 256 bounds image' + imageWithPrnuPil = toPilImage(imageWithPrnuNpArray) + imageWithPrnuCaiPil = contextAdaptiveInterpolator(imageWithPrnuPil.load(), imageWithPrnuPil) + imageWithPrnuCaiNpArray = np.array(imageWithPrnuCaiPil) + showImageWithMatplotlib(imageWithPrnuCaiNpArray) + break +