Skip to content

Commit 242f179

Browse files
committed
Add monocular visual odometry (update project configuration)
1 parent a48c4fb commit 242f179

9 files changed

Lines changed: 244 additions & 339 deletions

README.mdk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
## Invitation to 3D Vision (Tutorial for Everyone)
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 2013
44
VisualStudioVersion = 12.0.40629.0
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "video_stabilization", "video_stabilization.vcxproj", "{5CCD570B-06A1-48B8-A7EF-756587CA45C0}"
6+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "video_stabilization", "video_stabilization\video_stabilization.vcxproj", "{5CCD570B-06A1-48B8-A7EF-756587CA45C0}"
7+
EndProject
8+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mono_visual_odometry", "mono_visual_odometry\mono_visual_odometry.vcxproj", "{5549C427-BA0F-48AF-B3A0-029A8B57E71F}"
79
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -15,6 +17,10 @@ Global
1517
{5CCD570B-06A1-48B8-A7EF-756587CA45C0}.Debug|Win32.Build.0 = Debug|Win32
1618
{5CCD570B-06A1-48B8-A7EF-756587CA45C0}.Release|Win32.ActiveCfg = Release|Win32
1719
{5CCD570B-06A1-48B8-A7EF-756587CA45C0}.Release|Win32.Build.0 = Release|Win32
20+
{5549C427-BA0F-48AF-B3A0-029A8B57E71F}.Debug|Win32.ActiveCfg = Debug|Win32
21+
{5549C427-BA0F-48AF-B3A0-029A8B57E71F}.Debug|Win32.Build.0 = Debug|Win32
22+
{5549C427-BA0F-48AF-B3A0-029A8B57E71F}.Release|Win32.ActiveCfg = Release|Win32
23+
{5549C427-BA0F-48AF-B3A0-029A8B57E71F}.Release|Win32.Build.0 = Release|Win32
1824
EndGlobalSection
1925
GlobalSection(SolutionProperties) = preSolution
2026
HideSolutionNode = FALSE
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|Win32">
5+
<Configuration>Debug</Configuration>
6+
<Platform>Win32</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|Win32">
9+
<Configuration>Release</Configuration>
10+
<Platform>Win32</Platform>
11+
</ProjectConfiguration>
12+
</ItemGroup>
13+
<PropertyGroup Label="Globals">
14+
<ProjectGuid>{5549C427-BA0F-48AF-B3A0-029A8B57E71F}</ProjectGuid>
15+
<Keyword>Win32Proj</Keyword>
16+
<RootNamespace>3dv_tutorial</RootNamespace>
17+
</PropertyGroup>
18+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
19+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
20+
<ConfigurationType>Application</ConfigurationType>
21+
<UseDebugLibraries>true</UseDebugLibraries>
22+
<PlatformToolset>v120</PlatformToolset>
23+
<CharacterSet>Unicode</CharacterSet>
24+
</PropertyGroup>
25+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
26+
<ConfigurationType>Application</ConfigurationType>
27+
<UseDebugLibraries>false</UseDebugLibraries>
28+
<PlatformToolset>v120</PlatformToolset>
29+
<WholeProgramOptimization>true</WholeProgramOptimization>
30+
<CharacterSet>Unicode</CharacterSet>
31+
</PropertyGroup>
32+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
33+
<ImportGroup Label="ExtensionSettings">
34+
</ImportGroup>
35+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
36+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
37+
</ImportGroup>
38+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
39+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
40+
</ImportGroup>
41+
<PropertyGroup Label="UserMacros" />
42+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
43+
<LinkIncremental>true</LinkIncremental>
44+
<OutDir>..\..\bin</OutDir>
45+
</PropertyGroup>
46+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
47+
<LinkIncremental>false</LinkIncremental>
48+
<OutDir>..\..\bin</OutDir>
49+
</PropertyGroup>
50+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
51+
<ClCompile>
52+
<PrecompiledHeader>
53+
</PrecompiledHeader>
54+
<WarningLevel>Level3</WarningLevel>
55+
<Optimization>Disabled</Optimization>
56+
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
57+
<SDLCheck>true</SDLCheck>
58+
<AdditionalIncludeDirectories>..\..\EXTERNAL\OpenCV\include</AdditionalIncludeDirectories>
59+
</ClCompile>
60+
<Link>
61+
<SubSystem>Console</SubSystem>
62+
<GenerateDebugInformation>true</GenerateDebugInformation>
63+
<AdditionalLibraryDirectories>..\..\EXTERNAL\OpenCV\lib</AdditionalLibraryDirectories>
64+
</Link>
65+
</ItemDefinitionGroup>
66+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
67+
<ClCompile>
68+
<WarningLevel>Level3</WarningLevel>
69+
<PrecompiledHeader>
70+
</PrecompiledHeader>
71+
<Optimization>MaxSpeed</Optimization>
72+
<FunctionLevelLinking>true</FunctionLevelLinking>
73+
<IntrinsicFunctions>true</IntrinsicFunctions>
74+
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
75+
<SDLCheck>true</SDLCheck>
76+
<AdditionalIncludeDirectories>..\..\EXTERNAL\OpenCV\include</AdditionalIncludeDirectories>
77+
</ClCompile>
78+
<Link>
79+
<SubSystem>Console</SubSystem>
80+
<GenerateDebugInformation>true</GenerateDebugInformation>
81+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
82+
<OptimizeReferences>true</OptimizeReferences>
83+
<AdditionalLibraryDirectories>..\..\EXTERNAL\OpenCV\lib</AdditionalLibraryDirectories>
84+
</Link>
85+
</ItemDefinitionGroup>
86+
<ItemGroup>
87+
<ClCompile Include="..\..\src\mono_visual_odometry.cpp" />
88+
</ItemGroup>
89+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
90+
<ImportGroup Label="ExtensionTargets">
91+
</ImportGroup>
92+
</Project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
4+
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
5+
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
6+
</PropertyGroup>
7+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
8+
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
9+
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
10+
</PropertyGroup>
11+
</Project>

