Skip to content

Commit 41fb86d

Browse files
committed
Add pose estimation
1 parent 21345c5 commit 41fb86d

9 files changed

Lines changed: 196 additions & 17 deletions

bin/data/chessboard.avi

9.58 MB
Binary file not shown.

msvs/3dv_tutorial.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "camera_calibration", "camer
1313
EndProject
1414
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "distortion_correction", "distortion_correction\distortion_correction.vcxproj", "{E5444B55-196A-4A71-9BDE-C42E830B87B0}"
1515
EndProject
16+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "pose_estimation", "pose_estimation\pose_estimation.vcxproj", "{E28D077A-B8C3-4391-AE2E-757E72C87BF6}"
17+
EndProject
1618
Global
1719
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1820
Debug|Win32 = Debug|Win32
@@ -39,6 +41,10 @@ Global
3941
{E5444B55-196A-4A71-9BDE-C42E830B87B0}.Debug|Win32.Build.0 = Debug|Win32
4042
{E5444B55-196A-4A71-9BDE-C42E830B87B0}.Release|Win32.ActiveCfg = Release|Win32
4143
{E5444B55-196A-4A71-9BDE-C42E830B87B0}.Release|Win32.Build.0 = Release|Win32
44+
{E28D077A-B8C3-4391-AE2E-757E72C87BF6}.Debug|Win32.ActiveCfg = Debug|Win32
45+
{E28D077A-B8C3-4391-AE2E-757E72C87BF6}.Debug|Win32.Build.0 = Debug|Win32
46+
{E28D077A-B8C3-4391-AE2E-757E72C87BF6}.Release|Win32.ActiveCfg = Release|Win32
47+
{E28D077A-B8C3-4391-AE2E-757E72C87BF6}.Release|Win32.Build.0 = Release|Win32
4248
EndGlobalSection
4349
GlobalSection(SolutionProperties) = preSolution
4450
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>{E28D077A-B8C3-4391-AE2E-757E72C87BF6}</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\pose_estimation.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>

src/camera_calibration.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ int main(void)
2323
// Show the image and keep it if necessary
2424
cv::imshow("3DV Tutorial: Camera Calibration", image);
2525
int key = cv::waitKey(1);
26-
if (key == 27) break; // "ESC" key
27-
else if (key == 32) // "Space" key
26+
if (key == 27) break; // 'ESC' key
27+
else if (key == 32) // 'Space' key
2828
{
2929
std::vector<cv::Point2f> pts;
3030
bool complete = cv::findChessboardCorners(image, board_pattern, pts);
3131
cv::drawChessboardCorners(image, board_pattern, pts, complete);
3232
cv::imshow("3DV Tutorial: Camera Calibration", image);
3333
key = cv::waitKey();
34-
if (key == 27) break; // "ESC" key
35-
else if (complete && key == 13) // "Enter" key
34+
if (key == 27) break; // 'ESC' key
35+
else if (complete && key == 13) // 'Enter' key
3636
{
3737
image_points.push_back(pts);
3838
std::cout << image_points.size() << " images are selected for camera calibration." << std::endl;
@@ -46,7 +46,7 @@ int main(void)
4646
std::vector<std::vector<cv::Point3f> > object_points(1);
4747
for (int r = 0; r < board_pattern.height; r++)
4848
for (int c = 0; c < board_pattern.width; c++)
49-
object_points[0].push_back(cv::Point3d(board_cellsize * c, board_cellsize * r, 0));
49+
object_points[0].push_back(cv::Point3f(board_cellsize * c, board_cellsize * r, 0));
5050
object_points.resize(image_points.size(), object_points[0]); // Copy
5151

5252
// Calibrate the camera
@@ -65,4 +65,4 @@ int main(void)
6565
report << "* Distortion coefficient (k1, k2, p1, p2, k3, ...) = " << std::endl << " " << dist_coeff.t() << std::endl;
6666
report.close();
6767
return 0;
68-
}
68+
}

src/distortion_correction.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
int main(void)
44
{
5-
cv::Mat K = (cv::Mat_<double>(3, 3) << 864.43, 0, 952.16, 0, 861.90, 581.92, 0, 0, 1);
6-
cv::Mat dist_coeff = (cv::Mat_<double>(5, 1) << -0.2862454796798899, 0.104008092811024, -0.000707339764553983, 6.557555700709726e-005, -0.01922363820734248);
5+
cv::Mat K = (cv::Mat_<double>(3, 3) << 434.3588729143197, 0, 476.1230925877231, 0, 432.7138830286992, 289.2709802508451, 0, 0, 1);
6+
cv::Mat dist_coeff = (cv::Mat_<double>(5, 1) << -0.2918143346191932, 0.1095347774113121, -0.000105133686343854, 4.350475599617356e-005, -0.02083205595737927);
77

88
// Open an video
99
cv::VideoCapture video;
@@ -30,13 +30,13 @@ int main(void)
3030
// Show the image
3131
cv::imshow("3DV Tutorial: Distortion Correction", image);
3232
int key = cv::waitKey(1);
33-
if (key == 27) break; // "ESC" key
34-
else if (key == 9) show_rectify = !show_rectify; // "Tab" key
35-
else if (key == 32) // "Space" key
33+
if (key == 27) break; // 'ESC' key
34+
else if (key == 9) show_rectify = !show_rectify; // 'Tab' key
35+
else if (key == 32) // 'Space' key
3636
{
3737
key = cv::waitKey();
38-
if (key == 27) break; // "ESC" key
39-
else if (key == 9) show_rectify = !show_rectify; // "Tab" key
38+
if (key == 27) break; // 'ESC' key
39+
else if (key == 9) show_rectify = !show_rectify; // 'Tab' key
4040
}
4141
}
4242

