image_view.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011, Dirk Thomas, TU Darmstadt
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  *   * Redistributions of source code must retain the above copyright
00010  *     notice, this list of conditions and the following disclaimer.
00011  *   * Redistributions in binary form must reproduce the above
00012  *     copyright notice, this list of conditions and the following
00013  *     disclaimer in the documentation and/or other materials provided
00014  *     with the distribution.
00015  *   * Neither the name of the TU Darmstadt nor the names of its
00016  *     contributors may be used to endorse or promote products derived
00017  *     from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00022  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00023  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00024  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00025  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00026  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00027  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00028  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00029  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00030  * POSSIBILITY OF SUCH DAMAGE.
00031  */
00032 
00033 #include <bwi_rqt_plugins/image_view.h>
00034 
00035 #include <pluginlib/class_list_macros.h>
00036 #include <ros/master.h>
00037 #include <sensor_msgs/image_encodings.h>
00038 
00039 #include <cv_bridge/cv_bridge.h>
00040 #include <opencv2/imgproc/imgproc.hpp>
00041 
00042 #include <QMessageBox>
00043 #include <QPainter>
00044 
00045 namespace bwi_rqt_plugins {
00046 
00047 ImageView::ImageView()
00048   : rqt_gui_cpp::Plugin()
00049   , widget_(0)
00050 {
00051   setObjectName("ImageView");
00052 }
00053 
00054 void ImageView::initPlugin(qt_gui_cpp::PluginContext& context)
00055 {
00056   widget_ = new QWidget();
00057   ui_.setupUi(widget_);
00058 
00059   if (context.serialNumber() > 1)
00060   {
00061     widget_->setWindowTitle(widget_->windowTitle() + " (" + QString::number(context.serialNumber()) + ")");
00062   }
00063   context.addWidget(widget_);
00064 
00065   ros::NodeHandle nh;
00066   topic = nh.resolveName("image");
00067   connect(ui_.image_topic, SIGNAL(textChanged(QString)), this, SLOT(onTextChanged(QString)));
00068   ui_.image_topic->setText(QString::fromStdString(topic));
00069 
00070 }
00071 
00072 void ImageView::shutdownPlugin()
00073 {
00074   subscriber_.shutdown();
00075 }
00076 
00077 void ImageView::saveSettings(qt_gui_cpp::Settings& plugin_settings, qt_gui_cpp::Settings& instance_settings) const
00078 {
00079   instance_settings.setValue("topic", QString::fromStdString(topic));
00080 }
00081 
00082 void ImageView::restoreSettings(const qt_gui_cpp::Settings& plugin_settings, const qt_gui_cpp::Settings& instance_settings)
00083 {
00084   std::string topic = instance_settings.value("topic", "").toString().toStdString();
00085   ui_.image_topic->setText(QString::fromStdString(topic));
00086 }
00087 
00088 void ImageView::onTextChanged(const QString& string) {
00089   subscriber_.shutdown();
00090   try {
00091     topic = string.toStdString();
00092     ros::NodeHandle nh;
00093     image_transport::ImageTransport it(nh);
00094     image_transport::TransportHints hints("raw");
00095 
00096     subscriber_ = it.subscribe(topic, 1, &ImageView::callbackImage, this, hints);
00097     //qDebug("ImageView::onTopicChanged() to topic '%s' with transport '%s'", topic.toStdString().c_str(), subscriber_.getTransport().c_str());
00098   } catch (image_transport::TransportLoadException& e) {
00099     QMessageBox::warning(widget_, tr("Loading image transport plugin failed"), e.what());
00100   }
00101 }
00102 
00103 void ImageView::callbackImage(const sensor_msgs::Image::ConstPtr& msg)
00104 {
00105   try
00106   {
00107     // First let cv_bridge do its magic
00108     cv_bridge::CvImageConstPtr cv_ptr = cv_bridge::toCvShare(msg, sensor_msgs::image_encodings::RGB8);
00109     conversion_mat_ = cv_ptr->image;
00110   }
00111   catch (cv_bridge::Exception& e)
00112   {
00113     try
00114     {
00115       // If we're here, there is no conversion that makes sense, but let's try to imagine a few first
00116       cv_bridge::CvImageConstPtr cv_ptr = cv_bridge::toCvShare(msg);
00117       if (msg->encoding == "CV_8UC3")
00118       {
00119         // assuming it is rgb
00120         conversion_mat_ = cv_ptr->image;
00121       } else if (msg->encoding == "8UC1") {
00122         // convert gray to rgb
00123         cv::cvtColor(cv_ptr->image, conversion_mat_, CV_GRAY2RGB);
00124       } else if (msg->encoding == "16UC1" || msg->encoding == "32FC1") {
00125         // scale / quantify
00126         double min = 0;
00127         double max = 2;
00128         cv::Mat img_scaled_8u;
00129         cv::Mat(cv_ptr->image-min).convertTo(img_scaled_8u, CV_8UC1, 255. / (max - min));
00130         cv::cvtColor(img_scaled_8u, conversion_mat_, CV_GRAY2RGB);
00131       } else {
00132         qWarning("ImageView.callback_image() could not convert image from '%s' to 'rgb8' (%s)", msg->encoding.c_str(), e.what());
00133         ui_.image_frame->setImage(QImage());
00134         return;
00135       }
00136     }
00137     catch (cv_bridge::Exception& e)
00138     {
00139       qWarning("ImageView.callback_image() while trying to convert image from '%s' to 'rgb8' an exception was thrown (%s)", msg->encoding.c_str(), e.what());
00140       ui_.image_frame->setImage(QImage());
00141       return;
00142     }
00143   }
00144 
00145   // image must be copied since it uses the conversion_mat_ for storage which is asynchronously overwritten in the next callback invocation
00146   QImage image(conversion_mat_.data, conversion_mat_.cols, conversion_mat_.rows, conversion_mat_.step[0], QImage::Format_RGB888);
00147   ui_.image_frame->setImage(image);
00148 
00149 }
00150 
00151 }
00152 
00153 PLUGINLIB_EXPORT_CLASS(bwi_rqt_plugins::ImageView, rqt_gui_cpp::Plugin)


bwi_rqt_plugins
Author(s): Piyush Khandelwal
autogenerated on Thu Jun 6 2019 17:57:50