From 1449823bcd5a1fd396bd90426fdf046fb232e4ac Mon Sep 17 00:00:00 2001 From: Anatoly Baksheev Date: Sat, 11 Jan 2014 22:43:58 +0400 Subject: [PATCH] implemented showImage --- .../viz/launching_viz/launching_viz.rst | 2 +- modules/viz/doc/viz3d.rst | 11 ++++++++++ modules/viz/doc/widget.rst | 20 +++++++++---------- modules/viz/include/opencv2/viz.hpp | 3 +++ modules/viz/include/opencv2/viz/viz3d.hpp | 2 ++ modules/viz/include/opencv2/viz/widgets.hpp | 10 +++++----- modules/viz/src/shapes.cpp | 17 +++++++++------- modules/viz/src/viz3d.cpp | 11 ++++++++++ modules/viz/src/vizcore.cpp | 7 +++++++ modules/viz/test/tests_simple.cpp | 14 +++++++++++++ 10 files changed, 74 insertions(+), 23 deletions(-) diff --git a/doc/tutorials/viz/launching_viz/launching_viz.rst b/doc/tutorials/viz/launching_viz/launching_viz.rst index 0bf31977d1..a507a7f27a 100644 --- a/doc/tutorials/viz/launching_viz/launching_viz.rst +++ b/doc/tutorials/viz/launching_viz/launching_viz.rst @@ -43,7 +43,7 @@ You can download the code from :download:`here <../../../../samples/cpp/tutorial cout << "First event loop is over" << endl; /// Access window via its name - viz::Viz3d sameWindow = viz::get("Viz Demo"); + viz::Viz3d sameWindow = viz::getWindowByName("Viz Demo"); /// Start event loop sameWindow.spin(); diff --git a/modules/viz/doc/viz3d.rst b/modules/viz/doc/viz3d.rst index 9d4bb674d3..ec5b16e718 100644 --- a/modules/viz/doc/viz3d.rst +++ b/modules/viz/doc/viz3d.rst @@ -102,6 +102,8 @@ The Viz3d class represents a 3D visualizer window. This class is implicitly shar void setWidgetPose(const String &id, const Affine3d &pose); void updateWidgetPose(const String &id, const Affine3d &pose); Affine3d getWidgetPose(const String &id) const; + + void showImage(InputArray image, const Size& window_size = Size(-1, -1)); void setCamera(const Camera &camera); Camera getCamera() const; @@ -182,6 +184,15 @@ Removes all widgets from the window. .. ocv:function:: void removeAllWidgets() +viz::Viz3d::showImage +--------------------- +Removed all widgets and displays image scaled to whole window area. + +.. ocv:function:: void showImage(InputArray image, const Size& window_size = Size(-1, -1)); + + :param image: Image to be displayed. + :param size: Size of Viz3d window. Default value means no change. + viz::Viz3d::setWidgetPose ------------------------- Sets pose of a widget in the window. diff --git a/modules/viz/doc/widget.rst b/modules/viz/doc/widget.rst index ec4a858e2d..c286339c21 100644 --- a/modules/viz/doc/widget.rst +++ b/modules/viz/doc/widget.rst @@ -580,16 +580,16 @@ This 2D Widget represents an image overlay. :: class CV_EXPORTS WImageOverlay : public Widget2D { public: - WImageOverlay(const Mat &image, const Rect &rect); + WImageOverlay(InputArray image, const Rect &rect); - void setImage(const Mat &image); + void setImage(InputArray image); }; viz::WImageOverlay::WImageOverlay --------------------------------- Constructs an WImageOverlay. -.. ocv:function:: WImageOverlay(const Mat &image, const Rect &rect) +.. ocv:function:: WImageOverlay(InputArray image, const Rect &rect) :param image: BGR or Gray-Scale image. :param rect: Image is scaled and positioned based on rect. @@ -598,7 +598,7 @@ viz::WImageOverlay::setImage ---------------------------- Sets the image content of the widget. -.. ocv:function:: void setImage(const Mat &image) +.. ocv:function:: void setImage(InputArray image) :param image: BGR or Gray-Scale image. @@ -612,23 +612,23 @@ This 3D Widget represents an image in 3D space. :: { public: //! Creates 3D image at the origin - WImage3D(const Mat &image, const Size2d &size); + WImage3D(InputArray image, const Size2d &size); //! Creates 3D image at a given position, pointing in the direction of the normal, and having the up_vector orientation - WImage3D(const Mat &image, const Size2d &size, const Vec3d &position, const Vec3d &normal, const Vec3d &up_vector); + WImage3D(InputArray image, const Size2d &size, const Vec3d &position, const Vec3d &normal, const Vec3d &up_vector); - void setImage(const Mat &image); + void setImage(InputArray image); }; viz::WImage3D::WImage3D ----------------------- Constructs an WImage3D. -.. ocv:function:: WImage3D(const Mat &image, const Size2d &size) +.. ocv:function:: WImage3D(InputArray image, const Size2d &size) :param image: BGR or Gray-Scale image. :param size: Size of the image. -.. ocv:function:: WImage3D(const Mat &image, const Size2d &size, const Vec3d &position, const Vec3d &normal, const Vec3d &up_vector) +.. ocv:function:: WImage3D(InputArray image, const Size2d &size, const Vec3d &position, const Vec3d &normal, const Vec3d &up_vector) :param position: Position of the image. :param normal: Normal of the plane that represents the image. @@ -640,7 +640,7 @@ viz::WImage3D::setImage ----------------------- Sets the image content of the widget. -.. ocv:function:: void setImage(const Mat &image) +.. ocv:function:: void setImage(InputArray image) :param image: BGR or Gray-Scale image. diff --git a/modules/viz/include/opencv2/viz.hpp b/modules/viz/include/opencv2/viz.hpp index 9b1f21a8d5..32e32a7a3b 100644 --- a/modules/viz/include/opencv2/viz.hpp +++ b/modules/viz/include/opencv2/viz.hpp @@ -66,6 +66,9 @@ namespace cv //! Unregisters all Viz windows from internal database. After it 'getWindowByName()' will create new windows instead getting existing from the database. CV_EXPORTS void unregisterAllWindows(); + //! Displays image in specified window + CV_EXPORTS Viz3d imshow(const String& window_name, InputArray image, const Size& window_size = Size(-1, -1)); + //! checks float value for Nan inline bool isNan(float x) { diff --git a/modules/viz/include/opencv2/viz/viz3d.hpp b/modules/viz/include/opencv2/viz/viz3d.hpp index 7cd27eb616..4ffb6b3c5a 100644 --- a/modules/viz/include/opencv2/viz/viz3d.hpp +++ b/modules/viz/include/opencv2/viz/viz3d.hpp @@ -75,6 +75,8 @@ namespace cv Widget getWidget(const String &id) const; void removeAllWidgets(); + void showImage(InputArray image, const Size& window_size = Size(-1, -1)); + void setWidgetPose(const String &id, const Affine3d &pose); void updateWidgetPose(const String &id, const Affine3d &pose); Affine3d getWidgetPose(const String &id) const; diff --git a/modules/viz/include/opencv2/viz/widgets.hpp b/modules/viz/include/opencv2/viz/widgets.hpp index 455c965578..8ab95c92a4 100644 --- a/modules/viz/include/opencv2/viz/widgets.hpp +++ b/modules/viz/include/opencv2/viz/widgets.hpp @@ -210,8 +210,8 @@ namespace cv class CV_EXPORTS WImageOverlay : public Widget2D { public: - WImageOverlay(const Mat &image, const Rect &rect); - void setImage(const Mat &image); + WImageOverlay(InputArray image, const Rect &rect); + void setImage(InputArray image); }; class CV_EXPORTS WImage3D : public Widget3D @@ -219,12 +219,12 @@ namespace cv public: //! Creates 3D image in a plane centered at the origin with normal orientaion along z-axis, //! image x- and y-axes are oriented along x- and y-axes of 3d world - WImage3D(const Mat &image, const Size2d &size); + WImage3D(InputArray image, const Size2d &size); //! Creates 3D image at a given position, pointing in the direction of the normal, and having the up_vector orientation - WImage3D(const Mat &image, const Size2d &size, const Vec3d ¢er, const Vec3d &normal, const Vec3d &up_vector); + WImage3D(InputArray image, const Size2d &size, const Vec3d ¢er, const Vec3d &normal, const Vec3d &up_vector); - void setImage(const Mat &image); + void setImage(InputArray image); }; ///////////////////////////////////////////////////////////////////////////// diff --git a/modules/viz/src/shapes.cpp b/modules/viz/src/shapes.cpp index 5dffbfa0ee..611b1c3f95 100644 --- a/modules/viz/src/shapes.cpp +++ b/modules/viz/src/shapes.cpp @@ -613,15 +613,16 @@ cv::String cv::viz::WText::getText() const /////////////////////////////////////////////////////////////////////////////////////////////// /// image overlay widget implementation -cv::viz::WImageOverlay::WImageOverlay(const Mat &image, const Rect &rect) +cv::viz::WImageOverlay::WImageOverlay(InputArray image, const Rect &rect) { CV_Assert(!image.empty() && image.depth() == CV_8U); vtkSmartPointer source = vtkSmartPointer::New(); source->SetImage(image); + Size sz = image.size(); // Scale the image based on the Rect, and flip to match y-ais orientation vtkSmartPointer transform = vtkSmartPointer::New(); - transform->Scale(image.cols/(double)rect.width, image.rows/(double)rect.height, 1.0); + transform->Scale(sz.width/(double)rect.width, sz.height/(double)rect.height, 1.0); transform->RotateX(180); vtkSmartPointer image_reslice = vtkSmartPointer::New(); @@ -640,11 +641,12 @@ cv::viz::WImageOverlay::WImageOverlay(const Mat &image, const Rect &rect) vtkSmartPointer actor = vtkSmartPointer::New(); actor->SetMapper(image_mapper); actor->SetPosition(rect.x, rect.y); + actor->GetProperty()->SetDisplayLocationToForeground(); WidgetAccessor::setProp(*this, actor); } -void cv::viz::WImageOverlay::setImage(const Mat &image) +void cv::viz::WImageOverlay::setImage(InputArray image) { CV_Assert(!image.empty() && image.depth() == CV_8U); @@ -661,10 +663,11 @@ void cv::viz::WImageOverlay::setImage(const Mat &image) // Create the vtk image and set its parameters based on input image vtkSmartPointer source = vtkSmartPointer::New(); source->SetImage(image); + Size sz = image.size(); // Scale the image based on the Rect, and flip to match y-ais orientation vtkSmartPointer transform = vtkSmartPointer::New(); - transform->Scale(image.cols/(double)size.width, image.rows/(double)size.height, 1.0); + transform->Scale(sz.width/(double)size.width, sz.height/(double)size.height, 1.0); transform->RotateX(180); vtkSmartPointer image_reslice = vtkSmartPointer::New(); @@ -687,7 +690,7 @@ template<> cv::viz::WImageOverlay cv::viz::Widget::cast( /////////////////////////////////////////////////////////////////////////////////////////////// /// image 3D widget implementation -cv::viz::WImage3D::WImage3D(const Mat &image, const Size2d &size) +cv::viz::WImage3D::WImage3D(InputArray image, const Size2d &size) { CV_Assert(!image.empty() && image.depth() == CV_8U); @@ -717,7 +720,7 @@ cv::viz::WImage3D::WImage3D(const Mat &image, const Size2d &size) WidgetAccessor::setProp(*this, actor); } -cv::viz::WImage3D::WImage3D(const Mat &image, const Size2d &size, const Vec3d ¢er, const Vec3d &normal, const Vec3d &up_vector) +cv::viz::WImage3D::WImage3D(InputArray image, const Size2d &size, const Vec3d ¢er, const Vec3d &normal, const Vec3d &up_vector) { CV_Assert(!image.empty() && image.depth() == CV_8U); @@ -732,7 +735,7 @@ cv::viz::WImage3D::WImage3D(const Mat &image, const Size2d &size, const Vec3d &c *this = image3d; } -void cv::viz::WImage3D::setImage(const Mat &image) +void cv::viz::WImage3D::setImage(InputArray image) { CV_Assert(!image.empty() && image.depth() == CV_8U); diff --git a/modules/viz/src/viz3d.cpp b/modules/viz/src/viz3d.cpp index 33d851d642..a886322471 100644 --- a/modules/viz/src/viz3d.cpp +++ b/modules/viz/src/viz3d.cpp @@ -112,6 +112,17 @@ void cv::viz::Viz3d::showWidget(const String &id, const Widget &widget, const Af void cv::viz::Viz3d::removeWidget(const String &id) { impl_->removeWidget(id); } cv::viz::Widget cv::viz::Viz3d::getWidget(const String &id) const { return impl_->getWidget(id); } void cv::viz::Viz3d::removeAllWidgets() { impl_->removeAllWidgets(); } + + +void cv::viz::Viz3d::showImage(InputArray image, const Size& window_size) +{ + removeAllWidgets(); + if (window_size.width > 0 && window_size.height > 0) + setWindowSize(window_size); + + showWidget("showImage", WImageOverlay(image, Rect(Point(0,0), getWindowSize()))); +} + void cv::viz::Viz3d::setWidgetPose(const String &id, const Affine3d &pose) { impl_->setWidgetPose(id, pose); } void cv::viz::Viz3d::updateWidgetPose(const String &id, const Affine3d &pose) { impl_->updateWidgetPose(id, pose); } cv::Affine3d cv::viz::Viz3d::getWidgetPose(const String &id) const { return impl_->getWidgetPose(id); } diff --git a/modules/viz/src/vizcore.cpp b/modules/viz/src/vizcore.cpp index 21c7e0d700..a553f8d721 100644 --- a/modules/viz/src/vizcore.cpp +++ b/modules/viz/src/vizcore.cpp @@ -122,6 +122,13 @@ cv::String cv::viz::VizStorage::generateWindowName(const String &window_name) cv::viz::Viz3d cv::viz::getWindowByName(const String &window_name) { return Viz3d (window_name); } void cv::viz::unregisterAllWindows() { VizStorage::unregisterAll(); } +cv::viz::Viz3d cv::viz::imshow(const String& window_name, InputArray image, const Size& window_size) +{ + Viz3d viz = getWindowByName(window_name); + viz.showImage(image, window_size); + return viz; +} + /////////////////////////////////////////////////////////////////////////////////////////////// /// Read/write clouds. Supported formats: ply, stl, xyz, obj diff --git a/modules/viz/test/tests_simple.cpp b/modules/viz/test/tests_simple.cpp index ff35bd4169..bb3f2009eb 100644 --- a/modules/viz/test/tests_simple.cpp +++ b/modules/viz/test/tests_simple.cpp @@ -250,6 +250,20 @@ TEST(Viz, DISABLED_show_overlay_image) //viz.spin(); } + +TEST(Viz, DISABLED_show_image_method) +{ + Mat lena = imread(Path::combine(cvtest::TS::ptr()->get_data_path(), "lena.png")); + + Viz3d viz("show_image_method"); + viz.showImage(lena); + viz.spinOnce(1500, true); + viz.showImage(lena, lena.size()); + viz.spinOnce(1500, true); + + cv::viz::imshow("show_image_method", make_gray(lena)).spin(); +} + TEST(Viz, DISABLED_show_image_3d) { Mat lena = imread(Path::combine(cvtest::TS::ptr()->get_data_path(), "lena.png"));