src/mono_visual_odometry.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ int main(void)
7272
}
7373
cv::imshow("3DV Tutorial: Monocular Visual Odometry", image);
7474
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));
75-
if (cv::waitKey(1) == 27) break; // "ESC" key
75+
if (cv::waitKey(1) == 27) break; // 'ESC' key
7676
}
7777

7878
video.release();
7979
fclose(camera_traj);
8080
return 0;
81-
}
81+
}

src/pose_estimation.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include "opencv_all.hpp"
2+
3+
int main(void)
4+
{
5+
cv::Mat K = (cv::Mat_<double>(3, 3) << 434.3588729143197, 0, 476.1230925877231, 0, 432.7138830286992, 289.2709802508451, 0, 0, 1);
6+
cv::Mat dist_coeff = (cv::Mat_<double>(5, 1) << -0.2918143346191932, 0.1095347774113121, -0.000105133686343854, 4.350475599617356e-005, -0.02083205595737927);
7+
cv::Size board_pattern(10, 7);
8+
double board_cellsize = 0.025;
9+
10+
// Open an video
11+
cv::VideoCapture video;
12+
if (!video.open("data/chessboard.avi")) return -1;
13+
14+
// Run pose estimation
15+
std::vector<cv::Point3f> object_points;
16+
for (int r = 0; r < board_pattern.height; r++)
17+
for (int c = 0; c < board_pattern.width; c++)
18+
object_points.push_back(cv::Point3f(board_cellsize * c, board_cellsize * r, 0));
19+
std::vector<cv::Point3f> stick_points;
20+
stick_points.push_back(cv::Point3f(0, 0, 0));
21+
stick_points.push_back(cv::Point3f(0, 0, -board_cellsize * 2));
22+
stick_points.push_back(cv::Point3f(board_cellsize * (board_pattern.width - 1), 0, 0));
23+
stick_points.push_back(cv::Point3f(board_cellsize * (board_pattern.width - 1), 0, -board_cellsize * 2));
24+
stick_points.push_back(cv::Point3f(0, board_cellsize * (board_pattern.height - 1), 0));
25+
stick_points.push_back(cv::Point3f(0, board_cellsize * (board_pattern.height - 1), -board_cellsize * 2));
26+
stick_points.push_back(cv::Point3f(board_cellsize * (board_pattern.width - 1), board_cellsize * (board_pattern.height - 1), 0));
27+
stick_points.push_back(cv::Point3f(board_cellsize * (board_pattern.width - 1), board_cellsize * (board_pattern.height - 1), -board_cellsize * 2));
28+
while (true)
29+
{
30+
// Grab an image from the video
31+
cv::Mat image;
32+
video >> image;
33+
if (image.empty()) break;
34+
35+
// Esimate camera pose
36+
std::vector<cv::Point2f> image_points;
37+
bool complete = cv::findChessboardCorners(image, board_pattern, image_points, cv::CALIB_CB_ADAPTIVE_THRESH + cv::CALIB_CB_NORMALIZE_IMAGE + cv::CALIB_CB_FAST_CHECK);
38+
if (complete)
39+
{
40+
cv::Mat rvec, tvec;
41+
cv::solvePnP(object_points, image_points, K, dist_coeff, rvec, tvec);
42+
43+
// Draw four sticks
44+
std::vector<cv::Point2f> line_points;
45+
cv::projectPoints(stick_points, rvec, tvec, K, dist_coeff, line_points);
46+
for (int i = 0; i < 8; i += 2)
47+
cv::line(image, line_points[i], line_points[i + 1], cv::Scalar(0, 0, 255), 4);
48+
49+
// Print camera position
50+
cv::Mat R;
51+
cv::Rodrigues(rvec, R);
52+
cv::Mat p = -R.t() * tvec;
53+
cv::String info = cv::format("XYZ: [%.3f, %.3f, %.3f]", p.at<double>(0), p.at<double>(1), p.at<double>(2));
54+
cv::putText(image, info, cv::Point(5, 15), cv::FONT_HERSHEY_PLAIN, 1, cv::Scalar(0, 255, 0));
55+
}
56+
57+
// Show the image
58+
cv::imshow("3DV Tutorial: Pose Estimation", image);
59+
int key = cv::waitKey(1);
60+
if (key == 27) break; // 'ESC' key
61+
else if (key == 32) // 'Space' key
62+
{
63+
key = cv::waitKey();
64+
if (key == 27) break; // 'ESC' key
65+
}
66+
}
67+
68+
video.release();
69+
return 0;
70+
}

src/video_stabilization.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ int main(void)
5252
}
5353
cv::hconcat(image, warp, image);
5454
cv::imshow("3DV Tutorial: Video Stabilization", image);
55-
if (cv::waitKey(1) == 27) break; // "ESC" key
55+
if (cv::waitKey(1) == 27) break; // 'ESC' key
5656
}
5757

5858
video.release();
5959
return 0;
60-
}
60+
}

0 commit comments

Comments
 (0)