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

Added Kalman Filter example #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
13 changes: 13 additions & 0 deletions samples/ocvKalman/include/Resources.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once
#include "cinder/CinderResources.h"

//#define RES_MY_RES CINDER_RESOURCE( ../resources/, image_name.png, 128, IMAGE )









Binary file added samples/ocvKalman/resources/CinderApp.icns
Binary file not shown.
Binary file added samples/ocvKalman/resources/CinderApp_ios.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added samples/ocvKalman/resources/cinder_app_icon.ico
Binary file not shown.
125 changes: 125 additions & 0 deletions samples/ocvKalman/src/ocvKalmanApp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#include "cinder/app/App.h"
#include "cinder/app/RendererGl.h"
#include "cinder/gl/gl.h"

#include "CinderOpenCV.h"

using namespace ci;
using namespace ci::app;
using namespace std;

class ocvKalmanApp : public App {
public:
void setup() override;
void mouseDrag( MouseEvent event ) override;
void update() override;
void draw() override;

class KalmanFilter {

protected:
cv::KalmanFilter mKF;
cv::Mat_<float> mMeasurement;
cv::Point mPrediction;

public:

KalmanFilter( cv::Point initialPt )
{
mKF = cv::KalmanFilter( 4, 2, 0 );
mKF.transitionMatrix = ( cv::Mat_<float>(4, 4) << 1,0,1,0, 0,1,0,1, 0,0,1,0, 0,0,0,1 );
mMeasurement = cv::Mat_<float>( 2, 1 );
mMeasurement.setTo( cv::Scalar( 0 ) );

// init...
mKF.statePre.at<float>( 0 ) = initialPt.x;
mKF.statePre.at<float>( 1 ) = initialPt.y;
mKF.statePre.at<float>( 2 ) = 0;
mKF.statePre.at<float>( 3 ) = 0;
setIdentity( mKF.measurementMatrix );
setIdentity( mKF.processNoiseCov, cv::Scalar::all( 1e-4 ) );
setIdentity( mKF.measurementNoiseCov, cv::Scalar::all( 1e-1 ) );
setIdentity( mKF.errorCovPost, cv::Scalar::all( .1 ));

mPrediction = initialPt;

// prime the filter, otherwise it thinks it's at 0,0
correct( initialPt );
}

cv::Point updatePrediction()
{
cv::Mat prediction = mKF.predict();
mPrediction = cv::Point( prediction.at<float>(0), prediction.at<float>( 1 ) );

return mPrediction;
}

cv::Point correct( cv::Point pt )
{
// update point
mMeasurement( 0 ) = pt.x;
mMeasurement( 1 ) = pt.y;

cv::Point measPt( mMeasurement( 0 ), mMeasurement( 1 ) );

// The "correct" phase that is going to use the predicted value and our measurement
cv::Mat estimated = mKF.correct( mMeasurement );
cv::Point statePt( estimated.at<float>( 0 ), estimated.at<float>( 1 ) );

mPrediction = statePt;

return mPrediction;
}
}; // class KalmanFilter

KalmanFilter *mFilter;
vector<vec2> mMousePoints;
vector<vec2> mKalmanPoints;

}; // class ocvKalmanApp


void ocvKalmanApp::setup()
{
setFrameRate( 30.0f );
}

void ocvKalmanApp::mouseDrag( MouseEvent event )
{
mMousePoints.push_back( event.getPos() );

if( mKalmanPoints.empty() ) {
mFilter = new KalmanFilter( toOcv( event.getPos() ) );
mKalmanPoints.push_back( event.getPos() );
} else {
mFilter->correct( toOcv( event.getPos() ) );
mKalmanPoints.push_back( fromOcv( mFilter->updatePrediction() ) );
console() << mKalmanPoints.back() << endl;
}
}

void ocvKalmanApp::update()
{
}

void ocvKalmanApp::draw()
{
gl::clear( Color( 0, 0, 0 ) );

gl::color( 1.0f, 0.0f, 0.0f );
gl::begin( GL_LINE_STRIP );
for( const vec2 &point : mMousePoints ) {
gl::vertex( point );
}
gl::end();

gl::color( 1.0f, 1.0f, 0.0f );
gl::begin( GL_LINE_STRIP );
for( const vec2 &point : mKalmanPoints ) {
gl::vertex( point );
}
gl::end();
}

