TrackingError.cpp
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::TrackingError
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 #include "TrackingError.h"
24 
25 
27 
28 using namespace armarx;
29 
30 
32 {
35 }
36 
37 
39 {
40  providerName = getProperty<std::string>("providerName").getValue();
41  usingImageProvider(providerName);
42 
43  offeringTopic(getProperty<std::string>("TrackingErrorTopicName").getValue());
44 
45  offeringTopic(getProperty<std::string>("DebugObserverName").getValue());
46 
47  frameRate = getProperty<float>("Framerate").getValue();
48 
49  useChessboard = getProperty<bool>("UseChessBoard").getValue();
50 
51  chessboardWidth = getProperty<int>("Chessboard.Width").getValue();
52  chessboardHeight = getProperty<int>("Chessboard.Height").getValue();
53 }
54 
56 {
57  std::unique_lock lock(imageMutex);
58 
59  visionx::ImageProviderInfo imageProviderInfo = getImageProvider(providerName);
60  imageProviderPrx = getProxy<visionx::ImageProviderInterfacePrx>(providerName);
61 
62  cameraImages = new CByteImage*[2];
63  cameraImages[0] = visionx::tools::createByteImage(imageProviderInfo);
64  cameraImages[1] = visionx::tools::createByteImage(imageProviderInfo);
65 
66 
67  debugObserver = getTopic<DebugObserverInterfacePrx>(getProperty<std::string>("DebugObserverName").getValue());
68 
69  prx = getTopic<TrackingErrorListenerPrx>(getProperty<std::string>("TrackingErrorTopicName").getValue());
70 
71  imageWidth = imageProviderPrx->getImageFormat().dimension.width;
72  imageHeight = imageProviderPrx->getImageFormat().dimension.height;
73 
74  enableResultImages(1, imageProviderPrx->getImageFormat().dimension, imageProviderPrx->getImageFormat().type);
75 }
76 
78 {
79 }
80 
82 {
83  std::unique_lock lock(imageMutex);
84 
85  if (!waitForImages(getProperty<std::string>("providerName").getValue(), 1000))
86  {
87  ARMARX_WARNING << "Timeout while waiting for camera images (>1000ms)";
88  return;
89  }
90 
91  int numImages = getImages(cameraImages);
92 
93  if (numImages == 0)
94  {
95  ARMARX_WARNING << "Didn't receive one image! Aborting!";
96  return;
97  }
98 
99  IplImage* ppIplImages[1] = { IplImageAdaptor::Adapt(cameraImages[0]) };
100 
101  // convert to gray
102  cv::Mat grayImage;
103  cv::Mat resultImage = cv::cvarrToMat(ppIplImages[0]);
104  cv::cvtColor(resultImage, grayImage, cv::COLOR_BGR2GRAY);
105 
106  std::vector< cv::Point2f > features;
107 
108  cv::Size size = cv::Size(chessboardWidth, chessboardHeight);
109 
110 
111  if (useChessboard)
112  {
113  // + cv::CALIB_CB_FAST_CHECK)
114  bool foundCorners = cv::findChessboardCorners(grayImage, size, features, cv::CALIB_CB_ADAPTIVE_THRESH + cv::CALIB_CB_NORMALIZE_IMAGE);
115 
116  if (foundCorners)
117  {
118  float avgX = 0.f, avgY = 0.f;
119  for (cv::Point2f point : features)
120  {
121  avgX += point.x;
122  avgY += point.y;
123  }
124  avgX /= features.size();
125  avgY /= features.size();
126 
127  cv::Point2f imgCenter(imageWidth / 2, imageHeight / 2);
128  cv::Point2f chessboardCenter(avgX, avgY);
129  cv::Point2f point(avgX, imageHeight / 2);
130  cv::circle(resultImage, chessboardCenter, 2, CV_RGB(255, 0, 0), -1);
131  cv::circle(resultImage, imgCenter, 2, CV_RGB(255, 0, 0), -1);
132  cv::line(resultImage, chessboardCenter, imgCenter, CV_RGB(0, 0, 255), 1);
133  cv::line(resultImage, chessboardCenter, point, CV_RGB(0, 255, 0), 1);
134  cv::line(resultImage, imgCenter, point, CV_RGB(0, 255, 0), 1);
135 
136  float trackingErrorX = avgX - (imageWidth / 2);
137  float trackingErrorY = avgY - (imageHeight / 2);
138 
139  float angleX = (trackingErrorX / imageWidth) * 59.7;
140  float angleY = (trackingErrorY / imageHeight) * 44.775;
141 
142 
143  StringVariantBaseMap debugValues;
144  debugValues["trackingErrorX"] = new Variant(trackingErrorX);
145  debugValues["trackingErrorY"] = new Variant(trackingErrorY);
146  debugValues["angleX"] = new Variant(angleX);
147  debugValues["angleY"] = new Variant(angleY);
148 
149 
150  debugObserver->setDebugChannel("TrackingError", debugValues);
151 
152 
153  prx->reportNewTrackingError(trackingErrorX, trackingErrorY, angleX, angleY);
154 
155 
156  }
157  else
158  {
159  ARMARX_WARNING << deactivateSpam(3) << "Chessboard NOT found!";
160  return;
161  }
162  }
163  else
164  {
165  //TODO: calculate tracking error with vectors
166  }
167 
168 
169  CByteImage* resultImages[1] = { IplImageAdaptor::Adapt(ppIplImages[0]) };
170  provideResultImages(resultImages);
171 
172  if (frameRate > 0.0)
173  {
174  fpsCounter.assureFPS(frameRate);
175  }
176 
177 }
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:224
armarx::StringVariantBaseMap
std::map< std::string, VariantBasePtr > StringVariantBaseMap
Definition: ManagedIceObject.h:111
armarx::TrackingErrorPropertyDefinitions
Definition: TrackingError.h:51
visionx::tools::createByteImage
CByteImage * createByteImage(const ImageFormatInfo &imageFormat, const ImageType imageType)
Creates a ByteImage for the destination type specified in the given imageProviderInfo.
armarx::TrackingError::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: TrackingError.cpp:31
visionx::ImageProviderInfo
Definition: ImageProcessor.h:466
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:72
ObserverObjectFactories.h
armarx::TrackingError::process
void process() override
Process the vision component.
Definition: TrackingError.cpp:81
armarx::TrackingError::onExitImageProcessor
void onExitImageProcessor() override
Exit the ImapeProcessor component.
Definition: TrackingError.cpp:77
TrackingError.h
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:74
armarx::TrackingError::onInitImageProcessor
void onInitImageProcessor() override
Setup the vision component.
Definition: TrackingError.cpp:38
IceUtil::Handle< class PropertyDefinitionContainer >
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:34
armarx::TrackingError::onConnectImageProcessor
void onConnectImageProcessor() override
Implement this method in the ImageProcessor in order execute parts when the component is fully initia...
Definition: TrackingError.cpp:55
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28