camera_self_filter_server.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  *
00003  * Software License Agreement (BSD License)
00004  *
00005  *  Copyright (c) 2008, Robert Bosch LLC.
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions
00010  *  are met:
00011  *
00012  *   * Redistributions of source code must retain the above copyright
00013  *     notice, this list of conditions and the following disclaimer.
00014  *   * Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions and the following
00016  *     disclaimer in the documentation and/or other materials provided
00017  *     with the distribution.
00018  *   * Neither the name of the Robert Bosch nor the names of its
00019  *     contributors may be used to endorse or promote products derived
00020  *     from this software without specific prior written permission.
00021  *
00022  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033  *  POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  *********************************************************************/
00036 
00037 /*
00038  * camera_self_filter.cpp
00039  *
00040  *  Created on: Oct 6, 2010
00041  *      Author: christian
00042  */
00043 
00044 #include <cstdio>
00045 
00046 #include <ros/ros.h>
00047 #include <opencv/cv.h>
00048 #include <opencv/highgui.h>
00049 #include "cv_bridge/cv_bridge.h"
00050 #include "sensor_msgs/Image.h"
00051 #include "sensor_msgs/CameraInfo.h"
00052 #include "image_transport/image_transport.h"
00053 #include <image_geometry/pinhole_camera_model.h>
00054 
00055 #define GL_GLEXT_PROTOTYPES
00056 #include <GL/gl.h>
00057 #include <GL/glext.h>
00058 #include <GL/freeglut.h>
00059 
00060 #include "camera_self_filter/robotMeshModel.h"
00061 #include "camera_self_filter/mask.h"
00062 
00063 
00064 RobotMeshModel* robmod=NULL;
00065 IplImage* ipl_maskBGRA = NULL;
00066 IplImage* ipl_maskBW = NULL;
00067 
00068 bool inverted;
00069 
00070 void initializeGL ()
00071 {
00072     glClearColor(0, 0, 0, 0);
00073 //  glEnable(GL_LIGHTING);
00074 //  glEnable(GL_LIGHT0);
00075     glEnable(GL_NORMALIZE);
00076     glEnable(GL_COLOR_MATERIAL);
00077     glEnable(GL_CULL_FACE);
00078     glEnable(GL_DEPTH_TEST);
00079     glShadeModel(GL_SMOOTH);
00080         glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00081         glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
00082                      GL_LINEAR_MIPMAP_LINEAR);
00083 }
00084 
00085 
00086 
00087 //function that draws the robot at current state
00088 void display() {
00089 
00090    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00091 
00092    robmod->setCamera();
00093    robmod->setCameraPose();
00094    robmod->paintRobot();
00095    glReadPixels(0, 0, robmod->cam_info_->width, robmod->cam_info_->height , GL_BGRA, GL_UNSIGNED_BYTE, ipl_maskBGRA->imageData);
00096 
00097    glutSwapBuffers();
00098 }
00099 
00100 
00101 
00102 sensor_msgs::ImagePtr calc_and_publish_BWMask(const ros::Time time_stamp, const std::string frame_id ){
00103         robmod->updateRobotLinks(time_stamp);
00104         glutPostRedisplay();
00105         glutMainLoopEvent();
00106         cvCvtColor(ipl_maskBGRA, ipl_maskBW, CV_BGRA2GRAY);
00107         cvFlip(ipl_maskBW);
00108         if (inverted)
00109                 cvNot(ipl_maskBW,ipl_maskBW);
00110 
00111         std_msgs::Header header;
00112     header.frame_id = frame_id;
00113     header.stamp = time_stamp;
00114     cv_bridge::CvImage cv_image(header,"mono8",ipl_maskBW);
00115         return cv_image.toImageMsg();
00116 }
00117 
00118 bool calcMaskCB(camera_self_filter::maskRequest& req, camera_self_filter::maskResponse& res){
00119         sensor_msgs::ImagePtr  img = calc_and_publish_BWMask(req.header.stamp, req.header.frame_id);
00120         res.mask_image = *img;
00121         return true;
00122 
00123 }
00124 
00125 
00126 int main(int argc, char **argv) {
00127         glutInit(&argc, argv);
00128     ros::init(argc, argv, "robot_self_filter");
00129 
00130 
00131     //create selfilter service
00132     ros::NodeHandle nh;
00133     ros::NodeHandle nh_priv("~");
00134 
00135     //set parameters
00136 
00137 
00138     std::string camera_topic;
00139     std::string camera_info_topic;
00140 
00141     nh_priv.param("inverted",inverted, false);
00142     nh_priv.param<std::string>("camera_topic", camera_topic, "/wide_stereo/right/image_rect_color" );
00143     nh_priv.param<std::string>("camera_info_topic", camera_info_topic, "/wide_stereo/right/camera_info" );
00144 
00145     ROS_INFO("camera topic %s", camera_topic.c_str());
00146     ROS_INFO("camera info %s", camera_info_topic.c_str());
00147 
00148     //create robot model
00149     robmod = new RobotMeshModel;
00150 
00151     //initialize glut
00152     glutInitWindowSize (robmod->cam_info_->width, robmod->cam_info_->height);
00153     glutInitDisplayMode ( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
00154 
00155 
00156     glutCreateWindow ("dummy");
00157     glutHideWindow();
00158 
00159     initializeGL();
00160     // Register glut callbacks:
00161     glutDisplayFunc (display);
00162 
00163     ipl_maskBGRA = cvCreateImage(cvSize( robmod->cam_info_->width, robmod->cam_info_->height), IPL_DEPTH_8U, 4);
00164     ipl_maskBW = cvCreateImage(cvSize( robmod->cam_info_->width, robmod->cam_info_->height), IPL_DEPTH_8U, 1);
00165 
00166 
00167 
00168 
00169 
00170     ros::ServiceServer mask_service = nh.advertiseService("self_mask", calcMaskCB );
00171 
00172 
00173     ros::spin();
00174 
00175 
00176     //clean up
00177     cvReleaseImage(&ipl_maskBGRA);
00178     cvReleaseImage(&ipl_maskBW);
00179     delete robmod;
00180     return 0;
00181 }
00182 


camera_self_filter
Author(s): Christian Bersch (Maintained by Benjamin Pitzer)
autogenerated on Thu Jan 2 2014 11:07:31