void D3DCloudProjector::projectCloud(int id, const sensor_msgs::PointCloud& data, const std::vector<int>& interest_region_indices) { MatrixXf& oriented = orienter_->oriented_clouds_[id]; // -- Get a copy of the projected points. MatrixXf projected(oriented.rows(), 2); int c=0; for(int i=0; i<3; ++i) { if(i == axis_of_projection_) continue; projected.col(c) = oriented.col(i); ++c; } // -- Transform into pixel units. projected is currently in meters, centered at 0. //projected *= pixels_per_meter_; for(int i=0; i<projected.rows(); ++i) { projected(i, 0) *= pixels_per_meter_; projected(i, 1) *= pixels_per_meter_; } // -- Find min and max of u and v. TODO: noise sensitivity? // u is the col number in the image plane, v is the row number. float min_v = FLT_MAX; float min_u = FLT_MAX; float max_v = -FLT_MAX; float max_u = -FLT_MAX; for(int i=0; i<projected.rows(); ++i) { float u = projected(i, 0); float v = projected(i, 1); if(u < min_u) min_u = u; if(u > max_u) max_u = u; if(v < min_v) min_v = v; if(v > max_v) max_v = v; } // -- Translate to coordinate system where (0,0) is the upper right of the image. for(int i=0; i<projected.rows(); ++i) { projected(i, 0) -= min_u; projected(i, 1) = max_v - projected(i, 1); } // -- Get the max depth. float max_depth = -FLT_MAX; float min_depth = FLT_MAX; for(int i=0; i<oriented.rows(); ++i) { if(oriented(i, axis_of_projection_) > max_depth) max_depth = oriented(i, axis_of_projection_); if(oriented(i, axis_of_projection_) < min_depth) min_depth = oriented(i, axis_of_projection_); } // -- Compute the normalized depths. Depths are between 0 and 1, with 1 meaning closest and 0 meaning furthest. VectorXf depths = oriented.col(axis_of_projection_); if(axis_of_projection_ == 1) depths = -depths; depths = depths.cwise() - depths.minCoeff(); depths = depths / depths.maxCoeff(); // -- Fill the IplImages. assert(sizeof(float) == 4); CvSize size = cvSize(ceil(max_u - min_u), ceil(max_v - min_v)); IplImage* acc = cvCreateImage(size, IPL_DEPTH_32F, 1); IplImage* intensity = cvCreateImage(size, IPL_DEPTH_32F, 1); IplImage* depth = cvCreateImage(size, IPL_DEPTH_32F, 1); cvSetZero(acc); cvSetZero(depth); cvSetZero(intensity); assert(projected.rows() == (int)interest_region_indices.size()); for(int i=0; i<projected.rows(); ++i) { int row = floor(projected(i, 1)); int col = floor(projected(i, 0)); // Update accumulator. assert(interest_region_indices[i] < (int)data.channels[0].values.size() && (int)interest_region_indices[i] >= 0); ((float*)(acc->imageData + row * acc->widthStep))[col]++; // Add to intensity values. float val = (float)data.channels[0].values[interest_region_indices[i]] / 255.0 * (3.0 / 4.0) + 0.25; assert(val <= 1.0 && val >= 0.0); ((float*)(intensity->imageData + row * intensity->widthStep))[col] += val; // Add to depth values. ((float*)(depth->imageData + row * depth->widthStep))[col] += depths(i); // } // -- Normalize by the number of points falling in each pixel. for(int v=0; v<acc->height; ++v) { float* intensity_ptr = (float*)(intensity->imageData + v * intensity->widthStep); float* depth_ptr = (float*)(depth->imageData + v * depth->widthStep); float* acc_ptr = (float*)(acc->imageData + v * acc->widthStep); for(int u=0; u<acc->width; ++u) { if(*acc_ptr == 0) { *intensity_ptr = 0; *depth_ptr = 0; } else { *intensity_ptr = *intensity_ptr / *acc_ptr; *depth_ptr = *depth_ptr / *acc_ptr; } intensity_ptr++; depth_ptr++; acc_ptr++; } } // -- Store images. depth_projections_.push_back(depth); intensity_projections_.push_back(intensity); // -- Debugging. if(debug_) { float scale = 10; IplImage* intensity_big = cvCreateImage(cvSize(((float)intensity->width)*scale, ((float)intensity->height)*scale), intensity->depth, intensity->nChannels); cvResize(intensity, intensity_big, CV_INTER_AREA); IplImage* depth_big = cvCreateImage(cvSize(((float)depth->width)*scale, ((float)depth->height)*scale), depth->depth, depth->nChannels); cvResize(depth, depth_big, CV_INTER_AREA); CVSHOW("Intensity Image", intensity_big); CVSHOW("Depth Image", depth_big); cvWaitKey(0); cvDestroyWindow("Intensity Image"); cvDestroyWindow("Depth Image"); } // -- Clean up. cvReleaseImage(&acc); }