# Implement planar correction for skewed 3D profilometric data

I have some data that represents small-scale surface topography. The data is planar but not flat because it's impractical to level this microscope to a tolerance of microns. It looks like this:

I'd like to postprocess that slope out, such that the major axis is colinear with the long side of the blue rectangle and the vertical axis can then be rescaled to show meaningful topographic variation instead of the (really large) planar slope.

One approach here might be to do an orthogonal transformation via (basically) principal component analysis, eg. find the three eigenvalue/eigenvector pairs, and then treat the eigenvectors corresponding to the largest two eigenvalues as the new X and Y axes. I'd be very interested to see how this (or some better way) might be done efficiently in Mathematica (this data is generally in the millions of rows).

An example of my data is here.

and this is the code I've written so far (which depends on version 9+ for Downsample[]):

    pathToDatafile = "path\\to\\file\\6000s.xyz";
data = DeleteCases[data,{__,_String,_String}]; (*remove the "no data" lines that the instrument creates*)
data=Delete[data,-1]; (*throw away the instrument's pound sign at the end of the file*)
data = Downsample[data,{100,1}];(*downsample by a lot because we have way too many datapoints*)
ListPlot3D[data,
Mesh->None,
BoxRatios->Automatic,
PlotStyle->LightBlue,
Boxed->False,
Axes->{True,True,True},
AxesLabel->{"\[Mu]m","","\[Mu]m"},
PlotRange->{Automatic,Automatic,{0,250}},
ImageSize->Large
]

-

This solution is giving some artifacts, I believe, due to the downsampling, but the problem can be solved by using RotationTransform similar to this

ListPlot3D[data]
ListPlot3D[RotationTransform[-2.2 Degree, {1, 0, 0}, data[[1]]]@data]


You can make an interactive leveler. This requires a severely downsampled dataset in order to work smoothly (at least on my computer)

subdata = Downsample[data, {100, 1}];
Manipulate[Module[{d},
d = RotationTransform[x Degree, {1, 0, 0}, subdata[[1]]]@subdata;
d = RotationTransform[y Degree, {0, 1, 0}, d[[1]]]@d;
ListPlot3D[d]
], {{x, 0}, -5, 5}, {{y, 0}, -5, 5}]


And then use those results on the full set of data

Module[{d},
d = RotationTransform[-2.16 Degree, {1, 0, 0}, data[[1]]]@data;
d = RotationTransform[0.12 Degree, {0, 1, 0}, d[[1]]]@d;
ListPlot3D[d]
]


Which gets you a slightly tweaked version of the image I have above, so I won't repost it.

-
The interactivity is particularly handy because I'll have to batch process many of these plots and they'll all have a slightly different slope. It's much faster to do by eye than by trial/error/replot. Thanks! –  avn2109 Nov 5 '13 at 17:05

You can use LinearModelFit to create a linear-fit of the model, and use that to find an appropriate rotation.

model = LinearModelFit[
data,
{x, y},
{x, y}];
(* We have: z = c + a x + b y *)
{c, a, b} = model["BestFitParameters"];

rt = Composition[
RotationTransform[{
{1, 0, a},
{1, 0, 0}},
{0, 0, c}],
RotationTransform[{
{0, 1, b},
{0, 1, 0}},
{0, 0, c}]];
corrected = rt@data;
ListPlot3D@corrected


(Note: As I don't have Mathematica v9 I downsampled by data = data[[;; ;;100, All]] which I assume is more-or-less the same in this case)

Edit: Answer used to be just corrected = data; corrected[[All, 3]] -= model["Function"] @@@ corrected[[All, ;; 2]]; but as @bobthechemist points out that will squish together the plane.

-
I think one has to be careful with a linear subtraction, as this will change the size of the surface. Note that EuclideanDistance[data[[1]],data[[-1]]] does not equal EuclideanDistance[corrected[[1]],data[[-1]]] –  bobthechemist Nov 5 '13 at 1:56