ViSP
testConvert.cpp
1 /****************************************************************************
2  *
3  * $Id: testIoTools.cpp 5210 2015-01-26 10:51:11Z strinh $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Test functions in vpIoTools.
36  *
37  * Authors:
38  * Souriya Trinh
39  *
40  *****************************************************************************/
41 
50 #include <iostream> // std::cout
51 #include <limits> // std::numeric_limits
52 #include <visp/vpConfig.h>
53 #include <visp/vpConvert.h>
54 
55 
56 bool areSame(double a, double b) {
57  return fabs(a - b) < std::numeric_limits<double>::epsilon();
58 }
59 
60 void testConvertFromImagePointToPoint2f() {
61 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101)
62  vpImagePoint imPt1(12.5f, .85f);
63  vpImagePoint imPt2(-44.26f, 125.11f);
64  vpImagePoint imPt3(0.0f, -1.756e-10f);
65 
66  cv::Point2f pt1, pt2, pt3;
67  vpConvert::convertToOpenCV(imPt1, pt1);
68  vpConvert::convertToOpenCV(imPt2, pt2);
69  vpConvert::convertToOpenCV(imPt3, pt3);
70 
71  int nbOk = 0, nbNOk = 0;
72  if(areSame(imPt1.get_u(), pt1.x) && areSame(imPt1.get_v(), pt1.y)) nbOk++; else nbNOk++;
73  if(areSame(imPt2.get_u(), pt2.x) && areSame(imPt2.get_v(), pt2.y)) nbOk++; else nbNOk++;
74  if(areSame(imPt3.get_u(), pt3.x) && areSame(imPt3.get_v(), pt3.y)) nbOk++; else nbNOk++;
75 
76  std::vector<vpImagePoint> listOfImPts(3);
77  listOfImPts[0] = imPt1;
78  listOfImPts[1] = imPt2;
79  listOfImPts[2] = imPt3;
80 
81  std::vector<cv::Point2f> listOfPts;
82  vpConvert::convertToOpenCV(listOfImPts, listOfPts);
83 
84  if(listOfImPts.size() == listOfPts.size()) {
85  for(size_t i = 0; i < 3; i++) {
86  if(areSame(listOfImPts[i].get_u(), listOfPts[i].x) && areSame(listOfImPts[i].get_v(), listOfPts[i].y)) nbOk++; else nbNOk++;
87  }
88  } else {
89  nbNOk += 3;
90  }
91 
92  std::cout << "testConvertFromImagePointToPoint2f=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
93 #endif
94 }
95 
96 void testConvertFromPoint2fToImagePoint() {
97 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101)
98  vpImagePoint imPt1, imPt2, imPt3;
99 
100  cv::Point2f pt1(12.5f, .85f), pt2(-44.26f, 125.11f), pt3(0.0f, -1.756e-10f);
101  vpConvert::convertFromOpenCV(pt1, imPt1);
102  vpConvert::convertFromOpenCV(pt2, imPt2);
103  vpConvert::convertFromOpenCV(pt3, imPt3);
104 
105  int nbOk = 0, nbNOk = 0;
106  if(areSame(imPt1.get_u(), pt1.x) && areSame(imPt1.get_v(), pt1.y)) nbOk++; else nbNOk++;
107  if(areSame(imPt2.get_u(), pt2.x) && areSame(imPt2.get_v(), pt2.y)) nbOk++; else nbNOk++;
108  if(areSame(imPt3.get_u(), pt3.x) && areSame(imPt3.get_v(), pt3.y)) nbOk++; else nbNOk++;
109 
110  std::vector<vpImagePoint> listOfImPts;
111 
112  std::vector<cv::Point2f> listOfPts(3);
113  listOfPts[0] = pt1;
114  listOfPts[1] = pt2;
115  listOfPts[2] = pt3;
116 
117  vpConvert::convertFromOpenCV(listOfPts, listOfImPts);
118 
119  if(listOfImPts.size() == listOfPts.size()) {
120  for(size_t i = 0; i < 3; i++) {
121  if(areSame(listOfImPts[i].get_u(), listOfPts[i].x) && areSame(listOfImPts[i].get_v(), listOfPts[i].y)) nbOk++; else nbNOk++;
122  }
123  } else {
124  nbNOk += 3;
125  }
126 
127  std::cout << "testConvertFromPoint2fToImagePoint=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
128 #endif
129 }
130 
131 void testConvertFromImagePointToPoint2d() {
132 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101)
133  vpImagePoint imPt1(12.5, .85);
134  vpImagePoint imPt2(-44.26, 125.11);
135  vpImagePoint imPt3(0, -1.756e-10);
136 
137  cv::Point2d pt1, pt2, pt3;
138  vpConvert::convertToOpenCV(imPt1, pt1);
139  vpConvert::convertToOpenCV(imPt2, pt2);
140  vpConvert::convertToOpenCV(imPt3, pt3);
141 
142  int nbOk = 0, nbNOk = 0;
143  if(areSame(imPt1.get_u(), pt1.x) && areSame(imPt1.get_v(), pt1.y)) nbOk++; else nbNOk++;
144  if(areSame(imPt2.get_u(), pt2.x) && areSame(imPt2.get_v(), pt2.y)) nbOk++; else nbNOk++;
145  if(areSame(imPt3.get_u(), pt3.x) && areSame(imPt3.get_v(), pt3.y)) nbOk++; else nbNOk++;
146 
147  std::vector<vpImagePoint> listOfImPts(3);
148  listOfImPts[0] = imPt1;
149  listOfImPts[1] = imPt2;
150  listOfImPts[2] = imPt3;
151 
152  std::vector<cv::Point2d> listOfPts;
153  vpConvert::convertToOpenCV(listOfImPts, listOfPts);
154 
155  if(listOfImPts.size() == listOfPts.size()) {
156  for(size_t i = 0; i < 3; i++) {
157  if(areSame(listOfImPts[i].get_u(), listOfPts[i].x) && areSame(listOfImPts[i].get_v(), listOfPts[i].y)) nbOk++; else nbNOk++;
158  }
159  } else {
160  nbNOk += 3;
161  }
162 
163  std::cout << "testConvertFromImagePointToPoint2d=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
164 #endif
165 }
166 
167 void testConvertFromPoint2dToImagePoint() {
168 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101)
169  vpImagePoint imPt1, imPt2, imPt3;
170 
171  cv::Point2d pt1(12.5, .85), pt2(-44.26, 125.11), pt3(0, -1.756e-10);
172  vpConvert::convertFromOpenCV(pt1, imPt1);
173  vpConvert::convertFromOpenCV(pt2, imPt2);
174  vpConvert::convertFromOpenCV(pt3, imPt3);
175 
176  int nbOk = 0, nbNOk = 0;
177  if(areSame(imPt1.get_u(), pt1.x) && areSame(imPt1.get_v(), pt1.y)) nbOk++; else nbNOk++;
178  if(areSame(imPt2.get_u(), pt2.x) && areSame(imPt2.get_v(), pt2.y)) nbOk++; else nbNOk++;
179  if(areSame(imPt3.get_u(), pt3.x) && areSame(imPt3.get_v(), pt3.y)) nbOk++; else nbNOk++;
180 
181  std::vector<vpImagePoint> listOfImPts;
182 
183  std::vector<cv::Point2d> listOfPts(3);
184  listOfPts[0] = pt1;
185  listOfPts[1] = pt2;
186  listOfPts[2] = pt3;
187 
188  vpConvert::convertFromOpenCV(listOfPts, listOfImPts);
189 
190  if(listOfImPts.size() == listOfPts.size()) {
191  for(size_t i = 0; i < 3; i++) {
192  if(areSame(listOfImPts[i].get_u(), listOfPts[i].x) && areSame(listOfImPts[i].get_v(), listOfPts[i].y)) nbOk++; else nbNOk++;
193  }
194  } else {
195  nbNOk += 3;
196  }
197 
198  std::cout << "testConvertFromPoint2dToImagePoint=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
199 #endif
200 }
201 
202 void testConvertFromKeyPointToImagePoint() {
203 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101)
204  cv::KeyPoint kp1(12.5f, .85f, 0), kp2(-44.26f, 125.11f, 0), kp3(0.0f, -1.756e-10f, 0);
205  vpImagePoint imPt1, imPt2, imPt3;
206 
207  vpConvert::convertFromOpenCV(kp1, imPt1);
208  vpConvert::convertFromOpenCV(kp2, imPt2);
209  vpConvert::convertFromOpenCV(kp3, imPt3);
210 
211  int nbOk = 0, nbNOk = 0;
212  if(areSame(imPt1.get_u(), kp1.pt.x) && areSame(imPt1.get_v(), kp1.pt.y)) nbOk++; else nbNOk++;
213  if(areSame(imPt2.get_u(), kp2.pt.x) && areSame(imPt2.get_v(), kp2.pt.y)) nbOk++; else nbNOk++;
214  if(areSame(imPt3.get_u(), kp3.pt.x) && areSame(imPt3.get_v(), kp3.pt.y)) nbOk++; else nbNOk++;
215 
216  std::vector<cv::KeyPoint> listOfKeyPoints(3);
217  listOfKeyPoints[0] = kp1;
218  listOfKeyPoints[1] = kp2;
219  listOfKeyPoints[2] = kp3;
220 
221  std::vector<vpImagePoint> listOfImPts;
222  vpConvert::convertFromOpenCV(listOfKeyPoints, listOfImPts);
223 
224  if(listOfImPts.size() == listOfKeyPoints.size()) {
225  for(size_t i = 0; i < 3; i++) {
226  if(areSame(listOfImPts[i].get_u(), listOfKeyPoints[i].pt.x) && areSame(listOfImPts[i].get_v(), listOfKeyPoints[i].pt.y)) nbOk++; else nbNOk++;
227  }
228  } else {
229  nbNOk += 3;
230  }
231 
232  std::cout << "testConvertFromKeyPointToImagePoint=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
233 #endif
234 }
235 
236 void testConvertFromPoint3fToPoint() {
237 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101)
238  cv::Point3f pt1(12.5f, .85f, 110.0f), pt2(-44.26f, 125.11f, -98e2f), pt3(0.0f, -1.756e-10f, 0.00015f);
239  vpPoint point1, point2, point3;
240 
241  vpConvert::convertFromOpenCV(pt1, point1);
242  vpConvert::convertFromOpenCV(pt2, point2);
243  vpConvert::convertFromOpenCV(pt3, point3);
244 
245  int nbOk = 0, nbNOk = 0;
246  if(areSame(pt1.x, point1.get_oX()) && areSame(pt1.y, point1.get_oY()) && areSame(pt1.z, point1.get_oZ())) nbOk++; else nbNOk++;
247  if(areSame(pt2.x, point2.get_oX()) && areSame(pt2.y, point2.get_oY()) && areSame(pt2.z, point2.get_oZ())) nbOk++; else nbNOk++;
248  if(areSame(pt3.x, point3.get_oX()) && areSame(pt3.y, point3.get_oY()) && areSame(pt3.z, point3.get_oZ())) nbOk++; else nbNOk++;
249 
250  std::vector<cv::Point3f> listOfPoints3f(3);
251  listOfPoints3f[0] = pt1;
252  listOfPoints3f[1] = pt2;
253  listOfPoints3f[2] = pt3;
254 
255  std::vector<vpPoint> listOfPts;
256  vpConvert::convertFromOpenCV(listOfPoints3f, listOfPts);
257 
258  if(listOfPoints3f.size() == listOfPts.size()) {
259  for(size_t i = 0; i < 3; i++) {
260  if(areSame(listOfPts[i].get_oX(), listOfPoints3f[i].x) && areSame(listOfPts[i].get_oY(), listOfPoints3f[i].y) && areSame(listOfPts[i].get_oZ(), listOfPoints3f[i].z)) nbOk++; else nbNOk++;
261  }
262  } else {
263  nbNOk += 3;
264  }
265 
266  std::cout << "testConvertFromPoint3fToPoint=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
267 #endif
268 }
269 
270 void testConvertFromPointToPoint3f() {
271 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101)
272  cv::Point3f pt1, pt2, pt3;
273  vpPoint point1, point2, point3;
274  point1.set_oX(12.5f);
275  point1.set_oY(.85f);
276  point1.set_oZ(110.0f);
277 
278  point2.set_oX(-44.26f);
279  point2.set_oY(125.11f);
280  point2.set_oZ(-98e2f);
281 
282  point3.set_oX(0.0f);
283  point3.set_oY(-1.756e-10f);
284  point3.set_oZ(0.00015f);
285 
286  vpConvert::convertToOpenCV(point1, pt1);
287  vpConvert::convertToOpenCV(point2, pt2);
288  vpConvert::convertToOpenCV(point3, pt3);
289 
290  int nbOk = 0, nbNOk = 0;
291  if(areSame(pt1.x, point1.get_oX()) && areSame(pt1.y, point1.get_oY()) && areSame(pt1.z, point1.get_oZ())) nbOk++; else nbNOk++;
292  if(areSame(pt2.x, point2.get_oX()) && areSame(pt2.y, point2.get_oY()) && areSame(pt2.z, point2.get_oZ())) nbOk++; else nbNOk++;
293  if(areSame(pt3.x, point3.get_oX()) && areSame(pt3.y, point3.get_oY()) && areSame(pt3.z, point3.get_oZ())) nbOk++; else nbNOk++;
294 
295  std::vector<cv::Point3f> listOfPoints3f;
296  std::vector<vpPoint> listOfPts(3);
297  listOfPts[0] = point1;
298  listOfPts[1] = point2;
299  listOfPts[2] = point3;
300 
301  vpConvert::convertToOpenCV(listOfPts, listOfPoints3f);
302 
303  if(listOfPoints3f.size() == listOfPts.size()) {
304  for(size_t i = 0; i < 3; i++) {
305  if(areSame(listOfPts[i].get_oX(), listOfPoints3f[i].x) && areSame(listOfPts[i].get_oY(), listOfPoints3f[i].y) && areSame(listOfPts[i].get_oZ(), listOfPoints3f[i].z)) nbOk++; else nbNOk++;
306  }
307  } else {
308  nbNOk += 3;
309  }
310 
311  std::cout << "testConvertFromPointToPoint3f=" << nbOk << "/" << (nbOk + nbNOk) << std::endl;
312 #endif
313 }
314 
315 int main() {
316  testConvertFromImagePointToPoint2f();
317  testConvertFromPoint2fToImagePoint();
318  testConvertFromImagePointToPoint2d();
319  testConvertFromPoint2dToImagePoint();
320 
321  testConvertFromKeyPointToImagePoint();
322  testConvertFromPoint3fToPoint();
323  testConvertFromPointToPoint3f();
324  return 0;
325 }
double get_v() const
Definition: vpImagePoint.h:264
double get_u() const
Definition: vpImagePoint.h:253
double get_oY() const
Get the point Y coordinate in the object frame.
Definition: vpPoint.h:129
void set_oX(const double X)
Set the point X coordinate in the object frame.
Definition: vpPoint.h:185
Class that defines what is a point.
Definition: vpPoint.h:65
static void convertToOpenCV(const vpImagePoint &from, cv::Point2f &to)
Definition: vpConvert.cpp:345
void set_oZ(const double Z)
Set the point Z coordinate in the object frame.
Definition: vpPoint.h:189
double get_oZ() const
Get the point Z coordinate in the object frame.
Definition: vpPoint.h:131
double get_oX() const
Get the point X coordinate in the object frame.
Definition: vpPoint.h:127
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:93
static void convertFromOpenCV(const cv::KeyPoint &from, vpImagePoint &to)
Definition: vpConvert.cpp:217
void set_oY(const double Y)
Set the point Y coordinate in the object frame.
Definition: vpPoint.h:187