BakingTray
Search…
Stitching tweak walkthrough
Tweaking BakingTray and StitchIt for high stitching accuracy
This page is a walkthrough explaining how to improving stitching accuracy using BakingTray and StitchIt. It's description of a real session during which a mistake a was made that then needed correction.

First look

We begin by stitching a section and looking at it:
1
>> IM=chessboardStitch([75,1],2); %Slow for larger datasets
2
>> exploreChessboard(IM,4000) %Slow for larger datasets
Copied!
The fact the red tile is above the green on one side and below on the other tells me there is a rotation problem.
The bidirectional scanning correction in ScanImage wasn’t set quite right. We can fix this post- hoc, although the correction is a bit hackish at the moment.
Indeed, upon zooming in to an area with obvious features the microns per pixel is very far off:
Indeed, microns per pixel is very far off:

First tweak

What are the values?
1
>> IM.params.voxelSize
2
ans =
3
struct with fields:
4
X: 0.8060 Y: 0.8060 Z: 5
Copied!
We know it needs to be a larger number so the tiles end up overlapping more. Let’s try just one axis first:
1
>> IM(2)=IM; %copy
2
>> IM(2).params.voxelSize.Y=0.82; %New value
3
>> IM(2)=chessboardStitch([75,1],2,IM(2).params); %re-stitch
4
>> exploreChessboard(IM,4000) %Compare new and previous versions
Copied!
Note that now IM has a length of 2. When we run exploreChessboard with IM having a length of 2, we see the two images side by side:
And if we zoom in:

Adding a tile rotation

It looks much closer. They are offset because of the rotation we noted earlier, but we’ll fix that later. Let’s do the other axis too.
1
>> IM(2).params.voxelSize.X=0.815; %New value
2
>> IM(2)=chessboardStitch([75,1],2,IM(2).params); %re-stitch
3
>> exploreChessboard(IM,4000) %Compare new and original versions
Copied!
Closer. But’ll play with rotation before going back to mics per pixel. [NOTE: AT THIS POINT I WAS ASSUMING THAT THE GREEN FIBRE AT THE UPPER OVERLAP REGION IS PART OF THE RED BELOW IT. TURNS OUT LATER THAT IT WASN’T, BUT I DIDN’T REALISE THAT HERE]
To make this easier I will use affineMatGen that is in BakingTray. I don’t have this function in my path on my desktop PC so I add it now:
1
>> ls ~/work/Anatomy/code/bakingtray/code/BTresources/
2
LimitFigSize.m TileStepSize.m plotboxpos.m previewFilesToStack.m NumTiles.m affineMatGen.m prettyTime.m
3
4
>> addpath('~/work/Anatomy/code/bakingtray/code/BTresources/')
Copied!
Create a matrix with a small rotation and add it to IM(2).params:
1
>> affineMatGen('rot',-0.3)
2
- [0.999986, -0.005236, 0.0] - [0.005236, 0.999986, 0.0] - [0.0, 0.0, 1.0]
3
>> A=affineMatGen('rot',-0.3) A=
4
1.0000 -0.0052 0 0.0052 1.0000 0 0 0 1.0000
5
>> IM(2).params.affineMat ans =
6
[]
7
>> IM(2).params.affineMat=A;
Copied!
Let’s see what that does. Stitch and explore as before. In the area we were looking at before, stuff is better:
But I really care about the sample edges right now, as rotations are really obvious there. Especially along the dorsal and ventral surfaces. I therefore re-explore with a lower threshold and look there.
1
>> exploreChessboard(IM,2000)
Copied!
We can do better. There is still rotation and it looks like we didn’t over-compensate. Let’s start fine-tweaking by comparing the above, nicer, version to a new version:
1
>> IM(3)=IM(2); %Copy again
2
>> A=affineMatGen('rot',-0.35);
3
>> IM(3).params.affineMat=A;
4
>> IM(3)=chessboardStitch([75,1],2,IM(3).params);
5
>> exploreChessboard(IM(2:3),2000)
Copied!
That wasn’t enough [NOT SHOWN] so repeat with more rotation:
1
>> A=affineMatGen('rot',-0.45);
2
>> IM(3).params.affineMat=A;
3
>> IM(3)=chessboardStitch([75,1],2,IM(3).params); exploreChessboard(IM(2:3),2000)
Copied!
That still wasn’t enough [NOT SHOWN] so repeat with more rotation:
1
>> IM(3).params.affineMat=affineMatGen('rot',-0.5);
2
>> IM(3)=chessboardStitch([75,1],2,IM(3).params); exploreChessboard(IM(2:3),2000)
Copied!
Finally this is much better:
Certainly not perfect. Never is perfect. But very much better. Try another 0.05 degrees and compare what we just did to that:
1
>> IM(2)=IM(3); %Replace second with third to get a yet finer comparison
2
>> IM(3).params.affineMat=affineMatGen('rot',-0.55); IM(3)=chessboardStitch([75,1], 2,IM(3).params); exploreChessboard(IM(2:3),2000)
Copied!
This is again slightly better [NOT SHOWN]. We can leave the rotation for now and go back and explore the dataset more carefully, considering microns per pixel again.
1
>> exploreChessboard(IM(2:3),4000) %Change threshold to higher value again
Copied!
The region we first concentrated on looks better than before the rotation correction:

