var bound = ee.Geometry.Polygon([
[[35.96060746534647,33.815740435596645], [36.00472443922342,33.815740435596645], [36.00472443922342,33.85366939280121], [35.96060746534647,33.85366939280121], [35.96060746534647,33.815740435596645]]
]);
var bandIn = ['B2','B3','B4','B5','B6','B7'];
var bandOut = ['blue','green','red','nir','swir1','swir2'];
//Example images of S2 and L8
var imgS2SR = ee.Image('COPERNICUS/S2_SR/20181107T082129_20181107T082732_T36SYC').select(bandIn,bandOut).clip(bound);
var imgL8SR = ee.Image('LANDSAT/LC08/C01/T1_SR/LC08_174036_20181107').select(bandIn,bandOut).clip(bound);
// Choose to register using only the 'Red' band.
var L8RedBand = imgL8SR.select('red');
var S2RedBand = imgS2SR.select('red');
// Determine the displacement by matching only the 'Red' bands.
var displacement = L8RedBand.displacement({
referenceImage: S2RedBand,
maxOffset: 50.0,//The maximum offset allowed when attempting to align the input images, in meters
patchWidth: 100.0 // Small enough to capture texture and large enough that ignorable
//objects are small within the patch. Automatically ditermined if not provided
});
//wrap the imgL8SR image
var l8SR_aligned = imgL8SR.displace(displacement);
Map.centerObject(bound,12)
var visParams = {bands: ['red','green','blue'], min:0, max: 3500};
Map.addLayer(imgL8SR, visParams, 'imgL8SR 30m');
Map.addLayer(imgS2SR, visParams, 'imgS2SR 10m');
Map.addLayer(l8SR_aligned, visParams, 'l8SR 10m');
// Compute image offset and direction.
var offset = displacement.select('dx').hypot(displacement.select('dy'));//calculates the magnitude of the 2D vector [x, y]
var angle = displacement.select('dx').atan2(displacement.select('dy'));// calculates the angle formed by the 2D vector [x, y].
//histogram of offset and agle
var histogramOffset = ui.Chart.image.histogram(offset, bound, 10).setOptions({title: 'Offset Differences'});
var histogramAngle = ui.Chart.image.histogram(angle, bound, 10).setOptions({title: 'Angle Differences'});
print(histogramOffset,'Offset Differences Before')
// Determine the displacement after registration (Red band)
var L8RedBand_aligned = l8SR_aligned.select('red');
var displacement = L8RedBand_aligned.displacement({
referenceImage: S2RedBand,
maxOffset: 50.0,
patchWidth: 100.0
});
var offset_after = displacement.select('dx').hypot(displacement.select('dy'));
var angle_after = displacement.select('dx').atan2(displacement.select('dy'));
var histogramOffset_after = ui.Chart.image.histogram(offset_after, bound, 10).setOptions({title: 'Offset Differences'});
var histogramAngle_after = ui.Chart.image.histogram(angle_after, bound, 10).setOptions({title: 'Angle Differences'});
print(histogramOffset_after,'Offset Differences After');