From 8b0b58953d393f1a63e088452368c68c03dd7f91 Mon Sep 17 00:00:00 2001 From: Benjamin Loison Date: Mon, 25 Mar 2024 19:45:46 +0100 Subject: [PATCH] WIP: Matplotlib figure showing PRNU estimation by averaging Committed to showcase https://gitea.lemnoslife.com/Benjamin_Loison/Pillow/issues/4. --- datasets/fake/generate_dataset.py | 38 ++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/datasets/fake/generate_dataset.py b/datasets/fake/generate_dataset.py index 849feb7..e89ac4e 100644 --- a/datasets/fake/generate_dataset.py +++ b/datasets/fake/generate_dataset.py @@ -23,11 +23,13 @@ PRNU_FACTOR = 0.1 def randomImage(scale): return np.random.normal(loc = 0, scale = scale, size = (IMAGE_SIZE, IMAGE_SIZE)) +imagesWithoutPrnu = [[randomImage(scale = 1) for _ in range(NUMBER_OF_IMAGES_PER_PHONE)] for phoneIndex in range(NUMBER_OF_PHONES)] + prnus = [randomImage(scale = PRNU_FACTOR) for _ in range(NUMBER_OF_PHONES)] -images = [[randomImage(scale = 1) + prnus[phoneIndex] for _ in range(NUMBER_OF_IMAGES_PER_PHONE)] for phoneIndex in range(NUMBER_OF_PHONES)] +imagesWithPrnu = [[imageWithoutPrnu + prnus[phoneIndex] for imageWithoutPrnu in imagesWithoutPrnu[phoneIndex]] for phoneIndex in range(NUMBER_OF_PHONES)] -allImages = np.max([np.max(prnus) + np.max(images)]) +allImages = np.max([np.max(imagesWithoutPrnu) + np.max(prnus) + np.max(imagesWithPrnu)]) def toPilImage(npArray): return Image.fromarray(npArray) @@ -41,14 +43,33 @@ def showImageWithMatplotlib(npArray): plt.imshow(npArray) plt.show() +fig, axs = plt.subplots(1, 4) +fig.suptitle('Single PRNU estimation with images being Gaussian noise') + prnusPil = [toPilImage(prnu) for prnu in prnus] #showImageWithMatplotlib(prnus[0]) +axs[0].set_title('Actual PRNU') +axs[0].imshow(prnus[0]) -imagesPil = [[toPilImage(image) for image in images[phoneIndex]] for phoneIndex in range(NUMBER_OF_PHONES)] -#showImageWithMatplotlib(images[0][0]) +axs[1].set_title('Image without PRNU') +axs[1].imshow(imagesWithPrnu[0][0]) + +imagesWithPrnuPil = [[toPilImage(imageWithPrnu) for imageWithPrnu in imagesWithPrnu[phoneIndex]] for phoneIndex in range(NUMBER_OF_PHONES)] +#showImageWithMatplotlib(imagesWithPrnu[0][0]) +axs[2].set_title(f'First image with PRNU (RMS with image without PRNU: {rmsdiff(toPilImage(imagesWithPrnu[0][0]), toPilImage(imagesWithPrnu[0][0]))})') +axs[2].imshow(imagesWithPrnu[0][0]) + +imagesWithPrnuPil0Mean = np.array(imagesWithPrnuPil[0]).mean(axis = 0) +#showImageWithMatplotlib(imagesWithPrnuPil0Mean) +axs[3].set_title('Mean of images with PRNU') +axs[3].imshow(imagesWithPrnuPil0Mean) + +plt.show() + +## # Compute CAI of phone images. -caiImages = [[contextAdaptiveInterpolator(image.load(), image) for image in imagesPil[phoneIndex]] for phoneIndex in tqdm(range(NUMBER_OF_PHONES))] +caiImages = [[contextAdaptiveInterpolator(image.load(), image) for image in imagesWithPrnuPil[phoneIndex]] for phoneIndex in tqdm(range(NUMBER_OF_PHONES))] #caiImages[0][0].show() # Guess the phone by consider the one reaching the lowest RMS difference between the estimated PRNU and the actual one. @@ -69,20 +90,25 @@ correctGuessesByMeanCAIImages = 0 # Maybe make sense only because Gaussian images here. correctGuessesByCAIImagesMean = 0 for phoneIndex in range(NUMBER_OF_PHONES): - phoneImages = imagesPil[phoneIndex] + phoneImages = imagesWithPrnuPil[phoneIndex] phoneCaiImages = caiImages[phoneIndex] phoneImagesMean = toPilImage(np.array(phoneImages).mean(axis = 0)) caiImagesMean = toPilImage(np.array(phoneCaiImages).mean(axis = 0)) caiOverPhoneImagesMean = contextAdaptiveInterpolator(phoneImagesMean.load(), phoneImagesMean) + phonePrnu = prnusPil[phoneIndex] + print('RMS diff with mean image =', rmsdiff(phoneImagesMean, phonePrnu)) print('RMS diff with mean CAI images =', rmsdiff(caiImagesMean, phonePrnu)) print('RMS diff with CAI images mean =', rmsdiff(caiOverPhoneImagesMean, phonePrnu)) + guessedPhoneIndexByMeanImages = getPhoneIndexByNearestPrnu(phoneImagesMean) guessedPhoneIndexByMeanCAIImages = getPhoneIndexByNearestPrnu(caiImagesMean) guessedPhoneIndexByCAIImagesMean = getPhoneIndexByNearestPrnu(caiOverPhoneImagesMean) + print(f'Actual phone index {phoneIndex}, guessed phone index {guessedPhoneIndexByMeanImages} by mean images, {guessedPhoneIndexByMeanCAIImages} by mean CAI images and {guessedPhoneIndexByCAIImagesMean} by CAI images mean') + correctGuessesByMeanImages += 1 if phoneIndex == guessedPhoneIndexByMeanImages else 0 correctGuessesByMeanCAIImages += 1 if phoneIndex == guessedPhoneIndexByMeanCAIImages else 0 correctGuessesByCAIImagesMean += 1 if phoneIndex == guessedPhoneIndexByCAIImagesMean else 0