From 13875a7a112b2dbb80a2054640666e77815fdf41 Mon Sep 17 00:00:00 2001 From: Tobin Hall Date: Wed, 6 Nov 2019 12:47:22 +1300 Subject: [PATCH 1/2] Adding rotation to image_plugin --- .../include/mapviz_plugins/image_plugin.h | 5 + mapviz_plugins/src/image_plugin.cpp | 60 +++- mapviz_plugins/ui/image_config.ui | 302 ++++++++++-------- 3 files changed, 238 insertions(+), 129 deletions(-) diff --git a/mapviz_plugins/include/mapviz_plugins/image_plugin.h b/mapviz_plugins/include/mapviz_plugins/image_plugin.h index c2174a10..cb204edc 100644 --- a/mapviz_plugins/include/mapviz_plugins/image_plugin.h +++ b/mapviz_plugins/include/mapviz_plugins/image_plugin.h @@ -113,6 +113,7 @@ namespace mapviz_plugins void SetHeight(double height); void SetSubscription(bool visible); void SetTransport(const QString& transport); + void SetRotation(QString rotation); void KeepRatioChanged(bool checked); private: @@ -127,12 +128,14 @@ namespace mapviz_plugins double width_; double height_; std::string transport_; + int rotation_; bool force_resubscribe_; bool has_image_; double last_width_; double last_height_; + int last_rotation_; double original_aspect_ratio_; ros::NodeHandle local_node_; @@ -143,6 +146,7 @@ namespace mapviz_plugins cv_bridge::CvImagePtr cv_image_; cv::Mat scaled_image_; + cv::Mat rotated_image_; void imageCallback(const sensor_msgs::ImageConstPtr& image); @@ -151,6 +155,7 @@ namespace mapviz_plugins std::string AnchorToString(Anchor anchor); std::string UnitsToString(Units units); + std::string RotationToString(int rotation); }; } diff --git a/mapviz_plugins/src/image_plugin.cpp b/mapviz_plugins/src/image_plugin.cpp index 6cf3ea49..be36a5c5 100644 --- a/mapviz_plugins/src/image_plugin.cpp +++ b/mapviz_plugins/src/image_plugin.cpp @@ -59,6 +59,7 @@ namespace mapviz_plugins width_(320), height_(240), transport_("default"), + rotation_(-1), has_image_(false), last_width_(0), last_height_(0), @@ -88,6 +89,7 @@ namespace mapviz_plugins QObject::connect(ui_.keep_ratio, SIGNAL(toggled(bool)), this, SLOT(KeepRatioChanged(bool))); QObject::connect(ui_.transport_combo_box, SIGNAL(activated(const QString&)), this, SLOT(SetTransport(const QString&))); + QObject::connect(ui_.rotation, SIGNAL(activated(QString)), this, SLOT(SetRotation(QString))); ui_.width->setKeyboardTracking(false); ui_.height->setKeyboardTracking(false); @@ -217,6 +219,26 @@ namespace mapviz_plugins TopicEdited(); } + void ImagePlugin::SetRotation(QString rotation) + { + + if (rotation == "90°") + { + rotation_ = cv::ROTATE_90_CLOCKWISE; + } + else if (rotation == "180°") + { + rotation_ = cv::ROTATE_180; + } + else if (rotation == "270°") + { + rotation_ = cv::ROTATE_90_COUNTERCLOCKWISE; + } + else{ + rotation_ = -1; + } + } + void ImagePlugin::KeepRatioChanged(bool checked) { ui_.height->setEnabled( !checked ); @@ -446,6 +468,19 @@ namespace mapviz_plugins ScaleImage(width, height); } + // Rotate the source image if necessary + if(rotation_ >= cv::ROTATE_90_CLOCKWISE && rotation_ <= cv::ROTATE_90_COUNTERCLOCKWISE){ + cv::rotate(scaled_image_, rotated_image_, rotation_); + //-- If we did not rotate 180, we have flipped width / height + if(rotation_ != cv::ROTATE_180){ + height = width_; + width = height_; + } + } + else{ + rotated_image_ = scaled_image_; + } + // Calculate the correct render position double x_pos = 0; double y_pos = 0; @@ -502,7 +537,7 @@ namespace mapviz_plugins glRasterPos2d(x_pos, y_pos); - DrawIplImage(&scaled_image_); + DrawIplImage(&rotated_image_); glPopMatrix(); @@ -546,6 +581,14 @@ namespace mapviz_plugins SetAnchor(anchor.c_str()); } + if (node["rotation"]) + { + std::string rotation; + node["rotation"] >> rotation; + ui_.rotation->setCurrentIndex(ui_.rotation->findText(rotation.c_str())); + SetRotation(rotation.c_str()); + } + if (node["units"]) { std::string units; @@ -597,6 +640,7 @@ namespace mapviz_plugins emitter << YAML::Key << "height" << YAML::Value << height_; emitter << YAML::Key << "keep_ratio" << YAML::Value << ui_.keep_ratio->isChecked(); emitter << YAML::Key << "image_transport" << YAML::Value << transport_; + emitter << YAML::Key << "rotation" << YAML::Value << RotationToString(rotation_); } std::string ImagePlugin::AnchorToString(Anchor anchor) @@ -659,6 +703,20 @@ namespace mapviz_plugins return units_string; } + std::string ImagePlugin::RotationToString(int rotation) + { + std::string rotation_string = "0°"; + + if (rotation == cv::ROTATE_90_CLOCKWISE) + rotation_string = "90°"; + else if (rotation == cv::ROTATE_180) + rotation_string = "180°"; + else if (rotation == cv::ROTATE_90_COUNTERCLOCKWISE) + rotation_string = "270°"; + + return rotation_string; + } + void ImagePlugin::CreateLocalNode() { // This is the same way ROS generates anonymous node names. diff --git a/mapviz_plugins/ui/image_config.ui b/mapviz_plugins/ui/image_config.ui index 31c5ff45..19b59df5 100644 --- a/mapviz_plugins/ui/image_config.ui +++ b/mapviz_plugins/ui/image_config.ui @@ -17,8 +17,8 @@ - - + + Sans Serif @@ -26,7 +26,20 @@ - Topic: + Transport: + + + + + + + + Sans Serif + 8 + + + + Status: @@ -45,8 +58,8 @@ - - + + Sans Serif @@ -54,32 +67,47 @@ - Anchor: + Offset X: - - + + + + + Sans Serif + 8 + + - Keep original aspect ratio + Units: - - + + + + + 55 + 16777215 + + Sans Serif 8 + + + - Units: + Select - - + + Sans Serif @@ -87,7 +115,7 @@ - Height: + Anchor: @@ -117,91 +145,7 @@ - - - - - Sans Serif - 8 - - - - Transport: - - - - - - - - Sans Serif - 8 - - - - Status: - - - - - - - - Sans Serif - 8 - - - - Width: - - - - - - - - Sans Serif - 8 - - - - - - - No topic - - - true - - - - - - - - Sans Serif - 8 - - - - Offset Y: - - - - - - - - Sans Serif - 8 - - - - Offset X: - - - - + Qt::Vertical @@ -221,13 +165,6 @@ - - - - 2000 - - - @@ -289,6 +226,22 @@ + + + + 0 + + + 1.000000000000000 + + + 10000.000000000000000 + + + 320.000000000000000 + + + @@ -299,41 +252,43 @@ - - - - - 55 - 16777215 - + + + + Keep original aspect ratio + + + + Sans Serif 8 - - - - Select + Width: - - - - 0 + + + + + Sans Serif + 8 + - - 1.000000000000000 + + Height: + + + + - 10000.000000000000000 - - - 320.000000000000000 + 2000 @@ -353,6 +308,97 @@ + + + + + Sans Serif + 8 + + + + + + + No topic + + + true + + + + + + + + Sans Serif + 8 + + + + Offset Y: + + + + + + + + Sans Serif + 8 + + + + Topic: + + + + + + + + 16777215 + 25 + + + + false + + + + + + + + + 90° + + + + + 180° + + + + + 270° + + + + + + + + + Sans Serif + 8 + + + + Rotation: + + + From 1f92426e4c342d2d0b378d1e1d7c9a3647bd0df8 Mon Sep 17 00:00:00 2001 From: David Anthony Date: Fri, 6 Sep 2024 16:28:27 -0500 Subject: [PATCH 2/2] Update image_plugin.cpp Small formatting changes, make the degree symbol optional --- mapviz_plugins/src/image_plugin.cpp | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/mapviz_plugins/src/image_plugin.cpp b/mapviz_plugins/src/image_plugin.cpp index be36a5c5..94301cf8 100644 --- a/mapviz_plugins/src/image_plugin.cpp +++ b/mapviz_plugins/src/image_plugin.cpp @@ -222,19 +222,19 @@ namespace mapviz_plugins void ImagePlugin::SetRotation(QString rotation) { - if (rotation == "90°") + if ((rotation == "90°") || (rotation == "90")) { rotation_ = cv::ROTATE_90_CLOCKWISE; } - else if (rotation == "180°") + else if ((rotation == "180°") || (rotation == "180")) { rotation_ = cv::ROTATE_180; } - else if (rotation == "270°") + else if ((rotation == "270°") || (rotation == "270")) { rotation_ = cv::ROTATE_90_COUNTERCLOCKWISE; } - else{ + else { rotation_ = -1; } } @@ -707,13 +707,10 @@ namespace mapviz_plugins { std::string rotation_string = "0°"; - if (rotation == cv::ROTATE_90_CLOCKWISE) - rotation_string = "90°"; - else if (rotation == cv::ROTATE_180) - rotation_string = "180°"; - else if (rotation == cv::ROTATE_90_COUNTERCLOCKWISE) - rotation_string = "270°"; - + if (rotation == cv::ROTATE_90_CLOCKWISE) { rotation_string = "90°"; } + else if (rotation == cv::ROTATE_180) { rotation_string = "180°"; } + else if (rotation == cv::ROTATE_90_COUNTERCLOCKWISE) { rotation_string = "270°"; } + return rotation_string; }