Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/viz/test/tests_simple.cpp
16354 views
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
8
//
9
//
10
// License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14
// Copyright (C) 2008-2013, Willow Garage Inc., all rights reserved.
15
// Third party copyrights are property of their respective owners.
16
//
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
19
//
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
22
//
23
// * Redistribution's in binary form must reproduce the above copyright notice,
24
// this list of conditions and the following disclaimer in the documentation
25
// and / or other materials provided with the distribution.
26
//
27
// * The name of the copyright holders may not be used to endorse or promote products
28
// derived from this software without specific prior written permission.
29
//
30
// This software is provided by the copyright holders and contributors "as is" and
31
// any express or implied warranties, including, but not limited to, the implied
32
// warranties of merchantability and fitness for a particular purpose are disclaimed.
33
// In no event shall the Intel Corporation or contributors be liable for any direct,
34
// indirect, incidental, special, exemplary, or consequential damages
35
// (including, but not limited to, procurement of substitute goods or services;
36
// loss of use, data, or profits; or business interruption) however caused
37
// and on any theory of liability, whether in contract, strict liability,
38
// or tort (including negligence or otherwise) arising in any way out of
39
// the use of this software, even if advised of the possibility of such damage.
40
//
41
//M*/
42
43
#include "test_precomp.hpp"
44
45
namespace opencv_test { namespace {
46
47
TEST(Viz, show_cloud_bluberry)
48
{
49
Mat dragon_cloud = readCloud(get_dragon_ply_file_path());
50
51
Affine3d pose = Affine3d().rotate(Vec3d(0, 0.8, 0));
52
53
Viz3d viz("show_cloud_bluberry");
54
viz.setBackgroundColor(Color::black());
55
viz.showWidget("coosys", WCoordinateSystem());
56
viz.showWidget("dragon", WCloud(dragon_cloud, Color::bluberry()), pose);
57
58
viz.showWidget("text2d", WText("Bluberry cloud", Point(20, 20), 20, Color::green()));
59
viz.spin();
60
}
61
62
TEST(Viz, show_cloud_random_color)
63
{
64
Mat dragon_cloud = readCloud(get_dragon_ply_file_path());
65
66
Mat colors(dragon_cloud.size(), CV_8UC3);
67
theRNG().fill(colors, RNG::UNIFORM, 0, 255);
68
69
Affine3d pose = Affine3d().rotate(Vec3d(0, 0.8, 0));
70
71
Viz3d viz("show_cloud_random_color");
72
viz.setBackgroundMeshLab();
73
viz.showWidget("coosys", WCoordinateSystem());
74
viz.showWidget("dragon", WCloud(dragon_cloud, colors), pose);
75
viz.showWidget("text2d", WText("Random color cloud", Point(20, 20), 20, Color::green()));
76
viz.spin();
77
}
78
79
TEST(Viz, show_cloud_masked)
80
{
81
Mat dragon_cloud = readCloud(get_dragon_ply_file_path());
82
83
Vec3f qnan = Vec3f::all(std::numeric_limits<float>::quiet_NaN());
84
for(int i = 0; i < (int)dragon_cloud.total(); ++i)
85
if (i % 15 != 0)
86
dragon_cloud.at<Vec3f>(i) = qnan;
87
88
Affine3d pose = Affine3d().rotate(Vec3d(0, 0.8, 0));
89
90
Viz3d viz("show_cloud_masked");
91
viz.showWidget("coosys", WCoordinateSystem());
92
viz.showWidget("dragon", WCloud(dragon_cloud), pose);
93
viz.showWidget("text2d", WText("Nan masked cloud", Point(20, 20), 20, Color::green()));
94
viz.spin();
95
}
96
97
TEST(Viz, show_cloud_collection)
98
{
99
Mat cloud = readCloud(get_dragon_ply_file_path());
100
101
WCloudCollection ccol;
102
ccol.addCloud(cloud, Color::white(), Affine3d().translate(Vec3d(0, 0, 0)).rotate(Vec3d(CV_PI/2, 0, 0)));
103
ccol.addCloud(cloud, Color::blue(), Affine3d().translate(Vec3d(1, 0, 0)));
104
ccol.addCloud(cloud, Color::red(), Affine3d().translate(Vec3d(2, 0, 0)));
105
ccol.finalize();
106
107
Viz3d viz("show_cloud_collection");
108
viz.setBackgroundColor(Color::mlab());
109
viz.showWidget("coosys", WCoordinateSystem());
110
viz.showWidget("ccol", ccol);
111
viz.showWidget("text2d", WText("Cloud collection", Point(20, 20), 20, Color::green()));
112
viz.spin();
113
}
114
115
TEST(Viz, show_painted_clouds)
116
{
117
Mat cloud = readCloud(get_dragon_ply_file_path());
118
119
Viz3d viz("show_painted_clouds");
120
viz.setBackgroundMeshLab();
121
viz.showWidget("coosys", WCoordinateSystem());
122
viz.showWidget("cloud1", WPaintedCloud(cloud), Affine3d(Vec3d(0.0, -CV_PI/2, 0.0), Vec3d(-1.5, 0.0, 0.0)));
123
viz.showWidget("cloud2", WPaintedCloud(cloud, Vec3d(0.0, -0.75, -1.0), Vec3d(0.0, 0.75, 0.0)), Affine3d(Vec3d(0.0, CV_PI/2, 0.0), Vec3d(1.5, 0.0, 0.0)));
124
viz.showWidget("cloud3", WPaintedCloud(cloud, Vec3d(0.0, 0.0, -1.0), Vec3d(0.0, 0.0, 1.0), Color::blue(), Color::red()));
125
viz.showWidget("arrow", WArrow(Vec3d(0.0, 1.0, -1.0), Vec3d(0.0, 1.0, 1.0), 0.009, Color::raspberry()));
126
viz.showWidget("text2d", WText("Painted clouds", Point(20, 20), 20, Color::green()));
127
viz.spin();
128
}
129
130
TEST(Viz, show_mesh)
131
{
132
Mesh mesh = Mesh::load(get_dragon_ply_file_path());
133
134
Affine3d pose = Affine3d().rotate(Vec3d(0, 0.8, 0));
135
136
Viz3d viz("show_mesh");
137
viz.showWidget("coosys", WCoordinateSystem());
138
viz.showWidget("mesh", WMesh(mesh), pose);
139
viz.showWidget("text2d", WText("Just mesh", Point(20, 20), 20, Color::green()));
140
viz.spin();
141
}
142
143
TEST(Viz, show_mesh_random_colors)
144
{
145
Mesh mesh = Mesh::load(get_dragon_ply_file_path());
146
theRNG().fill(mesh.colors, RNG::UNIFORM, 0, 255);
147
148
Affine3d pose = Affine3d().rotate(Vec3d(0, 0.8, 0));
149
150
Viz3d viz("show_mesh_random_color");
151
viz.showWidget("coosys", WCoordinateSystem());
152
viz.showWidget("mesh", WMesh(mesh), pose);
153
viz.setRenderingProperty("mesh", SHADING, SHADING_PHONG);
154
viz.showWidget("text2d", WText("Random color mesh", Point(20, 20), 20, Color::green()));
155
viz.spin();
156
}
157
158
TEST(Viz, show_widget_merger)
159
{
160
WWidgetMerger merger;
161
merger.addWidget(WCube(Vec3d::all(0.0), Vec3d::all(1.0), true, Color::gold()));
162
163
RNG& rng = theRNG();
164
for(int i = 0; i < 77; ++i)
165
{
166
Vec3b c;
167
rng.fill(c, RNG::NORMAL, Scalar::all(128), Scalar::all(48), true);
168
merger.addWidget(WSphere(Vec3d(c)*(1.0/255.0), 7.0/255.0, 10, Color(c[2], c[1], c[0])));
169
}
170
merger.finalize();
171
172
Viz3d viz("show_mesh_random_color");
173
viz.showWidget("coo", WCoordinateSystem());
174
viz.showWidget("merger", merger);
175
viz.showWidget("text2d", WText("Widget merger", Point(20, 20), 20, Color::green()));
176
viz.spin();
177
}
178
179
TEST(Viz, show_textured_mesh)
180
{
181
Mat lena = imread(Path::combine(cvtest::TS::ptr()->get_data_path(), "lena.png"));
182
183
std::vector<Vec3d> points;
184
std::vector<Vec2d> tcoords;
185
std::vector<int> polygons;
186
for(size_t i = 0; i < 64; ++i)
187
{
188
double angle = CV_PI/2 * i/64.0;
189
points.push_back(Vec3d(0.00, cos(angle), sin(angle))*0.75);
190
points.push_back(Vec3d(1.57, cos(angle), sin(angle))*0.75);
191
tcoords.push_back(Vec2d(0.0, i/64.0));
192
tcoords.push_back(Vec2d(1.0, i/64.0));
193
}
194
195
for(int i = 0; i < (int)points.size()/2-1; ++i)
196
{
197
int polys[] = {3, 2*i, 2*i+1, 2*i+2, 3, 2*i+1, 2*i+2, 2*i+3};
198
polygons.insert(polygons.end(), polys, polys + sizeof(polys)/sizeof(polys[0]));
199
}
200
201
cv::viz::Mesh mesh;
202
mesh.cloud = Mat(points, true).reshape(3, 1);
203
mesh.tcoords = Mat(tcoords, true).reshape(2, 1);
204
mesh.polygons = Mat(polygons, true).reshape(1, 1);
205
mesh.texture = lena;
206
207
Viz3d viz("show_textured_mesh");
208
viz.setBackgroundMeshLab();
209
viz.showWidget("coosys", WCoordinateSystem());
210
viz.showWidget("mesh", WMesh(mesh));
211
viz.setRenderingProperty("mesh", SHADING, SHADING_PHONG);
212
viz.showWidget("text2d", WText("Textured mesh", Point(20, 20), 20, Color::green()));
213
viz.spin();
214
}
215
216
TEST(Viz, show_polyline)
217
{
218
const Color palette[] = { Color::red(), Color::green(), Color::blue(), Color::gold(), Color::raspberry(), Color::bluberry(), Color::lime() };
219
size_t palette_size = sizeof(palette)/sizeof(palette[0]);
220
221
Mat polyline(1, 32, CV_64FC3), colors(1, 32, CV_8UC3);
222
for(int i = 0; i < (int)polyline.total(); ++i)
223
{
224
polyline.at<Vec3d>(i) = Vec3d(i/16.0, cos(i * CV_PI/6), sin(i * CV_PI/6));
225
colors.at<Vec3b>(i) = palette[i & palette_size];
226
}
227
228
Viz3d viz("show_polyline");
229
viz.showWidget("polyline", WPolyLine(polyline, colors));
230
viz.showWidget("coosys", WCoordinateSystem());
231
viz.showWidget("text2d", WText("Polyline", Point(20, 20), 20, Color::green()));
232
viz.spin();
233
}
234
235
TEST(Viz, show_sampled_normals)
236
{
237
Mesh mesh = Mesh::load(get_dragon_ply_file_path());
238
computeNormals(mesh, mesh.normals);
239
240
Affine3d pose = Affine3d().rotate(Vec3d(0, 0.8, 0));
241
242
Viz3d viz("show_sampled_normals");
243
viz.showWidget("mesh", WMesh(mesh), pose);
244
viz.showWidget("normals", WCloudNormals(mesh.cloud, mesh.normals, 30, 0.1f, Color::green()), pose);
245
viz.setRenderingProperty("normals", LINE_WIDTH, 2.0);
246
viz.showWidget("text2d", WText("Cloud or mesh normals", Point(20, 20), 20, Color::green()));
247
viz.spin();
248
}
249
250
TEST(Viz, show_cloud_shaded_by_normals)
251
{
252
Mesh mesh = Mesh::load(get_dragon_ply_file_path());
253
computeNormals(mesh, mesh.normals);
254
255
Affine3d pose = Affine3d().rotate(Vec3d(0, 0.8, 0));
256
257
WCloud cloud(mesh.cloud, Color::white(), mesh.normals);
258
cloud.setRenderingProperty(SHADING, SHADING_GOURAUD);
259
260
Viz3d viz("show_cloud_shaded_by_normals");
261
viz.showWidget("cloud", cloud, pose);
262
viz.showWidget("text2d", WText("Cloud shaded by normals", Point(20, 20), 20, Color::green()));
263
viz.spin();
264
}
265
266
TEST(Viz, show_trajectories)
267
{
268
std::vector<Affine3d> path = generate_test_trajectory<double>(), sub0, sub1, sub2, sub3, sub4, sub5;
269
int size =(int)path.size();
270
271
Mat(path).rowRange(0, size/10+1).copyTo(sub0);
272
Mat(path).rowRange(size/10, size/5+1).copyTo(sub1);
273
Mat(path).rowRange(size/5, 11*size/12).copyTo(sub2);
274
Mat(path).rowRange(11*size/12, size).copyTo(sub3);
275
Mat(path).rowRange(3*size/4, 33*size/40).copyTo(sub4);
276
Mat(path).rowRange(33*size/40, 9*size/10).copyTo(sub5);
277
Matx33d K(1024.0, 0.0, 320.0, 0.0, 1024.0, 240.0, 0.0, 0.0, 1.0);
278
279
Viz3d viz("show_trajectories");
280
viz.showWidget("coos", WCoordinateSystem());
281
viz.showWidget("sub0", WTrajectorySpheres(sub0, 0.25, 0.07));
282
viz.showWidget("sub1", WTrajectory(sub1, WTrajectory::PATH, 0.2, Color::brown()));
283
viz.showWidget("sub2", WTrajectory(sub2, WTrajectory::FRAMES, 0.2));
284
viz.showWidget("sub3", WTrajectory(sub3, WTrajectory::BOTH, 0.2, Color::green()));
285
viz.showWidget("sub4", WTrajectoryFrustums(sub4, K, 0.3, Color::yellow()));
286
viz.showWidget("sub5", WTrajectoryFrustums(sub5, Vec2d(0.78, 0.78), 0.15));
287
viz.showWidget("text2d", WText("Different kinds of supported trajectories", Point(20, 20), 20, Color::green()));
288
289
int i = 0;
290
while(!viz.wasStopped())
291
{
292
double a = --i % 360;
293
Vec3d pose(sin(a * CV_PI/180), 0.7, cos(a * CV_PI/180));
294
viz.setViewerPose(makeCameraPose(pose * 7.5, Vec3d(0.0, 0.5, 0.0), Vec3d(0.0, 0.1, 0.0)));
295
viz.spinOnce(20, true);
296
}
297
viz.resetCamera();
298
viz.spin();
299
}
300
301
TEST(Viz, show_trajectory_reposition)
302
{
303
std::vector<Affine3f> path = generate_test_trajectory<float>();
304
305
Viz3d viz("show_trajectory_reposition_to_origin");
306
viz.showWidget("coos", WCoordinateSystem());
307
viz.showWidget("sub3", WTrajectory(Mat(path).rowRange(0, (int)path.size()/3), WTrajectory::BOTH, 0.2, Color::brown()), path.front().inv());
308
viz.showWidget("text2d", WText("Trajectory resposition to origin", Point(20, 20), 20, Color::green()));
309
viz.spin();
310
}
311
312
TEST(Viz, show_camera_positions)
313
{
314
Matx33d K(1024.0, 0.0, 320.0, 0.0, 1024.0, 240.0, 0.0, 0.0, 1.0);
315
Mat lena = imread(Path::combine(cvtest::TS::ptr()->get_data_path(), "lena.png"));
316
Mat gray = make_gray(lena);
317
318
Affine3d poses[2];
319
for(int i = 0; i < 2; ++i)
320
{
321
Vec3d pose = 5 * Vec3d(sin(3.14 + 2.7 + i*60 * CV_PI/180), 0.4 - i*0.3, cos(3.14 + 2.7 + i*60 * CV_PI/180));
322
poses[i] = makeCameraPose(pose, Vec3d(0.0, 0.0, 0.0), Vec3d(0.0, -0.1, 0.0));
323
}
324
325
Viz3d viz("show_camera_positions");
326
viz.showWidget("sphe", WSphere(Point3d(0,0,0), 1.0, 10, Color::orange_red()));
327
viz.showWidget("coos", WCoordinateSystem(1.5));
328
viz.showWidget("pos1", WCameraPosition(0.75), poses[0]);
329
viz.showWidget("pos2", WCameraPosition(Vec2d(0.78, 0.78), lena, 2.2, Color::green()), poses[0]);
330
viz.showWidget("pos3", WCameraPosition(0.75), poses[1]);
331
viz.showWidget("pos4", WCameraPosition(K, gray, 3, Color::indigo()), poses[1]);
332
viz.showWidget("text2d", WText("Camera positions with images", Point(20, 20), 20, Color::green()));
333
viz.spin();
334
}
335
336
TEST(Viz, show_overlay_image)
337
{
338
Mat lena = imread(Path::combine(cvtest::TS::ptr()->get_data_path(), "lena.png"));
339
Mat gray = make_gray(lena);
340
341
Size2d half_lsize = Size2d(lena.size()) * 0.5;
342
343
Viz3d viz("show_overlay_image");
344
viz.setBackgroundMeshLab();
345
Size vsz = viz.getWindowSize();
346
347
viz.showWidget("coos", WCoordinateSystem());
348
viz.showWidget("cube", WCube());
349
viz.showWidget("img1", WImageOverlay(lena, Rect(Point(10, 10), half_lsize)));
350
viz.showWidget("img2", WImageOverlay(gray, Rect(Point(vsz.width-10-lena.cols/2, 10), half_lsize)));
351
viz.showWidget("img3", WImageOverlay(gray, Rect(Point(10, vsz.height-10-lena.rows/2), half_lsize)));
352
viz.showWidget("img5", WImageOverlay(lena, Rect(Point(vsz.width-10-lena.cols/2, vsz.height-10-lena.rows/2), half_lsize)));
353
viz.showWidget("text2d", WText("Overlay images", Point(20, 20), 20, Color::green()));
354
355
int i = 0;
356
while(!viz.wasStopped())
357
{
358
double a = ++i % 360;
359
Vec3d pose(sin(a * CV_PI/180), 0.7, cos(a * CV_PI/180));
360
viz.setViewerPose(makeCameraPose(pose * 3, Vec3d(0.0, 0.5, 0.0), Vec3d(0.0, 0.1, 0.0)));
361
viz.getWidget("img1").cast<WImageOverlay>().setImage(lena * pow(sin(i*10*CV_PI/180) * 0.5 + 0.5, 1.0));
362
viz.spinOnce(1, true);
363
}
364
viz.showWidget("text2d", WText("Overlay images (stopped)", Point(20, 20), 20, Color::green()));
365
viz.spin();
366
}
367
368
369
TEST(Viz, show_image_method)
370
{
371
Mat lena = imread(Path::combine(cvtest::TS::ptr()->get_data_path(), "lena.png"));
372
373
Viz3d viz("show_image_method");
374
viz.showImage(lena);
375
viz.spinOnce(1500, true);
376
viz.showImage(lena, lena.size());
377
viz.spinOnce(1500, true);
378
379
cv::viz::imshow("show_image_method", make_gray(lena)).spin();
380
}
381
382
TEST(Viz, show_image_3d)
383
{
384
Mat lena = imread(Path::combine(cvtest::TS::ptr()->get_data_path(), "lena.png"));
385
Mat gray = make_gray(lena);
386
387
Viz3d viz("show_image_3d");
388
viz.setBackgroundMeshLab();
389
viz.showWidget("coos", WCoordinateSystem());
390
viz.showWidget("cube", WCube());
391
viz.showWidget("arr0", WArrow(Vec3d(0.5, 0.0, 0.0), Vec3d(1.5, 0.0, 0.0), 0.009, Color::raspberry()));
392
viz.showWidget("img0", WImage3D(lena, Size2d(1.0, 1.0)), Affine3d(Vec3d(0.0, CV_PI/2, 0.0), Vec3d(.5, 0.0, 0.0)));
393
viz.showWidget("arr1", WArrow(Vec3d(-0.5, -0.5, 0.0), Vec3d(0.2, 0.2, 0.0), 0.009, Color::raspberry()));
394
viz.showWidget("img1", WImage3D(gray, Size2d(1.0, 1.0), Vec3d(-0.5, -0.5, 0.0), Vec3d(1.0, 1.0, 0.0), Vec3d(0.0, 1.0, 0.0)));
395
396
viz.showWidget("arr3", WArrow(Vec3d::all(-0.5), Vec3d::all(0.5), 0.009, Color::raspberry()));
397
398
viz.showWidget("text2d", WText("Images in 3D", Point(20, 20), 20, Color::green()));
399
400
int i = 0;
401
while(!viz.wasStopped())
402
{
403
viz.getWidget("img0").cast<WImage3D>().setImage(lena * pow(sin(i++*7.5*CV_PI/180) * 0.5 + 0.5, 1.0));
404
viz.spinOnce(1, true);
405
}
406
viz.showWidget("text2d", WText("Images in 3D (stopped)", Point(20, 20), 20, Color::green()));
407
viz.spin();
408
}
409
410
TEST(Viz, show_simple_widgets)
411
{
412
Viz3d viz("show_simple_widgets");
413
viz.setBackgroundMeshLab();
414
415
viz.showWidget("coos", WCoordinateSystem());
416
viz.showWidget("cube", WCube());
417
viz.showWidget("cub0", WCube(Vec3d::all(-1.0), Vec3d::all(-0.5), false, Color::indigo()));
418
viz.showWidget("arro", WArrow(Vec3d::all(-0.5), Vec3d::all(0.5), 0.009, Color::raspberry()));
419
viz.showWidget("cir1", WCircle(0.5, 0.01, Color::bluberry()));
420
viz.showWidget("cir2", WCircle(0.5, Point3d(0.5, 0.0, 0.0), Vec3d(1.0, 0.0, 0.0), 0.01, Color::apricot()));
421
422
viz.showWidget("cyl0", WCylinder(Vec3d(-0.5, 0.5, -0.5), Vec3d(0.5, 0.5, -0.5), 0.125, 30, Color::brown()));
423
viz.showWidget("con0", WCone(0.25, 0.125, 6, Color::azure()));
424
viz.showWidget("con1", WCone(0.125, Point3d(0.5, -0.5, 0.5), Point3d(0.5, -1.0, 0.5), 6, Color::turquoise()));
425
426
viz.showWidget("text2d", WText("Different simple widgets", Point(20, 20), 20, Color::green()));
427
viz.showWidget("text3d", WText3D("Simple 3D text", Point3d( 0.5, 0.5, 0.5), 0.125, false, Color::green()));
428
429
viz.showWidget("plane1", WPlane(Size2d(0.25, 0.75)));
430
viz.showWidget("plane2", WPlane(Vec3d(0.5, -0.5, -0.5), Vec3d(0.0, 1.0, 1.0), Vec3d(1.0, 1.0, 0.0), Size2d(1.0, 0.5), Color::gold()));
431
432
viz.showWidget("grid1", WGrid(Vec2i(7,7), Vec2d::all(0.75), Color::gray()), Affine3d().translate(Vec3d(0.0, 0.0, -1.0)));
433
434
viz.spin();
435
viz.getWidget("text2d").cast<WText>().setText("Different simple widgets (updated)");
436
viz.getWidget("text3d").cast<WText3D>().setText("Updated text 3D");
437
viz.spin();
438
}
439
440
TEST(Viz, show_follower)
441
{
442
Viz3d viz("show_follower");
443
444
viz.showWidget("coos", WCoordinateSystem());
445
viz.showWidget("cube", WCube());
446
viz.showWidget("t3d_2", WText3D("Simple 3D follower", Point3d(-0.5, -0.5, 0.5), 0.125, true, Color::green()));
447
viz.showWidget("text2d", WText("Follower: text always facing camera", Point(20, 20), 20, Color::green()));
448
viz.setBackgroundMeshLab();
449
viz.spin();
450
viz.getWidget("t3d_2").cast<WText3D>().setText("Updated follower 3D");
451
viz.spin();
452
}
453
454
}} // namespace
455
456