Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
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
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
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
00116 cv_bridge::CvImageConstPtr cv_ptr = cv_bridge::toCvShare(msg);
00117 if (msg->encoding == "CV_8UC3")
00118 {
00119
00120 conversion_mat_ = cv_ptr->image;
00121 } else if (msg->encoding == "8UC1") {
00122
00123 cv::cvtColor(cv_ptr->image, conversion_mat_, CV_GRAY2RGB);
00124 } else if (msg->encoding == "16UC1" || msg->encoding == "32FC1") {
00125
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
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)