Skip to content

Commit dff2704

Browse files
committed
Add triangulation (revise all others little bit)
1 parent 99d7bff commit dff2704

12 files changed

Lines changed: 207 additions & 38 deletions

msvs/3dv_tutorial.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "perspective_correction", "p
1919
EndProject
2020
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "image_stitching", "image_stitching\image_stitching.vcxproj", "{C6A12B5E-7F27-4316-9B09-BD93595A033A}"
2121
EndProject
22+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "triangulation", "triangulation\triangulation.vcxproj", "{F3C46820-CF91-48EF-94E1-5ADB573D8D8B}"
23+
EndProject
2224
Global
2325
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2426
Debug|Win32 = Debug|Win32
@@ -57,6 +59,10 @@ Global
5759
{C6A12B5E-7F27-4316-9B09-BD93595A033A}.Debug|Win32.Build.0 = Debug|Win32
5860
{C6A12B5E-7F27-4316-9B09-BD93595A033A}.Release|Win32.ActiveCfg = Release|Win32
5961
{C6A12B5E-7F27-4316-9B09-BD93595A033A}.Release|Win32.Build.0 = Release|Win32
62+
{F3C46820-CF91-48EF-94E1-5ADB573D8D8B}.Debug|Win32.ActiveCfg = Debug|Win32
63+
{F3C46820-CF91-48EF-94E1-5ADB573D8D8B}.Debug|Win32.Build.0 = Debug|Win32
64+
{F3C46820-CF91-48EF-94E1-5ADB573D8D8B}.Release|Win32.ActiveCfg = Release|Win32
65+
{F3C46820-CF91-48EF-94E1-5ADB573D8D8B}.Release|Win32.Build.0 = Release|Win32
6066
EndGlobalSection
6167
GlobalSection(SolutionProperties) = preSolution
6268
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>{F3C46820-CF91-48EF-94E1-5ADB573D8D8B}</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\triangulation.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: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,20 @@ int main(void)
2121

2222
if (select_images)
2323
{
24-
// Show the image and keep it if necessary
24+
// Show the image and keep it if selected
2525
cv::imshow("3DV Tutorial: Camera Calibration", image);
2626
int key = cv::waitKey(1);
27-
if (key == 27) break; // 'ESC' key
28-
else if (key == 32) // 'Space' key
27+
if (key == 27) break; // 'ESC' key: Exit
28+
else if (key == 32) // 'Space' key: Pause
2929
{
3030
std::vector<cv::Point2f> pts;
3131
bool complete = cv::findChessboardCorners(image, board_pattern, pts);
3232
cv::Mat display = image.clone();
3333
cv::drawChessboardCorners(display, board_pattern, pts, complete);
3434
cv::imshow("3DV Tutorial: Camera Calibration", display);
3535
key = cv::waitKey();
36-
if (key == 27) break; // 'ESC' key
37-
else if (key == 13) images.push_back(image); // 'Enter' key
36+
if (key == 27) break; // 'ESC' key: Exit
37+
else if (key == 13) images.push_back(image); // 'Enter' key: Select
3838
}
3939
}
4040
else images.push_back(image);
@@ -52,7 +52,7 @@ int main(void)
5252
}
5353
if (image_points.empty()) return -1;
5454

55-
// Prepare 3D points from the chess board
55+
// Prepare 3D points of the chess board
5656
std::vector<std::vector<cv::Point3f> > object_points(1);
5757
for (int r = 0; r < board_pattern.height; r++)
5858
for (int c = 0; c < board_pattern.width; c++)
@@ -75,4 +75,4 @@ int main(void)
7575
report << "* Distortion coefficient (k1, k2, p1, p2, k3, ...) = " << std::endl << " " << dist_coeff.t() << std::endl;
7676
report.close();
7777
return 0;
78-
}
78+
}

src/distortion_correction.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@ 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: Exit
34+
else if (key == 9) show_rectify = !show_rectify; // 'Tab' key: Toggle rectificaiton
35+
else if (key == 32) // 'Space' key: Pause
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: Exit
39+
else if (key == 9) show_rectify = !show_rectify; // 'Tab' key: Toggle rectificaiton
4040
}
4141
}
4242

