Blame view

src/3dReconst.cpp 4.64 KB
b0bb08d1c   tristan   init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
  #include "3dReconst.h"
  #include "Autocalib.h"
  
  
  //rectifier epipolar Lines and image
  vector<cv::Mat> Reconst::RectificaEpipolarLines(vector<cv::Mat> image, vector<vector<cv::Point2d> > MeasureVector, vector<cv::Mat> fundMatrix)
  {
  	vector<cv::Mat> outImage;
  	vector<cv::Mat> tempImage=image;
  	vector<vector<cv::Point2d> > tempMeasureVector=MeasureVector;
  	
  	cv::Mat Frect = (cv::Mat_<double>(3,3) << 0,0,0,0,0,-1.0,0,1.0,0);
  	cout << Frect << endl;
  	Autocalib calib;
  	
  	for (int i=0;i<tempImage.size()-1;i++)
  	{
  		RectifierAffine *rec = new RectifierAffine();
  		rec->init(&tempImage[i],&tempImage[i+1],&fundMatrix[i],&MeasureVector[i],&MeasureVector[i+1]);
  		rec->rectify();
  		
  		cv::Mat input1, input2;
  		rec->getResult(input1, input2,&MeasureVector[i],&MeasureVector[i+1]);
  		outImage.push_back(input1);
  		outImage.push_back(input2);		
  	
  		cv::Mat retifImg; 
  		retifImg=calib.plotStereoWithEpilines(outImage[2*i],outImage[2*i+1],Frect,MeasureVector[i],MeasureVector[i+1]);
  		cv::namedWindow( "Epipolar lines: rectified", cv::WINDOW_NORMAL );
  		cv::imshow("Epipolar lines: rectified", retifImg);
  
  		tempMeasureVector[i+1].swap(MeasureVector[i+1]);
  	
  		cv::waitKey(0);
  	}
  	return outImage;
  }
  
  //get disparity map and the dense point
  void Reconst::disparity(vector<cv::Mat> image)
  {
  	DenseMatcher DM;
  	for (int i=0;i<image.size()/2;i++)
  	{
  		DM.init(&image[2*i],&image[2*i+1]);
  		DM.calculateDisparityMap();
  		DM.plotDisparityMap();
  
  		cv::Mat densePt=DM.getDensePoint();
  		_densePoint.push_back(densePt);
  		
  	}
  }
  
  
  //do the triangulation and get 3D point cloud 
  void Reconst::GetPointCloud(vector<cv::Mat> P, cv::Mat image )
  {
  	
          vector<cv::Mat> rectifiedMeseureMatrice=_densePoint;
  	vector<cv::Mat> cameraMatrix=GetCameraMatrix(P);
          //cout<<rectifiedMeseureMatrice[1].cols<< endl;
  	pcl::PointCloud<pcl::PointXYZ> allPointCloud;
  	Triangulator triang;
  	for (int i=0;i<cameraMatrix.size();i++)
  	{
  		pcl::PointCloud<pcl::PointXYZ> ptCloud;
  		ptCloud=triang.triangulatePoints_LinearLS(_densePoint[i], cameraMatrix[i]);
                  
                  //ptCloud.is_dense = true;
  		int nbr=i;
  		//save 3D point cloud in a file 
  		string name = "pointCloud"+to_string(nbr)+".pcd"; 
  		pcl::io::savePCDFileASCII (name, ptCloud);
    		cerr << "Saved " << ptCloud.points.size () << " data points to pointCloud" <<i<<".pcd." << endl;
  	}
  }
  
  
  vector<cv::Mat> Reconst::GetCameraMatrix(vector<cv::Mat> P)
  {
  	vector<cv::Mat> CameraMatrix;
  	CameraMatrix.resize(P.size()-1);
  	for(int i=0;i<P.size()-1;i++) 
  	{
  		CameraMatrix[i].push_back(P[i]);
  		CameraMatrix[i].push_back(P[i+1]);
  	} 
  	return CameraMatrix;
  }
  
  
  void Reconst::filterPointCloud(int taille)
  {
       	for(int i=0;i<taille;i++ )
  	{
  		std::stringstream ss1;
          	ss1 << "pointCloud" <<std::to_string(i)<<".pcd" ; 
  		pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
  		
  		pcl::PCDWriter writer;
  
    		// Fill in the cloud data
    		pcl::PCDReader reader;
    		// Replace the path below with the path where you saved your file
    		reader.read<pcl::PointXYZ> (ss1.str (), *cloud);
  
    		std::cerr << "Cloud before filtering: " << std::endl;
    		std::cerr << *cloud << std::endl;
  
   		// Creating the KdTree object for the search method of the extraction
    		pcl::search::KdTree<pcl::PointXYZ>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZ>);
    		tree->setInputCloud (cloud);
  
    		std::vector<pcl::PointIndices> cluster_indices;
    		pcl::EuclideanClusterExtraction<pcl::PointXYZ> ec;
    		ec.setClusterTolerance (8); // 2cm
    		ec.setMinClusterSize (30);
   		ec.setMaxClusterSize (1000000);
   		ec.setSearchMethod (tree);
    		ec.setInputCloud (cloud);
    		ec.extract (cluster_indices);
  
    		if(cluster_indices.size()==0){ std::cout<<"no data"<<std::endl; }
  
    		
    		int cloudSize=0;
    		for (std::vector<pcl::PointIndices>::const_iterator it = cluster_indices.begin (); it != cluster_indices.end (); ++it)
    		{
      			pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_cluster (new pcl::PointCloud<pcl::PointXYZ>);
      			for (std::vector<int>::const_iterator pit = it->indices.begin (); pit != it->indices.end (); ++pit)
        			cloud_cluster->points.push_back (cloud->points[*pit]); //*
     			cloud_cluster->width = cloud_cluster->points.size ();
     	 		cloud_cluster->height = 1;
     	 		cloud_cluster->is_dense = true;
      			if(cloud_cluster->points.size ()>cloudSize)
     			{
      				std::cout << "PointCloud representing the Cluster: " << cloud_cluster->points.size () << " data points." << std::endl;
      				std::stringstream ss2;
      				ss2 << "pointCloud_filtered" <<std::to_string(i)<< ".pcd";
      				writer.write<pcl::PointXYZ> (ss2.str (), *cloud_cluster, false);
      				cloudSize=cloud_cluster->points.size ();
      			} //*
      			
    		}
  	
  	}
  
  
  }