virtual_camera_display.cpp
Go to the documentation of this file.
00001 /*
00002  * virtual_camera_display.cpp
00003  *
00004  *  Created on: Aug 25, 2010
00005  *      Author: petrito1@cmp.felk.cvut.cz
00006  *
00007  * ROS node for displaying images from virtual camera (e.g. used with omnicamera node).
00008  * Publishes virtual camera configuration on topic "virtual_camera/config",
00009  * subscribes for images from "virtual_camera/image".
00010  */
00011 
00012 #include <cv_bridge/cv_bridge.h>
00013 #include <image_transport/image_transport.h>
00014 #include <omnicamera_msgs/VirtualCameraConfig.h>
00015 #include <opencv2/core/core.hpp>
00016 #include <opencv2/highgui/highgui.hpp>
00017 #include <ros/ros.h>
00018 
00019 using std::string;
00020 using omnicamera_msgs::VirtualCameraConfig;
00021 
00022 #define KEY_UP    0xff52
00023 #define KEY_DOWN  0xff54
00024 #define KEY_LEFT  0xff51
00025 #define KEY_RIGHT 0xff53
00026 #define KEY_Q     0x71
00027 #define KEY_A     0x61
00028 #define KEY_W     0x77
00029 #define KEY_S     0x73
00030 #define KEY_E     0x65
00031 #define KEY_D     0x64
00032 
00033 // Zoom sensitivity = 2^(1/4)
00034 #define ZOOM_SENSITIVITY        1.189207115
00035 #define VIEWPORT_CHANGE_FACTOR  1.189207115
00036 // Rotation sensitivity in terms of FOV
00037 #define ROTATION_SENSITIVITY    0.25
00038 
00039 namespace {
00040 const string nodeName = "virtual_camera_display";
00041 }
00042 
00043 void imageCallback(const sensor_msgs::ImageConstPtr &msg) {
00044   cv_bridge::CvImageConstPtr cvPtr;
00045   try {
00046     cvPtr = cv_bridge::toCvShare(msg);
00047     cv::imshow(nodeName, cvPtr->image);
00048   } catch (cv_bridge::Exception& e) {
00049     ROS_ERROR("cv_bridge exception: %s", e.what());
00050     return;
00051   }
00052 }
00053 
00054 int main(int argc, char **argv) {
00055 
00056   ros::init(argc, argv, nodeName);
00057 
00058   printf("Controls:\n");
00059   printf("left and right arrows: pan\n");
00060   printf("up and down arrows:    tilt\n");
00061   printf("'q' and 'a':           zoom in and zoom out\n");
00062   printf("'w' and 's':           raise or lower image update interval\n");
00063   printf("'e' and 'd':           scale viewport up or down\n");
00064 
00065   ros::NodeHandle nh;
00066   ros::Publisher p = nh.advertise<VirtualCameraConfig> ("virtual_camera/config", 1);
00067 
00068   image_transport::ImageTransport it(nh);
00069   image_transport::Subscriber sub = it.subscribe("virtual_camera/image", 1, imageCallback);
00070 
00071   cv::namedWindow(nodeName);
00072   cv::startWindowThread();
00073 
00074   // Init config to default values.
00075   ros::NodeHandle pnh("~");
00076   VirtualCameraConfig config;
00077   pnh.param("viewport_width", config.viewportWidth, 512);
00078   pnh.param("viewport_height", config.viewportHeight, 512);
00079   pnh.param("pan", config.pan, 0.0);
00080   pnh.param("tilt", config.tilt, 0.0);
00081   pnh.param("horizontal_fov", config.horizontalFov, 90.0);
00082   pnh.param("vertical_fov", config.verticalFov, 90.0);
00083   pnh.param("image_update_interval", config.imageUpdateInterval, 0.0);
00084 
00085   while (nh.ok()) {
00086     ros::spinOnce();
00087     int keyPressed = cv::waitKey(100);
00088     if (keyPressed >= 0) {
00089       printf("Key pressed: %#x %d %d %d %d.\n", keyPressed, (keyPressed >> 24) & 0xff, (keyPressed >> 16) & 0xff,
00090           (keyPressed >> 8) & 0xff, keyPressed & 0xff);
00091       switch (keyPressed & 0xffff) {
00092       case KEY_LEFT:
00093         config.pan += config.horizontalFov * ROTATION_SENSITIVITY;
00094         p.publish(config);
00095         break;
00096       case KEY_RIGHT:
00097         config.pan -= config.horizontalFov * ROTATION_SENSITIVITY;
00098         p.publish(config);
00099         break;
00100       case KEY_UP:
00101         config.tilt += config.verticalFov * ROTATION_SENSITIVITY;
00102         p.publish(config);
00103         break;
00104       case KEY_DOWN:
00105         config.tilt -= config.verticalFov * ROTATION_SENSITIVITY;
00106         p.publish(config);
00107         break;
00108       case KEY_Q:
00109         config.horizontalFov /= ZOOM_SENSITIVITY;
00110         config.verticalFov /= ZOOM_SENSITIVITY;
00111         p.publish(config);
00112         break;
00113       case KEY_A:
00114         config.horizontalFov *= ZOOM_SENSITIVITY;
00115         config.verticalFov *= ZOOM_SENSITIVITY;
00116         p.publish(config);
00117         break;
00118       case KEY_W:
00119         config.imageUpdateInterval += 1;
00120         printf("Image update interval changed to %f sec.\n", config.imageUpdateInterval);
00121         p.publish(config);
00122         break;
00123       case KEY_S:
00124         config.imageUpdateInterval -= 1;
00125         if (config.imageUpdateInterval < 0) {
00126           config.imageUpdateInterval = 0;
00127         }
00128         printf("Image update interval changed to %f sec.\n", config.imageUpdateInterval);
00129         p.publish(config);
00130         break;
00131       case KEY_E:
00132         config.viewportWidth = static_cast<int32_t> (config.viewportWidth * VIEWPORT_CHANGE_FACTOR);
00133         config.viewportHeight = static_cast<int32_t> (config.viewportHeight * VIEWPORT_CHANGE_FACTOR);
00134         p.publish(config);
00135         break;
00136       case KEY_D:
00137         config.viewportWidth = static_cast<int32_t> (config.viewportWidth / VIEWPORT_CHANGE_FACTOR);
00138         config.viewportHeight = static_cast<int32_t> (config.viewportHeight / VIEWPORT_CHANGE_FACTOR);
00139         p.publish(config);
00140         break;
00141       }
00142     }
00143   }
00144 
00145   cv::destroyWindow(nodeName.data());
00146 }
00147 
 All Classes Namespaces Files Functions Variables Typedefs Defines


omnicamera
Author(s): Tomas Petricek / petrito1@cmp.felk.cvut.cz
autogenerated on Tue Dec 10 2013 14:26:53