Skip to content

Commit 21345c5

Browse files
committed
Add distortion correction
1 parent 2663652 commit 21345c5

6 files changed

Lines changed: 162 additions & 7 deletions

File tree

msvs/3dv_tutorial.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "image_generation", "image_g
1111
EndProject
1212
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "camera_calibration", "camera_calibration\camera_calibration.vcxproj", "{8FE6C6CB-6D5D-4745-A4CE-0621BC8633B9}"
1313
EndProject
14+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "distortion_correction", "distortion_correction\distortion_correction.vcxproj", "{E5444B55-196A-4A71-9BDE-C42E830B87B0}"
15+
EndProject
1416
Global
1517
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1618
Debug|Win32 = Debug|Win32
@@ -33,6 +35,10 @@ Global
3335
{8FE6C6CB-6D5D-4745-A4CE-0621BC8633B9}.Debug|Win32.Build.0 = Debug|Win32
3436
{8FE6C6CB-6D5D-4745-A4CE-0621BC8633B9}.Release|Win32.ActiveCfg = Release|Win32
3537
{8FE6C6CB-6D5D-4745-A4CE-0621BC8633B9}.Release|Win32.Build.0 = Release|Win32
38+
{E5444B55-196A-4A71-9BDE-C42E830B87B0}.Debug|Win32.ActiveCfg = Debug|Win32
39+
{E5444B55-196A-4A71-9BDE-C42E830B87B0}.Debug|Win32.Build.0 = Debug|Win32
40+
{E5444B55-196A-4A71-9BDE-C42E830B87B0}.Release|Win32.ActiveCfg = Release|Win32
41+
{E5444B55-196A-4A71-9BDE-C42E830B87B0}.Release|Win32.Build.0 = Release|Win32
3642
EndGlobalSection
3743
GlobalSection(SolutionProperties) = preSolution
3844
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>{E5444B55-196A-4A71-9BDE-C42E830B87B0}</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\distortion_correction.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
@@ -5,11 +5,11 @@ int main(void)
55
cv::Size board_pattern(10, 7);
66
double board_cellsize = 0.025;
77

8-
// Open an video and get the initial image
8+
// Open an video
99
cv::VideoCapture video;
1010
if (!video.open("data/chessboard.avi")) return -1;
1111

12-
// Find 2D points from selected images
12+
// Select images and find 2D corner points from them
1313
cv::Size image_size;
1414
std::vector<std::vector<cv::Point2f> > image_points;
1515
while (true)
@@ -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;

src/distortion_correction.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include "opencv_all.hpp"
2+
3+
int main(void)
4+
{
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);
7+
8+
// Open an video
9+
cv::VideoCapture video;
10+
if (!video.open("data/chessboard.avi")) return -1;
11+
12+
// Run distortion correction
13+
bool show_rectify = true;
14+
cv::Mat map1, map2;
15+
while (true)
16+
{
17+
// Grab an image from the video
18+
cv::Mat image;
19+
video >> image;
20+
if (image.empty()) break;
21+
22+
// Rectify geometric distortion (c.f. 'cv::undistort()' can be applied for one-time remapping.)
23+
if (show_rectify)
24+
{
25+
if (map1.empty() || map2.empty())
26+
cv::initUndistortRectifyMap(K, dist_coeff, cv::Mat(), cv::Mat(), image.size(), CV_32FC1, map1, map2);
27+
cv::remap(image, image, map1, map2, cv::InterpolationFlags::INTER_LINEAR);
28+
}
29+
30+
// Show the image
31+
cv::imshow("3DV Tutorial: Distortion Correction", image);
32+
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
36+
{
37+
key = cv::waitKey();
38+
if (key == 27) break; // "ESC" key
39+
else if (key == 9) show_rectify = !show_rectify; // "Tab" key
40+
}
41+
}
42+
43+
video.release();
44+
return 0;
45+
}

src/image_generation.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ int main(void)
3131
fclose(fin);
3232
X = X.reshape(1, X.rows / 4).t(); // Convert to a 4 x N matrix
3333

34+
// Generate images for each camera pose
3435
cv::Mat K = (cv::Mat_<double>(3, 3) << camera_focal, 0, camera_center.x, 0, camera_focal, camera_center.y, 0, 0, 1);
3536
for (int i = 0; i < sizeof(camera_pos) / sizeof(cv::Point3d); i++)
3637
{
@@ -65,4 +66,4 @@ int main(void)
6566
}
6667

6768
return 0;
68-
}
69+
}

0 commit comments

Comments
 (0)