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 
38 namespace find_object {
39 
40 class ObjSignature {
41 public:
43  id_(-1)
44  {}
45  ObjSignature(int id, const cv::Mat & image, const QString & filePath) :
46  id_(id),
47  image_(image),
48  rect_(0,0,image.cols, image.rows),
49  filePath_(filePath)
50  {}
51  virtual ~ObjSignature() {}
52 
53  void setData(const std::vector<cv::KeyPoint> & keypoints, const cv::Mat & descriptors)
54  {
57  }
58  void setWords(const QMultiMap<int, int> & words) {words_ = words;}
59  void setId(int id) {id_ = id;}
60  void removeImage() {image_ = cv::Mat();}
61 
62  const QRect & rect() const {return rect_;}
63 
64  int id() const {return id_;}
65  const QString & filePath() const {return filePath_;}
66  const cv::Mat & image() const {return image_;}
67  const std::vector<cv::KeyPoint> & keypoints() const {return keypoints_;}
68  const cv::Mat & descriptors() const {return descriptors_;}
69  const QMultiMap<int, int> & words() const {return words_;}
70 
71  void save(QDataStream & streamPtr) const
72  {
73  streamPtr << id_;
74  streamPtr << filePath_;
75  streamPtr << (int)keypoints_.size();
76  for(unsigned int j=0; j<keypoints_.size(); ++j)
77  {
78  streamPtr << keypoints_.at(j).angle <<
79  keypoints_.at(j).class_id <<
80  keypoints_.at(j).octave <<
81  keypoints_.at(j).pt.x <<
82  keypoints_.at(j).pt.y <<
83  keypoints_.at(j).response <<
84  keypoints_.at(j).size;
85  }
86 
87  qint64 dataSize = descriptors_.elemSize()*descriptors_.cols*descriptors_.rows;
88  streamPtr << descriptors_.rows <<
89  descriptors_.cols <<
90  descriptors_.type() <<
91  dataSize;
92  streamPtr << QByteArray((char*)descriptors_.data, dataSize);
93 
94  streamPtr << words_;
95 
96  std::vector<unsigned char> bytes;
97  cv::imencode(".png", image_, bytes);
98  streamPtr << QByteArray((char*)bytes.data(), (int)bytes.size());
99 
100  streamPtr << rect_;
101  }
102 
103  void load(QDataStream & streamPtr, bool ignoreImage)
104  {
105  int nKpts;
106  streamPtr >> id_ >> filePath_ >> nKpts;
107  keypoints_.resize(nKpts);
108  for(int i=0;i<nKpts;++i)
109  {
110  streamPtr >>
111  keypoints_[i].angle >>
112  keypoints_[i].class_id >>
113  keypoints_[i].octave >>
114  keypoints_[i].pt.x >>
115  keypoints_[i].pt.y >>
116  keypoints_[i].response >>
117  keypoints_[i].size;
118  }
119 
120  int rows,cols,type;
121  qint64 dataSize;
122  streamPtr >> rows >> cols >> type >> dataSize;
123  QByteArray data;
124  streamPtr >> data;
125  descriptors_ = cv::Mat(rows, cols, type, data.data()).clone();
126 
127  streamPtr >> words_;
128 
129  QByteArray image;
130  streamPtr >> image;
131  if(!ignoreImage)
132  {
133  std::vector<unsigned char> bytes(image.size());
134  memcpy(bytes.data(), image.data(), image.size());
135  image_ = cv::imdecode(bytes, cv::IMREAD_UNCHANGED);
136  }
137 
138  streamPtr >> rect_;
139  }
140 
141 private:
142  int id_;
143  cv::Mat image_;
144  QRect rect_;
145  QString filePath_;
146  std::vector<cv::KeyPoint> keypoints_;
147  cv::Mat descriptors_;
148  QMultiMap<int, int> words_; // <word id, keypoint indexes>
149 };
150 
151 } // namespace find_object
152 
153 #endif /* OBJSIGNATURE_H_ */
QMultiMap< int, int > words_
Definition: ObjSignature.h:148
void save(QDataStream &streamPtr) const
Definition: ObjSignature.h:71
const cv::Mat & descriptors() const
Definition: ObjSignature.h:68
void setWords(const QMultiMap< int, int > &words)
Definition: ObjSignature.h:58
ObjSignature(int id, const cv::Mat &image, const QString &filePath)
Definition: ObjSignature.h:45
const cv::Mat & image() const
Definition: ObjSignature.h:66
std::vector< cv::KeyPoint > keypoints_
Definition: ObjSignature.h:146
void setData(const std::vector< cv::KeyPoint > &keypoints, const cv::Mat &descriptors)
Definition: ObjSignature.h:53
const QMultiMap< int, int > & words() const
Definition: ObjSignature.h:69
const QString & filePath() const
Definition: ObjSignature.h:65
const std::vector< cv::KeyPoint > & keypoints() const
Definition: ObjSignature.h:67
void load(QDataStream &streamPtr, bool ignoreImage)
Definition: ObjSignature.h:103
const QRect & rect() const
Definition: ObjSignature.h:62


find_object_2d
Author(s): Mathieu Labbe
autogenerated on Mon Jun 10 2019 13:21:31