stereo-vision
All Data Structures Namespaces Functions Modules Pages
utils.cpp
1 /*
2  * Copyright (C) 2011 Department of Robotics Brain and Cognitive Sciences - Istituto Italiano di Tecnologia
3  * Authors: Vadim Tikhanoff
4  * email: vadim.tikhanoff@iit.it
5  * website: www.robotcub.org
6  * Permission is granted to copy, distribute, and/or modify this program
7  * under the terms of the GNU General Public License, version 2 or any
8  * later version published by the Free Software Foundation.
9  *
10  * A copy of the license can be found at
11  * http://www.robotcub.org/icub/license/gpl.txtd
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16  * Public License for more details
17  */
18 #include <iostream>
19 #include <string>
20 
21 #include <yarp/sig/Image.h>
22 #include <yarp/os/BufferedPort.h>
23 #include <yarp/os/RFModule.h>
24 #include <yarp/os/Network.h>
25 #include <yarp/os/Time.h>
26 #include <yarp/os/Stamp.h>
27 #include <cv.h>
28 #include <highgui.h>
29 
30 #include <iCub/stereoVision/utils.h>
31 
32 
33 using namespace std;
34 using namespace cv;
35 
36 /************************************************************************/
37 Utilities::Utilities()
38 {
39 }
40 
41 /************************************************************************/
42 Utilities::~Utilities()
43 {
44 }
45 /************************************************************************/
46 void Utilities::initSIFT_GPU()
47 {
48  SiftGPU* (*pCreateNewSiftGPU)(int) = NULL;
49  SiftMatchGPU* (*pCreateNewSiftMatchGPU)(int) = NULL;
50 
51  char * pPath;
52  pPath = getenv ("SIFTGPU_DIR");
53  printf ("\n\nThe current path is: %s\n\n",pPath);
54 
55  std::string str = pPath;
56  str.append("/bin/libsiftgpu.so");
57  hsiftgpu = dlopen(str.c_str(), RTLD_LAZY);
58 
59  pCreateNewSiftGPU = (SiftGPU* (*) (int)) GET_MYPROC(hsiftgpu, "CreateNewSiftGPU");
60  pCreateNewSiftMatchGPU = (SiftMatchGPU* (*)(int)) GET_MYPROC(hsiftgpu, "CreateNewSiftMatchGPU");
61  sift = pCreateNewSiftGPU(1);
62  matcher = pCreateNewSiftMatchGPU(4096);
63 
64 
65  char * argv[] = {(char*)"-fo", (char*)"-1", (char*) "-v",(char*) "1", (char*)"-winpos",(char*)"-maxd", (char*)"1024", (char*)"-cuda"};
66  int argc = sizeof(argv)/sizeof(char*);
67 
68  sift->ParseParam(argc, argv);
69  //verbose on sift to remove unwanted printouts (put 1 for data)
70  sift->SetVerbose(0);
71 
72  if(sift->CreateContextGL() != SiftGPU::SIFTGPU_FULL_SUPPORTED)
73  fprintf(stdout,"boh, some error\n");
74 
75  matcher->VerifyContextGL();
76  writeS=false;
77 }
78 
79 /************************************************************************/
80 void Utilities::extractMatch_GPU(Mat &leftMat, Mat &rightMat, Mat &matMatches,
81  double displacement)
82 {
83  sift->RunSIFT( leftMat.cols, leftMat.rows, leftMat.data, GL_RGB, GL_UNSIGNED_BYTE );
84  num1 = sift->GetFeatureNum();
85  keys1.resize(num1); descriptors1.resize(128*num1);
86  sift->GetFeatureVector(&keys1[0], &descriptors1[0]);
87 
88  sift->RunSIFT( rightMat.cols, rightMat.rows, rightMat.data, GL_RGB, GL_UNSIGNED_BYTE );
89  num2 = sift->GetFeatureNum();
90  keys2.resize(num2); descriptors2.resize(128*num2);
91  sift->GetFeatureVector(&keys2[0], &descriptors2[0]);
92 
93  matcher->SetDescriptors(0, num1, &descriptors1[0]);
94  matcher->SetDescriptors(1, num2, &descriptors2[0]);
95 
96  int (*match_buf)[2] = new int[num1][2];
97  int num_match = matcher->GetSiftMatch(num1, match_buf);
98  //fprintf(stdout, "%d SIFT matches were found ", num_match);
99 
100  pointsL.clear();
101  pointsR.clear();
102  //enumerate all the feature matches
103  for (int i=0; i<num_match; i++)
104  {
105  SiftGPU::SiftKeypoint & key1 = keys1[match_buf[i][0]];
106  SiftGPU::SiftKeypoint & key2 = keys2[match_buf[i][1]];
107  //key1 in the first image matches with key2 in the second image
108  if (fabs(key1.y-key2.y)<displacement)
109  {
110  double x=key1.x;
111  double y=key1.y;
112  circle(matMatches,cvPoint(x,y),2,cvScalar(255,0,0),2);
113 
114  double x2=leftMat.cols+key2.x;
115  double y2=key2.y;
116  circle(matMatches,cvPoint(x2,y2),2,cvScalar(255,0,0),2);
117  line(matMatches, cvPoint(x,y),cvPoint(x2,y2),cvScalar(255,255,255));
118 
119  Point2f p1(x,y);
120  Point2f p2(x2-leftMat.cols,y2);
121 
122  pointsL.push_back(p1);
123  pointsR.push_back(p2);
124  }
125  }
126 }
127 
128 /************************************************************************/
129 void Utilities::extractMatch_GPU(Mat &leftMat, Mat &rightMat, double displacement)
130 {
131  sift->RunSIFT( leftMat.cols, leftMat.rows, leftMat.data, GL_RGB, GL_UNSIGNED_BYTE );
132  num1 = sift->GetFeatureNum();
133  keys1.resize(num1); descriptors1.resize(128*num1);
134  sift->GetFeatureVector(&keys1[0], &descriptors1[0]);
135 
136  sift->RunSIFT( rightMat.cols, rightMat.rows, rightMat.data, GL_RGB, GL_UNSIGNED_BYTE );
137  num2 = sift->GetFeatureNum();
138  keys2.resize(num2); descriptors2.resize(128*num2);
139  sift->GetFeatureVector(&keys2[0], &descriptors2[0]);
140 
141  matcher->SetDescriptors(0, num1, &descriptors1[0]);
142  matcher->SetDescriptors(1, num2, &descriptors2[0]);
143 
144  int (*match_buf)[2] = new int[num1][2];
145  int num_match = matcher->GetSiftMatch(num1, match_buf);
146  //fprintf(stdout, "%d SIFT matches were found ", num_match);
147 
148  pointsL.clear();
149  pointsR.clear();
150  //enumerate all the feature matches
151  for(int i = 0; i < num_match; ++i)
152  {
153  SiftGPU::SiftKeypoint & key1 = keys1[match_buf[i][0]];
154  SiftGPU::SiftKeypoint & key2 = keys2[match_buf[i][1]];
155  //key1 in the first image matches with key2 in the second image
156  if(fabs(key1.y-key2.y)<displacement)
157  {
158  double x=key1.x;
159  double y=key1.y;
160 
161  double x2=leftMat.cols+key2.x;
162  double y2=key2.y;
163 
164  Point2f p1(x,y);
165  Point2f p2(x2-leftMat.cols,y2);
166 
167  pointsL.push_back(p1);
168  pointsR.push_back(p2);
169  }
170  }
171 }
172 
173 void Utilities::getMatches(std::vector<cv::Point2f> & points1, std::vector<cv::Point2f> & points2)
174 {
175  points1=pointsL;
176  points2=pointsR;
177 }
178 
179 
180 void Utilities::writeSIFTs(std::string filePath, std::vector<float> &des, std::vector<SiftGPU::SiftKeypoint> &points)
181 {
182 
183 
184  string line;
185  ofstream infile;
186  infile.open (filePath.c_str());
187 
188  int cnt=0;
189  for (int i=0; i<points.size(); i++)
190  {
191  infile << points[i].x << " " << points[i].y << " ";
192  for (int k=0; k<128; k++)
193  {
194  infile << des[cnt] << " ";
195  cnt++;
196  }
197  infile << endl;
198 
199  }
200  infile.close();
201 
202 }
203 
204 void Utilities::writeMatch(std::string filePath,std::vector<cv::Point2f> &pointsL, std::vector<cv::Point2f> &pointsR)
205 {
206 
207 
208  string line;
209  ofstream infile;
210  infile.open (filePath.c_str());
211 
212  int cnt=0;
213  for (int i=0; i<pointsL.size(); i++)
214  {
215  infile << pointsL[i].x << " " << pointsL[i].y << " " << pointsR[i].x << " " << pointsR[i].y ;
216 
217  infile << endl;
218 
219  }
220  infile.close();
221 
222 }