4343
video.release();
4444
return 0;
45-
}
45+
}

src/image_generation.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ int main(void)
1010
double camera_focal = 1000;
1111
cv::Point2d camera_center(320, 240);
1212
cv::Size camera_res(640, 480);
13-
cv::Point3d camera_pos[] = { cv::Point3d(0, 0, 0), cv::Point3d(-2, -2, 0), cv::Point3d(2, 2, 0) };
14-
cv::Point3d camera_ori[] = { cv::Point3d(0, 0, 0), cv::Point3d(-CV_PI / 12, CV_PI / 12, 0), cv::Point3d(CV_PI / 12, -CV_PI / 12, 0) };
13+
cv::Point3d camera_pos[] = { cv::Point3d(0, 0, 0), cv::Point3d(-2, -2, 0), cv::Point3d(2, 2, 0), cv::Point3d(-2, 2, 0), cv::Point3d(2, -2, 0) };
14+
cv::Point3d camera_ori[] = { cv::Point3d(0, 0, 0), cv::Point3d(-CV_PI / 12, CV_PI / 12, 0), cv::Point3d(CV_PI / 12, -CV_PI / 12, 0), cv::Point3d(CV_PI / 12, CV_PI / 12, 0), cv::Point3d(-CV_PI / 12, -CV_PI / 12, 0) };
15+
double camera_noise = 0;
1516

1617
// Load a point cloud in the homogeneous coordinate
1718
FILE* fin = fopen("data/box.xyz", "rt");
@@ -48,6 +49,10 @@ int main(void)
4849
x.row(1) = x.row(1) / x.row(2);
4950
x.row(2) = 1;
5051

52+
cv::Mat noise(2, x.cols, x.type());
53+
cv::randn(noise, cv::Scalar(0), cv::Scalar(camera_noise));
54+
x.rowRange(0, 2) = x.rowRange(0, 2) + noise; // Add noise
55+
5156
// Show and store the points
5257
cv::Mat image = cv::Mat::zeros(camera_res, CV_8UC1);
5358
for (int c = 0; c < x.cols; c++)
@@ -57,13 +62,14 @@ int main(void)
5762
cv::circle(image, p, 2, 255, -1);
5863
}
5964
cv::imshow(cv::format("3DV_Tutorial: Image Generation %d", i), image);
60-
cv::waitKey(0);
6165

6266
std::ofstream fout(cv::format("image_generation%d.csv", i));
6367
if (!fout.is_open()) break;
6468
fout << cv::format(x.t(), cv::Formatter::FMT_CSV);
6569
fout.close();
6670
}
6771

72+
std::cout << "Press any key to terminate tihs program!" << std::endl;
73+
cv::waitKey(0);
6874
return 0;
6975
}

src/image_stitching.cpp

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

33
int main(void)
44
{
5-
// Load two images (c.f. Assume that two images have the same size and type)
5+
// Load two images (c.f. We assume that two images have the same size and type)
66
cv::Mat image1 = cv::imread("data/hill01.jpg");
77
cv::Mat image2 = cv::imread("data/hill02.jpg");
88
if (image1.empty() || image2.empty()) return -1;
@@ -48,4 +48,4 @@ int main(void)
4848
printf("Press any key to terminate tihs program!\n");
4949
cv::waitKey(0);
5050
return 0;
51-
}
51+
}

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: Exit
7676
}
7777

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

src/perspective_correction.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ void MouseEventHandler(int event, int x, int y, int flags, void* param)
1313

