00001
00034 #include <iostream>
00035 #include <fstream>
00036 #include <opencv/cv.h>
00037 #include <opencv/highgui.h>
00038 #include <string>
00039 #include <vector>
00040
00041 #include "Mask.h"
00042
00043 #include "DUtils.h"
00044 #include "DUtilsCV.h"
00045 #include "DVision.h"
00046
00047 typedef DVision::PixelPointFile::PixelPoint PixelPoint;
00048 typedef DVision::PMVS::PatchFile PatchFile;
00049 typedef DVision::PMVS::PatchFile::Patch Patch;
00050 typedef DVision::PMVS::CameraFile CameraFile;
00051 typedef DVision::PMVS::CameraFile::Camera Camera;
00052 typedef DVision::PMVS::PLYFile PLYFile;
00053 typedef DVision::PMVS::PLYFile::PLYPoint PLYPoint;
00054
00055 using namespace std;
00056
00057
00058
00059
00060 void treatAll(const std::vector<PLYPoint> &allplypoints,
00061 const std::vector<Camera> &cameras,
00062 const std::vector<std::vector<int> > &visibility,
00063 const std::vector<Mask> &masks,
00064 std::vector<PLYPoint> &object_plypoints);
00065
00066 void loadMasks(const std::string &mask_dir, int N,
00067 std::vector<Mask> &masks);
00068
00069 void treatAll(const std::vector<PLYPoint> &allplypoints,
00070 const std::vector<Camera> &cameras,
00071 const std::vector<Patch> &patches,
00072 const std::vector<Mask> &masks,
00073 std::vector<PLYPoint> &object_plypoints,
00074 std::vector<Patch> &object_patches);
00075
00076
00077
00078 int main(int argc, char *argv[])
00079 {
00080
00081 if(argc < 7)
00082 {
00083 cout << "Usage: " << argv[0] << " <complete ply file> <patch file> "
00084 "<camera dir> <mask dir> <out ply file> <out patch file>"
00085 << endl;
00086 return 1;
00087 }
00088
00089 string ply_in_file = argv[1];
00090 string patch_file = argv[2];
00091 string camera_dir = argv[3];
00092 string mask_dir = argv[4];
00093 string ply_out_file = argv[5];
00094 string patch_out_file = argv[6];
00095
00096 try
00097 {
00098
00099 cout << "Reading data..." << endl;
00100
00101 vector<PLYPoint> allplypoints;
00102 PLYFile::readFile(ply_in_file, allplypoints);
00103
00104 cout << "-- " << allplypoints.size() << " PLY points read" << endl;
00105
00106 vector<Camera> cameras;
00107 CameraFile::readFile(camera_dir, cameras);
00108
00109 cout << "-- " << cameras.size() << " cameras read" << endl;
00110
00111
00112
00113 vector<Patch> patches;
00114 PatchFile::readFile(patch_file, patches);
00115
00116 cout << "-- " << patches.size() << " patches read" << endl;
00117
00118 cout << "Selecting points..." << endl;
00119
00120 vector<Mask> masks;
00121 loadMasks(mask_dir, cameras.size(), masks);
00122
00123 cout << "-- " << masks.size() << " masks read" << endl;
00124
00125 vector<PLYPoint> object_plypoints;
00126 vector<Patch> object_patches;
00127
00128
00129 treatAll(allplypoints, cameras, patches, masks, object_plypoints,
00130 object_patches);
00131
00132 cout << "Saving..." << endl;
00133
00134 PLYFile::saveFile(ply_out_file, object_plypoints);
00135 PatchFile::saveFile(patch_out_file, object_patches);
00136
00137 }catch(std::string ex)
00138 {
00139 cout << ex << endl;
00140 }
00141
00142 return 0;
00143
00144 }
00145
00146
00147
00148 void loadMasks(const std::string &mask_dir, int N,
00149 std::vector<Mask> &masks)
00150 {
00151 const int MARGIN = 1;
00152 char buffer[1024];
00153 masks.reserve(N);
00154
00155 for(int i = 0; i < N; ++i)
00156 {
00157 sprintf(buffer, "%s/im%02d_mask.png", mask_dir.c_str(), i);
00158 masks.push_back(Mask(buffer));
00159
00160 if(masks.back().empty())
00161 {
00162 throw string("Could not read mask file ") + buffer;
00163 }
00164
00165 masks.back().shrink(MARGIN);
00166 }
00167 }
00168
00169
00170
00171
00172 void treatAll(const std::vector<PLYPoint> &allplypoints,
00173 const std::vector<Camera> &cameras,
00174 const std::vector<std::vector<int> > &visibility,
00175 const std::vector<Mask> &masks,
00176 std::vector<PLYPoint> &object_plypoints)
00177 {
00178 object_plypoints.clear();
00179 object_plypoints.reserve(allplypoints.size());
00180
00181 for(unsigned int i = 0; i < visibility.size(); ++i)
00182 {
00183
00184 const Camera& camera = cameras[i];
00185 const std::vector<int> &visible = visibility[i];
00186 const Mask &mask = masks[i];
00187
00188 for(unsigned int j = 0; j < visible.size(); ++j)
00189 {
00190 int idx = visible[j];
00191 const PLYPoint& p3d = allplypoints[idx];
00192
00193 cv::Mat X = (cv::Mat_<double>(4, 1) <<
00194 p3d.x, p3d.y, p3d.z, 1.);
00195
00196 cv::Mat p = camera.P * X;
00197
00198 int x = int(p.at<double>(0,0) / p.at<double>(2,0));
00199 int y = int(p.at<double>(1,0) / p.at<double>(2,0));
00200
00201
00202 if(mask.test(x, y))
00203 {
00204 object_plypoints.push_back(p3d);
00205 }
00206 }
00207 }
00208 }
00209
00210
00211
00212 void treatAll(const std::vector<PLYPoint> &allplypoints,
00213 const std::vector<Camera> &cameras,
00214 const std::vector<Patch> &patches,
00215 const std::vector<Mask> &masks,
00216 std::vector<PLYPoint> &object_plypoints,
00217 std::vector<Patch> &object_patches)
00218 {
00219 object_plypoints.clear();
00220 object_plypoints.reserve(allplypoints.size());
00221
00222 object_patches.clear();
00223 object_patches.reserve(allplypoints.size());
00224
00225 for(unsigned int idx = 0; idx < patches.size(); ++idx)
00226 {
00227 const Patch& patch = patches[idx];
00228 const PLYPoint& p3d = allplypoints[idx];
00229
00230 bool ok = true;
00231 for(unsigned int j = 0; j < patch.strong_visibility_list.size(); ++j)
00232 {
00233 int img_idx = patch.strong_visibility_list[j];
00234 const Camera& camera = cameras[img_idx];
00235 const Mask& mask = masks[img_idx];
00236
00237 cv::Mat X = (cv::Mat_<double>(4, 1) <<
00238 p3d.x, p3d.y, p3d.z, 1.);
00239
00240 cv::Mat p = camera.P * X;
00241
00242 int x = int(p.at<double>(0,0) / p.at<double>(2,0));
00243 int y = int(p.at<double>(1,0) / p.at<double>(2,0));
00244
00245
00246 if(!mask.test(x, y))
00247 {
00248 ok = false;
00249 break;
00250 }
00251 }
00252
00253 if(ok)
00254 {
00255
00256 object_plypoints.push_back(p3d);
00257 object_patches.push_back(patch);
00258 }
00259 }
00260
00261 }
00262
00263
00264