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 
27 
29 
33 
34 #include <VisionX/interface/components/Calibration.h>
35 #include <VisionX/interface/components/OpticalFlowInterface.h>
36 
37 #include <opencv2/opencv.hpp>
38 
39 #include <Image/IplImageAdaptor.h>
40 
41 #include <Eigen/Core>
42 #include <Eigen/Geometry>
43 
44 #include <mutex>
45 #include <queue>
46 
47 
48 namespace armarx
49 {
50 
52  {
54  };
55 
56  /**
57  * @class OpticalFlowPropertyDefinitions
58  * @brief
59  */
62  {
63  public:
66  {
67  //defineRequiredProperty<std::string>("PropertyName", "Description");
68  //defineOptionalProperty<std::string>("PropertyName", "DefaultValue", "Description");
69  defineOptionalProperty<float>("Framerate", 0.0, "the framerate");
70  defineOptionalProperty<std::string>("providerName", "Armar3ImageProvider", "ImageProvider name");
71  defineOptionalProperty<OPTICAL_FLOW_METHOD>("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");
78  defineOptionalProperty<std::string>("OpticalFlowTopicName", "OpticalFlowTopic", "OpticalFlowTopicName name");
79  defineOptionalProperty<std::string>("DebugObserverName", "DebugObserver", "Name of the topic the DebugObserver listens on");
80  defineOptionalProperty<std::string>("RobotNodeSetName", "Robot", "Name of the RobotNodeSet");
81 
82  defineOptionalProperty<std::string>("CalibrationUpdateTopicName", "StereoCalibrationInterface", "Topic name of the stereo calibration provider");
83  }
84  };
85 
86  /**
87  * @defgroup Component-OpticalFlow OpticalFlow
88  * @ingroup VisionX-Components
89  * A description of the component OpticalFlow.
90  *
91  * @class OpticalFlow
92  * @ingroup Component-OpticalFlow
93  * @brief Brief description of class OpticalFlow.
94  *
95  * Detailed description of class OpticalFlow.
96  */
97  class OpticalFlow :
98  virtual public visionx::ImageProcessor,
99  virtual public OpticalFlowInterface
100  {
101  public:
102  /**
103  * @see armarx::ManagedIceObject::getDefaultName()
104  */
105  std::string getDefaultName() const override
106  {
107  return "OpticalFlow";
108  }
109 
110  void reportStereoCalibrationChanged(const visionx::StereoCalibration& stereoCalibration, bool, const std::string&, const Ice::Current& c = Ice::emptyCurrent) override
111  {
112 
113  std::unique_lock lock(imageMutex);
114 
115  float f_x = stereoCalibration.calibrationRight.cameraParam.focalLength[0];
116  float f_y = stereoCalibration.calibrationRight.cameraParam.focalLength[1];
117 
118  unitFactorX = 2.0 * std::atan(imageDimension.width / (2.0 * f_x)) / imageDimension.width;
119  unitFactorY = 2.0 * std::atan(imageDimension.height / (2.0 * f_y)) / imageDimension.height;
120 
121  previousTime = 0;
122  }
123 
124 
125  protected:
126  /**
127  * @see PropertyUser::createPropertyDefinitions()
128  */
130 
131  // ImageProcessor interface
132  protected:
133  void onInitImageProcessor() override;
134  void onConnectImageProcessor() override;
135  void onExitImageProcessor() override;
136  void process() override;
137 
138  private:
139 
140  std::vector<float> removeHighestAndLowestMember(std::vector<float>& input);
141  void drawDenseFlowMap(const cv::Mat& flow, cv::Mat& cflowmap, int step, const cv::Scalar& color);
142  void computeAverageDenseFlow(const cv::Mat& flow, double& mean, double& rmse);
143  void ComputeOpticalFlow(cv::Mat resultImage, cv::Mat grayImage);
144 
145  CByteImage** cameraImages;
146  std::string providerName;
147  std::mutex imageMutex;
148  cv::Mat previousImage, old2;
149  std::vector< cv::Point2f > previousFeatures;
150  visionx::ImageProviderInterfacePrx imageProviderPrx;
151  OpticalFlowListenerPrx prx;
152  visionx::ImageDimension imageDimension;
153 
154  float resultX, resultY, timeDiff;
155 
156  Ice::Long previousTime;
157 
158  int chessboardWidth, chessboardHeight;
159 
160  visionx::FPSCounter fpsCounter;
161 
162 
163  DebugObserverInterfacePrx debugObserver;
164 
165  float frameRate;
166  float unitFactorX, unitFactorY;
167 
168  OPTICAL_FLOW_METHOD method;
169  };
170 }
171 
Feature
Definition: Feature.hpp:44
armarx::OpticalFlowPropertyDefinitions
Definition: OpticalFlow.h:60
DebugObserver.h
armarx::OpticalFlow::reportStereoCalibrationChanged
void reportStereoCalibrationChanged(const visionx::StereoCalibration &stereoCalibration, bool, const std::string &, const Ice::Current &c=Ice::emptyCurrent) override
Definition: OpticalFlow.h:110
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
visionx::ImageProcessor
The ImageProcessor class provides an interface for access to ImageProviders via Ice and shared memory...
Definition: ImageProcessor.h:87
armarx::OPTICAL_FLOW_METHOD
OPTICAL_FLOW_METHOD
Definition: OpticalFlow.h:51
armarx::PropertyDefinitionContainer::prefix
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
Definition: PropertyDefinitionContainer.h:333
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
visionx::ImageProcessorPropertyDefinitions::ImageProcessorPropertyDefinitions
ImageProcessorPropertyDefinitions(std::string prefix)
Definition: ImageProcessor.h:65
armarx::Dense
@ Dense
Definition: OpticalFlow.h:53
armarx::Chessboard
@ Chessboard
Definition: OpticalFlow.h:53
armarx::mean
std::optional< float > mean(const boost::circular_buffer< NameValueMap > &buffer, const std::string &key)
Definition: KinematicUnitGuiPlugin.cpp:1615
ImageProcessor.h
armarx::aron::input
ReaderT::InputType & input
Definition: rw.h:19
armarx::VariantType::Long
const VariantTypeId Long
Definition: Variant.h:917
armarx::OpticalFlow::process
void process() override
Process the vision component.
Definition: OpticalFlow.cpp:99
armarx::OpticalFlow::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: OpticalFlow.cpp:31
Component.h
armarx::OpticalFlow::onConnectImageProcessor
void onConnectImageProcessor() override
Implement this method in the ImageProcessor in order execute parts when the component is fully initia...
Definition: OpticalFlow.cpp:57
armarx::OpticalFlow::getDefaultName
std::string getDefaultName() const override
Definition: OpticalFlow.h:105
armarx::OpticalFlow
Brief description of class OpticalFlow.
Definition: OpticalFlow.h:97
armarx::OpticalFlow::onExitImageProcessor
void onExitImageProcessor() override
Exit the ImapeProcessor component.
Definition: OpticalFlow.cpp:95
IceUtil::Handle< class PropertyDefinitionContainer >
visionx::ImageProcessorPropertyDefinitions
Definition: ImageProcessor.h:61
Scalar
float Scalar
Definition: basic.h:14
IceInternal::ProxyHandle<::IceProxy::armarx::DebugObserverInterface >
ImageUtil.h
armarx::OpticalFlowPropertyDefinitions::OpticalFlowPropertyDefinitions
OpticalFlowPropertyDefinitions(std::string prefix)
Definition: OpticalFlow.h:64
armarx::OpticalFlow::onInitImageProcessor
void onInitImageProcessor() override
Setup the vision component.
Definition: OpticalFlow.cpp:39
FPSCounter.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
visionx::FPSCounter
The FPSCounter class provides methods for calculating the frames per second (FPS) count in periodic t...
Definition: FPSCounter.h:36