Skip to content

Commit 99d7bff

Browse files
committed
Add image stitching
1 parent 253f8e1 commit 99d7bff

8 files changed

Lines changed: 163 additions & 3 deletions

File tree

bin/data/hill01.jpg

67.5 KB
Loading

bin/data/hill02.jpg

64.8 KB
Loading

msvs/3dv_tutorial.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "pose_estimation", "pose_est
1717
EndProject
1818
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "perspective_correction", "perspective_correction\perspective_correction.vcxproj", "{53A4EA3F-D463-4D4F-A991-77A16129D664}"
1919
EndProject
20+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "image_stitching", "image_stitching\image_stitching.vcxproj", "{C6A12B5E-7F27-4316-9B09-BD93595A033A}"
21+
EndProject
2022
Global
2123
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2224
Debug|Win32 = Debug|Win32
@@ -51,6 +53,10 @@ Global
5153
{53A4EA3F-D463-4D4F-A991-77A16129D664}.Debug|Win32.Build.0 = Debug|Win32
5254
{53A4EA3F-D463-4D4F-A991-77A16129D664}.Release|Win32.ActiveCfg = Release|Win32
5355
{53A4EA3F-D463-4D4F-A991-77A16129D664}.Release|Win32.Build.0 = Release|Win32
56+
{C6A12B5E-7F27-4316-9B09-BD93595A033A}.Debug|Win32.ActiveCfg = Debug|Win32
57+
{C6A12B5E-7F27-4316-9B09-BD93595A033A}.Debug|Win32.Build.0 = Debug|Win32
58+
{C6A12B5E-7F27-4316-9B09-BD93595A033A}.Release|Win32.ActiveCfg = Release|Win32
59+
{C6A12B5E-7F27-4316-9B09-BD93595A033A}.Release|Win32.Build.0 = Release|Win32
5460
EndGlobalSection
5561
GlobalSection(SolutionProperties) = preSolution
5662
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>{C6A12B5E-7F27-4316-9B09-BD93595A033A}</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\image_stitching.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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
int main(void)
44
{
5-
bool select_images = false;
5+
bool select_images = true;
66
cv::Size board_pattern(10, 7);
77
double board_cellsize = 0.025;
88

99
// Open an video
1010
cv::VideoCapture video;
11-
if (!video.open("data/chessboard/%02d.png")) return -1;
11+
if (!video.open("data/chessboard.avi")) return -1;
1212

1313
// Select images
1414
std::vector<cv::Mat> images;

src/image_generation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ int main(void)
5959
cv::imshow(cv::format("3DV_Tutorial: Image Generation %d", i), image);
6060
cv::waitKey(0);
6161

62-
std::ofstream fout(cv::format("camera_points%d.csv", i));
62+
std::ofstream fout(cv::format("image_generation%d.csv", i));
6363
if (!fout.is_open()) break;
6464
fout << cv::format(x.t(), cv::Formatter::FMT_CSV);
6565
fout.close();

src/image_stitching.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include "opencv_all.hpp"
2+
3+
int main(void)
4+
{
5+
// Load two images (c.f. Assume that two images have the same size and type)
6+
cv::Mat image1 = cv::imread("data/hill01.jpg");
7+
cv::Mat image2 = cv::imread("data/hill02.jpg");
8+
if (image1.empty() || image2.empty()) return -1;
9+
10+
// Retrieve matching points
11+
cv::Mat gray1, gray2;
12+
if (image1.channels() > 1)
13+
{
14+
cv::cvtColor(image1, gray1, CV_RGB2GRAY);
15+
cv::cvtColor(image2, gray2, CV_RGB2GRAY);
16+
}
17+
else
18+
{
19+
gray1 = image1.clone();
20+
gray2 = image2.clone();
21+
}
22+
cv::Ptr<cv::FeatureDetector> detector = cv::xfeatures2d::SURF::create(); // SURF features
23+
std::vector<cv::KeyPoint> keypoint1, keypoint2;
24+
detector->detect(gray1, keypoint1);
25+
detector->detect(gray2, keypoint2);
26+
cv::Ptr<cv::FeatureDetector> extractor = cv::xfeatures2d::SURF::create(); // SURF descriptors
27+
cv::Mat descriptor1, descriptor2;
28+
extractor->compute(gray1, keypoint1, descriptor1);
29+
extractor->compute(gray2, keypoint2, descriptor2);
30+
cv::FlannBasedMatcher matcher; // Approximate Nearest Neighbors (ANN) matcher
31+
std::vector<cv::DMatch> match;
32+
matcher.match(descriptor1, descriptor2, match);
33+
34+
// Calculate planar homography and merge them
35+
std::vector<cv::Point2f> points1, points2;
36+
for (size_t i = 0; i < match.size(); i++)
37+
{
38+
points1.push_back(keypoint1.at(match.at(i).queryIdx).pt);
39+
points2.push_back(keypoint2.at(match.at(i).trainIdx).pt);
40+
}
41+
cv::Mat H = cv::findHomography(points2, points1, cv::RANSAC);
42+
cv::Mat merge;
43+
cv::warpPerspective(image2, merge, H, cv::Size(image1.cols * 2, image1.rows));
44+
merge.colRange(0, image1.cols) = image1 * 1; // Copy
45+
46+
// Show the merged image
47+
cv::imshow("3DV Tutorial: Image Stitching", merge);
48+
printf("Press any key to terminate tihs program!\n");
49+
cv::waitKey(0);
50+
return 0;
51+
}

0 commit comments

Comments
 (0)