video_stabilization.vcxproj renamed to msvs/video_stabilization/video_stabilization.vcxproj

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@
4141
<PropertyGroup Label="UserMacros" />
4242
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
4343
<LinkIncremental>true</LinkIncremental>
44+
<OutDir>..\..\bin</OutDir>
4445
</PropertyGroup>
4546
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
4647
<LinkIncremental>false</LinkIncremental>
48+
<OutDir>..\..\bin</OutDir>
4749
</PropertyGroup>
4850
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
4951
<ClCompile>
@@ -53,12 +55,12 @@
5355
<Optimization>Disabled</Optimization>
5456
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
5557
<SDLCheck>true</SDLCheck>
56-
<AdditionalIncludeDirectories>EXTERNAL\OpenCV\include</AdditionalIncludeDirectories>
58+
<AdditionalIncludeDirectories>..\..\EXTERNAL\OpenCV\include</AdditionalIncludeDirectories>
5759
</ClCompile>
5860
<Link>
5961
<SubSystem>Console</SubSystem>
6062
<GenerateDebugInformation>true</GenerateDebugInformation>
61-
<AdditionalLibraryDirectories>EXTERNAL\OpenCV\lib</AdditionalLibraryDirectories>
63+
<AdditionalLibraryDirectories>..\..\EXTERNAL\OpenCV\lib</AdditionalLibraryDirectories>
6264
</Link>
6365
</ItemDefinitionGroup>
6466
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
@@ -71,20 +73,20 @@
7173
<IntrinsicFunctions>true</IntrinsicFunctions>
7274
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
7375
<SDLCheck>true</SDLCheck>
74-
<AdditionalIncludeDirectories>EXTERNAL\OpenCV\include</AdditionalIncludeDirectories>
76+
<AdditionalIncludeDirectories>..\..\EXTERNAL\OpenCV\include</AdditionalIncludeDirectories>
7577
</ClCompile>
7678
<Link>
7779
<SubSystem>Console</SubSystem>
7880
<GenerateDebugInformation>true</GenerateDebugInformation>
7981
<EnableCOMDATFolding>true</EnableCOMDATFolding>
8082
<OptimizeReferences>true</OptimizeReferences>
81-
<AdditionalLibraryDirectories>EXTERNAL\OpenCV\lib</AdditionalLibraryDirectories>
83+
<AdditionalLibraryDirectories>..\..\EXTERNAL\OpenCV\lib</AdditionalLibraryDirectories>
8284
</Link>
8385
</ItemDefinitionGroup>
8486
<ItemGroup>
85-
<ClCompile Include="src\video_stabilization.cpp" />
87+
<ClCompile Include="..\..\src\video_stabilization.cpp" />
8688
</ItemGroup>
8789
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
8890
<ImportGroup Label="ExtensionTargets">
8991
</ImportGroup>
90-
</Project>
92+
</Project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
4+
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
5+
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
6+
</PropertyGroup>
7+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
8+
<LocalDebuggerWorkingDirectory>$(OutDir)</LocalDebuggerWorkingDirectory>
9+
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
10+
</PropertyGroup>
11+
</Project>

