Saturday, January 30, 2010

Thin-plate spline example

Hello again,
Since there have been some questions about an example, I will show you how to use the CThinPlateSpline-Class. First of all we have to load an image from our disk. The image can be of any size, depth or color format. Just call


// load a nice picture
cv::Mat img = cv::imread("C:\\lena512color.jpg");




Now that we have an image we should set some reference points on which the spline algorithm will evaluate the distortion. Normally you would use a interest point detector, but it was easier to just add some generic points to the dataset.
 
// generate some generic points
// usually you would use a interest point detector such as SURF or SIFT
 
std::vector iP, iiP;

// push some points into the vector for the source image
iP.push_back(cv::Point(50,50));
iP.push_back(cv::Point(400,50));
iP.push_back(cv::Point(50,400));
iP.push_back(cv::Point(400,400));
iP.push_back(cv::Point(256,256));
iP.push_back(cv::Point(150,256));

// push some point into the vector for the dst image
iiP.push_back(cv::Point(70,70));
iiP.push_back(cv::Point(430,60));
iiP.push_back(cv::Point(60,410));
iiP.push_back(cv::Point(430,420));
iiP.push_back(cv::Point(220,280));
iiP.push_back(cv::Point(180,240));



Now we have already done the tedious part, lets create a CThinPlateSpline object that will do all the work for you.

 // create thin plate spline object and put the vectors into the constructor

CThinPlateSpline tps(iP,iiP);

The only thing we have to do now is to call the internal warping function and set the correct parameters.

// warp the image to dst
Mat dst;
tps.warpImage(img,dst,0.01,INTER_CUBIC,BACK_WARP);


To see the results just call the imshow function out of opencv

// show images
cv::imshow("original",img);
cv::imshow("distorted",dst);
cv::waitKey(0);


Because it's quite a tradition I used our good old gal "lena" to show you the advantages of the spline algorithm. Just take a look. 

Thin-plate spline interpolation scheme

Few days have passed since the last post. Since then I was busy implementing my new CThinPlateSpline-Class that wraps the functionality I intended to provide you with. To give you all better access I started a project on code.google.com where you can checkout the sources an use them.

svn checkout http://ipwithopencv.googlecode.com/svn/trunk/

This link will provide you the access to the svn repository where you can find the actual source code in the trunk folder. I had not the time to intensely check the code for bugs, so I hope you will provide me with bug reports if you find something.

The code is not really in its final state, since I want to add more TPS approximations and improvements. But as it is now, you should get acquainted with the code really fast since it's kind of self-explaining. Nonetheless I will try to post a simple code example so everyone knows how to use the class.

I also took the time to comment the code, at least the header file. It was done in doxygen-style so the is a small but nice documentation included in the project. The documentation is located at the source folder and can be found in the folder "doc". If you really trying to use my code base, take a look there.

Since time was of the essence, I simply uploaded the VisualStudio2010(beta2) project to the server. I know this isn't the nicest way to provide you with my code, but know this - you really do need only two files to get going with your own project. Simply check out  the following files:

CThinPlateSpline.h
CThinPlateSpline.cpp

All functionality is provided by those two files. Just add them to your own project and link against the OpenCV 2.0 libraries. In the coming week I will try to provide you with a more suitable approach. I'm planning to wrap the sources into a library or a dll (for windows users) so you simply have to compile the code.

Saturday, January 23, 2010

Non-rigid image transformations with Thin-Plate-Spline interpolation scheme

It has been several month that I wrote something, but because there were many questions about my OpenCV implementation of Bookstein's Thin-Plate-Spline Bookstein-ThinPlateSpline [pdf] algorithm, I decided to write a few words about it and show you my implementation so that everyone who might need the capabilities of the algorithm can use it.

Since my implementation was implemented over a year ago, it is based on the old OpenCV 1.1pre release. Since the new C++-Interface introduced with OpenCV 2.0 has more capabilities and is much more convenient to use, I think it is best to port the TPS to this version.

Over the next few days I will rework my functions into a easy to use c++-class and show you my implementation which was somewhat improved over the originally introduced spline algorithm. I will start today to wrap my source into that class and start several threads to show you the steps and the results, even the process of working with the Thin Plate Spline.