How to align the image in order to overlap the two images

I would like to learn if there is a way to adjust the image in order to align the two images with different offset so that they can overlap on top each others. h

ow ?

By hand? You could us the â€śAlign_RGB_planesâ€ť plugin from here: http://www.mecourse.com/landinig/software/software.html

Good day Jason Li,

if youâ€™re happy with pixel accuracy, then the following ImageJ-macro does what you are looking for:

``````// macro start
name = getTitle();
nme = split( name, "." );
setBatchMode( true );
run( "Duplicate...", "title=temp" );
if ( getHeight() < getWidth() ) sz = getHeight(); else sz = getWidth();
sz = nextPowerOfTwo( round( 0.5 * sz ) );
run( "Canvas Size...", "width=" + sz + " height=" + sz + " position=Center" );
sz *= 0.5;
run( "Split Channels" );
close( "temp (blue)" );
run( "FD Math...", "image1=[temp (red)] operation=Correlate image2=[temp (green)] result=Result do" );
getRawStatistics( n, mn, mi, mx );
run( "Find Maxima...", "noise=" + d2s( mx, 9 ) + " output=[Point Selection]" );
getSelectionCoordinates( x, y );
close( "Result" );
close( "temp (green)" );
close( "temp (red)" );
run( "Split Channels" );
close( name + " (blue)" );
selectImage( name + " (red)" );
run( "Translate...", "x=" + (sz-x[0]) + " y=" + (sz-y[0]) + " interpolation=None" );
run( "Merge Channels...", "c1=[" + name + " (red)]" + " c2=[" + name + " (green)]" );
rename( nme[0] + "_aligned." + nme[1] );
setBatchMode( false );
exit();
///////
function nextPowerOfTwo( size ) {
size--;
for ( i = 1; i <= 32; i *= 2 ) { size = size | (size >> i); }
size++;
return size;
}
// macro end
``````

Paste the above macro code to an empty macro window (Plugins >> New >> Macro) and run it.

Please note that only translations are compensated although your sample image shows a rotational component as well.

Regards

Herbie

1 Like

Hi @nano,

you can convert your image into a composite (â–şImage â–şColor â–ş Make composite) if you have it standard wise as an RGB image or combine the two channels into a stack by using â–şImageâ–şColorâ–şMerge Channelsâ€¦

Thereafter, several stack alignment options are available (this suggestions are based on running Fiji making the installation partially easier because of the update site distribution of some of the plugins):

Hope this helps you in managing the registration

1 Like

Good day Jan,

does this mean that my macro fails?

Regards

Herbie

this is just a general list of additional suggestions independent of your macro and might be helpful for others also interested in (stack) registration in general. It might able to test different options which might be different in performance depending on the input image also achieving rigid, or affine transformations.

Regarding your macro: I quickly tested it on the RGB image posted here and in my case it did not give me an output image. Silencing the batch mode resulted in the display of a non-aligned image version.

The solution to make it run in my case was to out-comment/delete the `closeImg( "Composite" );` part in line 23.

Then the result looks nicely aligned (with exception of the rotation as you mentioned already).

Kind regards

2 Likes

Funny Jan,

my macro runs perfectly here. Perhaps one needs to introduce

`wait( 10 );`

before the

`closeImg( "Composite" );`

Not closing the â€śCompositeâ€ť image appears not being an option: It has to work this way.

Regards

Herbie

Strange

the wait command does not help in my case, while the before mentioned method with skipping the closing of the composite works since only the merged image is displayed in the end and all other images are anyway discarded at the end of the macro.

I have the feeling that it might be related to the batch mode. once in a while I encountered some issue occurring when executing a macro in batch mode while running normal without the batch mode. But even this should be consistent on different systems.

Honestly, I have no idea, why this behaviour might be different.

1 Like

Hi Herbie,
If I run your macro, I get an error that says:

Selection required in line 13.
getSelectionCoordinates(x,y<)>;

Switching off the batchmode I noted that this error happens on the Result image.
I guess that it is expecting some peaks to appear, which are detected by the Find Maxima? However there are none.
Then I noted that the noise parameter is set to 1000000000 (!)
If I set it to something smaller (e.g. 10) the macro aligns the image very well (although I guess it might need a small rotation to make it perfect).

