44 #include <pcl/console/print.h>
45 #include <pcl/visualization/interactor_style.h>
46 #include <pcl/visualization/vtk/pcl_image_canvas_source_2d.h>
47 #include <pcl/visualization/vtk/pcl_context_item.h>
48 #include <pcl/geometry/planar_polygon.h>
49 #include <pcl/correspondence.h>
51 #include <boost/shared_array.hpp>
53 #include <vtkVersion.h>
54 #include <vtkInteractorStyleImage.h>
55 #include <vtkRenderWindowInteractor.h>
58 class vtkContextActor;
64 namespace visualization
66 using Vector3ub = Eigen::Array<unsigned char, 3, 1>;
121 using Ptr = shared_ptr<ImageViewer>;
139 interactor_->SetInteractorStyle (style);
151 const std::string &layer_id =
"mono_image",
double opacity = 1.0);
161 addMonoImage (
const unsigned char* data,
unsigned width,
unsigned height,
162 const std::string &layer_id =
"mono_image",
double opacity = 1.0);
171 const std::string &layer_id =
"mono_image",
double opacity = 1.0)
173 return (showMonoImage (*cloud, layer_id, opacity));
183 const std::string &layer_id =
"mono_image",
double opacity = 1.0)
185 return (addMonoImage (*cloud, layer_id, opacity));
195 const std::string &layer_id =
"mono_image",
double opacity = 1.0);
204 const std::string &layer_id =
"mono_image",
double opacity = 1.0);
213 const std::string &layer_id =
"mono_image",
double opacity = 1.0)
215 return (showMonoImage (*cloud, layer_id, opacity));
225 const std::string &layer_id =
"mono_image",
double opacity = 1.0)
227 return (addMonoImage (*cloud, layer_id, opacity));
237 const std::string &layer_id =
"mono_image",
double opacity = 1.0);
246 const std::string &layer_id =
"mono_image",
double opacity = 1.0);
256 showRGBImage (
const unsigned char* data,
unsigned width,
unsigned height,
257 const std::string &layer_id =
"rgb_image",
double opacity = 1.0);
268 addRGBImage (
const unsigned char* data,
unsigned width,
unsigned height,
269 const std::string &layer_id =
"rgb_image",
double opacity = 1.0,
270 bool autoresize =
true);
277 template <
typename T>
inline void
279 const std::string &layer_id =
"rgb_image",
double opacity = 1.0)
281 return (showRGBImage<T> (*cloud, layer_id, opacity));
289 template <
typename T>
inline void
291 const std::string &layer_id =
"rgb_image",
double opacity = 1.0)
293 return (addRGBImage<T> (*cloud, layer_id, opacity));
301 template <
typename T>
void
303 const std::string &layer_id =
"rgb_image",
double opacity = 1.0);
310 template <
typename T>
void
312 const std::string &layer_id =
"rgb_image",
double opacity = 1.0);
326 float min_value = std::numeric_limits<float>::min (),
327 float max_value = std::numeric_limits<float>::max (),
bool grayscale =
false,
328 const std::string &layer_id =
"float_image",
double opacity = 1.0);
342 float min_value = std::numeric_limits<float>::min (),
343 float max_value = std::numeric_limits<float>::max (),
bool grayscale =
false,
344 const std::string &layer_id =
"float_image",
double opacity = 1.0);
357 showShortImage (
const unsigned short* short_image,
unsigned int width,
unsigned int height,
358 unsigned short min_value = std::numeric_limits<unsigned short>::min (),
359 unsigned short max_value = std::numeric_limits<unsigned short>::max (),
bool grayscale =
false,
360 const std::string &layer_id =
"short_image",
double opacity = 1.0);
373 addShortImage (
const unsigned short* short_image,
unsigned int width,
unsigned int height,
374 unsigned short min_value = std::numeric_limits<unsigned short>::min (),
375 unsigned short max_value = std::numeric_limits<unsigned short>::max (),
bool grayscale =
false,
376 const std::string &layer_id =
"short_image",
double opacity = 1.0);
387 const std::string &layer_id =
"angle_image",
double opacity = 1.0);
398 const std::string &layer_id =
"angle_image",
double opacity = 1.0);
409 const std::string &layer_id =
"half_angle_image",
double opacity = 1.0);
420 const std::string &layer_id =
"half_angle_image",
double opacity = 1.0);
433 const std::string &layer_id =
"points",
double opacity = 1.0);
445 const std::string &layer_id =
"markers",
double opacity = 1.0);
457 const std::string &layer_id =
"markers",
double opacity = 1.0);
482 boost::signals2::connection
484 void* cookie =
nullptr)
495 template<
typename T> boost::signals2::connection
497 T& instance,
void* cookie =
nullptr)
506 boost::signals2::connection
514 boost::signals2::connection
516 void* cookie =
nullptr)
527 template<
typename T> boost::signals2::connection
529 T& instance,
void* cookie =
nullptr)
538 boost::signals2::connection
569 interactor_->TerminateApp ();
580 addCircle (
unsigned int x,
unsigned int y,
double radius,
581 const std::string &layer_id =
"circles",
double opacity = 1.0);
594 addCircle (
unsigned int x,
unsigned int y,
double radius,
595 double r,
double g,
double b,
596 const std::string &layer_id =
"circles",
double opacity = 1.0);
606 const std::string &layer_id =
"rectangles",
double opacity = 1.0);
619 double r,
double g,
double b,
620 const std::string &layer_id =
"rectangles",
double opacity = 1.0);
631 addRectangle (
unsigned int x_min,
unsigned int x_max,
unsigned int y_min,
unsigned int y_max,
632 const std::string &layer_id =
"rectangles",
double opacity = 1.0);
646 addRectangle (
unsigned int x_min,
unsigned int x_max,
unsigned int y_min,
unsigned int y_max,
647 double r,
double g,
double b,
648 const std::string &layer_id =
"rectangles",
double opacity = 1.0);
657 template <
typename T>
bool
659 const T &min_pt,
const T &max_pt,
660 const std::string &layer_id =
"rectangles",
double opacity = 1.0);
672 template <
typename T>
bool
674 const T &min_pt,
const T &max_pt,
675 double r,
double g,
double b,
676 const std::string &layer_id =
"rectangles",
double opacity = 1.0);
687 template <
typename T>
bool
689 double r,
double g,
double b,
690 const std::string &layer_id =
"rectangles",
double opacity = 1.0);
698 template <
typename T>
bool
700 const std::string &layer_id =
"image_mask",
double opacity = 1.0);
712 const std::string &layer_id =
"boxes",
double opacity = 0.5);
727 double r,
double g,
double b,
728 const std::string &layer_id =
"boxes",
double opacity = 0.5);
742 addLine (
unsigned int x_min,
unsigned int y_min,
unsigned int x_max,
unsigned int y_max,
743 double r,
double g,
double b,
744 const std::string &layer_id =
"line",
double opacity = 1.0);
755 addLine (
unsigned int x_min,
unsigned int y_min,
unsigned int x_max,
unsigned int y_max,
756 const std::string &layer_id =
"line",
double opacity = 1.0);
769 addText (
unsigned int x,
unsigned int y,
const std::string& text,
770 double r,
double g,
double b,
771 const std::string &layer_id =
"line",
double opacity = 1.0);
781 addText (
unsigned int x,
unsigned int y,
const std::string& text,
782 const std::string &layer_id =
"line",
double opacity = 1.0);
793 template <
typename T>
bool
795 double r,
double g,
double b,
796 const std::string &layer_id =
"image_mask",
double opacity = 0.5);
804 template <
typename T>
bool
806 const std::string &layer_id =
"image_mask",
double opacity = 0.5);
818 template <
typename T>
bool
820 double r,
double g,
double b,
821 const std::string &layer_id =
"planar_polygon",
double opacity = 1.0);
830 template <
typename T>
bool
832 const std::string &layer_id =
"planar_polygon",
double opacity = 1.0);
841 addLayer (
const std::string &layer_id,
int width,
int height,
double opacity = 0.5);
856 template <
typename Po
intT>
bool
861 const std::string &layer_id =
"correspondences");
876 boost::shared_array<unsigned char> data);
886 boost::shared_array<unsigned char> data);
894 template <
typename T>
void
896 boost::shared_array<unsigned char> &data);
916 MouseCallback (vtkObject*,
unsigned long eid,
void* clientdata,
void *calldata);
930 Execute (vtkObject* vtkNotUsed (caller),
unsigned long event_id,
void* call_data)
override
932 if (event_id != vtkCommand::TimerEvent)
934 int timer_id = *
static_cast<int*
> (call_data);
935 if (timer_id != right_timer_id)
937 window->interactor_->TerminateApp ();
939 int right_timer_id{0};
951 Execute (vtkObject*,
unsigned long event_id,
void*)
override
953 if (event_id != vtkCommand::ExitEvent)
955 window->stopped_ =
true;
956 window->interactor_->TerminateApp ();
967 std::string layer_name;
970 using LayerMap = std::vector<Layer>;
980 createLayer (
const std::string &layer_id,
int width,
int height,
double opacity = 0.5,
bool fill_box =
true);
1009 boost::shared_array<unsigned char> data_;
1012 std::size_t data_size_{0};
1015 bool stopped_{
false};
1024 LayerMap layer_map_;
1032 std::vector<unsigned char*> image_data_{};
1034 struct LayerComparator
1036 LayerComparator (
const std::string &str) : str_ (str) {}
1037 const std::string &str_;
1040 operator () (
const Layer &layer)
1042 return (layer.layer_name == str_);
1052 #include <pcl/visualization/impl/image_viewer.hpp>
PlanarPolygon represents a planar (2D) polygon, potentially in a 3D space.
PointCloud represents the base class in PCL for storing collections of 3D points.
shared_ptr< const PointCloud< PointT > > ConstPtr
ImageViewer is a class for 2D image visualization.
void markPoints(const std::vector< float > &uv, Vector3ub fg_color, Vector3ub bg_color=red_color, double size=3.0, const std::string &layer_id="markers", double opacity=1.0)
Sets the pixel at coordinates(u,v) to color while setting the neighborhood to another (float coordina...
bool addRectangle(unsigned int x_min, unsigned int x_max, unsigned int y_min, unsigned int y_max, const std::string &layer_id="rectangles", double opacity=1.0)
Add a 2D box and color its edges with a given color.
void setWindowTitle(const std::string &name)
Set the window title name.
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 resetStoppedFlag()
Set the stopped flag back to false.
bool addLine(unsigned int x_min, unsigned int y_min, unsigned int x_max, unsigned int y_max, const std::string &layer_id="line", double opacity=1.0)
Add a 2D line with a given color.
boost::signals2::connection registerKeyboardCallback(void(T::*callback)(const pcl::visualization::KeyboardEvent &, void *), T &instance, void *cookie=nullptr)
Register a callback function for keyboard events.
void showAngleImage(const float *data, unsigned width, unsigned height, const std::string &layer_id="angle_image", double opacity=1.0)
Show a 2D image on screen representing angle data.
shared_ptr< const ImageViewer > ConstPtr
boost::signals2::connection registerMouseCallback(void(*callback)(const pcl::visualization::MouseEvent &, void *), void *cookie=nullptr)
Register a callback std::function for mouse events.
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.
void convertIntensityCloudToUChar(const pcl::PointCloud< pcl::Intensity > &cloud, boost::shared_array< unsigned char > data)
Convert the Intensity information in a PointCloud<Intensity> to an unsigned char array.
void addMonoImage(const pcl::PointCloud< pcl::Intensity8u >::ConstPtr &cloud, const std::string &layer_id="mono_image", double opacity=1.0)
Add a monochrome 2D image layer, but do not render it (use spin/spinOnce to update).
void showRGBImage(const typename pcl::PointCloud< T >::ConstPtr &cloud, const std::string &layer_id="rgb_image", double opacity=1.0)
Show a 2D image on screen, obtained from the RGB channel of a point cloud.
bool addFilledRectangle(unsigned int x_min, unsigned int x_max, unsigned int y_min, unsigned int y_max, const std::string &layer_id="boxes", double opacity=0.5)
Add a 2D box and fill it in with a given color.
void spinOnce(int time=1, bool force_redraw=true)
Spin once method.
static void MouseCallback(vtkObject *, unsigned long eid, void *clientdata, void *calldata)
void addMonoImage(const pcl::PointCloud< pcl::Intensity > &cloud, const std::string &layer_id="mono_image", double opacity=1.0)
Add a monochrome 2D image layer, but do not render it (use spin/spinOnce to update).
bool addRectangle(const pcl::PointXY &min_pt, const pcl::PointXY &max_pt, double r, double g, double b, const std::string &layer_id="rectangles", double opacity=1.0)
Add a 2D box and color its edges with a given color.
bool addRectangle(unsigned int x_min, unsigned int x_max, unsigned int y_min, unsigned int y_max, double r, double g, double b, const std::string &layer_id="rectangles", double opacity=1.0)
Add a 2D box and color its edges with a given color.
bool addLayer(const std::string &layer_id, int width, int height, double opacity=0.5)
Add a new 2D rendering layer to the viewer.
void removeLayer(const std::string &layer_id)
Remove a 2D layer given by its ID.
void addHalfAngleImage(const float *data, unsigned width, unsigned height, const std::string &layer_id="half_angle_image", double opacity=1.0)
Add a half angle 2D image layer, but do not render it (use spin/spinOnce to update).
boost::signals2::connection registerKeyboardCallback(void(*callback)(const pcl::visualization::KeyboardEvent &, void *), void *cookie=nullptr)
Register a callback function for keyboard events.
bool addText(unsigned int x, unsigned int y, const std::string &text, double r, double g, double b, const std::string &layer_id="line", double opacity=1.0)
Add a 2D text with a given color.
void addMonoImage(const unsigned char *data, unsigned width, unsigned height, const std::string &layer_id="mono_image", double opacity=1.0)
Add a monochrome 2D image layer, but do not render it (use spin/spinOnce to update).
void showFloatImage(const float *data, unsigned int width, unsigned int height, float min_value=std::numeric_limits< float >::min(), float max_value=std::numeric_limits< float >::max(), bool grayscale=false, const std::string &layer_id="float_image", double opacity=1.0)
Show a 2D image (float) on screen.
void showMonoImage(const pcl::PointCloud< pcl::Intensity8u >::ConstPtr &cloud, const std::string &layer_id="mono_image", double opacity=1.0)
Show a monochrome 2D image on screen.
void markPoints(const std::vector< int > &uv, Vector3ub fg_color, Vector3ub bg_color=red_color, double size=3.0, const std::string &layer_id="markers", double opacity=1.0)
Sets the pixel at coordinates(u,v) to color while setting the neighborhood to another.
void showHalfAngleImage(const float *data, unsigned width, unsigned height, const std::string &layer_id="half_angle_image", double opacity=1.0)
Show a 2D image on screen representing half angle data.
bool addLine(unsigned int x_min, unsigned int y_min, unsigned int x_max, unsigned int y_max, double r, double g, double b, const std::string &layer_id="line", double opacity=1.0)
Add a 2D line with a given color.
void addMonoImage(const pcl::PointCloud< pcl::Intensity8u > &cloud, const std::string &layer_id="mono_image", double opacity=1.0)
Add a monochrome 2D image layer, but do not render it (use spin/spinOnce to update).
void showShortImage(const unsigned short *short_image, unsigned int width, unsigned int height, unsigned short min_value=std::numeric_limits< unsigned short >::min(), unsigned short max_value=std::numeric_limits< unsigned short >::max(), bool grayscale=false, const std::string &layer_id="short_image", double opacity=1.0)
Show a 2D image (unsigned short) on screen.
boost::signals2::connection registerMouseCallback(void(T::*callback)(const pcl::visualization::MouseEvent &, void *), T &instance, void *cookie=nullptr)
Register a callback function for mouse events.
void emitMouseEvent(unsigned long event_id)
Fire up a mouse event with a specified event ID.
void addShortImage(const unsigned short *short_image, unsigned int width, unsigned int height, unsigned short min_value=std::numeric_limits< unsigned short >::min(), unsigned short max_value=std::numeric_limits< unsigned short >::max(), bool grayscale=false, const std::string &layer_id="short_image", double opacity=1.0)
Add a short 2D image layer, but do not render it (use spin/spinOnce to update).
static void KeyboardCallback(vtkObject *, unsigned long eid, void *clientdata, void *calldata)
virtual ~ImageViewer()
Destructor.
void showMonoImage(const pcl::PointCloud< pcl::Intensity8u > &cloud, const std::string &layer_id="mono_image", double opacity=1.0)
Show a monochrome 2D image on screen.
void showMonoImage(const unsigned char *data, unsigned width, unsigned height, const std::string &layer_id="mono_image", double opacity=1.0)
Show a monochrome 2D image on screen.
void emitKeyboardEvent(unsigned long event_id)
Fire up a keyboard event with a specified event ID.
ImageViewer(const std::string &window_title="")
Constructor.
void close()
Stop the interaction and close the visualization window.
void setSize(int xw, int yw)
Set the window size in screen coordinates.
bool addText(unsigned int x, unsigned int y, const std::string &text, const std::string &layer_id="line", double opacity=1.0)
Add a 2D text with a given color.
void addRGBImage(const typename pcl::PointCloud< T >::ConstPtr &cloud, const std::string &layer_id="rgb_image", double opacity=1.0)
Add an RGB 2D image layer, but do not render it (use spin/spinOnce to update).
boost::signals2::connection registerMouseCallback(std::function< void(const pcl::visualization::MouseEvent &)> cb)
Register a callback function for mouse events.
void addFloatImage(const float *data, unsigned int width, unsigned int height, float min_value=std::numeric_limits< float >::min(), float max_value=std::numeric_limits< float >::max(), bool grayscale=false, const std::string &layer_id="float_image", double opacity=1.0)
Add a float 2D image layer, but do not render it (use spin/spinOnce to update).
shared_ptr< ImageViewer > Ptr
void showMonoImage(const pcl::PointCloud< pcl::Intensity > &cloud, const std::string &layer_id="mono_image", double opacity=1.0)
Show a monochrome 2D image on screen.
void setPosition(int x, int y)
Set the position in screen coordinates.
void markPoint(std::size_t u, std::size_t v, Vector3ub fg_color, Vector3ub bg_color=red_color, double radius=3.0, const std::string &layer_id="points", double opacity=1.0)
Sets the pixel at coordinates(u,v) to color while setting the neighborhood to another.
void addMonoImage(const pcl::PointCloud< pcl::Intensity >::ConstPtr &cloud, const std::string &layer_id="mono_image", double opacity=1.0)
Add a monochrome 2D image layer, but do not render it (use spin/spinOnce to update).
void addAngleImage(const float *data, unsigned width, unsigned height, const std::string &layer_id="angle_image", double opacity=1.0)
Add an angle 2D image layer, but do not render it (use spin/spinOnce to update).
boost::signals2::connection registerKeyboardCallback(std::function< void(const pcl::visualization::KeyboardEvent &)> cb)
Register a callback std::function for keyboard events.
int * getSize()
Return the window size in pixels.
bool addFilledRectangle(unsigned int x_min, unsigned int x_max, unsigned int y_min, unsigned int y_max, double r, double g, double b, const std::string &layer_id="boxes", double opacity=0.5)
Add a 2D box and fill it in with a given color.
void render()
Trigger a render call.
void convertIntensityCloud8uToUChar(const pcl::PointCloud< pcl::Intensity8u > &cloud, boost::shared_array< unsigned char > data)
Convert the Intensity8u information in a PointCloud<Intensity8u> to an unsigned char array.
bool addCircle(unsigned int x, unsigned int y, double radius, const std::string &layer_id="circles", double opacity=1.0)
Add a circle shape from a point and a radius.
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).
bool wasStopped() const
Returns true when the user tried to close the window.
bool addCircle(unsigned int x, unsigned int y, double radius, double r, double g, double b, const std::string &layer_id="circles", double opacity=1.0)
Add a circle shape from a point and a radius.
void showMonoImage(const pcl::PointCloud< pcl::Intensity >::ConstPtr &cloud, const std::string &layer_id="mono_image", double opacity=1.0)
Show a monochrome 2D image on screen.
void setInteractorStyle(vtkInteractorObserver *style)
Set up the interactor style.
An image viewer interactor style, tailored for ImageViewer.
void adjustCamera(vtkRenderer *ren)
void OnMouseWheelForward() override
void OnMiddleButtonDown() override
ImageViewerInteractorStyle()
void OnLeftButtonDown() override
static ImageViewerInteractorStyle * New()
void OnRightButtonDown() override
void adjustCamera(vtkImageData *image, vtkRenderer *ren)
void OnMouseWheelBackward() override
/brief Class representing key hit/release events
Defines all the PCL implemented PointT point type structures.
#define PCL_MAKE_ALIGNED_OPERATOR_NEW
Macro to signal a class requires a custom allocator.
Defines functions, macros and traits for allocating and using memory.
Eigen::Array< unsigned char, 3, 1 > Vector3ub
static const Vector3ub red_color(255, 0, 0)
static const Vector3ub blue_color(0, 0, 255)
static const Vector3ub green_color(0, 255, 0)
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences
Defines all the PCL and non-PCL macros used.
A 2D point structure representing Euclidean xy coordinates.
static ExitCallback * New()
void Execute(vtkObject *, unsigned long event_id, void *) override
ExitMainLoopTimerCallback()=default
void Execute(vtkObject *vtkNotUsed(caller), unsigned long event_id, void *call_data) override
static ExitMainLoopTimerCallback * New()