OpticalFlow.h
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package VisionX::ArmarXObjects::OpticalFlow
17 * @author David Sippel ( uddoe at student dot kit dot edu )
18 * @date 2016
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22
23#pragma once
24
25
26#include <mutex>
27#include <queue>
28
29#include <Eigen/Core>
30#include <Eigen/Geometry>
31
32#include <opencv2/opencv.hpp>
33
36
38#include <VisionX/interface/components/Calibration.h>
39#include <VisionX/interface/components/OpticalFlowInterface.h>
42
43#include <Image/IplImageAdaptor.h>
44
45namespace armarx
46{
47
54
55 /**
56 * @class OpticalFlowPropertyDefinitions
57 * @brief
58 */
60 {
61 public:
64 {
65 //defineRequiredProperty<std::string>("PropertyName", "Description");
66 //defineOptionalProperty<std::string>("PropertyName", "DefaultValue", "Description");
67 defineOptionalProperty<float>("Framerate", 0.0, "the framerate");
69 "providerName", "Armar3ImageProvider", "ImageProvider name");
71 "Method", Chessboard, "use chessboard for feature tracking")
72 .map("Chessboard", Chessboard)
73 .map("Dense", Dense)
74 .map("Feature", Feature);
75
76 defineOptionalProperty<int>("Chessboard.Width", 7, "Chessboard width");
77 defineOptionalProperty<int>("Chessboard.Height", 5, "Chessboard height");
79 "OpticalFlowTopicName", "OpticalFlowTopic", "OpticalFlowTopicName name");
80 defineOptionalProperty<std::string>("DebugObserverName",
81 "DebugObserver",
82 "Name of the topic the DebugObserver listens on");
84 "RobotNodeSetName", "Robot", "Name of the RobotNodeSet");
85
86 defineOptionalProperty<std::string>("CalibrationUpdateTopicName",
87 "StereoCalibrationInterface",
88 "Topic name of the stereo calibration provider");
89 }
90 };
91
92 /**
93 * @defgroup Component-OpticalFlow OpticalFlow
94 * @ingroup VisionX-Components
95 * A description of the component OpticalFlow.
96 *
97 * @class OpticalFlow
98 * @ingroup Component-OpticalFlow
99 * @brief Brief description of class OpticalFlow.
100 *
101 * Detailed description of class OpticalFlow.
102 */
103 class OpticalFlow : virtual public visionx::ImageProcessor, virtual public OpticalFlowInterface
104 {
105 public:
106 /**
107 * @see armarx::ManagedIceObject::getDefaultName()
108 */
109 static std::string
111 {
112 return "OpticalFlow";
113 }
114
115 std::string
116 getDefaultName() const override
117 {
118 return GetDefaultName();
119 }
120
121 void
122 reportStereoCalibrationChanged(const visionx::StereoCalibration& stereoCalibration,
123 bool,
124 const std::string&,
125 const Ice::Current& c = Ice::emptyCurrent) override
126 {
127
128 std::unique_lock lock(imageMutex);
129
130 float f_x = stereoCalibration.calibrationRight.cameraParam.focalLength[0];
131 float f_y = stereoCalibration.calibrationRight.cameraParam.focalLength[1];
132
133 unitFactorX =
134 2.0 * std::atan(imageDimension.width / (2.0 * f_x)) / imageDimension.width;
135 unitFactorY =
136 2.0 * std::atan(imageDimension.height / (2.0 * f_y)) / imageDimension.height;
137
138 previousTime = 0;
139 }
140
141
142 protected:
143 /**
144 * @see PropertyUser::createPropertyDefinitions()
145 */
147
148 // ImageProcessor interface
149 protected:
150 void onInitImageProcessor() override;
151 void onConnectImageProcessor() override;
152 void onExitImageProcessor() override;
153 void process() override;
154
155 private:
156 std::vector<float> removeHighestAndLowestMember(std::vector<float>& input);
157 void
158 drawDenseFlowMap(const cv::Mat& flow, cv::Mat& cflowmap, int step, const cv::Scalar& color);
159 void computeAverageDenseFlow(const cv::Mat& flow, double& mean, double& rmse);
160 void ComputeOpticalFlow(cv::Mat resultImage, cv::Mat grayImage);
161
162 CByteImage** cameraImages;
163 std::string providerName;
164 std::mutex imageMutex;
165 cv::Mat previousImage, old2;
166 std::vector<cv::Point2f> previousFeatures;
167 visionx::ImageProviderInterfacePrx imageProviderPrx;
168 OpticalFlowListenerPrx prx;
169 visionx::ImageDimension imageDimension;
170
171 float resultX, resultY, timeDiff;
172
173 Ice::Long previousTime;
174
175 int chessboardWidth, chessboardHeight;
176
177 visionx::FPSCounter fpsCounter;
178
179
180 DebugObserverInterfacePrx debugObserver;
181
182 float frameRate;
183 float unitFactorX, unitFactorY;
184
185 OPTICAL_FLOW_METHOD method;
186 };
187} // namespace armarx
constexpr T c
OpticalFlowPropertyDefinitions(std::string prefix)
Definition OpticalFlow.h:62
Brief description of class OpticalFlow.
void onConnectImageProcessor() override
Implement this method in the ImageProcessor in order execute parts when the component is fully initia...
void onExitImageProcessor() override
Exit the ImapeProcessor component.
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
void process() override
Process the vision component.
void onInitImageProcessor() override
Setup the vision component.
void reportStereoCalibrationChanged(const visionx::StereoCalibration &stereoCalibration, bool, const std::string &, const Ice::Current &c=Ice::emptyCurrent) override
static std::string GetDefaultName()
std::string getDefaultName() const override
Retrieve default name of component.
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
PropertyDefinition< PropertyType > & defineOptionalProperty(const std::string &name, PropertyType defaultValue, const std::string &description="", PropertyDefinitionBase::PropertyConstness constness=PropertyDefinitionBase::eConstant)
The FPSCounter class provides methods for calculating the frames per second (FPS) count in periodic t...
Definition FPSCounter.h:37
ImageProcessorPropertyDefinitions(std::string prefix)
The ImageProcessor class provides an interface for access to ImageProviders via Ice and shared memory...
This file offers overloads of toIce() and fromIce() functions for STL container types.
::IceInternal::ProxyHandle<::IceProxy::armarx::DebugObserverInterface > DebugObserverInterfacePrx
std::optional< float > mean(const boost::circular_buffer< NameValueMap > &buffer, const std::string &key)
OPTICAL_FLOW_METHOD
Definition OpticalFlow.h:49
@ Chessboard
Definition OpticalFlow.h:50
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
ArmarX headers.