Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lesson2 hometask #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 148 additions & 0 deletions HarrisCornerDetector/HarrisCornerDetector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
///@File: HarrisCornerDetector.cpp
///@Brief: Contains implementation of HarrisCornerDetector class
///@Author: Stepan Sidorov
///@Date: 05 October 2015

#include "stdafx.h"
#include "HarrisCornerDetector.h"


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

достаточно одной пустой строки

HarrisCornerDetector::HarrisCornerDetector()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Замените все табы на пробелы. 1 таб = 3 пробела.

{
static const Params defaultParams = {
5, 5,
1,
1,
"New window"
};

m_param = defaultParams;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Для чего вам статическая переменная?

Можно ведь сразу инициализировать структуру:

m_param = {5, 5,1,1,"New window"};

}

void HarrisCornerDetector::Show(const cv::String& imgPath)
{
// Load an image
m_param.srcImage = cv::imread(imgPath);
if (!m_param.srcImage.data)
{
return;
}

// Create window with original image
cv::namedWindow(imgPath, CV_WINDOW_AUTOSIZE);
cv::imshow(imgPath, m_param.srcImage);

// Create DEMO window
m_param.windowName = imgPath + " " + ReplyName();
cv::namedWindow(m_param.windowName, CV_WINDOW_AUTOSIZE);

cv::createTrackbar("WinSize",
m_param.windowName, &m_param.windowSize, 7, detectFeatures, static_cast<void*>(&m_param));
cv::createTrackbar("GSigma",
m_param.windowName, &m_param.gaussianSigma, 15, detectFeatures, static_cast<void*>(&m_param));
cv::createTrackbar("K*100",
m_param.windowName, &m_param.parameterK, 15, detectFeatures, static_cast<void*>(&m_param));
cv::createTrackbar("Thresh",
m_param.windowName, &m_param.threshold, 25, detectFeatures, static_cast<void*>(&m_param));

cv::waitKey(0);
}

void HarrisCornerDetector::detectFeatures(int pos, void* data)
{
const Params& userData = *static_cast<Params*>(data);

if (userData.windowSize % 2 == 0)
{
// skip invalid values
return;
}

// Calculate weight matrix
cv::Mat weightMatrix;
if (userData.gaussianSigma == 0)
{
// Use weighted average window
weightMatrix = cv::Mat::ones(userData.windowSize, userData.windowSize, CV_32FC1) / userData.windowSize / userData.windowSize;
}
else
{
// Use gaussian window
cv::Mat gaussianKernel = cv::getGaussianKernel(userData.windowSize, userData.gaussianSigma, CV_32FC1);
cv::mulTransposed(gaussianKernel, weightMatrix, false);
}

// Convert to grayscale
cv::Mat gray;
cvtColor(userData.srcImage, gray, CV_RGB2GRAY);

// Remove noise by blurring with a Gaussian filter
cv::Mat bluredImg;
cv::GaussianBlur(gray, bluredImg, cv::Size(3, 3), 1);

// Filtering blured image with Sobel masks
cv::Mat IxxMat, IxyMat, IyyMat;
cv::Sobel(bluredImg, IxxMat, CV_32FC1, 2, 0);
cv::Sobel(bluredImg, IxyMat, CV_32FC1, 1, 1);
cv::Sobel(bluredImg, IyyMat, CV_32FC1, 0, 2);

cv::Mat result = cv::Mat::zeros(userData.srcImage.rows, userData.srcImage.cols, CV_32FC1);

// Compute result matrix
for (int i = (userData.windowSize - 1) / 2; i < userData.srcImage.rows - (userData.windowSize - 1) / 2; i++)
{
for (int j = (userData.windowSize - 1) / 2; j < userData.srcImage.cols - (userData.windowSize - 1) / 2; j++)
{
float Ixx = 0, Ixy = 0, Iyy = 0;
for (int p = 0; p < userData.windowSize; p++)
{
for (int q = 0; q < userData.windowSize; q++)
{
int x = j - (userData.windowSize - 1) / 2 + q;
int y = i - (userData.windowSize - 1) / 2 + p;
Ixx += weightMatrix.at<float>(p, q) * IxxMat.at<float>(y, x);
Ixy += weightMatrix.at<float>(p, q) * IxyMat.at<float>(y, x);
Iyy += weightMatrix.at<float>(p, q) * IyyMat.at<float>(y, x);
}
}

// Use original Harris corner measure
result.at<float>(i, j) = (Ixx * Iyy - Ixy*Ixy) * static_cast<float>(userData.parameterK) / 100 * ((Ixx + Iyy) * (Ixx + Iyy));
}
}

cv::Mat dst = userData.srcImage.clone();
// Threshold filtering and extracting local maxima
for (int i = 1; i < userData.srcImage.rows - 1; i++)
{
for (int j = 1; j < userData.srcImage.cols - 1; j++)
{
// Threshold filtering
if (result.at<float>(i, j) < static_cast<float>(userData.threshold))
{
continue;
}

// Extract local maxima
bool isFeature = true;
for (int p = i - 1; i < p + 1; i++)
{
for (int q = j - 1; j < q + 1; j++)
{
if (result.at<float>(p, q) > result.at<float>(i, j))
{
isFeature = false;
break;
}
}
}
if (isFeature)
{
cv::circle(dst, cv::Point(j, i), 2, cv::Scalar(255, 255, 0));
}
}
}

// Show result
cv::imshow(userData.windowName, dst);
}
48 changes: 48 additions & 0 deletions HarrisCornerDetector/HarrisCornerDetector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
///@File: HarrisCornerDetector.h
///@Brief: Contains definition of HarrisCornerDetector class
///@Author: Stepan Sidorov
///@Date: 05 October 2015

#pragma once

#include "stdafx.h"

///@class HarrisCornerDetector
///@brief Demonstrates the Harris corner detector
class HarrisCornerDetector
{
public:
///@brief ctor
HarrisCornerDetector();

///@brief Launch demonstration for passed image
///@param imgPath, in - the path to the image to be processed
void Show(const cv::String& imgPath);

///@brief Returns the string with full name of this detector
static cv::String ReplyName()
{
return "Harris Corner Detector";
}

private:
///@brief applies algorithm according to the passed data
///@see cv::TrackbarCallback
static void detectFeatures(int pos, void* data);

private:
///@brief Contains main configurable parametrs for demo
struct Params
{
int windowSize; // size of window
int gaussianSigma; // sigma of Gaussian
int parameterK; // value of K*100 (because of too small values for integer type)
int threshold; // threshold value

cv::String windowName; // name of the window to be used for showing result image
cv::Mat srcImage; // source image
};

///@brief current parameters
Params m_param;
};
168 changes: 168 additions & 0 deletions HarrisCornerDetector/HarrisCornerDetector.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.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">
<ProjectGuid>{8D088730-E07F-4B6A-8763-80689CEE7474}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>HarrisCornerDetector</RootNamespace>
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
<ProjectName>lesson2Sidorov</ProjectName>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</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" />
<Import Project="Debug.props" />
</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" />
<Import Project="Release.props" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>Use</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>Use</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<Text Include="ReadMe.txt" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="HarrisCornerDetector.h" />
<ClInclude Include="stdafx.h" />
<ClInclude Include="targetver.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="HarrisCornerDetector.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="stdafx.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
</ClCompile>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
Loading