ImageSubsrciption.cpp
Go to the documentation of this file.
00001 /* 
00002  * Copyright (c) 2010, Ulrich Klank, Dejan Pangercic
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 are met:
00007  * 
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  * 
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #include "ImageSubscription.h"
00031 
00032 #include "cv.h"
00033 #include "highgui.h"
00034 
00035 
00036 #include "cv_bridge/CvBridge.h"
00037 
00038 
00039 using namespace cop;
00040 Reading*        ImageSubscription::GetReading(const long &Frame )
00041 {
00042   if((signed)m_images.size() < (Frame - m_deletedOffset + 1) || m_images.size() == 0)
00043   {
00044     if(m_grabbing)
00045     {
00046       while(m_grabbing && ((signed)m_images.size() < (Frame - m_deletedOffset + 1) || m_images.size() == 0))
00047       {
00048         printf("waiting for the camera %s to start grabbing\n", GetSensorID().c_str());
00049         sleep(0.2);
00050       }
00051       printf("Got a new image: %d\n", (int)m_images.size());
00052     }
00053     if((signed)m_images.size() < (Frame - m_deletedOffset + 1) || m_images.size() == 0)
00054     {
00055       printf("unexpected error\n");
00056       throw "Asking for images from a camera that has no images";
00057     }
00058   }
00059   if(Frame == -1 || (Frame - m_deletedOffset < 0 && (unsigned)(Frame - m_deletedOffset) >= m_images.size()))
00060   {
00061     return GetReading_Lock(m_images.size() -1);
00062     /*return m_images[m_images.size() -1];*/
00063   }
00064   return GetReading_Lock(Frame - m_deletedOffset);
00065 }
00066 
00067 std::string ImageSubscription::GetWindowName()
00068 {
00069   if(!m_bWindowCreated)
00070   {
00071     cvNamedWindow( GetSensorID().c_str(), 1 );
00072     m_bWindowCreated = true;
00073   }
00074   return GetSensorID();
00075 }
00076 
00077 bool ImageSubscription::CanSee(RelPose &pose) const
00078 {
00079   Matrix m = pose.GetMatrix(m_relPose->m_uniqueID);
00080   double x = m.element(0,3);  
00081   double y = m.element(1,3);  
00082   double z = m.element(2,3);  
00083   x = (x / z) * m_calib.focal_length;
00084   y = (y / z) * m_calib.focal_length; /* bring z to focallength*/
00085   double column = (x / m_calib.pix_size_x) - m_calib.proj_center_x;
00086   double row    = (y / m_calib.pix_size_y) - m_calib.proj_center_y;
00087   printf("ImageSubscription::CanSee:  P[X=%f, Y=%f, Z=%f] *K = p[r=%f, c=%f]\n", x,y,z,row, column);
00088   if(row > 0 && column > 0 && row <  m_calib.pix_size_y * 2.2 &&
00089      column < m_calib.proj_center_x*2.2)
00090     return true;
00091   return false;
00092 }
00093 
00094 
00095 void ImageSubscription::CallBack(const sensor_msgs::ImageConstPtr& msg_ptr)
00096 {
00097   try
00098   {
00099     IplImageReading *reading = new IplImageReading(m_bridge->imgMsgToCv(msg_ptr, "bgr8"));
00100     PushBack(reading);
00101     while(m_images.size() > m_max_cameraImages)
00102     {
00103       if(DeleteReading())
00104         continue;
00105       else
00106       {
00107         printf("SR: Could not delete an image!");
00108         break;
00109       }
00110     }
00111 
00112    }
00113   catch (sensor_msgs::CvBridgeException error)
00114   {
00115     ROS_ERROR("error");
00116   }
00117 }
00118 
00119 void ImageSubscription::SetData(XMLTag* tag)
00120 {
00121      Sensor::SetData(tag);
00122      m_bridge = new sensor_msgs::CvBridge();
00123     m_stTopicName = tag->GetProperty(XML_PROPERTY_TOPIC, "/yarp_to_ros_image/yarp_to_ros_image");
00124     m_calib.focal_length = tag->GetPropertyDouble(XML_ATTRIBUTE_FOCAL_LENGTH, 0.06);
00125     m_calib.pix_size_x = tag->GetPropertyDouble(XML_ATTRIBUTE_PIX_SIZE_X, 0.000001);
00126     m_calib.pix_size_y = tag->GetPropertyDouble(XML_ATTRIBUTE_PIX_SIZE_Y, 0.000001);
00127     m_calib.proj_center_x = tag->GetPropertyDouble(XML_ATTRIBUTE_PROJ_CENTER_X, 320.0);
00128     m_calib.proj_center_y =tag->GetPropertyDouble(XML_ATTRIBUTE_PROJ_CENTER_Y, 240.0);
00129 }
00130 
00131 void ImageSubscription::Show(const long frame)
00132 {
00133  IplImageReading *reading = (IplImageReading*)GetReading(frame);
00134  cv::Mat &img = reading->m_image;
00135  cv::imshow(GetWindowName().c_str() , img);
00136  reading->Free();
00137 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends Defines


cop_ros_plugins
Author(s): U. Klank, Dejan Pangercic
autogenerated on Thu May 23 2013 12:29:47