CINDER_APP( ocvKalmanApp, RendererGl )
3 changes: 3 additions & 0 deletions samples/ocvKalman/vc2013/Resources.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "../include/Resources.h"

1 ICON "..\\resources\\cinder_app_icon.ico"
20 changes: 20 additions & 0 deletions samples/ocvKalman/vc2013/ocvKalman.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ocvKalman", "ocvKalman.vcxproj", "{2A74E0BA-B60B-49C2-8142-EC9C70554A17}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{2A74E0BA-B60B-49C2-8142-EC9C70554A17}.Debug|x64.ActiveCfg = Debug|x64
{2A74E0BA-B60B-49C2-8142-EC9C70554A17}.Debug|x64.Build.0 = Debug|x64
{2A74E0BA-B60B-49C2-8142-EC9C70554A17}.Release|x64.ActiveCfg = Release|x64
{2A74E0BA-B60B-49C2-8142-EC9C70554A17}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
112 changes: 112 additions & 0 deletions samples/ocvKalman/vc2013/ocvKalman.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<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>{2A74E0BA-B60B-49C2-8142-EC9C70554A17}</ProjectGuid>
<RootNamespace>ocvKalman</RootNamespace>
<Keyword>Win32Proj</Keyword>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v120</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
<WholeProgramOptimization>true</WholeProgramOptimization>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v120</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings" />
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup>
<_ProjectFileVersion>10.0.30319.1</_ProjectFileVersion>
<LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</LinkIncremental>
<LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<Optimization>Disabled</Optimization>
<AdditionalIncludeDirectories>..\include;"..\..\..\..\..\include";..\..\..\include</AdditionalIncludeDirectories>
<PreprocessorDefinitions>WIN32;_WIN32_WINNT=0x0601;_WINDOWS;NOMINMAX;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<PrecompiledHeader />
<WarningLevel>Level3</WarningLevel>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
</ClCompile>
<ResourceCompile>
<AdditionalIncludeDirectories>"..\..\..\..\..\include";..\include</AdditionalIncludeDirectories>
</ResourceCompile>
<Link>
<AdditionalDependencies>cinder-$(PlatformToolset)_d.lib;OpenGL32.lib;%(AdditionalDependencies);..\..\..\lib\vc2013\x86\opencv_calib3d300d.lib;..\..\..\lib\vc2013\x86\opencv_core300d.lib;..\..\..\lib\vc2013\x86\opencv_features2d300d.lib;..\..\..\lib\vc2013\x86\opencv_flann300d.lib;..\..\..\lib\vc2013\x86\opencv_hal300d.lib;..\..\..\lib\vc2013\x86\opencv_imgproc300d.lib;..\..\..\lib\vc2013\x86\opencv_ml300d.lib;..\..\..\lib\vc2013\x86\opencv_objdetect300d.lib;..\..\..\lib\vc2013\x86\opencv_photo300d.lib;..\..\..\lib\vc2013\x86\opencv_shape300d.lib;..\..\..\lib\vc2013\x86\opencv_stitching300d.lib;..\..\..\lib\vc2013\x86\opencv_superres300d.lib;..\..\..\lib\vc2013\x86\opencv_ts300d.lib;..\..\..\lib\vc2013\x86\opencv_video300d.lib;..\..\..\lib\vc2013\x86\opencv_videostab300d.lib;..\..\..\lib\vc2013\x86\ippicvmt.lib</AdditionalDependencies>
<AdditionalLibraryDirectories>"..\..\..\..\..\lib\msw\$(PlatformTarget)"</AdditionalLibraryDirectories>
<GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Windows</SubSystem>
<RandomizedBaseAddress>false</RandomizedBaseAddress>
<DataExecutionPrevention />
<IgnoreSpecificDefaultLibraries>LIBCMT;LIBCPMT</IgnoreSpecificDefaultLibraries>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<AdditionalIncludeDirectories>..\include;"..\..\..\..\..\include";..\..\..\include</AdditionalIncludeDirectories>
<PreprocessorDefinitions>WIN32;_WIN32_WINNT=0x0601;_WINDOWS;NOMINMAX;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<PrecompiledHeader />
<WarningLevel>Level3</WarningLevel>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
</ClCompile>
<ProjectReference>
<LinkLibraryDependencies>true</LinkLibraryDependencies>
</ProjectReference>
<ResourceCompile>
<AdditionalIncludeDirectories>"..\..\..\..\..\include";..\include</AdditionalIncludeDirectories>
</ResourceCompile>
<Link>
<AdditionalDependencies>cinder-$(PlatformToolset).lib;OpenGL32.lib;%(AdditionalDependencies);..\..\..\lib\vc2013\x86\opencv_calib3d300.lib;..\..\..\lib\vc2013\x86\opencv_core300.lib;..\..\..\lib\vc2013\x86\opencv_features2d300.lib;..\..\..\lib\vc2013\x86\opencv_flann300.lib;..\..\..\lib\vc2013\x86\opencv_hal300.lib;..\..\..\lib\vc2013\x86\opencv_imgproc300.lib;..\..\..\lib\vc2013\x86\opencv_ml300.lib;..\..\..\lib\vc2013\x86\opencv_objdetect300.lib;..\..\..\lib\vc2013\x86\opencv_photo300.lib;..\..\..\lib\vc2013\x86\opencv_shape300.lib;..\..\..\lib\vc2013\x86\opencv_stitching300.lib;..\..\..\lib\vc2013\x86\opencv_superres300.lib;..\..\..\lib\vc2013\x86\opencv_ts300.lib;..\..\..\lib\vc2013\x86\opencv_video300.lib;..\..\..\lib\vc2013\x86\opencv_videostab300.lib;..\..\..\lib\vc2013\x86\ippicvmt.lib</AdditionalDependencies>
<AdditionalLibraryDirectories>"..\..\..\..\..\lib\msw\$(PlatformTarget)"</AdditionalLibraryDirectories>
<GenerateDebugInformation>false</GenerateDebugInformation>
<GenerateMapFile>true</GenerateMapFile>
<SubSystem>Windows</SubSystem>
<OptimizeReferences>true</OptimizeReferences>
<EnableCOMDATFolding />
<RandomizedBaseAddress>false</RandomizedBaseAddress>
<DataExecutionPrevention />
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ResourceCompile Include="Resources.rc" />
</ItemGroup>
<ItemGroup />
<ItemGroup />
<ItemGroup>
<ClCompile Include="..\src\ocvKalmanApp.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\include\Resources.h" />
<ClInclude Include="..\..\..\include\CinderOpenCV.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets" />
</Project>
49 changes: 49 additions & 0 deletions samples/ocvKalman/vc2013/ocvKalman.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<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</Extensions>
</Filter>
<Filter Include="Blocks">
<UniqueIdentifier>{0D429678-E93C-4003-B82E-680E84A9DA85}</UniqueIdentifier>
</Filter>
<Filter Include="Blocks\OpenCV3">
<UniqueIdentifier>{32C40B97-A5FC-42FA-9E1D-6C294CAD206C}</UniqueIdentifier>
</Filter>
<Filter Include="Blocks\OpenCV3\include">
<UniqueIdentifier>{7BD4CBB0-5935-48F8-9CBC-1FADC1874EB8}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\src\ocvKalmanApp.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\ocvKalmanApp.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClInclude Include="..\include\Resources.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\..\include\CinderOpenCV.h">
<Filter>Blocks\OpenCV3\include</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\include\Resources.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="Resources.rc">
<Filter>Resource Files</Filter>
</ResourceCompile>
</ItemGroup>
</Project>
34 changes: 34 additions & 0 deletions samples/ocvKalman/xcode/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIconFile</key>
<string>CinderApp.icns</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>${PRODUCT_NAME}</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSMinimumSystemVersion</key>
<string>${MACOSX_DEPLOYMENT_TARGET}</string>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2015 __MyCompanyName__. All rights reserved.</string>
<key>NSMainNibFile</key>
<string>MainMenu</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
</dict>
</plist>
Loading