Implement correlation #66

Open
opened 2024-05-03 13:19:04 +02:00 by Benjamin_Loison · 4 comments

The Stack Overflow answer 15192809 is very slow for real resolution images. Why is it so slow, is not the element result only dependent of the associated initial elements, as it seems to be the case on the trivial example described in the git issue comment issues/63#issuecomment-1811?

from scipy import signal
from PIL import Image
import numpy as np

def getImage(imagePath):
    return np.array(Image.open(imagePath).convert('RGB'))[:100,:100,0]

im1 = getImage('RAISE,Rafael 23_04_24_mean_estimated_prnu_camera_Rafael 23_04_24.png')
im2 = getImage('RAISE,Rafael 23_04_24_mean_estimated_prnu_camera_RAISE.png')
print(f'{im1.shape=} {im2.shape=}')
cor = signal.correlate2d(im1, im2)
print(f'{cor.shape=}')

https://en.wikipedia.org/w/index.php?title=Cross-correlation&oldid=1220518127#Cross-correlation_of_deterministic_signals

https://en.wikipedia.org/wiki/Digital_image_correlation_and_tracking

Related to the Stack Overflow question 189943.

Treat image as a single array? This does not seem correct. Can think for the 2D case about a subimage scanning a greater sized image. Then for the same size images correlation can think how would proceed in a subimage case and generalize. The Wikipedia example shows how to proceed for valued functions so it is identical. The question is then when have the correlation matrix how do we attribute a source camera?

Should give a try and verify the correctness of the Stack Overflow answer 75690643.

Related to #63.

[The Stack Overflow answer 15192809](https://stackoverflow.com/a/15192809) is very slow for real resolution images. Why is it so slow, is not the element result only dependent of the associated initial elements, as it seems to be the case on the trivial example described in the git issue comment [issues/63#issuecomment-1811](https://gitea.lemnoslife.com/Benjamin_Loison/Robust_image_source_identification_on_modern_smartphones/issues/63#issuecomment-1811)? ```py from scipy import signal from PIL import Image import numpy as np def getImage(imagePath): return np.array(Image.open(imagePath).convert('RGB'))[:100,:100,0] im1 = getImage('RAISE,Rafael 23_04_24_mean_estimated_prnu_camera_Rafael 23_04_24.png') im2 = getImage('RAISE,Rafael 23_04_24_mean_estimated_prnu_camera_RAISE.png') print(f'{im1.shape=} {im2.shape=}') cor = signal.correlate2d(im1, im2) print(f'{cor.shape=}') ``` https://en.wikipedia.org/w/index.php?title=Cross-correlation&oldid=1220518127#Cross-correlation_of_deterministic_signals https://en.wikipedia.org/wiki/Digital_image_correlation_and_tracking Related to [the Stack Overflow question 189943](https://stackoverflow.com/q/189943). Treat image as a single array? This does not seem correct. Can think for the 2D case about a subimage scanning a greater sized image. Then for the same size images correlation can think how would proceed in a subimage case and generalize. The Wikipedia example shows how to proceed for valued functions so it is identical. The question is then when have the correlation matrix how do we attribute a source camera? Should give a try and verify the correctness of [the Stack Overflow answer 75690643](https://stackoverflow.com/a/75690643). Related to #63.
Benjamin_Loison added the
enhancement
high priority
medium
labels 2024-05-03 13:19:04 +02:00
Author
Owner

https://en.wikipedia.org/wiki/Pearson_correlation_coefficient

https://numpy.org/doc/1.26/reference/generated/numpy.corrcoef.html

Still returns a matrix it seems, see:

import numpy as np
import scipy

im1 = np.array([[0, 1], [2, 3]]).flatten()
#im2 = np.array([[0, 1], [2, 3]]).flatten()
im2 = np.array([[3, 2], [1, 0]]).flatten()
#f = np.corrcoef
f = scipy.stats.pearsonr
print(f(im1, im2))

https://docs.scipy.org/doc/scipy-1.13.0/reference/generated/scipy.stats.pearsonr.html (source: Wikipedia)

https://en.wikipedia.org/wiki/Pearson_correlation_coefficient https://numpy.org/doc/1.26/reference/generated/numpy.corrcoef.html Still returns a matrix it seems, see: ```python import numpy as np import scipy im1 = np.array([[0, 1], [2, 3]]).flatten() #im2 = np.array([[0, 1], [2, 3]]).flatten() im2 = np.array([[3, 2], [1, 0]]).flatten() #f = np.corrcoef f = scipy.stats.pearsonr print(f(im1, im2)) ``` https://docs.scipy.org/doc/scipy-1.13.0/reference/generated/scipy.stats.pearsonr.html (source: Wikipedia)
Author
Owner

Unclear if correlation should in absolute be close to 1 or just close to 1.
According to supervisors the absolute distance to 1 makes sense, for instance for a signal with a phase offset of half its period, then the correlation should be -1 but it is the same signal to some extent.

Unclear if correlation should in absolute be close to 1 or just close to 1. According to supervisors the absolute distance to 1 makes sense, for instance for a signal with a phase offset of half its period, then the correlation should be -1 but it is the same signal to some extent.
Author
Owner

Should implement an enum to choose between RMS and correlation. Done, see 6a9900df91.

~~Should implement an enum to choose between RMS and correlation.~~ Done, see https://gitea.lemnoslife.com/Benjamin_Loison/Robust_image_source_identification_on_modern_smartphones/commit/6a9900df91e3bf3f5ade7b984c9fa0775fd21018.
Author
Owner

Should execute one more time to see if face negative Pearson correlation with current setup.

Should execute one more time to see if face negative Pearson correlation with current setup.
Sign in to join this conversation.
No description provided.