I have two image collections with 744 images each (and the same resolution and extent), covering the whole Earth. I need to compute the correlation between the image collections for each pixel.
2 Answers
I don't think there's a way to do this on all the pixels at once.
For a single image pair you can first fuse them into one two-band image, and then use reduceRegion() with the reducer ee.Reducer.pearsonsCorrelation to return an r-value and a p-value. If there are an equal number of pixels in each image pair, then you could iterate this procedure across the image collections with a .map() function, and average all the r-values together at the end.
(If not you could construct a combined reducer including pearsons and 'count', and then weight your averaging by the number of pixels in each image.)
Even though there is no ready-made function for calculating pixelwise correlation in Earth Engine, you can write the code manually to achieve this. This code will help you calculate the pixelwise correlation between the two image collections, assuming they have the same resolution and extent.
// Load the two image collections you want to calculate pixelwise correlation for
var collection1 = ImageCollection1; // Replace with your own collection
var collection2 = ImageCollection2; // Replace with your own collection
// Calculate the mean of image collections
var meanCollection1 = collection1.reduce(ee.Reducer.mean());
var meanCollection2 = collection2.reduce(ee.Reducer.mean());
// Subtract each image in the collection from its mean
var subtractedCollection1 = collection1.map(function(image) {
var meanImage = meanCollection1;
return image.subtract(meanImage);
});
var subtractedCollection2 = collection2.map(function(image) {
var meanImage = meanCollection2;
return image.subtract(meanImage);
});
// Create an empty list to store the multiplied images
var multipliedImages = ee.List([]);
// Iterate through each image in the collections and multiply them
for (var i = 0; i < 744; i++) { // Adjust the loop range as needed
var image1 = ee.Image(subtractedCollection1.toList(744).get(i));
var image2 = ee.Image(subtractedCollection2.toList(744).get(i));
var multipliedImage = image1.multiply(image2);
multipliedImages = multipliedImages.add(multipliedImage);
}
// Convert the list of multiplied images back to an ImageCollection
var multipliedCollection = ee.ImageCollection.fromImages(multipliedImages);
// Reduce the multiplied collection to a single summed image
var summedImage = multipliedCollection.reduce(ee.Reducer.sum());
// Define a custom function to square each image
var squareImage = function(image) {
return image.multiply(image); // This squares the image
};
// Use the map function to apply the custom function to each image in the collections
var squaredSubtractedCollection1 = subtractedCollection1.map(squareImage);
var squaredSubtractedCollection2 = subtractedCollection2.map(squareImage);
// Reduce the squared subtracted collections to single summed images
var summedSquaredImage1 = squaredSubtractedCollection1.reduce(ee.Reducer.sum());
var summedSquaredImage2 = squaredSubtractedCollection2.reduce(ee.Reducer.sum());
// Multiply the summed squared subtracted images
var multipliedSquaredImages = summedSquaredImage1.multiply(summedSquaredImage2);
// Use the map function to apply the square root function to the multiplied images
var sqrtMultipliedSquaredImages = multipliedSquaredImages.sqrt();
// Calculate the pixelwise correlation
var correlation = summedImage.divide(sqrtMultipliedSquaredImages);