CImageCapturer.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2013, Institute for Artificial Intelligence,
00005  *  Universität Bremen.
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 Institute for Artificial Intelligence,
00019  *     Universität Bremen, nor the names of its contributors may be
00020  *     used to endorse or promote products derived from this software
00021  *     without specific prior written permission.
00022  *
00023  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00027  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00033  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034  *  POSSIBILITY OF SUCH DAMAGE.
00035  *********************************************************************/
00036 
00040 #include <plugins/imagecapturer/CImageCapturer.h>
00041 
00042 
00043 namespace beliefstate {
00044   CImageCapturer::CImageCapturer() {
00045     m_strImagesTopic = "";
00046   }
00047   
00048   CImageCapturer::~CImageCapturer() {
00049   }
00050   
00051   bool CImageCapturer::fileExists(std::string strFileName) {
00052     std::ifstream ifile(strFileName.c_str());
00053     
00054     if(ifile) {
00055       return true;
00056     }
00057     
00058     return false;
00059   }
00060   
00061   void CImageCapturer::freeFilename(std::string& strFileName, std::string strWorkingDirectory) {
00062     int nIndex = 0;
00063     std::string strBase = strWorkingDirectory + strFileName;
00064     std::string strFile = strFileName;
00065     
00066     while(this->fileExists(strBase)) {
00067       std::stringstream sts;
00068       sts << nIndex;
00069       sts << "_";
00070       sts << strFileName;
00071       
00072       nIndex++;
00073       
00074       strFile = sts.str();
00075       strBase = strWorkingDirectory + sts.str();
00076     }
00077     
00078     strFileName = strFile;
00079   }
00080   
00081   bool CImageCapturer::captureFromTopic(std::string strTopicName, std::string& strFileName, std::string strWorkingDirectory, bool bUseFreeName) {
00082     int nTimeout = 1000;
00083     bool bReturnvalue = false;
00084     bool bGoon = true;
00085     m_bReceived = false;
00086     
00087     ros::NodeHandle nh;
00088     m_subImage = nh.subscribe(strTopicName, 1, &CImageCapturer::imageCallback, this);
00089     
00090     while(bGoon) {
00091       nTimeout--;
00092       
00093       ros::Duration(0.1).sleep();
00094       
00095       // NOTE(winkler): Is spinning actually necessary here? There is
00096       // another thread maintained by the ''ros'' plugin that does
00097       // continuous spinning. Check if this mechanism works without an
00098       // epxlicit ''spinOnce'' here. This could be a reason for images
00099       // not appearing on the publishing topic.
00100       
00101       ros::spinOnce();
00102       
00103       if(nTimeout <= 0 || m_bReceived) {
00104         bGoon = false;
00105       }
00106     }
00107     
00108     m_subImage.shutdown();
00109     
00110     if(m_bReceived) {
00111       cv_bridge::CvImagePtr cv_ptr;
00112       
00113       try {
00114         cv_ptr = cv_bridge::toCvCopy(m_imgReceived, sensor_msgs::image_encodings::BGR8);
00115         
00116         cv::Mat imgMat = cv_ptr->image;
00117         std::string strUseFilename = "";
00118         
00119         if(bUseFreeName) {
00120           this->freeFilename(strFileName, strWorkingDirectory);
00121           strUseFilename = strWorkingDirectory + strFileName;
00122         } else {
00123           strUseFilename = strWorkingDirectory + strFileName;
00124         }
00125         
00126         cv::imwrite(strUseFilename, imgMat);
00127         
00128         if(m_strImagesTopic != "") {
00129           m_pubImages.publish(m_imgReceived);
00130         }
00131         
00132         bReturnvalue = true;
00133       } catch(cv_bridge::Exception& e) {
00134       }
00135     }
00136     
00137     return bReturnvalue;
00138   }
00139   
00140   void CImageCapturer::imageCallback(const sensor_msgs::Image &imgData) {
00141     if(!m_bReceived) {
00142       m_bReceived = true;
00143       m_imgReceived = imgData;
00144     }
00145   }
00146 
00147   void CImageCapturer::publishImages(std::string strImagesTopic) {
00148     m_strImagesTopic = strImagesTopic;
00149     ros::NodeHandle nh;
00150     
00151     m_pubImages = nh.advertise<sensor_msgs::Image>(strImagesTopic, 1);
00152   }
00153 }


beliefstate
Author(s): Jan Winkler
autogenerated on Sun Oct 5 2014 22:30:15