src/mono_visual_odometry.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include "opencv_all.hpp"
2+
3+
int main(void)
4+
{
5+
bool use_5pt = false;
6+
double camera_focal = 718.8560;
7+
cv::Point2d camera_center(607.1928, 185.2157);
8+
9+
// Open a file to write camera trajectory
10+
FILE* camera_traj = NULL;
11+
camera_traj = fopen("camera_trajectory.csv", "wt");
12+
if (camera_traj == NULL) return -1;
13+
14+
// Open an video and get the initial image
15+
cv::VideoCapture video;
16+
if (!video.open("data/KITTI_00_L/%06d.png")) return -1;
17+
18+
cv::Mat gray_prev;
19+
video >> gray_prev;
20+
if (gray_prev.empty())
21+
{
22+
video.release();
23+
return -1;
24+
}
25+
if (gray_prev.channels() > 1) cv::cvtColor(gray_prev, gray_prev, CV_RGB2GRAY);
26+
27+
// Run and show video stabilization
28+
cv::Mat camera_pose = cv::Mat::eye(4, 4, CV_64F);
29+
while (true)
30+
{
31+
// Grab an image from the video
32+
cv::Mat image, gray;
33+
video >> image;
34+
if (image.empty()) break;
35+
if (image.channels() > 1) cv::cvtColor(image, gray, CV_RGB2GRAY);
36+
else gray = image.clone();
37+
38+
// Extract optical flow
39+
std::vector<cv::Point2f> point_prev, point;
40+
cv::goodFeaturesToTrack(gray_prev, point_prev, 2000, 0.01, 10);
41+
std::vector<uchar> m_status;
42+
cv::Mat err;
43+
cv::calcOpticalFlowPyrLK(gray_prev, gray, point_prev, point, m_status, err);
44+
gray_prev = gray;
45+
46+
// Calculate relative pose
47+
cv::Mat E, inlier_mask;
48+
if (use_5pt)
49+
{
50+
E = cv::findEssentialMat(point_prev, point, camera_focal, camera_center, cv::RANSAC, 0.99, 1, inlier_mask);
51+
}
52+
else
53+
{
54+
cv::Mat F = cv::findFundamentalMat(point_prev, point, cv::FM_RANSAC, 1, 0.99, inlier_mask);
55+
cv::Mat K = (cv::Mat_<double>(3, 3) << camera_focal, 0, camera_center.x, 0, camera_focal, camera_center.y, 0, 0, 1);
56+
E = K.t() * F * K;
57+
}
58+
cv::Mat R, t;
59+
int inlier_num = cv::recoverPose(E, point_prev, point, R, t, camera_focal, camera_center, inlier_mask);
60+
61+
// Accumulate pose
62+
cv::Mat T = cv::Mat::eye(4, 4, R.type());
63+
T(cv::Range(0, 3), cv::Range(0, 3)) = R * 1.0;
64+
T(cv::Range(0, 3), cv::Range(3, 4)) = t * 1.0;
65+
camera_pose = camera_pose * T.inv();
66+
67+
// Show the image and write camera pose
68+
if (image.channels() < 3) cv::cvtColor(image, image, CV_GRAY2RGB);
69+
for (size_t i = 0; i < point_prev.size(); i++)
70+
{
71+
if (inlier_mask.at<uchar>(i) > 0) cv::line(image, point_prev[i], point[i], cv::Scalar(0, 0, 255));
72+
else cv::line(image, point_prev[i], point[i], cv::Scalar(0, 255, 0));
73+
}
74+
cv::imshow("3DVT Tutorial: Monocular Visual Odometry", image);
75+
fprintf(camera_traj, "%.6f, %.6f, %.6f\n", camera_pose.at<double>(0, 3), camera_pose.at<double>(1, 3), camera_pose.at<double>(2, 3));
76+
if (cv::waitKey(1) == 27) break; // "ESC" key
77+
}
78+
79+
video.release();
80+
fclose(camera_traj);
81+
return 0;
82+
}

0 commit comments

Comments
 (0)