1414
int main(void)
1515
{
16-
cv::Size plate_size(450, 250);
16+
cv::Size card_size(450, 250);
1717

1818
// Prepare the rectified points
1919
std::vector<cv::Point> points_dst;
2020
points_dst.push_back(cv::Point(0, 0));
21-
points_dst.push_back(cv::Point(plate_size.width, 0));
22-
points_dst.push_back(cv::Point(0, plate_size.height));
23-
points_dst.push_back(cv::Point(plate_size.width, plate_size.height));
21+
points_dst.push_back(cv::Point(card_size.width, 0));
22+
points_dst.push_back(cv::Point(0, card_size.height));
23+
points_dst.push_back(cv::Point(card_size.width, card_size.height));
2424

2525
// Load an image
2626
cv::Mat original = cv::imread("data/sunglok.jpg");
@@ -33,22 +33,22 @@ int main(void)
3333
while (points_src.size() < 4)
3434
{
3535
cv::Mat display = original.clone();
36-
cv::rectangle(display, cv::Rect(cv::Point(10, 10), plate_size), cv::Scalar(0, 0, 255), 2);
36+
cv::rectangle(display, cv::Rect(cv::Point(10, 10), card_size), cv::Scalar(0, 0, 255), 2);
3737
int idx = cv::min(points_src.size(), points_dst.size() - 1);
3838
cv::circle(display, points_dst[idx] + cv::Point(10, 10), 5, cv::Scalar(0, 255, 0), -1);
3939
cv::imshow("3DV Tutorial: Perspective Correction", display);
40-
if (cv::waitKey(1) == 27) break; // 'ESC' key
40+
if (cv::waitKey(1) == 27) break; // 'ESC' key: Exit
4141
}
4242
if (points_src.size() < 4) return -1;
4343

4444
// Calculate planar homography and rectify perspective distortion
4545
cv::Mat H = cv::findHomography(points_src, points_dst);
4646
cv::Mat rectify;
47-
cv::warpPerspective(original, rectify, H, plate_size);
47+
cv::warpPerspective(original, rectify, H, card_size);
4848

4949
// Show the rectified image
5050
cv::imshow("3DV Tutorial: Perspective Correction", rectify);
5151
printf("Press any key to terminate tihs program!\n");
5252
cv::waitKey(0);
5353
return 0;
54-
}
54+
}

src/pose_estimation.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@ int main(void)
1111
cv::VideoCapture video;
1212
if (!video.open("data/chessboard.avi")) return -1;
1313

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));
14+
// Prepare four sticks for simple AR
1915
std::vector<cv::Point3f> stick_points;
2016
stick_points.push_back(cv::Point3f(0, 0, 0));
2117
stick_points.push_back(cv::Point3f(0, 0, -board_cellsize * 2));
@@ -25,6 +21,12 @@ int main(void)
2521
stick_points.push_back(cv::Point3f(0, board_cellsize * (board_pattern.height - 1), -board_cellsize * 2));
2622
stick_points.push_back(cv::Point3f(board_cellsize * (board_pattern.width - 1), board_cellsize * (board_pattern.height - 1), 0));
2723
stick_points.push_back(cv::Point3f(board_cellsize * (board_pattern.width - 1), board_cellsize * (board_pattern.height - 1), -board_cellsize * 2));
24+
25+
// Run pose estimation
26+
std::vector<cv::Point3f> object_points;
27+
for (int r = 0; r < board_pattern.height; r++)
28+
for (int c = 0; c < board_pattern.width; c++)
29+
object_points.push_back(cv::Point3f(board_cellsize * c, board_cellsize * r, 0));
2830
while (true)
2931
{
3032
// Grab an image from the video
@@ -57,11 +59,11 @@ int main(void)
5759
// Show the image
5860
cv::imshow("3DV Tutorial: Pose Estimation", image);
5961
int key = cv::waitKey(1);
60-
if (key == 27) break; // 'ESC' key
61-
else if (key == 32) // 'Space' key
62+
if (key == 27) break; // 'ESC' key: Exit
63+
else if (key == 32) // 'Space' key: Pause
6264
{
6365
key = cv::waitKey();
64-
if (key == 27) break; // 'ESC' key
66+
if (key == 27) break; // 'ESC' key: Exit
6567
}
6668
}
6769

0 commit comments

Comments
 (0)