00001 #include<iostream>
00002 #include<iomanip>
00003 #include<fstream>
00004
00005 #include<opencv/cv.h>
00006 #include<opencv/highgui.h>
00007
00008 #include<pcl/common/transform.h>
00009 #include<pcl/point_types.h>
00010 #include <pcl_ros/point_cloud.h>
00011
00012 #include<sensor_msgs/PointCloud2.h>
00013 #include<sensor_msgs/Image.h>
00014 #include<sensor_msgs/image_encodings.h>
00015 #include <cv_bridge/cv_bridge.h>
00016 class scene_publisher
00017 {
00018 public:
00019
00020
00021 scene_publisher()
00022 {
00023 this->persp = 1;
00024 this->shot = 0;
00025
00026 file = "-1";
00027 n_.param("/cob_people_detection/face_recognizer/file", file, file);
00028 std::cout << "input file: " << file << std::endl;
00029
00030 scene_pub_ = n_.advertise<sensor_msgs::PointCloud2>("/camera/depth_registered/points", 1);
00031 img_pub_ = n_.advertise<sensor_msgs::Image>("/camera/rgb/image_color", 1);
00032
00033 }
00034
00035
00036 ~scene_publisher()
00037 {
00038 }
00039
00040 void process()
00041 {
00042
00043 pc.clear();
00044
00045 std::stringstream xml_stream, jpg_stream;
00046 xml_stream << file.c_str() << "/" << std::setw(3) << std::setfill('0') << persp << "_" << shot << "_d.xml";
00047 jpg_stream << file.c_str() << "/" << std::setw(3) << std::setfill('0') << persp << "_" << shot << "_c.bmp";
00048
00049 std::cout << xml_stream.str() << " " << jpg_stream.str() << std::endl;
00050
00051
00052 cv::Mat dm = cv::Mat(640, 480, CV_64FC1);
00053 std::cout << xml_stream.str().c_str() << std::endl;
00054 std::cout << jpg_stream.str().c_str() << std::endl;
00055 cv::FileStorage fs(xml_stream.str().c_str(), cv::FileStorage::READ);
00056 fs["depthmap"] >> dm;
00057 fs.release();
00058 cv::Mat dm_cp, dm_cp_roi;
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 cv::Mat img;
00069 img = cv::imread(jpg_stream.str().c_str());
00070 img.convertTo(img, CV_8UC3);
00071 cv::resize(img, img, cv::Size(640, 480));
00072
00073
00074
00075
00076
00077
00078 std::cout << "calculcating" << std::endl;
00079
00080
00081
00082 pc.width = 640;
00083 pc.height = 480;
00084 cv::Mat cam_mat = (cv::Mat_<double>(3, 3) << 524.90160178307269, 0.0, 320.13543361773458, 0.0, 525.85226379335393, 240.73474482242005, 0.0, 0.0, 1.0);
00085
00086 int index = 0;
00087 cv::Vec3f rvec, tvec;
00088 cv::Mat dist_coeff;
00089
00090 tvec = cv::Vec3f(0.03, 0.0, 0.0);
00091 rvec = cv::Vec3f(0.0, 0.0, 0);
00092 dist_coeff = cv::Mat::zeros(1, 5, CV_32FC1);
00093 cv::Mat pc_trans = cv::Mat::zeros(640, 480, CV_64FC1);
00094 for (int r = 0; r < dm.rows; r++)
00095 {
00096 for (int c = 0; c < dm.cols; c++)
00097 {
00098
00099
00100
00101 cv::Point3f pt;
00102 pt.x = (c - 320);
00103 pt.y = (r - 240);
00104 pt.z = 525;
00105
00106 double nrm = cv::norm(pt);
00107 pt.x /= nrm;
00108 pt.y /= nrm;
00109 pt.z /= nrm;
00110
00111 pt = pt * dm.at<double>(r, c);
00112 std::vector<cv::Point3f> pt3_vec;
00113 pt3_vec.push_back(pt);
00114 std::vector<cv::Point2f> pt2_vec;
00115
00116 cv::projectPoints(pt3_vec, rvec, tvec, cam_mat, dist_coeff, pt2_vec);
00117
00118 int x_t, y_t;
00119 x_t = round(pt2_vec[0].x);
00120 y_t = round(pt2_vec[0].y);
00121 if (x_t < 0 || x_t > 640 || y_t < 0 || y_t > 480)
00122 continue;
00123 pc_trans.at<double>(y_t, x_t) = dm.at<double>(r, c);
00124
00125 }
00126 }
00127
00128 for (int ra = 0; ra < dm.rows; ra++)
00129 {
00130 for (int ca = 0; ca < dm.cols; ca++)
00131 {
00132 cv::Point3f pt;
00133 pt.x = (ca - 320);
00134 pt.y = (ra - 240);
00135 pt.z = 525;
00136
00137 double nrm = cv::norm(pt);
00138 pt.x /= nrm;
00139 pt.y /= nrm;
00140 pt.z /= nrm;
00141
00142 pt = pt * pc_trans.at<double>(ra, ca);
00143
00144 pcl::PointXYZRGB point;
00145 point.x = (float)pt.x;
00146 point.y = (float)pt.y;
00147 point.z = (float)pt.z;
00148
00149 uint32_t rgb = (static_cast<uint32_t>(img.at<cv::Vec3b>(ra, ca)[0]) << 16 | static_cast<uint32_t>(img.at<cv::Vec3b>(ra, ca)[1]) << 8
00150 | static_cast<uint32_t>(img.at<cv::Vec3b>(ra, ca)[2]));
00151 point.rgb = *reinterpret_cast<float*>(&rgb);
00152 pc.points.push_back(point);
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162 index++;
00163 }
00164 }
00165
00166 cv_bridge::CvImage cv_ptr;
00167 cv_ptr.image = img;
00168 cv_ptr.encoding = sensor_msgs::image_encodings::BGR8;
00169 out_img = *(cv_ptr.toImageMsg());
00170
00171 pcl::toROSMsg(pc, out_pc2);
00172
00173 }
00174
00175 void publish()
00176 {
00177 out_pc2.header.frame_id = "/camera/";
00178 out_pc2.header.stamp = ros::Time::now();
00179 scene_pub_.publish(out_pc2);
00180
00181 out_img.header.frame_id = "/camera/";
00182 out_img.header.stamp = ros::Time::now();
00183 img_pub_.publish(out_img);
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194 }
00195
00196 ros::NodeHandle n_;
00197 int persp;
00198 int shot;
00199
00200 protected:
00201 ros::Publisher scene_pub_;
00202 ros::Publisher img_pub_;
00203 sensor_msgs::PointCloud2 out_pc2;
00204 pcl::PointCloud<pcl::PointXYZRGB> pc;
00205 sensor_msgs::Image out_img;
00206 std::string file;
00207
00208 };
00209
00210 int main(int argc, char** argv)
00211 {
00212
00213 ros::init(argc, argv, "scene publisher");
00214 int persp_filter = 17;
00215
00216 scene_publisher sp;
00217
00218 ros::Rate loop_rate(1);
00219 while (ros::ok())
00220 {
00221 {
00222 sp.shot++;
00223 std::cout << "PERSPECTIVE:" << sp.persp << std::endl;
00224 if (sp.shot == 4)
00225 {
00226 sp.shot = 1;
00227 sp.persp++;
00228 }
00229 if (sp.persp == 18)
00230 break;
00231
00232 if (sp.persp == persp_filter)
00233 {
00234 sp.process();
00235 sp.publish();
00236 ros::spinOnce();
00237 loop_rate.sleep();
00238 }
00239 }
00240 }
00241 }