ObjSignature.h
Go to the documentation of this file.
1 /*
2 Copyright (c) 2011-2014, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of the Universite de Sherbrooke nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #ifndef OBJSIGNATURE_H_
29 #define OBJSIGNATURE_H_
30 
31 #include <opencv2/opencv.hpp>
32 #include <QtCore/QString>
33 #include <QtCore/QMultiMap>
34 #include <QtCore/QRect>
35 #include <QtCore/QDataStream>
36 #include <QtCore/QByteArray>
37 #include <QtCore/QFileInfo>
38 #include <Compression.h>
39 
40 namespace find_object {
41 
42 class ObjSignature {
43 public:
45  id_(-1)
46  {}
47  ObjSignature(int id, const cv::Mat & image, const QString & filePath) :
48  id_(id),
49  image_(image),
50  rect_(0,0,image.cols, image.rows),
51  filePath_(filePath)
52  {}
53  virtual ~ObjSignature() {}
54 
55  void setData(const std::vector<cv::KeyPoint> & keypoints, const cv::Mat & descriptors)
56  {
59  }
60  void setWords(const QMultiMap<int, int> & words) {words_ = words;}
61  void setId(int id) {id_ = id;}
62  void removeImage() {image_ = cv::Mat();}
63 
64  const QRect & rect() const {return rect_;}
65 
66  int id() const {return id_;}
67  const QString & filePath() const {return filePath_;}
68  const cv::Mat & image() const {return image_;}
69  const std::vector<cv::KeyPoint> & keypoints() const {return keypoints_;}
70  const cv::Mat & descriptors() const {return descriptors_;}
71  const QMultiMap<int, int> & words() const {return words_;}
72 
73  void save(QDataStream & streamPtr) const
74  {
75  streamPtr << id_;
76  streamPtr << filePath_;
77  streamPtr << (int)keypoints_.size();
78  for(unsigned int j=0; j<keypoints_.size(); ++j)
79  {
80  streamPtr << keypoints_.at(j).angle <<
81  keypoints_.at(j).class_id <<
82  keypoints_.at(j).octave <<
83  keypoints_.at(j).pt.x <<
84  keypoints_.at(j).pt.y <<
85  keypoints_.at(j).response <<
86  keypoints_.at(j).size;
87  }
88 
89  std::vector<unsigned char> bytes = compressData(descriptors_);
90 
91  qint64 dataSize = bytes.size();
92  int old = 0;
93  if(dataSize <= std::numeric_limits<int>::max())
94  {
95  // old: rows, cols, type
96  streamPtr << old << old << old << dataSize;
97  streamPtr << QByteArray::fromRawData((const char*)bytes.data(), dataSize);
98  }
99  else
100  {
101  UERROR("Descriptors (compressed) are too large (%d MB) to be saved! Limit is 2 GB (based on max QByteArray size).",
102  dataSize/(1024*1024));
103  // old: rows, cols, type, dataSize
104  streamPtr << old << old << old << old;
105  streamPtr << QByteArray(); // empty
106  }
107 
108  streamPtr << words_;
109 
110  if(!image_.empty())
111  {
112  std::vector<unsigned char> bytes;
113  QString ext = QFileInfo(filePath_).suffix();
114  if(ext.isEmpty())
115  {
116  // default png
117  cv::imencode(".png", image_, bytes);
118  }
119  else
120  {
121  cv::imencode(std::string(".")+ext.toStdString(), image_, bytes);
122  }
123  streamPtr << QByteArray::fromRawData((const char*)bytes.data(), (int)bytes.size());
124  }
125  else
126  {
127  streamPtr << QByteArray();
128  }
129 
130  streamPtr << rect_;
131  }
132 
133  void load(QDataStream & streamPtr, bool ignoreImage)
134  {
135  int nKpts;
136  streamPtr >> id_ >> filePath_ >> nKpts;
137  keypoints_.resize(nKpts);
138  for(int i=0;i<nKpts;++i)
139  {
140  streamPtr >>
141  keypoints_[i].angle >>
142  keypoints_[i].class_id >>
143  keypoints_[i].octave >>
144  keypoints_[i].pt.x >>
145  keypoints_[i].pt.y >>
146  keypoints_[i].response >>
147  keypoints_[i].size;
148  }
149 
150  int rows,cols,type;
151  qint64 dataSize;
152  streamPtr >> rows >> cols >> type >> dataSize;
153  if(rows == 0 && cols == 0 && type == 0)
154  {
155  // compressed descriptors
156  UASSERT(dataSize <= std::numeric_limits<int>::max());
157  QByteArray data;
158  streamPtr >> data;
159  descriptors_ = uncompressData((unsigned const char*)data.data(), dataSize);
160  }
161  else
162  {
163  // old raw format
164  QByteArray data;
165  streamPtr >> data;
166  if(data.size())
167  {
168  descriptors_ = cv::Mat(rows, cols, type, data.data()).clone();
169  }
170  else if(dataSize)
171  {
172  UERROR("Error reading descriptor data for object=%d", id_);
173  }
174  }
175 
176  streamPtr >> words_;
177 
178  QByteArray image;
179  streamPtr >> image;
180  if(!ignoreImage && image.size())
181  {
182  std::vector<unsigned char> bytes(image.size());
183  memcpy(bytes.data(), image.data(), image.size());
184  image_ = cv::imdecode(bytes, cv::IMREAD_UNCHANGED);
185  }
186 
187  streamPtr >> rect_;
188  }
189 
190 private:
191  int id_;
192  cv::Mat image_;
193  QRect rect_;
194  QString filePath_;
195  std::vector<cv::KeyPoint> keypoints_;
196  cv::Mat descriptors_;
197  QMultiMap<int, int> words_; // <word id, keypoint indexes>
198 };
199 
200 } // namespace find_object
201 
202 #endif /* OBJSIGNATURE_H_ */
QMultiMap< int, int > words_
Definition: ObjSignature.h:197
void save(QDataStream &streamPtr) const
Definition: ObjSignature.h:73
cv::Mat uncompressData(const unsigned char *bytes, unsigned long size)
Definition: Compression.cpp:45
const QString & filePath() const
Definition: ObjSignature.h:67
const std::vector< cv::KeyPoint > & keypoints() const
Definition: ObjSignature.h:69
const cv::Mat & image() const
Definition: ObjSignature.h:68
void setWords(const QMultiMap< int, int > &words)
Definition: ObjSignature.h:60
#define UASSERT(condition)
ObjSignature(int id, const cv::Mat &image, const QString &filePath)
Definition: ObjSignature.h:47
std::vector< cv::KeyPoint > keypoints_
Definition: ObjSignature.h:195
void setData(const std::vector< cv::KeyPoint > &keypoints, const cv::Mat &descriptors)
Definition: ObjSignature.h:55
std::vector< unsigned char > compressData(const cv::Mat &data)
Definition: Compression.cpp:14
const cv::Mat & descriptors() const
Definition: ObjSignature.h:70
void load(QDataStream &streamPtr, bool ignoreImage)
Definition: ObjSignature.h:133
#define UERROR(...)
const QMultiMap< int, int > & words() const
Definition: ObjSignature.h:71
const QRect & rect() const
Definition: ObjSignature.h:64


find_object_2d
Author(s): Mathieu Labbe
autogenerated on Mon Dec 12 2022 03:20:09