intmain(int argc,char **argv) { cv:: Mat image = cv::imread(argv[1]); if(image.data == nullptr) { cout<<"Error:the image file may not exist"<<endl; return0; } //output some basic information cout<<"Width:"<<image.cols<<" height:"<<image.rows<<" channel number:"<<image.channels()<<endl; cv::imshow("image",image); cv::waitKey(0); // judge the type of the image if(image.type()!=CV_8UC1 && image.type()!=CV_8UC3) { cout<<"the image must be rgb image or grey-scale map"<<endl; return0; } //iteration //use chrono to compute the time. chrono::steady_clock::time_point t1 = chrono::steady_clock::now(); for(size_t y=0;y<image.cols;++y) { for (size_t x = 0;x<image.rows;++x) { //left the ptr point at the y row. unsignedchar* row_ptr = image.ptr<unsignedchar>(y); //get position (x,y)'s ptr,because there are image.channels()*1 char. unsignedchar *data_ptr = &row_ptr[x*image.channels()];
for (int c = 0;c!=image.channels();++c) { //Do someting ; } } } chrono::steady_clock::time_point t2 = chrono::steady_clock::now(); chrono::duration<double> time_used = chrono::duration_cast<chrono::duration<double>>(t2-t1); cout<<"We use "<<time_used.count()<<" seconds to scan the image"<<endl;
// copy or reference //this is a reference,or ptr, so if we change the image_another,the original image will be changed cv::Mat image_another = image; //let the left top corner(100,100) block to be 0 image_another( cv::Rect(0,0,100,100)).setTo(0); cv::imshow("image",image); cv::waitKey(0);
intmain(int argc,char **argv) { vector<cv::Mat> colorImgs,depthImgs; vector<Eigen::Isometry3d,Eigen::aligned_allocator<Eigen::Isometry3d>> poses;//poses of camera ifstream fin(argv[2]); if(!fin) { cout<<"We need information about poses"<<endl; return0; } for(int i = 0;i!=5;++i) { boost::format fmt(argv[1]+string("/%s/%d.%s"));//format of image files colorImgs.push_back(cv::imread((fmt%"color"%(i+1)%"png").str())); depthImgs.push_back(cv::imread((fmt%"depth"%(i+1)%"pgm").str(),-1));
double data[7] = {0}; //get poses to data for(auto &d:data) fin>>d; Eigen::Quaterniond q(data[6],data[3],data[4],data[5]); Eigen::Isometry3d T(q); T.pretranslate(Eigen::Vector3d(data[0],data[1],data[2])); poses.push_back(T); } double cx = 325.5; double cy=253.5; double fx = 518.0; double fy = 519.0; double depthScale = 1000.0; cout<<"try to get pointcloud..."<<endl; //use XYZRGB format typedef pcl::PointXYZRGB PointT; typedef pcl:: PointCloud<PointT> PointCloud;
PointCloud::Ptr pointCloud(new PointCloud()); for(int i = 0;i<5;++i) { cout<<"transform the images..."<<i<<endl; cv::Mat color = colorImgs[i]; cv::Mat depth = depthImgs[i]; Eigen::Isometry3d T = poses[i]; for (int v = 0;v<color.rows;++v) for (int u = 0;u<color.cols;++u) { unsignedint d = depth.ptr<unsignedshort>(v)[u];//get depth of the pixel unsignedchar* cptr = color.ptr<unsignedchar>(v); if(d == 0) continue; Eigen::Vector3d point; point[2] = double(d) / depthScale; point[0] = (u - cx)*point[2]/fx; point[1] = (v - cy)*point[2]/fy; Eigen::Vector3d pointWorld = T*point;