Skip to content

Commit 2663652

Browse files
committed
Add camera calibration
1 parent a9f68a5 commit 2663652

7 files changed

Lines changed: 179 additions & 2 deletions

File tree

bin/data/chessboard.avi

9.3 MB
Binary file not shown.

msvs/3dv_tutorial.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mono_visual_odometry", "mon
99
EndProject
1010
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "image_generation", "image_generation\image_generation.vcxproj", "{27C07F93-22C6-4ACD-A55B-B449F2212937}"
1111
EndProject
12+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "camera_calibration", "camera_calibration\camera_calibration.vcxproj", "{8FE6C6CB-6D5D-4745-A4CE-0621BC8633B9}"
13+
EndProject
1214
Global
1315
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1416
Debug|Win32 = Debug|Win32
@@ -27,6 +29,10 @@ Global
2729
{27C07F93-22C6-4ACD-A55B-B449F2212937}.Debug|Win32.Build.0 = Debug|Win32
2830
{27C07F93-22C6-4ACD-A55B-B449F2212937}.Release|Win32.ActiveCfg = Release|Win32
2931
{27C07F93-22C6-4ACD-A55B-B449F2212937}.Release|Win32.Build.0 = Release|Win32
32+
{8FE6C6CB-6D5D-4745-A4CE-0621BC8633B9}.Debug|Win32.ActiveCfg = Debug|Win32
33+
{8FE6C6CB-6D5D-4745-A4CE-0621BC8633B9}.Debug|Win32.Build.0 = Debug|Win32
34+
{8FE6C6CB-6D5D-4745-A4CE-0621BC8633B9}.Release|Win32.ActiveCfg = Release|Win32
35+
{8FE6C6CB-6D5D-4745-A4CE-0621BC8633B9}.Release|Win32.Build.0 = Release|Win32
3036
EndGlobalSection
3137
GlobalSection(SolutionProperties) = preSolution
3238
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>{8FE6C6CB-6D5D-4745-A4CE-0621BC8633B9}</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\camera_calibration.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: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include "opencv_all.hpp"
2+
3+
int main(void)
4+
{
5+
cv::Size board_pattern(10, 7);
6+
double board_cellsize = 0.025;
7+
8+
// Open an video and get the initial image
9+
cv::VideoCapture video;
10+
if (!video.open("data/chessboard.avi")) return -1;
11+
12+
// Find 2D points from selected images
13+
cv::Size image_size;
14+
std::vector<std::vector<cv::Point2f> > image_points;
15+
while (true)
16+
{
17+
// Grab an image from the video
18+
cv::Mat image;
19+
video >> image;
20+
if (image.empty()) break;
21+
image_size = image.size();
22+
23+
// Show the image and keep it if necessary
24+
cv::imshow("3DV Tutorial: Camera Calibration", image);
25+
int key = cv::waitKey(1);
26+
if (key == 27) break; // "ESC" key
27+
else if (key == 32) // "Space" key
28+
{
29+
std::vector<cv::Point2f> pts;
30+
bool complete = cv::findChessboardCorners(image, board_pattern, pts);
31+
cv::drawChessboardCorners(image, board_pattern, pts, complete);
32+
cv::imshow("3DV Tutorial: Camera Calibration", image);
33+
key = cv::waitKey();
34+
if (key == 27) break; // "ESC" key
35+
else if (complete && key == 13) // "Enter" key
36+
{
37+
image_points.push_back(pts);
38+
std::cout << image_points.size() << " images are selected for camera calibration." << std::endl;
39+
}
40+
}
41+
}
42+
video.release();
43+
if (image_points.empty()) return -1;
44+
45+
// Prepare 3D points from the chess board
46+
std::vector<std::vector<cv::Point3f> > object_points(1);
47+
for (int r = 0; r < board_pattern.height; r++)
48+
for (int c = 0; c < board_pattern.width; c++)
49+
object_points[0].push_back(cv::Point3d(board_cellsize * c, board_cellsize * r, 0));
50+
object_points.resize(image_points.size(), object_points[0]); // Copy
51+
52+
// Calibrate the camera
53+
cv::Mat K = cv::Mat::eye(3, 3, CV_64F);
54+
cv::Mat dist_coeff = cv::Mat::zeros(4, 1, CV_64F);
55+
std::vector<cv::Mat> rvecs, tvecs;
56+
double rms = cv::calibrateCamera(object_points, image_points, image_size, K, dist_coeff, rvecs, tvecs);
57+
58+
// Report calibration results
59+
std::ofstream report("camera_calibration.txt");
60+
if (!report.is_open()) return -1;
61+
report << "## Camera Calbration Results" << std::endl;
62+
report << "* The number of applied images = " << image_points.size() << std::endl;
63+
report << "* RMS error = " << rms << std::endl;
64+
report << "* Camera matrix (K) = " << std::endl << " " << K.row(0) << K.row(1) << K.row(2) << std::endl;
65+
report << "* Distortion coefficient (k1, k2, p1, p2, k3, ...) = " << std::endl << " " << dist_coeff.t() << std::endl;
66+
report.close();
67+
return 0;
68+
}

src/image_generation.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ int main(void)
5959
cv::waitKey(0);
6060

6161
std::ofstream fout(cv::format("camera_points%d.csv", i));
62+
if (!fout.is_open()) break;
6263
fout << cv::format(x.t(), cv::Formatter::FMT_CSV);
6364
fout.close();
6465
}

src/mono_visual_odometry.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ int main(void)
77
cv::Point2d camera_center(607.1928, 185.2157);
88

99
// Open a file to write camera trajectory
10-
FILE* camera_traj = NULL;
11-
camera_traj = fopen("camera_trajectory.csv", "wt");
10+
FILE* camera_traj = fopen("camera_trajectory.csv", "wt");
1211
if (camera_traj == NULL) return -1;
1312

1413
// Open an video and get the initial image

0 commit comments

Comments
 (0)