Try the following script:
#include <pjsr/SampleType.jsh>
#include <pjsr/ColorSpace.jsh>
/*
* Projective transformation matrix.
*/
var H = new Matrix(
+1.0000, +0.0000, +0.0000,
+0.0000, +1.0000, +0.0000,
+0.0000, +0.0000, +1.0000
);
/*
* If working with an inverse projection matrix (e.g., StarAlignment), leave
* the next line as is.
*/
var matrix = H;
/*
* If working with a direct projection matrix, uncomment the next line and
* comment out the previous one.
*/
//var matrix = H.inverse();
/*
* Filling color for unmapped target image pixels.
*/
var backgroundColor = 0;
var h00 = matrix.at( 0, 0 );
var h01 = matrix.at( 0, 1 );
var h02 = matrix.at( 0, 2 );
var h10 = matrix.at( 1, 0 );
var h11 = matrix.at( 1, 1 );
var h12 = matrix.at( 1, 2 );
var h20 = matrix.at( 2, 0 );
var h21 = matrix.at( 2, 1 );
var h22 = matrix.at( 2, 2 );
if ( 1 + h22 == 1 )
throw new Error( "Zero or insignificant transformation scale." );
var window = ImageWindow.activeWindow;
if ( window.isNull )
throw Error( "No image window." );
var sourceImage = window.mainView.image;
var targetWindow = new ImageWindow (
sourceImage.width, sourceImage.height, sourceImage.numberOfChannels,
sourceImage.bitsPerSample,
sourceImage.sampleType == SampleType_Real,
sourceImage.colorSpace != ColorSpace_Gray,
"projected"
);
var targetView = targetWindow.currentView;
var targetImage = targetView.image;
targetView.beginProcess();
targetImage.fill( backgroundColor );
console.show();
console.writeln( "<end><cbr><br><b>" + ImageWindow.activeWindow.mainView.id + "</b>" );
console.abortEnabled = true;
targetImage.statusEnabled = true;
targetImage.initializeStatus( "Homographic projection",
targetImage.bounds.area*targetImage.numberOfChannels );
/*
* Interpolate target image by inverse coordinate mapping.
*/
for ( var ch = 0; ch < targetImage.numberOfChannels; ++ch )
{
targetImage.initSampleIterator( targetImage.bounds, ch );
for ( var y = 0; y < targetImage.height; ++y )
{
var h01yh02 = h01*y + h02;
var h11yh12 = h11*y + h12;
var h21yh22 = h21*y + h22;
for ( var x = 0; x < targetImage.width; ++x, targetImage.nextSample() )
{
var w = h20*x + h21yh22;
var x0 = (h00*x + h01yh02)/w;
var y0 = (h10*x + h11yh12)/w;
// Assert x0 and y0 are within source image bounds
if ( x0 >= 0 && y0 >= 0 && x0 < sourceImage.width && y0 < sourceImage.height )
targetImage.setSampleValue( sourceImage.interpolate( x0, y0, ch ) );
}
targetImage.advanceStatus( targetImage.width );
}
}
targetView.endProcess();
targetWindow.show();
targetWindow.zoomToOptimalFit();
targetWindow.bringToFront();
Simply replace the components of the H matrix at the top of the script with the ones that SA writes to the console, select the image that you want to transform, and run the script. You may have to crop the transformed image to the same dimensions of the reference image; the script cannot do this automatically. This is easy if you use SA matrices, since then the transformed image will match the reference image at {0,0} (= top left corner).
Let me know if this helps.
(Edited: Replaced script with a new version with better error checking)