draw.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 *
00004 *  Copyright (c) 2009, Willow Garage, Inc.
00005 *  All rights reserved.
00006 *
00007 *  Redistribution and use in source and binary forms, with or without
00008 *  modification, are permitted provided that the following conditions
00009 *  are met:
00010 *
00011 *   * Redistributions of source code must retain the above copyright
00012 *     notice, this list of conditions and the following disclaimer.
00013 *   * Redistributions in binary form must reproduce the above
00014 *     copyright notice, this list of conditions and the following
00015 *     disclaimer in the documentation and/or other materials provided
00016 *     with the distribution.
00017 *   * Neither the name of the Willow Garage nor the names of its
00018 *     contributors may be used to endorse or promote products derived
00019 *     from this software without specific prior written permission.
00020 *
00021 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 *  POSSIBILITY OF SUCH DAMAGE.
00033 *********************************************************************/
00034 
00035 
00036 //
00037 // drawing VO traces
00038 //
00039 
00040 #include <frame_common/frame.h>
00041 #include <frame_common/frame_extended.h>
00042 
00043 using namespace std;
00044 
00045 namespace frame_common {
00046 
00047   static void drawSmallKeypoints(const cv::Mat& image, const std::vector<cv::KeyPoint>& keypoints,
00048                                  cv::Mat& display, cv::Scalar color)
00049   {
00050     int shift_bits = 4;
00051     int multiplier = 1 << shift_bits;
00052     for (std::vector<cv::KeyPoint>::const_iterator i = keypoints.begin(), ie = keypoints.end();
00053          i != ie; ++i) {
00054       cv::Point center(i->pt.x * multiplier, i->pt.y * multiplier);
00055       int radius = 1*multiplier;
00056       cv::circle(display, center, radius, color, 1, CV_AA, shift_bits);
00058     }
00059   }
00060 
00061   void drawVOtracks(const cv::Mat &image,
00062                     const vector<Frame, Eigen::aligned_allocator<Frame> > &frames, 
00063                     cv::Mat &display)
00064   {
00065     // Set up image display
00066     display = cv::Mat::zeros(image.rows, image.cols, CV_8UC3);
00067     int shift_bits = 4;
00068     int multiplier = 1 << shift_bits;
00069 
00070     // Last frame
00071     const Frame &f0 = frames.back();
00072 
00073     // Draw keypoints
00074     cv::cvtColor(image, display, CV_GRAY2BGR);
00075     drawSmallKeypoints(image, f0.kpts, display, CV_RGB(255, 0, 0));
00076     drawSmallKeypoints(image, f0.tkpts, display, CV_RGB(0, 255, 0));
00077 
00078     return;
00079 
00080     if (frames.size() < 2) return;
00081 
00082     // make a map of the points referred to in the last frame
00083     const vector<int> &ipts = f0.ipts;
00084     map<int,int> pmap;
00085     for (int i=0; i<(int)ipts.size(); i++)
00086       if (ipts[i] >= 0)
00087         pmap.insert(pair<int,int>(ipts[i],i));
00088     
00089     // iterate over frames, drawing tracks
00090     for (int fi=0; fi<(int)frames.size(); fi++)
00091       {
00092         int c = ((frames.size()-1-fi)*255*2)/(frames.size()-1);
00093         if (c > 255) c=255;
00094         cv::Scalar color(255,c,0); // BGR order
00095 
00096         const Frame &f1 = frames[fi];
00097         const vector<int> &iipts = f1.ipts;
00098         map<int,int>::iterator it;
00099         for (int i=0; i<(int)iipts.size(); i++)
00100           {
00101             int k1 = iipts[i];
00102             it = pmap.find(k1);
00103             if (it != pmap.end())     // found a match!
00104               {
00105                 int k0 = it->second;
00106                 const cv::KeyPoint& kpt0 = f0.kpts[k0];
00107                 const cv::KeyPoint& kpt1 = f1.kpts[i];
00108                 cv::Point center1(kpt0.pt.x * multiplier, kpt0.pt.y * multiplier);
00109                 cv::Point center2(kpt1.pt.x * multiplier, kpt1.pt.y * multiplier);
00110                 cv::line(display, center1, center2, color, 1, CV_AA, shift_bits);
00111                 pmap.erase(it);
00112               }
00113           }
00114       }
00115 
00116   }
00117   
00118   void drawVOtracks(const cv::Mat &image,
00119                     const vector<FrameExtended, Eigen::aligned_allocator<FrameExtended> > &frames, 
00120                     cv::Mat &display)
00121   {
00122     // Set up image display
00123     display = cv::Mat::zeros(image.rows, image.cols, CV_8UC3);
00124     int shift_bits = 4;
00125     int multiplier = 1 << shift_bits;
00126 
00127     // Last frame
00128     const Frame &f0 = frames.back();
00129 
00130     // Draw keypoints
00131     cv::cvtColor(image, display, CV_GRAY2BGR);
00132     drawSmallKeypoints(image, f0.kpts, display, CV_RGB(255, 0, 0));
00133 
00134     if (frames.size() < 2) return;
00135 
00136     // make a map of the points referred to in the last frame
00137     const vector<int> &ipts = f0.ipts;
00138     map<int,int> pmap;
00139     for (int i=0; i<(int)ipts.size(); i++)
00140       if (ipts[i] >= 0)
00141         pmap.insert(pair<int,int>(ipts[i],i));
00142     
00143     // iterate over frames, drawing tracks
00144     for (int fi=0; fi<(int)frames.size(); fi++)
00145       {
00146         int c = ((frames.size()-1-fi)*255*2)/(frames.size()-1);
00147         if (c > 255) c=255;
00148         cv::Scalar color(255,c,0); // BGR order
00149 
00150         const Frame &f1 = frames[fi];
00151         const vector<int> &iipts = f1.ipts;
00152         map<int,int>::iterator it;
00153         for (int i=0; i<(int)iipts.size(); i++)
00154           {
00155             int k1 = iipts[i];
00156             it = pmap.find(k1);
00157             if (it != pmap.end())     // found a match!
00158               {
00159                 int k0 = it->second;
00160                 const cv::KeyPoint& kpt0 = f0.kpts[k0];
00161                 const cv::KeyPoint& kpt1 = f1.kpts[i];
00162                 cv::Point center1(kpt0.pt.x * multiplier, kpt0.pt.y * multiplier);
00163                 cv::Point center2(kpt1.pt.x * multiplier, kpt1.pt.y * multiplier);
00164                 cv::line(display, center1, center2, color, 1, CV_AA, shift_bits);
00165                 pmap.erase(it);
00166               }
00167           }
00168       }
00169 
00170   }
00171 
00172 
00173 
00174 } //namespace frame_common


frame_common
Author(s): Kurt Konolige
autogenerated on Thu Jan 2 2014 12:12:04