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
00031
00032
00033
00034
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
00096
00097
00098
00099
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 }