Exploring Y microns/pixel

But maybe microns per pixel in Y still needs tweaking? We need to pan around the sample to see what’s going on elsewhere. Can’t base everything on just one area. I see this, which I don’t like. This is miles out and can only be explained by stage motion errors, or the sample shifting, or a tilted focal plane, or the X value still being wrong. Tilted focal plane is unlikely to have such a big effect, though. [NOTE: THIS IS WHEN I STARTED TO QUESTION WHAT THAT FIBRE WAS AT THE START]
I also see this, which looks very much like tilted focal plane, but that’s an error along the other scan axis.
Let’s go with my X value being wrong. Let’s look around for other evidence of this.

Looking at the EM grid image for this system

Yes, it looks like that’s what’s happening. Note that I’m paying attention to regions near the middle of the tiles. Looking at the grid image quickly in Fiji for this microscope I see this:
I had to rotate tiles 90 degrees to stitch, so the left and right edges are the ones I care about. Note there is pincushion distortion there. The other two axes seem good. Let’s correct the grid image first. The X value was set based upon pixels near a corner, so we’ll not touch it yet. I also note that this grid image has a bidirectional scanning artefact too.
Let’s have a fiddle:
1
>> imagesc(G(:,:,1)) %Loaded into MATLAB now
2
>> caxis([0,5000]), axis square
Copied!
1
%Play a bit until we get this
2
>> imagesc(stitchit.tools.lensdistort(G(:,:,1),[0.04,0.0]))
Copied!
1
>> IM(3).params.lensDistort.cols=rows; %We want to work along the rows
2
>> IM(3)=chessboardStitch([75,1],2,IM(3).params); exploreChessboard(IM(2:3),2000)
Copied!
This looks like we’ve over-compensated, but we know that the grid image looks square.

Go back and tweak X

So we need to tweak X. This is why what I should have done was fix the barrel distortion before doing anything else.
1
>> IM(3).params.voxelSize
2
X: 0.8150 Y: 0.8200 Z: 5
3
>> IM(3).params.voxelSize.X=0.8;
4
>> IM(3)=chessboardStitch([75,1],2,IM(3).params); exploreChessboard(IM(2:3),2000)
Copied!
Iterate a bit until stuff looks good. We cycle through:
1
>> IM(3).params.voxelSize.X=0.805;
2
>> IM(3).params.voxelSize.X=0.8075;
Copied!
And now we see:
That stuff that was previously bad is now good. Pan around a bit. It all looks about as good as expected considering the tilt in the focal plane. This can only be corrected by tilting the stage. The only other option would be affine transformation in Z, but that would require very dense sampling in Z which we don’t typically do.

Commit to recipe file

In this case we want to tweak an already acquired sample so we commit our changes to the recipe file:
It would also be a good idea to add these changes to the frameSizes settings file in BakingTray so all future samples have these good parameters saved with them.
1
% stitching from this file produces the good stuff
2
>>IMfinal=chessboardStitch([75,1],2);
3
>> exploreChessboard(IMfinal,4000)
Copied!
Last modified 1yr ago