[Bf-blender-cvs] [4a1fdb4] soc-2016-multiview: update autotrack two-view reconstrution

Tianwei Shen noreply at git.blender.org
Tue Jun 14 11:01:57 CEST 2016


Commit: 4a1fdb48eb1914c54042b76b16e0f7fd099d3bba
Author: Tianwei Shen
Date:   Tue Jun 14 17:01:38 2016 +0800
Branches: soc-2016-multiview
https://developer.blender.org/rB4a1fdb48eb1914c54042b76b16e0f7fd099d3bba

update autotrack two-view reconstrution

===================================================================

M	intern/libmv/intern/reconstructionN.cc
M	intern/libmv/libmv/autotrack/reconstruction.cc
M	intern/libmv/libmv/autotrack/reconstruction.h

===================================================================

diff --git a/intern/libmv/intern/reconstructionN.cc b/intern/libmv/intern/reconstructionN.cc
index 61954cc..f625833 100644
--- a/intern/libmv/intern/reconstructionN.cc
+++ b/intern/libmv/intern/reconstructionN.cc
@@ -177,7 +177,9 @@ libmv_ReconstructionN** libmv_solveMultiviewReconstruction(const int clip_num,
 
 	///* TODO(tianwei): chain the tracks and correspondences */
 
-	if(!mv::ReconstructTwoFrames(keyframe_markers, &reconstruction)) {
+	// reconstruct two views from the main clip
+	if(!mv::ReconstructTwoFrames(keyframe_markers, 0, *(all_libmv_reconstruction[0]->intrinsics), &reconstruction))
+	{
 		printf("mv::ReconstrucTwoFrames failed\n");
 		for(int i = 0; i < clip_num; i++)
 			all_libmv_reconstruction[i]->is_valid = false;
diff --git a/intern/libmv/libmv/autotrack/reconstruction.cc b/intern/libmv/libmv/autotrack/reconstruction.cc
index 73fc02c..611191d 100644
--- a/intern/libmv/libmv/autotrack/reconstruction.cc
+++ b/intern/libmv/libmv/autotrack/reconstruction.cc
@@ -21,6 +21,7 @@
 // Author: shentianweipku at gmail.com (Tianwei Shen)
 
 #include "libmv/autotrack/reconstruction.h"
+#include "libmv/multiview/fundamental.h"
 #include "libmv/autotrack/marker.h"
 #include "libmv/autotrack/tracks.h"
 #include "libmv/numeric/numeric.h"
@@ -28,10 +29,51 @@
 
 using mv::Marker;
 using mv::Tracks;
+using libmv::Mat;
+using libmv::Mat2;
+using libmv::Mat3;
+using libmv::Vec;
+using libmv::Vec2;
 
 namespace mv {
 
+void GetFramesInMarkers(const vector<Marker> &markers,
+                        int *image1, int *image2) {
+	if (markers.size() < 2) {
+		return;
+	}
+	*image1 = markers[0].frame;
+	for (int i = 1; i < markers.size(); ++i) {
+		if (markers[i].frame != *image1) {
+			*image2 = markers[i].frame;
+			return;
+		}
+	}
+	*image2 = -1;
+	LOG(FATAL) << "Only one image in the markers.";
+}
+
+void CoordinatesForMarkersInFrame(const vector<Marker> &markers,
+                                  int clip, int frame,
+                                  Mat *coordinates) {
+	vector<Vec2> coords;
+	for (int i = 0; i < markers.size(); ++i) {
+		const Marker &marker = markers[i];
+		if (markers[i].clip == clip && markers[i].frame == frame) {
+			coords.push_back(Vec2(marker.center[0], marker.center[1]));
+		}
+	}
+	coordinates->resize(2, coords.size());
+	for (int i = 0; i < coords.size(); i++) {
+		coordinates->col(i) = coords[i];
+	}
+}
+/* markers come from two views in the same clip,
+ * reconstruction should be empty
+ */
 bool ReconstructTwoFrames(const vector<Marker> &markers,
+                          const int clip,
+                          libmv::CameraIntrinsics &cam_intrinsics,
                           Reconstruction *reconstruction)
 {
 	if (markers.size() < 16) {
@@ -39,40 +81,63 @@ bool ReconstructTwoFrames(const vector<Marker> &markers,
 		return false;
 	}
 
-	int image1, image2;
-	//GetImagesInMarkers(markers, &image1, &image2);
+	int frame1, frame2;
+	GetFramesInMarkers(markers, &frame1, &frame2);
 
-	//Mat x1, x2;
-	//CoordinatesForMarkersInImage(markers, image1, &x1);
-	//CoordinatesForMarkersInImage(markers, image2, &x2);
+	Mat x1, x2;
+	CoordinatesForMarkersInFrame(markers, clip, frame1, &x1);
+	CoordinatesForMarkersInFrame(markers, clip, frame2, &x2);
 
-	//Mat3 F;
-	//NormalizedEightPointSolver(x1, x2, &F);
+	Mat3 F;
+	libmv::NormalizedEightPointSolver(x1, x2, &F);
 
-	//// The F matrix should be an E matrix, but squash it just to be sure.
-	//Mat3 E;
-	//FundamentalToEssential(F, &E);
+	// The F matrix should be an E matrix, but squash it just to be sure.
+	Mat3 E;
+	libmv::FundamentalToEssential(F, &E);
 
-	//// Recover motion between the two images. Since this function assumes a
-	//// calibrated camera, use the identity for K.
-	//Mat3 R;
-	//Vec3 t;
-	//Mat3 K = Mat3::Identity();
-	//if (!MotionFromEssentialAndCorrespondence(E,
-	//                                          K, x1.col(0),
-	//                                          K, x2.col(0),
-	//                                          &R, &t)) {
-	//	LG << "Failed to compute R and t from E and K.";
-	//	return false;
-	//}
+	// Recover motion between the two images. Since this function assumes a
+	// calibrated camera, use the identity for K.
+	Mat3 R;
+	Vec3 t;
+	Mat3 K = Mat3::Identity();
+	if (!libmv::MotionFromEssentialAndCorrespondence(E, K, x1.col(0), K, x2.col(0), &R, &t))
+	{
+		LG << "Failed to compute R and t from E and K.";
+		return false;
+	}
 
-	//// Image 1 gets the reference frame, image 2 gets the relative motion.
-	//reconstruction->InsertCamera(image1, Mat3::Identity(), Vec3::Zero());
-	//reconstruction->InsertCamera(image2, R, t);
+	// frame 1 gets the reference frame, frame 2 gets the relative motion.
+	reconstruction->AddCameraIntrinsics(&cam_intrinsics, clip);
+	CameraPose pose1(clip, frame1, clip, Mat3::Identity(), Vec3::Zero());
+	CameraPose pose2(clip, frame2, clip, R, t);
+	reconstruction->AddCameraPose(pose1);
+	reconstruction->AddCameraPose(pose2);
 
-	//LG << "From two frame reconstruction got:\nR:\n" << R
-	//   << "\nt:" << t.transpose();
+	LG << "From two frame reconstruction got:\nR:\n" << R
+	   << "\nt:" << t.transpose();
 	return true;
 }
 
+void Reconstruction::AddCameraPose(const CameraPose& pose)
+{
+	if(camera_poses_.size() < pose.clip + 1)
+		camera_poses_.resize(pose.clip+1);
+	camera_poses_[pose.clip].push_back(pose);
+}
+
+/**
+ * @brief Reconstruction::AddCameraIntrinsics insert camera intrinsics into Reconstruction by index,
+ * overwrite the old intrinsic if there is any, resize the CameraIntrinsics vector if the size is smaller
+ * than index.
+ * @param intrinsics_ptr: camera intrinsic pointer
+ * @param intrisic_index: index
+ * @return
+ */
+void Reconstruction::AddCameraIntrinsics(CameraIntrinsics* intrinsics_ptr, const int intrinsic_index)
+{
+	if(camera_intrinsics_.size() < intrinsic_index+1)
+		camera_intrinsics_.resize(intrinsic_index+1);
+	camera_intrinsics_[intrinsic_index] = intrinsics_ptr;
+}
+
 }  // namespace mv
diff --git a/intern/libmv/libmv/autotrack/reconstruction.h b/intern/libmv/libmv/autotrack/reconstruction.h
index 0a9fd29..6f45739 100644
--- a/intern/libmv/libmv/autotrack/reconstruction.h
+++ b/intern/libmv/libmv/autotrack/reconstruction.h
@@ -39,12 +39,15 @@ class Model;
 struct Marker;
 class Tracks;
 
-class CameraPose {
-  int clip;
-  int frame;
-  int intrinsics;
-  Mat3 R;
-  Vec3 t;
+struct CameraPose {
+	CameraPose(int clip_, int frame_, int intrinsics_, Mat3 R_, Vec3 t_):
+	    clip(clip_), frame(frame_), intrinsics(intrinsics_), R(R_), t(t_) {}
+
+	int clip;
+	int frame;
+	int intrinsics;
+	Mat3 R;
+	Vec3 t;
 };
 
 class Point {
@@ -61,7 +64,7 @@ class Reconstruction {
  public:
   // All methods copy their input reference or take ownership of the pointer.
   void AddCameraPose(const CameraPose& pose);
-  int  AddCameraIntrinsics(CameraIntrinsics* intrinsics);
+  void AddCameraIntrinsics(CameraIntrinsics* intrinsics_ptr, const int intrinsic_index);
   int  AddPoint(const Point& point);
   int  AddModel(Model* model);
 
@@ -90,6 +93,8 @@ class Reconstruction {
 };
 
 bool ReconstructTwoFrames(const vector<Marker> &markers,
+                          const int clip,
+                          libmv::CameraIntrinsics &cam_intrinsics,
                           Reconstruction *reconstruction);
 }  // namespace mv




More information about the Bf-blender-cvs mailing list