39 #ifndef PCL_VISUALIZATION_IMAGE_VISUALIZER_HPP_
40 #define PCL_VISUALIZATION_IMAGE_VISUALIZER_HPP_
42 #include <vtkVersion.h>
43 #include <vtkContextActor.h>
44 #include <vtkContextScene.h>
45 #include <vtkImageData.h>
46 #include <vtkImageFlip.h>
47 #include <vtkPointData.h>
48 #include <vtkImageViewer.h>
50 #include <pcl/visualization/common/common.h>
51 #include <pcl/search/organized.h>
54 template <
typename T>
void
57 boost::shared_array<unsigned char> &data)
60 for (
const auto& point: cloud)
69 template <
typename T>
void
71 const std::string &layer_id,
77 data_.reset (
new unsigned char[data_size_]);
80 convertRGBCloudToUChar (cloud, data_);
82 return (addRGBImage (data_.get (), cloud.
width, cloud.
height, layer_id, opacity));
86 template <
typename T>
void
88 const std::string &layer_id,
91 addRGBImage<T> (cloud, layer_id, opacity);
96 template <
typename T>
bool
100 double r,
double g,
double b,
101 const std::string &layer_id,
double opacity)
108 auto am_it = std::find_if (layer_map_.begin (), layer_map_.end (), LayerComparator (layer_id));
109 if (am_it == layer_map_.end ())
111 PCL_DEBUG (
"[pcl::visualization::ImageViewer::addMask] No layer with ID'=%s' found. Creating new one...\n", layer_id.c_str ());
112 am_it = createLayer (layer_id, getSize ()[0] - 1, getSize ()[1] - 1, opacity,
false);
118 std::vector<float> xy;
119 xy.reserve (mask.
size () * 2);
120 for (std::size_t i = 0; i < mask.
size (); ++i)
125 xy.push_back (p_projected.
x);
126 xy.push_back (
static_cast<float> (image->
height) - p_projected.
y);
130 points->setColors (
static_cast<unsigned char> (r*255.0),
131 static_cast<unsigned char> (g*255.0),
132 static_cast<unsigned char> (b*255.0));
133 points->setOpacity (opacity);
135 am_it->actor->GetScene ()->AddItem (points);
140 template <
typename T>
bool
144 const std::string &layer_id,
double opacity)
146 return (addMask (image, mask, 1.0, 0.0, 0.0, layer_id, opacity));
150 template <
typename T>
bool
154 double r,
double g,
double b,
155 const std::string &layer_id,
double opacity)
162 auto am_it = std::find_if (layer_map_.begin (), layer_map_.end (), LayerComparator (layer_id));
163 if (am_it == layer_map_.end ())
165 PCL_DEBUG (
"[pcl::visualization::ImageViewer::addPlanarPolygon] No layer with ID'=%s' found. Creating new one...\n", layer_id.c_str ());
166 am_it = createLayer (layer_id, getSize ()[0] - 1, getSize ()[1] - 1, opacity,
false);
172 std::vector<float> xy;
173 xy.reserve ((polygon.
getContour ().size () + 1) * 2);
174 for (std::size_t i = 0; i < polygon.
getContour ().size (); ++i)
183 xy[xy.size () - 2] = xy[0];
184 xy[xy.size () - 1] = xy[1];
187 poly->setColors (
static_cast<unsigned char> (r * 255.0),
188 static_cast<unsigned char> (g * 255.0),
189 static_cast<unsigned char> (b * 255.0));
190 poly->setOpacity (opacity);
192 am_it->actor->GetScene ()->AddItem (poly);
198 template <
typename T>
bool
202 const std::string &layer_id,
double opacity)
204 return (addPlanarPolygon (image, polygon, 1.0, 0.0, 0.0, layer_id, opacity));
208 template <
typename T>
bool
213 double r,
double g,
double b,
214 const std::string &layer_id,
double opacity)
221 auto am_it = std::find_if (layer_map_.begin (), layer_map_.end (), LayerComparator (layer_id));
222 if (am_it == layer_map_.end ())
224 PCL_DEBUG (
"[pcl::visualization::ImageViewer::addRectangle] No layer with ID'=%s' found. Creating new one...\n", layer_id.c_str ());
225 am_it = createLayer (layer_id, getSize ()[0] - 1, getSize ()[1] - 1, opacity,
false);
232 T p1, p2, p3, p4, p5, p6, p7, p8;
233 p1.x = min_pt.x; p1.y = min_pt.y; p1.z = min_pt.z;
234 p2.x = min_pt.x; p2.y = min_pt.y; p2.z = max_pt.z;
235 p3.x = min_pt.x; p3.y = max_pt.y; p3.z = min_pt.z;
236 p4.x = min_pt.x; p4.y = max_pt.y; p4.z = max_pt.z;
237 p5.x = max_pt.x; p5.y = min_pt.y; p5.z = min_pt.z;
238 p6.x = max_pt.x; p6.y = min_pt.y; p6.z = max_pt.z;
239 p7.x = max_pt.x; p7.y = max_pt.y; p7.z = min_pt.z;
240 p8.x = max_pt.x; p8.y = max_pt.y; p8.z = max_pt.z;
242 std::vector<pcl::PointXY> pp_2d (8);
253 min_pt_2d.
x = min_pt_2d.
y = std::numeric_limits<float>::max ();
254 max_pt_2d.
x = max_pt_2d.
y = -std::numeric_limits<float>::max ();
256 for (
const auto &point : pp_2d)
258 if (point.x < min_pt_2d.
x) min_pt_2d.
x = point.x;
259 if (point.y < min_pt_2d.
y) min_pt_2d.
y = point.y;
260 if (point.x > max_pt_2d.
x) max_pt_2d.
x = point.x;
261 if (point.y > max_pt_2d.
y) max_pt_2d.
y = point.y;
263 min_pt_2d.
y = float (image->
height) - min_pt_2d.
y;
264 max_pt_2d.
y = float (image->
height) - max_pt_2d.
y;
267 rect->setColors (
static_cast<unsigned char> (255.0 * r),
268 static_cast<unsigned char> (255.0 * g),
269 static_cast<unsigned char> (255.0 * b));
270 rect->setOpacity (opacity);
271 rect->set (min_pt_2d.
x, min_pt_2d.
y, max_pt_2d.
x, max_pt_2d.
y);
272 am_it->actor->GetScene ()->AddItem (rect);
278 template <
typename T>
bool
283 const std::string &layer_id,
double opacity)
285 return (addRectangle<T> (image, min_pt, max_pt, 0.0, 1.0, 0.0, layer_id, opacity));
289 template <
typename T>
bool
293 double r,
double g,
double b,
294 const std::string &layer_id,
double opacity)
301 auto am_it = std::find_if (layer_map_.begin (), layer_map_.end (), LayerComparator (layer_id));
302 if (am_it == layer_map_.end ())
304 PCL_DEBUG (
"[pcl::visualization::ImageViewer::addRectangle] No layer with ID'=%s' found. Creating new one...\n", layer_id.c_str ());
305 am_it = createLayer (layer_id, getSize ()[0] - 1, getSize ()[1] - 1, opacity,
false);
311 std::vector<pcl::PointXY> pp_2d (mask.
size ());
312 for (std::size_t i = 0; i < mask.
size (); ++i)
316 min_pt_2d.
x = min_pt_2d.
y = std::numeric_limits<float>::max ();
317 max_pt_2d.
x = max_pt_2d.
y = -std::numeric_limits<float>::max ();
319 for (
const auto &point : pp_2d)
321 if (point.x < min_pt_2d.
x) min_pt_2d.
x = point.x;
322 if (point.y < min_pt_2d.
y) min_pt_2d.
y = point.y;
323 if (point.x > max_pt_2d.
x) max_pt_2d.
x = point.x;
324 if (point.y > max_pt_2d.
y) max_pt_2d.
y = point.y;
326 min_pt_2d.
y = float (image->
height) - min_pt_2d.
y;
327 max_pt_2d.
y = float (image->
height) - max_pt_2d.
y;
330 rect->setColors (
static_cast<unsigned char> (255.0 * r),
331 static_cast<unsigned char> (255.0 * g),
332 static_cast<unsigned char> (255.0 * b));
333 rect->setOpacity (opacity);
334 rect->set (min_pt_2d.
x, min_pt_2d.
y, max_pt_2d.
x, max_pt_2d.
y);
335 am_it->actor->GetScene ()->AddItem (rect);
341 template <
typename T>
bool
345 const std::string &layer_id,
double opacity)
347 return (addRectangle (image, mask, 0.0, 1.0, 0.0, layer_id, opacity));
351 template <
typename Po
intT>
bool
357 const std::string &layer_id)
359 if (correspondences.empty ())
361 PCL_DEBUG (
"[pcl::visualization::ImageViewer::addCorrespondences] An empty set of correspondences given! Nothing to display.\n");
366 auto am_it = std::find_if (layer_map_.begin (), layer_map_.end (), LayerComparator (layer_id));
367 if (am_it == layer_map_.end ())
369 PCL_DEBUG (
"[pcl::visualization::ImageViewer::addCorrespondences] No layer with ID='%s' found. Creating new one...\n", layer_id.c_str ());
370 am_it = createLayer (layer_id, source_img.
width + target_img.
width, std::max (source_img.
height, target_img.
height), 1.0,
false);
373 int src_size = source_img.
width * source_img.
height * 3;
374 int tgt_size = target_img.
width * target_img.
height * 3;
380 if (data_size_ <
static_cast<std::size_t
> (src_size + tgt_size))
382 data_size_ = src_size + tgt_size;
383 data_.reset (
new unsigned char[data_size_]);
388 for (std::size_t i = 0; i < std::max (source_img.
height, target_img.
height); ++i)
391 if (i < source_img.
height)
393 for (std::size_t k = 0; k < source_img.
width; ++k)
395 data_[j++] = source_img[i * source_img.
width + k].r;
396 data_[j++] = source_img[i * source_img.
width + k].g;
397 data_[j++] = source_img[i * source_img.
width + k].b;
402 std::fill_n(&data_[j], source_img.
width * 3, 0);
403 j += source_img.
width * 3;
407 if (i < source_img.
height)
409 for (std::size_t k = 0; k < target_img.
width; ++k)
411 data_[j++] = target_img[i * source_img.
width + k].r;
412 data_[j++] = target_img[i * source_img.
width + k].g;
413 data_[j++] = target_img[i * source_img.
width + k].b;
418 std::fill_n(&data_[j], target_img.
width * 3, 0);
419 j += target_img.
width * 3;
423 void* data =
const_cast<void*
> (
reinterpret_cast<const void*
> (data_.get ()));
426 image->SetDimensions (source_img.
width + target_img.
width, std::max (source_img.
height, target_img.
height), 1);
427 image->AllocateScalars (VTK_UNSIGNED_CHAR, 3);
428 image->GetPointData ()->GetScalars ()->SetVoidArray (data, data_size_, 1);
430 image_item->set (0, 0, image);
431 interactor_style_->adjustCamera (image, ren_);
432 am_it->actor->GetScene ()->AddItem (image_item);
433 image_viewer_->SetSize (image->GetDimensions ()[0], image->GetDimensions ()[1]);
436 for (std::size_t i = 0; i < correspondences.size (); i += nth)
440 auto u_r =
static_cast<unsigned char> (255.0 * r);
441 auto u_g =
static_cast<unsigned char> (255.0 * g);
442 auto u_b =
static_cast<unsigned char> (255.0 * b);
444 query_circle->setColors (u_r, u_g, u_b);
446 match_circle->setColors (u_r, u_g, u_b);
448 line->setColors (u_r, u_g, u_b);
450 float query_x = correspondences[i].index_query % source_img.
width;
451 float match_x = correspondences[i].index_match % target_img.
width + source_img.
width;
452 float query_y = getSize ()[1] - correspondences[i].index_query / source_img.
width;
453 float match_y = getSize ()[1] - correspondences[i].index_match / target_img.
width;
455 query_circle->set (query_x, query_y, 3.0);
456 match_circle->set (match_x, match_y, 3.0);
457 line->set (query_x, query_y, match_x, match_y);
459 am_it->actor->GetScene ()->AddItem (query_circle);
460 am_it->actor->GetScene ()->AddItem (match_circle);
461 am_it->actor->GetScene ()->AddItem (line);
PlanarPolygon represents a planar (2D) polygon, potentially in a 3D space.
pcl::PointCloud< PointT >::VectorType & getContour()
Getter for the contour / boundary.
PointCloud represents the base class in PCL for storing collections of 3D points.
std::uint32_t width
The point cloud width (if organized as an image-structure).
bool isOrganized() const
Return whether a dataset is organized (e.g., arranged in a structured grid).
std::uint32_t height
The point cloud height (if organized as an image-structure).
shared_ptr< const PointCloud< PointT > > ConstPtr
OrganizedNeighbor is a class for optimized nearest neighbor search in organized projectable point clo...
bool setInputCloud(const PointCloudConstPtr &cloud, const IndicesConstPtr &indices=IndicesConstPtr()) override
Provide a pointer to the input data set, if user has focal length he must set it before calling this.
bool projectPoint(const PointT &p, pcl::PointXY &q) const
projects a point into the image
bool addMask(const typename pcl::PointCloud< T >::ConstPtr &image, const pcl::PointCloud< T > &mask, double r, double g, double b, const std::string &layer_id="image_mask", double opacity=0.5)
Add a generic 2D mask to an image.
bool addRectangle(const pcl::PointXY &min_pt, const pcl::PointXY &max_pt, const std::string &layer_id="rectangles", double opacity=1.0)
Add a 2D box and color its edges with a given color.
void showRGBImage(const unsigned char *data, unsigned width, unsigned height, const std::string &layer_id="rgb_image", double opacity=1.0)
Show a 2D RGB image on screen.
bool addPlanarPolygon(const typename pcl::PointCloud< T >::ConstPtr &image, const pcl::PlanarPolygon< T > &polygon, double r, double g, double b, const std::string &layer_id="planar_polygon", double opacity=1.0)
Add a generic 2D planar polygon to an image.
bool showCorrespondences(const pcl::PointCloud< PointT > &source_img, const pcl::PointCloud< PointT > &target_img, const pcl::Correspondences &correspondences, int nth=1, const std::string &layer_id="correspondences")
Add the specified correspondences to the display.
void convertRGBCloudToUChar(const pcl::PointCloud< T > &cloud, boost::shared_array< unsigned char > &data)
Convert the RGB information in a PointCloud<T> to an unsigned char array.
void addRGBImage(const unsigned char *data, unsigned width, unsigned height, const std::string &layer_id="rgb_image", double opacity=1.0, bool autoresize=true)
Add an RGB 2D image layer, but do not render it (use spin/spinOnce to update).
PCL_EXPORTS void getRandomColors(double &r, double &g, double &b, double min=0.2, double max=2.8)
Get (good) random values for R/G/B.
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences
A 2D point structure representing Euclidean xy coordinates.