Skip to content

Commit 6bbe54a

Browse files
committed
Add perspective correction
1 parent 41fb86d commit 6bbe54a

5 files changed

Lines changed: 163 additions & 0 deletions

File tree

bin/data/sunglok.jpg

64.2 KB
Loading

msvs/3dv_tutorial.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "distortion_correction", "di
1515
EndProject
1616
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "pose_estimation", "pose_estimation\pose_estimation.vcxproj", "{E28D077A-B8C3-4391-AE2E-757E72C87BF6}"
1717
EndProject
18+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "perspective_correction", "perspective_correction\perspective_correction.vcxproj", "{53A4EA3F-D463-4D4F-A991-77A16129D664}"
19+
EndProject
1820
Global
1921
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2022
Debug|Win32 = Debug|Win32
@@ -45,6 +47,10 @@ Global
4547
{E28D077A-B8C3-4391-AE2E-757E72C87BF6}.Debug|Win32.Build.0 = Debug|Win32
4648
{E28D077A-B8C3-4391-AE2E-757E72C87BF6}.Release|Win32.ActiveCfg = Release|Win32
4749
{E28D077A-B8C3-4391-AE2E-757E72C87BF6}.Release|Win32.Build.0 = Release|Win32
50+
{53A4EA3F-D463-4D4F-A991-77A16129D664}.Debug|Win32.ActiveCfg = Debug|Win32
51+
{53A4EA3F-D463-4D4F-A991-77A16129D664}.Debug|Win32.Build.0 = Debug|Win32
52+
{53A4EA3F-D463-4D4F-A991-77A16129D664}.Release|Win32.ActiveCfg = Release|Win32
53+
{53A4EA3F-D463-4D4F-A991-77A16129D664}.Release|Win32.Build.0 = Release|Win32
4854
EndGlobalSection
4955
GlobalSection(SolutionProperties) = preSolution
5056
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>{53A4EA3F-D463-4D4F-A991-77A16129D664}</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\perspective_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/perspective_correction.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include "opencv_all.hpp"
2+
3+
void MouseEventHandler(int event, int x, int y, int flags, void* param)
4+
{
5+
if (event == cv::EVENT_LBUTTONDOWN)
6+
{
7+
// Add the point to the given vector
8+
std::vector<cv::Point> *points = (std::vector<cv::Point> *)param;
9+
points->push_back(cv::Point(x, y));
10+
printf("A point (index: %d) is selectd at (%d, %d).\n", points->size() - 1, x, y);
11+
}
12+
}
13+
14+
int main(void)
15+
{
16+
cv::Size plate_size(450, 250);
17+
18+
// Prepare the rectified points
19+
std::vector<cv::Point> points_dst;
20+
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));
24+
25+
// Load an image
26+
cv::Mat original = cv::imread("data/sunglok.jpg");
27+
if (original.empty()) return -1;
28+
29+
// Get the matched points from a user's mouse
30+
std::vector<cv::Point> points_src;
31+
cv::namedWindow("3DV Tutorial: Perspective Correction");
32+
cv::setMouseCallback("3DV Tutorial: Perspective Correction", MouseEventHandler, &points_src);
33+
while (points_src.size() < 4)
34+
{
35+
cv::Mat display = original.clone();
36+
cv::rectangle(display, cv::Rect(cv::Point(10, 10), plate_size), cv::Scalar(0, 0, 255), 2);
37+
int idx = cv::min(points_src.size(), points_dst.size() - 1);
38+
cv::circle(display, points_dst[idx] + cv::Point(10, 10), 5, cv::Scalar(0, 255, 0), -1);
39+
cv::imshow("3DV Tutorial: Perspective Correction", display);
40+
if (cv::waitKey(1) == 27) break; // 'ESC' key
41+
}
42+
if (points_src.size() < 4) return -1;
43+
44+
// Calculate planar homography and rectify perspective distortion
45+
cv::Mat H = cv::findHomography(points_src, points_dst);
46+
cv::Mat rectify;
47+
cv::warpPerspective(original, rectify, H, plate_size);
48+
49+
// Show the rectified image
50+
cv::imshow("3DV Tutorial: Perspective Correction", rectify);
51+
printf("Press any key to terminate tihs program!\n");
52+
cv::waitKey(0);
53+
return 0;
54+
}

0 commit comments

Comments
 (0)