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 #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     
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; 
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 }