mapstitch.cpp
Go to the documentation of this file.
00001 
00025 #include "mapstitch/mapstitch.h"
00026 #include "math.h"
00027 
00028 StitchedMap::StitchedMap(Mat &img1, Mat &img2, float max_pairwise_distance)
00029 {
00030   // load images, TODO: check that they're grayscale
00031   image1 = img1.clone();
00032   image2 = img2.clone();
00033 
00034   // create feature detector set.
00035   OrbFeatureDetector detector;
00036   OrbDescriptorExtractor dexc;
00037   BFMatcher dematc(NORM_HAMMING, false);
00038 
00039   // 1. extract keypoints
00040   detector.detect(image1, kpv1);
00041   detector.detect(image2, kpv2);
00042 
00043   // 2. extract descriptors
00044   dexc.compute(image1, kpv1, dscv1);
00045   dexc.compute(image2, kpv2, dscv2);
00046 
00047   // 3. match keypoints
00048   dematc.match(dscv1, dscv2, matches);
00049 
00050   // 4. find matching point pairs with same distance in both images
00051   for (size_t i=0; i<matches.size(); i++) {
00052     KeyPoint a1 = kpv1[matches[i].queryIdx],
00053              b1 = kpv2[matches[i].trainIdx];
00054 
00055     if (matches[i].distance > 30)
00056       continue;
00057 
00058     for (size_t j=0; j<matches.size(); j++) {
00059       KeyPoint a2 = kpv1[matches[j].queryIdx],
00060                b2 = kpv2[matches[j].trainIdx];
00061 
00062       if (matches[j].distance > 30)
00063         continue;
00064 
00065       if ( fabs(norm(a1.pt-a2.pt) - norm(b1.pt-b2.pt)) > max_pairwise_distance ||
00066            fabs(norm(a1.pt-a2.pt) - norm(b1.pt-b2.pt)) == 0)
00067         continue;
00068 
00069       coord1.push_back(a1.pt);
00070       coord1.push_back(a2.pt);
00071       coord2.push_back(b1.pt);
00072       coord2.push_back(b2.pt);
00073 
00074       fil1.push_back(a1);
00075       fil1.push_back(a2);
00076       fil2.push_back(b1);
00077       fil2.push_back(b2);
00078     }
00079   }
00080 
00081   if (coord1.size() == 0)
00082     ;
00083 
00084   // 5. find homography
00085   H = estimateRigidTransform(coord2, coord1, false);
00086 
00087   // 6. calculate this stuff for information
00088   rotation = 180./M_PI*atan2(H.at<double>(0,1),H.at<double>(1,1)),
00089   transx   = H.at<double>(0,2),
00090   transy   = H.at<double>(1,2);
00091   scalex   = sqrt(pow(H.at<double>(0,0),2)+pow(H.at<double>(0,1),2));
00092   scaley   = sqrt(pow(H.at<double>(1,0),2)+pow(H.at<double>(1,1),2));
00093 }
00094 
00095 Mat
00096 StitchedMap::get_debug()
00097 {
00098   Mat out;
00099   drawKeypoints(image1, kpv1, image1, Scalar(255,0,0));
00100   drawKeypoints(image2, kpv2, image2, Scalar(255,0,0));
00101   drawMatches(image1,fil1, image2,fil2, matches,out,Scalar::all(-1),Scalar::all(-1));
00102   return out;
00103 }
00104 
00105 Mat // return the stitched maps
00106 StitchedMap::get_stitch()
00107 {
00108   // create storage for new image and get transformations
00109   Mat image(image2.size(), image2.type());
00110   warpAffine(image2,image,H,image.size());
00111 
00112   // blend image1 onto the transformed image2
00113   addWeighted(image,.5,image1,.5,0.0,image);
00114 
00115   return image;
00116 }
00117 
00118 StitchedMap::~StitchedMap() { }


mapstitch
Author(s): Philipp M. Scholl
autogenerated on Mon Oct 6 2014 02:09:53