initial commit

This commit is contained in:
2018-06-03 22:18:30 +08:00
parent f4e9aea3ff
commit 1bc6968b3c
8 changed files with 422 additions and 0 deletions
+31
View File
@@ -0,0 +1,31 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27703.2026
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "HistoryMap", "HistoryMap.vcxproj", "{27678521-6FA7-4200-BE5F-F25263CF24FA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{27678521-6FA7-4200-BE5F-F25263CF24FA}.Debug|x64.ActiveCfg = Debug|x64
{27678521-6FA7-4200-BE5F-F25263CF24FA}.Debug|x64.Build.0 = Debug|x64
{27678521-6FA7-4200-BE5F-F25263CF24FA}.Debug|x86.ActiveCfg = Debug|Win32
{27678521-6FA7-4200-BE5F-F25263CF24FA}.Debug|x86.Build.0 = Debug|Win32
{27678521-6FA7-4200-BE5F-F25263CF24FA}.Release|x64.ActiveCfg = Release|x64
{27678521-6FA7-4200-BE5F-F25263CF24FA}.Release|x64.Build.0 = Release|x64
{27678521-6FA7-4200-BE5F-F25263CF24FA}.Release|x86.ActiveCfg = Release|Win32
{27678521-6FA7-4200-BE5F-F25263CF24FA}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D77B6622-39E7-48B6-AC86-5EA2142D2F9F}
EndGlobalSection
EndGlobal
+130
View File
@@ -0,0 +1,130 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>15.0</VCProjectVersion>
<ProjectGuid>{27678521-6FA7-4200-BE5F-F25263CF24FA}</ProjectGuid>
<RootNamespace>HistoryMap</RootNamespace>
<WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="main.cpp" />
<ClCompile Include="MapHandler.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="MapHandler.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
+30
View File
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="源文件">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="头文件">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="资源文件">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="MapHandler.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="MapHandler.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
</Project>
+103
View File
@@ -0,0 +1,103 @@
#include "MapHandler.h"
MapHandler::MapHandler(cv::Mat src1, cv::Mat src2) :map1(src1), map2(src2)
{
}
MapHandler::~MapHandler()
{
}
double MapHandler::Match(const cv::Point2i& point1, const cv::Point2i& point2, cv::Point& out)
{
this->templateImg = map1(cv::Rect(point1.x - roiWid / 2, point1.y - roiHei / 2, roiWid, roiHei));
cv::Mat tmpMap2 = map2.clone();
double maxValue = 0, maybeMax = 0, angle = 0, scale = 1;
cv::Point maxPoint;
cv::minMaxLoc(result, nullptr, &maxValue, &out, nullptr);
cv::Point tp;
for (int i = -30; i <= 30; i++) {
for (double j = 0.7; j <= 1.3; j += 0.05)
{
cv::Mat M = cv::getRotationMatrix2D(point2, i, j);
cv::warpAffine(map2, tmpMap2, M, cv::Size(tmpMap2.size().width, tmpMap2.size().height));//旋转map2
cv::Mat tmp = tmpMap2(cv::Rect(point2.x - 1.5 * roiWid / 2, point2.y - 1.5 * roiHei / 2, 1.5 * roiWid, 1.5 * roiHei));//map2 ROI
result = cv::Mat(tmpMap2.cols - templateImg.cols + 1, tmpMap2.rows - templateImg.rows + 1, CV_32FC1);
cv::matchTemplate(tmp, templateImg, result, cv::TM_CCOEFF_NORMED);
cv::minMaxLoc(result, nullptr, &maybeMax, &out, nullptr);
if (maybeMax > maxValue)
{
maxValue = maybeMax;
angle = i;
maxPoint = out;
scale = j;
}
}
}
cv::Mat M = cv::getRotationMatrix2D(point2, angle, scale);
cv::warpAffine(map2, tmpMap2, M, cv::Size(tmpMap2.size().width, tmpMap2.size().height));//旋转map2
cv::Mat tmp = tmpMap2(cv::Rect(point2.x - 1.5 * roiWid / 2, point2.y - 1.5 * roiHei / 2, 1.5 * roiWid, 1.5 * roiHei));//map2 ROI
tp.x = maxPoint.x + roiWid / 2;
tp.y = maxPoint.y + roiHei / 2;
cv::circle(tmp, tp, 3, cv::Scalar(0, 0, 255), 3);
cv::imshow("TmpMap2", tmpMap2);
//out.x += point2.x /*- 2 * roiWid / 2*/;
//out.y += point2.y /*- 2 * roiHei / 2*/;
return maxValue;
}
cv::Mat MapHandler::Match2(const cv::Point2i & point1, const cv::Point2i & point2, const cv::Point2i & point3, const cv::Point2i & point4)
{
//计算两点间距离以确定缩放比
double d1 = std::sqrt(std::pow(point1.x - point2.x, 2) + cv::pow(point1.y - point2.y, 2));
double d2 = std::sqrt(std::pow(point3.x - point4.x, 2) + std::pow(point3.y - point4.y, 2));
double scale = d2 / d1;
std::cout << "Scale: " << scale << std::endl;
//计算斜率及角度
double k1 = (1.0*point1.y - point2.y) / (point1.x - point2.x);
double k2 = (1.0*point3.y - point4.y) / (point3.x - point4.x);
double angle1 = std::atan(k1);
double angle2 = std::atan(k2);
double angle = (k1 - k2) * 180 / PI;
std::cout << "Angle: " << angle << std::endl;
//以map1中心点旋转
cv::Point2f center = cv::Point2f(map1.cols / 2.0, map1.rows / 2.0);
cv::Mat transMatrix = cv::getRotationMatrix2D(center, angle, scale);
cv::warpAffine(map1, map1, transMatrix, map1.size());
cv::Mat map22 = cv::Mat::zeros(map2.size(), map2.type());
cv::Mat tar = map22(cv::Rect(0, 0, map1.cols, map1.rows));
//将变换后的map1与map2叠加在一起
map1.copyTo(tar);
Shift(map22, point3.x - point1.x, point4.y - point2.y);
cv::Mat out;
cv::addWeighted(map2, 0.5, map22, 0.5, 0, out);
//cv::imshow("Final", out);
return out;
}
void MapHandler::Shift(cv::Mat & src, const float & dx, const float & dy)
{
cv::Mat t_mat = cv::Mat::zeros(2, 3, CV_32FC1);
t_mat.at<float>(0, 0) = 1;
t_mat.at<float>(0, 2) = dx; //水平平移量
t_mat.at<float>(1, 1) = 1;
t_mat.at<float>(1, 2) = dy; //竖直平移量
cv::warpAffine(src, src, t_mat, src.size());
}
+44
View File
@@ -0,0 +1,44 @@
#pragma once
#include <opencv2\opencv.hpp>
#include <cmath>
#include <iostream>
class MapHandler
{
public:
MapHandler(cv::Mat src1, cv::Mat src2);
~MapHandler();
/*
@func 一个点的坐标匹配(模板匹配)
@return 匹配的最大值 out为最大值的坐标
@param point1 The first point of the first image
@param point3 The first point of the second image
*/
double Match(const cv::Point2i& point1, const cv::Point2i& point2, cv::Point& out);
/*
@func 两个点的坐标匹配(只根据坐标匹配)
@param point1 The first point of the first image
@param point2 The second point of the first image
@param point3 The first point of the second image
@param point4 The second point of the second image
*/
cv::Mat Match2(const cv::Point2i& point1, const cv::Point2i& point2, const cv::Point2i& point3, const cv::Point2i& point4);
private:
const int roiWid = 100, roiHei = 100;
const double PI = 3.1415926;
cv::Mat map1, map2;
cv::Mat templateImg;
cv::Mat result;
/*
@func 对图像src进行平移
@param src 待操作的图像
@param dx 横向移动距离
@param dy 纵向移动距离
*/
void Shift(cv::Mat & src, const float& dx, const float& dy);
};
+84
View File
@@ -0,0 +1,84 @@
#include<iostream>
#include<opencv.hpp>
#include"MapHandler.h"
#define MAP1_FILE ("map_pic/map1.jpg")
#define MAP2_FILE ("map_pic/map2.jpg")
using namespace cv;
using namespace std;
#define TEM_WIE (30)
#define TEM_HEI (30)
Point2i p1, p2, p3, p4;
Mat ROI;
Mat src1 = imread(MAP1_FILE);
Mat src2 = imread(MAP2_FILE);
MapHandler handler(src1, src2);
Point maxPoint;
bool map1Clicked = false, map2Clicked = false;
int cCount1 = 0;
int cCount2 = 0;
void on_mouse(int event, int x, int y, int flags, void* param)
{
int flag = int(param);
switch (event)
{
case EVENT_LBUTTONDOWN:
if (flag == 1)
{
if (cCount1 == 0)
{
p1 = Point2i(x, y);
}
else if (cCount1 == 1)
{
p2 = Point2i(x, y);
}
cCount1++;
cout << "Click on map1: (" << x << ", " << y << ")" << endl;
}
else if (flag == 2)
{
if (cCount2 == 0)
{
p3 = Point2i(x, y);
}
else if (cCount2 == 1)
{
p4 = Point2i(x, y);
}
cCount2++;
cout << "Click on map2: (" << x << ", " << y << ")" << endl;
}
if (cCount1 == 2 && cCount2 == 2)
{
cCount1 = 0;
cCount2 = 0;
cout << "Matching..." << p1 << p2 << p3 << p4 << endl;
Mat out = handler.Match2(p1, p2, p3, p4);
imshow("Out", out);
}
break;
default:
break;
}
}
int main()
{
namedWindow("Map1");
namedWindow("Map2");
imshow("Map1", src1);
imshow("Map2", src2);
setMouseCallback("Map1", on_mouse, (void *)1);
setMouseCallback("Map2", on_mouse, (void *)2);
waitKey(0);
return 0;
}
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 120 KiB

BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 278 KiB