ImageKeypointBuffer.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 <PACKAGE_NAME>::<CATEGORY>::ImageKeypointBuffer
17  * @author Stefan Reither ( stef dot reither at web dot de )
18  * @date 2018
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #include "ImageKeypointBuffer.h"
24 #include <Image/ImageProcessor.h>
25 
26 using namespace armarx;
27 
29 {
30  this->_imageProviderInfo = imageProviderInfo;
31 }
32 
34 {
35  _buffer.clear();
36 }
37 
38 bool ImageKeypointBuffer::addRGBImage(CByteImage* rgbImage, long timestamp)
39 {
40  std::unique_lock lock(_bufferMutex);
41  ImageKeypointTriplePtr triple = ensureTriple(timestamp);
42  triple->rgbImage = new CByteImage(_imageProviderInfo.imageFormat.dimension.width, _imageProviderInfo.imageFormat.dimension.height, CByteImage::eRGB24);
43  ::ImageProcessor::CopyImage(rgbImage, triple->rgbImage);
44 
45  return isComplete(timestamp);
46 }
47 
48 bool ImageKeypointBuffer::addDepthImage(CByteImage* depthImage, long timestamp)
49 {
50  std::unique_lock lock(_bufferMutex);
51  ImageKeypointTriplePtr triple = ensureTriple(timestamp);
52  triple->depthImage = new CByteImage(_imageProviderInfo.imageFormat.dimension.width, _imageProviderInfo.imageFormat.dimension.height, CByteImage::eRGB24);
53  ::ImageProcessor::CopyImage(depthImage, triple->depthImage);
54 
55  return isComplete(timestamp);
56 }
57 
59 {
60  std::unique_lock lock(_bufferMutex);
61  ImageKeypointTriplePtr triple = ensureTriple(timestamp);
62  triple->keypoints.reset(new KeypointManager(*keypoints));
63 
64  return isComplete(timestamp);
65 }
66 
68 {
69  std::unique_lock lock(_bufferMutex);
70  ImageKeypointTriplePtr triple = ensureTriple(timestamp);
71  if (triple->keypoints && triple->depthImage && triple->rgbImage)
72  {
73  std::map<long, ImageKeypointBuffer::ImageKeypointTriplePtr>::iterator it = _buffer.erase(_buffer.find(timestamp));
74 
75  if (deleteOlderTriples)
76  {
77  deleteOlderEntries(it);
78  }
79  return triple;
80  }
81  else
82  {
83  return ImageKeypointTriplePtr();
84  }
85 }
86 
87 std::pair<long, ImageKeypointBuffer::ImageKeypointTriplePtr> ImageKeypointBuffer::getOldestTriple()
88 {
89  std::unique_lock lock(_bufferMutex);
90  if (_buffer.size() > 0)
91  {
92  auto pair = *_buffer.begin();
93  _buffer.erase(_buffer.begin());
94  return std::move(pair);
95  }
96  else
97  {
98  return std::make_pair(0, ImageKeypointTriplePtr());
99  }
100 }
101 
102 std::pair<long, ImageKeypointBuffer::ImageKeypointTriplePtr> ImageKeypointBuffer::getOldestCompleteTriple(bool deleteOlderTriples)
103 {
104  std::unique_lock lock(_bufferMutex);
105  // Iterate from the beginning over all entries until a complete one is found
106  std::map<long, ImageKeypointBuffer::ImageKeypointTriplePtr>::iterator it = _buffer.begin();
107  while (!isComplete((*it).first))
108  {
109  it++;
110  }
111  auto pair = *it;
112 
113  if (deleteOlderTriples)
114  {
115  deleteOlderEntries(it);
116  }
117  return std::move(pair);
118 }
119 
121 {
122  // remove all entires
123  _buffer.clear();
124 }
125 
126 ImageKeypointBuffer::ImageKeypointTriplePtr ImageKeypointBuffer::ensureTriple(long timestamp)
127 {
128  if (_buffer.count(timestamp) == 0)
129  {
130  _buffer.insert(std::make_pair(timestamp, std::make_shared<ImageKeypointTriple>()));
131  }
132  return _buffer.at(timestamp);
133 }
134 
135 bool ImageKeypointBuffer::isComplete(long timestamp)
136 {
137  ImageKeypointTriplePtr triple = ensureTriple(timestamp);
138  return (triple->keypoints && triple->depthImage && triple->rgbImage) ? true : false;
139 }
140 
141 void ImageKeypointBuffer::deleteOlderEntries(std::map<long, ImageKeypointBuffer::ImageKeypointTriplePtr>::iterator it)
142 {
143  if (it == _buffer.end())
144  {
145  _buffer.clear();
146  }
147  else
148  {
149  _buffer.erase(_buffer.begin(), it);
150  }
151 }
armarx::ImageKeypointBuffer::getOldestCompleteTriple
std::pair< long, ImageKeypointBuffer::ImageKeypointTriplePtr > getOldestCompleteTriple(bool deleteOlderTriples=true)
Definition: ImageKeypointBuffer.cpp:102
armarx::ImageKeypointBuffer::addDepthImage
bool addDepthImage(CByteImage *depthImage, long timestamp)
Definition: ImageKeypointBuffer.cpp:48
armarx::ImageKeypointBuffer::addKeypoints
bool addKeypoints(KeypointManagerPtr keypoints, long timestamp)
Definition: ImageKeypointBuffer.cpp:58
armarx::KeypointManagerPtr
std::shared_ptr< KeypointManager > KeypointManagerPtr
Definition: KeypointManager.h:159
armarx::KeypointManager
A brief description.
Definition: KeypointManager.h:166
armarx::ImageKeypointBuffer::getTripleAtTimestamp
ImageKeypointTriplePtr getTripleAtTimestamp(long timestamp, bool deleteOlderTriples=true)
Definition: ImageKeypointBuffer.cpp:67
armarx::ImageKeypointBuffer::ImageKeypointBuffer
ImageKeypointBuffer(const visionx::ImageProviderInfo &imageProviderInfo)
ImageKeypointBuffer Constructor.
Definition: ImageKeypointBuffer.cpp:28
visionx::ImageProviderInfo::imageFormat
ImageFormatInfo imageFormat
Image format struct that contains all necessary image information.
Definition: ImageProcessor.h:496
armarx::ImageKeypointBuffer::getOldestTriple
std::pair< long, ImageKeypointBuffer::ImageKeypointTriplePtr > getOldestTriple()
Definition: ImageKeypointBuffer.cpp:87
armarx::ImageKeypointBuffer::clear
void clear()
Definition: ImageKeypointBuffer.cpp:120
visionx::ImageProviderInfo
Definition: ImageProcessor.h:466
ImageKeypointBuffer.h
armarx::ImageKeypointBuffer::~ImageKeypointBuffer
~ImageKeypointBuffer()
ImageKeypointBuffer Destructor.
Definition: ImageKeypointBuffer.cpp:33
armarx::ImageKeypointBuffer::ImageKeypointTriplePtr
std::shared_ptr< ImageKeypointTriple > ImageKeypointTriplePtr
Definition: ImageKeypointBuffer.h:61
armarx::ImageKeypointBuffer::addRGBImage
bool addRGBImage(CByteImage *rgbImage, long timestamp)
Definition: ImageKeypointBuffer.cpp:38
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28