00001
00002
00003
00004
00005
00006 #include "ros/ros.h"
00007 #include "std_msgs/String.h"
00008 #include <pcl_ros/point_cloud.h>
00009 #include <sensor_msgs/PointCloud.h>
00010 #include <pcl/point_types.h>
00011 #include <boost/foreach.hpp>
00012 #include <boost/progress.hpp>
00013 #include <geometry_msgs/PoseStamped.h>
00014
00015 #include <image_transport/image_transport.h>
00016 #include <cv_bridge/cv_bridge.h>
00017 #include <sensor_msgs/image_encodings.h>
00018 #include <opencv2/imgproc/imgproc.hpp>
00019 #include <opencv2/highgui/highgui.hpp>
00020
00021 #include <iostream>
00022 #include <fstream>
00023 #include <boost/foreach.hpp>
00024 #include "Optimal_affine_tracking_3d16_fast_realtime.h"
00025 #include "Optimal_affine_tracking_3d16_fast_realtime_initialize.h"
00026 #include "Optimal_affine_tracking_3d16_fast_realtime_terminate.h"
00027
00028 #include "rt_nonfinite.h"
00029 #include "compute_prob1.h"
00030 #include "LoadKinectMesh_realtime.h"
00031 #include "init_variables.h"
00032
00033 #include <vector>
00034 #include <algorithm>
00035
00036
00037 ros::Publisher g_pose_publisher;
00038
00039 real_T Ixyz[504063];
00040 real_T corner_p[12];
00041 static real_T mean_img[3888];
00042 static real_T point_matrix[3888];
00043 static real_T AR_velocity[2250000];
00044 static real_T X_par[2250000];
00045 static real_T dw_dp[15552];
00046 static real_T tracked_images[38880000];
00047 static real_T X_par_pred[2250000];
00048 real_T centroid[3];
00049 int32_T t;
00050 real_T Aff_matrix[9];
00051 int32_T i0;
00052 int32_T i1;
00053 int32_T i2;
00054 int32_T row;
00055 int32_T column;
00056 int i;
00057 typedef pcl::PointCloud<pcl::PointXYZRGB> PointCloud;
00058 real_T p[12];
00059 cv::Rect region_of_interest = cv::Rect(100, 50, 441, 381);
00060 cv::Mat color_img;
00061 cv::Mat xyz_img;
00062 cv::Mat color_img_roi;
00063 cv::Mat xyz_img_roi;
00064 static int c_x=0,c_y=0;
00065 static float v_x=0,v_y=0;
00066 int mouse_loc_x[4];
00067 int mouse_loc_y[4];
00068 int mosue_click_time=0;
00069 real_T center_y;
00070 real_T center_x;
00071 double xx=0,yy=0,zz=0,dxy=0,xx_C=0,yy_C=0,zz_C=0;
00072
00073 void onMouse(int event, int x, int y, int flags, void* data)
00074 {
00075 if (event != cv::EVENT_LBUTTONDOWN)
00076 return;
00077 mouse_loc_x[mosue_click_time] = x;
00078 mouse_loc_y[mosue_click_time] = y;
00079
00080
00081 printf("%d %d\n",mouse_loc_x[mosue_click_time],mouse_loc_y[mosue_click_time]);
00082 ++mosue_click_time;
00083 }
00084
00085
00086 void processPointCloud(const PointCloud::ConstPtr& msg) {
00087
00088 boost::progress_timer timer;
00089
00090 float x,y,z,ptrgb;
00091 uint8_t r,g,b;
00092 uint32_t rgb;
00093 i=0;
00094
00095 color_img = cv::Mat(msg->height, msg->width, CV_8UC3);
00096 xyz_img = cv::Mat(msg->height, msg->width, CV_32FC3);
00097
00098 BOOST_FOREACH (const pcl::PointXYZRGB& pt, msg->points)
00099 {
00100 ptrgb = pt.rgb;
00101 rgb = *reinterpret_cast<int*>(&ptrgb);
00102 r = (rgb >> 16) & 0x0000ff;
00103 g = (rgb >> 8) & 0x0000ff;
00104 b = (rgb) & 0x0000ff;
00105 x = (float)pt.x;
00106 y = (float)pt.y;
00107 z = (float)pt.z;
00108
00109 if (x != x || y != y || z != z )
00110 { x=0; y=0; z=0; r=0; g=0; b=0;
00111 }
00112 column = (int)(i / msg->width);
00113 row = i % (msg->width);
00114
00115 color_img.at<cv::Vec3b>(column,row)[0] = b;
00116 color_img.at<cv::Vec3b>(column,row)[1] = g;
00117 color_img.at<cv::Vec3b>(column,row)[2] = r;
00118 xyz_img.at<cv::Vec3f>(column,row)[0] = x;
00119 xyz_img.at<cv::Vec3f>(column,row)[1] = y;
00120 xyz_img.at<cv::Vec3f>(column,row)[2] = z;
00121 ++i;
00122 }
00123
00124 color_img_roi = color_img(region_of_interest);
00125 xyz_img_roi = xyz_img(region_of_interest);
00126
00127 static int callback_counter_ = 0;
00128 int k,i3;
00129 if (callback_counter_ == 0) {
00130
00131
00132 for (k = 0; k < 441; k++) {
00133 for (i3 = 0; i3 < 381; i3++) {
00134 Ixyz[i3 + 381 * k] = xyz_img_roi.at<cv::Vec3f>(i3,k)[0];
00135 Ixyz[168021 + (i3 + 381 * k)] = xyz_img_roi.at<cv::Vec3f>(i3,k)[1];
00136 Ixyz[336042 + (i3 + 381 * k)] = xyz_img_roi.at<cv::Vec3f>(i3,k)[2];
00137 }
00138 }
00139 real_T ptx[4];
00140 real_T pty[4];
00141
00142 cvNamedWindow("color image", CV_WINDOW_KEEPRATIO);
00143 cv::imshow( "color image", color_img_roi );
00144 std::vector<cv::Mat> split_xyz(3,cv::Mat(xyz_img_roi.size(),CV_64FC1));
00145 cv::split(xyz_img_roi, split_xyz);
00146 static double min;
00147 static double max;
00148 cv::minMaxIdx(split_xyz.at(2), &min, &max);
00149 static cv::Mat adjMap;
00150 cv::convertScaleAbs(split_xyz.at(2), adjMap, 255 / max);
00151 cvNamedWindow("depth image", CV_WINDOW_KEEPRATIO);
00152 cv::imshow( "depth image", adjMap );
00153 cv::setMouseCallback("depth image", onMouse);
00154 cv::waitKey(0);
00155
00156 ptx[0] = (double)mouse_loc_y[0];
00157 ptx[1] = (double)mouse_loc_y[1];
00158 ptx[2] = (double)mouse_loc_y[2];
00159 ptx[3] = (double)mouse_loc_y[3];
00160
00161 pty[0] = (double)mouse_loc_x[0];
00162 pty[1] = (double)mouse_loc_x[1];
00163 pty[2] = (double)mouse_loc_x[2];
00164 pty[3] = (double)mouse_loc_x[3];
00165
00166 init_variables(Ixyz, ptx, pty, X_par_pred, tracked_images, dw_dp, X_par,
00167 AR_velocity, point_matrix, mean_img, corner_p, ¢er_x,
00168 ¢er_y);
00169 ++callback_counter_;
00170
00171 }
00172 else {
00173
00174
00175 for (k = 0; k < 441; k++) {
00176 for (i3 = 0; i3 < 381; i3++) {
00177 Ixyz[i3 + 381 * k] = xyz_img_roi.at<cv::Vec3f>(i3,k)[0];
00178 Ixyz[168021 + (i3 + 381 * k)] = xyz_img_roi.at<cv::Vec3f>(i3,k)[1];
00179 Ixyz[336042 + (i3 + 381 * k)] = xyz_img_roi.at<cv::Vec3f>(i3,k)[2];
00180 }
00181 }
00182
00183 compute_prob1(X_par, X_par_pred, AR_velocity, dw_dp, (real_T)t + 2.0,
00184 center_x, center_y, Ixyz, point_matrix, mean_img,
00185 tracked_images, Aff_matrix, centroid);
00186 ++t;
00187
00188
00189 for (i0 = 0; i0 < 3; i0++) {
00190 for (i1 = 0; i1 < 4; i1++) {
00191 p[i0 + 3 * i1] = 0.0;
00192 for (i2 = 0; i2 < 3; i2++) {
00193 p[i0 + 3 * i1] += Aff_matrix[i0 + 3 * i2] * corner_p[i2 + 3 * i1];
00194 }
00195 }
00196 }
00197 ++callback_counter_;
00198 }
00199
00200 geometry_msgs::PoseStamped pose_stamped;
00201 pose_stamped.header.frame_id = "/camera_rgb_optical_frame";
00202 pose_stamped.header.stamp.sec = callback_counter_;
00203
00204
00205 c_x = (int) (p[4]+p[7]+p[10]+p[1])/4;
00206 c_y = (int) (p[3]+p[6]+p[9]+p[0])/4;
00207
00208 cv::line(color_img_roi, cv::Point(p[4],p[3]), cv::Point(p[1],p[0]), cv::Scalar(0, 0, 255), 1, CV_AA);
00209 cv::line(color_img_roi, cv::Point(p[7],p[6]), cv::Point(p[4],p[3]), cv::Scalar(0, 0, 255), 1, CV_AA);
00210 cv::line(color_img_roi, cv::Point(p[10],p[9]), cv::Point(p[7],p[6]), cv::Scalar(0, 0, 255), 1, CV_AA);
00211 cv::line(color_img_roi, cv::Point(p[1],p[0]), cv::Point(p[10],p[9]), cv::Scalar(0, 0, 255), 1, CV_AA);
00212 cv::line(color_img_roi, cv::Point(c_x,c_y), cv::Point((p[7]+p[4])/2,(p[6]+p[3])/2), cv::Scalar(0, 0, 255), 1, CV_AA);
00213
00214 v_x = c_x-((p[7]+p[4])/2);
00215 v_y = c_y-((p[6]+p[3])/2);
00216
00217
00218
00219 cvNamedWindow("color image", CV_WINDOW_KEEPRATIO);
00220 cv::imshow( "color image", color_img_roi );
00221
00222
00223 std::vector<cv::Mat> split_xyz(3,cv::Mat(xyz_img_roi.size(),CV_64FC1));
00224 cv::split(xyz_img_roi, split_xyz);
00225 static double min;
00226 static double max;
00227 cv::minMaxIdx(split_xyz.at(2), &min, &max);
00228 static cv::Mat adjMap;
00229 cv::convertScaleAbs(split_xyz.at(2), adjMap, 255 / max);
00230 cv::line(adjMap, cv::Point(p[4],p[3]), cv::Point(p[1],p[0]), cv::Scalar(0, 0, 255), 1, CV_AA);
00231 cv::line(adjMap, cv::Point(p[7],p[6]), cv::Point(p[4],p[3]), cv::Scalar(0, 0, 255), 1, CV_AA);
00232 cv::line(adjMap, cv::Point(p[10],p[9]), cv::Point(p[7],p[6]), cv::Scalar(0, 0, 255), 1, CV_AA);
00233 cv::line(adjMap, cv::Point(p[1],p[0]), cv::Point(p[10],p[9]), cv::Scalar(0, 0, 255), 1, CV_AA);
00234 cv::line(adjMap, cv::Point(c_x,c_y), cv::Point((p[7]+p[4])/2,(p[6]+p[3])/2), cv::Scalar(0, 0, 255), 1, CV_AA);
00235 cvNamedWindow("depth image", CV_WINDOW_KEEPRATIO);
00236 cv::imshow( "depth image", adjMap );
00237 cv::waitKey(1);
00238
00239
00240 pose_stamped.pose.position.x = centroid[0]-2;
00241 pose_stamped.pose.position.y = centroid[1]-2;
00242 pose_stamped.pose.position.z = centroid[2];
00243 pose_stamped.pose.orientation.x = atan(v_y/v_x);
00244
00245
00246 cv::Point pt_C((p[4]+p[7]+p[10]+p[1])/4,(p[3]+p[6]+p[9]+p[0])/4);
00247 xx_C = xyz_img_roi.at<cv::Vec3f>(pt_C)[0];
00248 yy_C = xyz_img_roi.at<cv::Vec3f>(pt_C)[1];
00249 zz_C = xyz_img_roi.at<cv::Vec3f>(pt_C)[2];
00250 cv::Point pt((p[7]+p[4])/2,(p[6]+p[3])/2);
00251 xx = xyz_img_roi.at<cv::Vec3f>(pt)[0];
00252 yy = xyz_img_roi.at<cv::Vec3f>(pt)[1];
00253 zz = xyz_img_roi.at<cv::Vec3f>(pt)[2];
00254 dxy = pow(xx-xx_C,2)+pow(yy-yy_C,2);
00255 pose_stamped.pose.orientation.z = atan((zz-zz_C)/(sqrt(dxy)));
00256
00257 g_pose_publisher.publish(pose_stamped);
00258
00259 }
00260
00261 int main(int argc, char **argv)
00262 {
00263 ros::init(argc, argv, "depth_tracker");
00264 ros::NodeHandle n;
00265 Optimal_affine_tracking_3d16_fast_realtime_initialize();
00266 g_pose_publisher = n.advertise<geometry_msgs::PoseStamped>("pose_surface", 50);
00267
00268
00269 ros::Subscriber sub = n.subscribe<PointCloud>("/camera/depth_registered/points", 5, processPointCloud);
00270
00271 Optimal_affine_tracking_3d16_fast_realtime_terminate();
00272 ros::spin();
00273 return 0;
00274 }