Any suggestions?
Gabriel

Good day Gabriel,

and thanks for the feedback.

As Iâ€™ve written before, the macro runs perfectly here: macOS 10.11.6, Java 6.

The noise parameter is also perfectly OK. Here is the result from the cross-correlation with the point selection as flattened overlay:

(With the noise parameter set to 10 I get 233 maxima.)

Could you please have a look at the image sizes?
The original image is 1600 x 1200 and the cross-correlation is done with slices that are 1024 x 1024.

I have no idea about the reason for the different results: Everything works as expected on my side â€¦

In my first post Iâ€™ve mentioned that the slices show a slight rotational difference as well which of course canâ€™t be compensated by my macro which does translational compensation only. And thatâ€™s what the original poster was after.

Best greetings

Herbie

EDIT:
Iâ€™ve modified the macro code a bit by making the â€śnoiseâ€ť-parameter signal-dependent.
Thanks for the hint!

Thanks Herbie,
I tracked down the problem to calling this line:
run( â€śFind Maximaâ€¦â€ť, â€śnoise=1000000000 output=[Point Selection] excludeâ€ť );
The strangest thing is that if I remove the keyword â€śexcludeâ€ť, it finds the peak in the centre, but certainly it is not an edge maximaâ€¦
So I cannot understand why the point selection disappears when â€śexcludeâ€ť is in the code.

[â€¦]
Now I realise that I was trying the macro with the image I get by right clicking and â€śdownloadâ€ť. This image is smaller than the original that I can obtain my right clicking on it and then press the Download button.
With the original it works fine!

I wonder what would take to modify the macro so it is not so dependent on the image size.

Also I noted that you created a closeImg function to close a given image, but there is already such function, e.g. close(â€śCompositeâ€ť); closes the Composite image.

Thanks again for the nice macro.

Thanks Gabriel!

I wonder what would take to modify the macro so it is not so dependent on the image size.

See the edit in my recent post and the updated macro.

This image is smaller than the original [â€¦]

Thatâ€™s what Iâ€™ve suspected â€¦

(Could you please have a look at the image sizes?)

Also I noted that you created a closeImg function to close a given image, but there is already such function, e.g. close(â€śCompositeâ€ť); closes the Composite image.

Thanks for pointing this out!

Best

Herbie

1 Like

Hi @anon96376101 and @gabriel,

I quickly ran @anon96376101â€™s macro on OPâ€™s image and it does not run in an older version of ImageJ (1.51n) but when I update to the daily build (1.51s35) it runs fine. I have no idea why, I just thought I would mention it. Nice macro Herbie.

On 1.51n I got the same error as @gabriel:

(On Ubuntu 17.04.)

Screenshot of the Herbie-aligned image:

Just a simple suggestion: there is no need to make a composite to then convert to rgb and close it. You could call

run( â€śMerge Channelsâ€¦â€ť, â€śc1=[â€ť + name + " (red)]" + " c2=[" + name + " (green)]" );
rename( nme[0] + â€ś_aligned.â€ť + nme[1] );

and avoid:
run( â€śStack to RGBâ€ť );
closeImg( â€śCompositeâ€ť );

Gabriel

See my previous post. you are doing the same I did processing the â€śshownâ€ť version of the image. Click on the original and download the large version.

Excellent Gabriel!

I hope, in the end, there will be some code-lines left â€¦

Best

Herbie

1 Like

Good day Sverre,

You may try the recently updated macro â€¦

Regards

Herbie

Yes, and yet it worked in the latest daily build but not an older version of ImageJâ€¦

Maybe you updated it just as I was updating

Just by chance I landed in this page (after all these years), and looking at the aligned image once more, I realised that the OP had the two planes flipped!
I think that this is the right orientation of the two planes (look at the scratched patterns that match in this version, but do not in the previous alignments).

Letâ€™s update that link for the â€śAlign_RGB_planesâ€ť plugin:
https://blog.bham.ac.uk/intellimic/g-landini-software/