WIP: Matplotlib figure showing PRNU estimation by averaging

Committed to showcase
Benjamin_Loison/Pillow#4.
This commit is contained in:
Benjamin Loison 2024-03-25 19:45:46 +01:00
parent 044d25cbaa
commit 8b0b58953d
Signed by: Benjamin_Loison
SSH Key Fingerprint: SHA256:BtnEgYTlHdOg1u+RmYcDE0mnfz1rhv5dSbQ2gyxW8B8

View File

@ -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