Skip to content

Commit 44f7c18

Browse files
committed
Add image generation
1 parent 242f179 commit 44f7c18

5 files changed

Lines changed: 177 additions & 1 deletion

File tree

msvs/3dv_tutorial.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "video_stabilization", "vide
77
EndProject
88
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mono_visual_odometry", "mono_visual_odometry\mono_visual_odometry.vcxproj", "{5549C427-BA0F-48AF-B3A0-029A8B57E71F}"
99
EndProject
10+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "image_generation", "image_generation\image_generation.vcxproj", "{27C07F93-22C6-4ACD-A55B-B449F2212937}"
11+
EndProject
1012
Global
1113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1214
Debug|Win32 = Debug|Win32
@@ -21,6 +23,10 @@ Global
2123
{5549C427-BA0F-48AF-B3A0-029A8B57E71F}.Debug|Win32.Build.0 = Debug|Win32
2224
{5549C427-BA0F-48AF-B3A0-029A8B57E71F}.Release|Win32.ActiveCfg = Release|Win32
2325
{5549C427-BA0F-48AF-B3A0-029A8B57E71F}.Release|Win32.Build.0 = Release|Win32
26+
{27C07F93-22C6-4ACD-A55B-B449F2212937}.Debug|Win32.ActiveCfg = Debug|Win32
27+
{27C07F93-22C6-4ACD-A55B-B449F2212937}.Debug|Win32.Build.0 = Debug|Win32
28+
{27C07F93-22C6-4ACD-A55B-B449F2212937}.Release|Win32.ActiveCfg = Release|Win32
29+
{27C07F93-22C6-4ACD-A55B-B449F2212937}.Release|Win32.Build.0 = Release|Win32
2430
EndGlobalSection
2531
GlobalSection(SolutionProperties) = preSolution
2632
HideSolutionNode = FALSE
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>
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>{27C07F93-22C6-4ACD-A55B-B449F2212937}</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_generation.cpp" />
88+
</ItemGroup>
89+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
90+
<ImportGroup Label="ExtensionTargets">
91+
</ImportGroup>
92+
</Project>

src/image_generation.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include "opencv_all.hpp"
2+
3+
#define Rx(rx) (cv::Mat_<double>(3, 3) << 1, 0, 0, 0, cos(rx), -sin(rx), 0, sin(rx), cos(rx))
4+
#define Ry(ry) (cv::Mat_<double>(3, 3) << cos(ry), 0, sin(ry), 0, 1, 0, -sin(ry), 0, cos(ry))
5+
#define Rz(rz) (cv::Mat_<double>(3, 3) << cos(rz), -sin(rz), 0, sin(rz), cos(rz), 0, 0, 0, 1)
6+
7+
int main(void)
8+
{
9+
// The given camera configuration: focal length, principal point, image resolution, position, and orientation
10+
double camera_focal = 1000;
11+
cv::Point2d camera_center(320, 240);
12+
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) };
15+
16+
// Load a point cloud in the homogeneous coordinate
17+
FILE* fin = fopen("data/box.xyz", "rt");
18+
if (fin == NULL) return -1;
19+
cv::Mat X;
20+
while (!feof(fin))
21+
{
22+
double x, y, z;
23+
if (fscanf(fin, "%lf %lf %lf", &x, &y, &z) == 3)
24+
{
25+
X.push_back<double>(x);
26+
X.push_back<double>(y);
27+
X.push_back<double>(z);
28+
X.push_back<double>(1);
29+
}
30+
}
31+
fclose(fin);
32+
X = X.reshape(1, X.rows / 4).t(); // Convert to a 4 x N matrix
33+
34+
cv::Mat K = (cv::Mat_<double>(3, 3) << camera_focal, 0, camera_center.x, 0, camera_focal, camera_center.y, 0, 0, 1);
35+
for (int i = 0; i < sizeof(camera_pos) / sizeof(cv::Point3d); i++)
36+
{
37+
// Derive a projection matrix
38+
cv::Mat Rc = Rz(camera_ori[i].z) * Ry(camera_ori[i].y) * Rx(camera_ori[i].x);
39+
cv::Mat tc = (cv::Mat_<double>(3, 1) << camera_pos[i].x, camera_pos[i].y, camera_pos[i].z);
40+
cv::Mat Rt;
41+
cv::hconcat(Rc.t(), -Rc.t() * tc, Rt);
42+
cv::Mat P = K * Rt;
43+
44+
// Project the points (c.f. OpenCV provide 'cv::projectPoints' with consideration of distortion.)
45+
cv::Mat x = P * X;
46+
x.row(0) = x.row(0) / x.row(2);
47+
x.row(1) = x.row(1) / x.row(2);
48+
x.row(2) = 1;
49+
50+
// Show and store the points
51+
cv::Mat image = cv::Mat::zeros(camera_res, CV_8UC1);
52+
for (int c = 0; c < x.cols; c++)
53+
{
54+
cv::Point p(x.at<double>(0, c), x.at<double>(1, c));
55+
if (p.x >= 0 && p.x < camera_res.width && p.y >= 0 && p.y < camera_res.height)
56+
cv::circle(image, p, 2, 255, -1);
57+
}
58+
cv::imshow(cv::format("camera_image%d", i), image);
59+
cv::waitKey(0);
60+
61+
std::ofstream fout(cv::format("camera_points%d.csv", i));
62+
fout << cv::format(x.t(), cv::Formatter::FMT_CSV);
63+
fout.close();
64+
}
65+
66+
return 0;
67+
}

src/mono_visual_odometry.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ int main(void)
2424
}
2525
if (gray_prev.channels() > 1) cv::cvtColor(gray_prev, gray_prev, CV_RGB2GRAY);
2626

27-
// Run and show video stabilization
27+
// Run and record monocular visual odometry
2828
cv::Mat camera_pose = cv::Mat::eye(4, 4, CV_64F);
2929
while (true)
3030
{

0 commit comments

Comments
 (0)