UCv2Qt.h
Go to the documentation of this file.
1 /*
2 * utilite is a cross-platform library with
3 * useful utilities for fast and small developing.
4 * Copyright (C) 2010 Mathieu Labbe
5 *
6 * utilite is free library: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * utilite is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef UCV2QT_H_
21 #define UCV2QT_H_
22 
23 #include <QtGui/QImage>
24 #include <QtGui/QColor>
25 #include <opencv2/core/core.hpp>
26 #include <rtabmap/utilite/UMath.h>
28 #include <stdio.h>
29 
35 };
36 
44 inline QImage uCvMat2QImage(const cv::Mat & image, bool isBgr = true, uCvQtDepthColorMap colorMap = uCvQtDepthWhiteToBlack)
45 {
46  QImage qtemp;
47  if(!image.empty() && image.depth() == CV_8U)
48  {
49  if(image.channels()==3)
50  {
51  const unsigned char * data = image.data;
52  if(image.channels() == 3)
53  {
54  qtemp = QImage(image.cols, image.rows, QImage::Format_RGB32);
55  for(int y = 0; y < image.rows; ++y, data += image.cols*image.elemSize())
56  {
57  for(int x = 0; x < image.cols; ++x)
58  {
59  QRgb * p = ((QRgb*)qtemp.scanLine (y)) + x;
60  if(isBgr)
61  {
62  *p = qRgb(data[x * image.channels()+2], data[x * image.channels()+1], data[x * image.channels()]);
63  }
64  else
65  {
66  *p = qRgb(data[x * image.channels()], data[x * image.channels()+1], data[x * image.channels()+2]);
67  }
68  }
69  }
70  }
71  }
72  else if(image.channels() == 1)
73  {
74  // mono grayscale
75  qtemp = QImage(image.data, image.cols, image.rows, image.cols, QImage::Format_Indexed8).copy();
76  QVector<QRgb> my_table;
77  for(int i = 0; i < 256; i++)
78  my_table.push_back(qRgb(i,i,i));
79  qtemp.setColorTable(my_table);
80  }
81  else
82  {
83  printf("Wrong image format, must have 1 or 3 channels\n");
84  }
85  }
86  else if(image.depth() == CV_32F && image.channels()==1)
87  {
88  // Assume depth image (float in meters)
89  const float * data = (const float *)image.data;
90  float min=data[0], max=data[0];
91  for(unsigned int i=1; i<image.total(); ++i)
92  {
93  if(uIsFinite(data[i]) && data[i] > 0)
94  {
95  if(!uIsFinite(min) || (data[i] > 0 && data[i]<min))
96  {
97  min = data[i];
98  }
99  if(!uIsFinite(max) || (data[i] > 0 && data[i]>max))
100  {
101  max = data[i];
102  }
103  }
104  }
105 
106  qtemp = QImage(image.cols, image.rows, QImage::Format_Indexed8);
107  for(int y = 0; y < image.rows; ++y, data += image.cols)
108  {
109  for(int x = 0; x < image.cols; ++x)
110  {
111  uchar * p = qtemp.scanLine (y) + x;
112  if(data[x] < min || data[x] > max || !uIsFinite(data[x]) || max == min)
113  {
114  *p = 0;
115  }
116  else
117  {
118  *p = uchar(255.0f - ((data[x]-min)*255.0f)/(max-min));
119  if(*p == 255)
120  {
121  *p = 0;
122  }
123  }
124  if(*p!=0 && (colorMap == uCvQtDepthBlackToWhite || colorMap == uCvQtDepthRedToBlue))
125  {
126  *p = 255-*p;
127  }
128  }
129  }
130 
131  QVector<QRgb> my_table;
132  my_table.reserve(256);
133  if(colorMap == uCvQtDepthRedToBlue || colorMap == uCvQtDepthBlueToRed)
134  {
135  my_table.push_back(qRgb(0,0,0));
136  for(int i = 1; i < 256; i++)
137  my_table.push_back(QColor::fromHsv(i, 255, 255, 255).rgb());
138  }
139  else
140  {
141  for(int i = 0; i < 256; i++)
142  my_table.push_back(qRgb(i,i,i));
143  }
144  qtemp.setColorTable(my_table);
145  }
146  else if(image.depth() == CV_16U && image.channels()==1)
147  {
148  // Assume depth image (unsigned short in mm)
149  const unsigned short * data = (const unsigned short *)image.data;
150  unsigned short min=data[0], max=data[0];
151  for(unsigned int i=1; i<image.total(); ++i)
152  {
153  if(uIsFinite(data[i]) && data[i] > 0)
154  {
155  if(!uIsFinite(min) || (data[i] > 0 && data[i]<min))
156  {
157  min = data[i];
158  }
159  if(!uIsFinite(max) || (data[i] > 0 && data[i]>max))
160  {
161  max = data[i];
162  }
163  }
164  }
165 
166  qtemp = QImage(image.cols, image.rows, QImage::Format_Indexed8);
167  for(int y = 0; y < image.rows; ++y, data += image.cols)
168  {
169  for(int x = 0; x < image.cols; ++x)
170  {
171  uchar * p = qtemp.scanLine (y) + x;
172  if(data[x] < min || data[x] > max || !uIsFinite(data[x]) || max == min)
173  {
174  *p = 0;
175  }
176  else
177  {
178  *p = uchar(255.0f - (float(data[x]-min)/float(max-min))*255.0f);
179  if(*p == 255)
180  {
181  *p = 0;
182  }
183  }
184  if(*p!=0 && (colorMap == uCvQtDepthBlackToWhite || colorMap == uCvQtDepthRedToBlue))
185  {
186  *p = 255-*p;
187  }
188  }
189  }
190 
191  QVector<QRgb> my_table;
192  my_table.reserve(256);
193  if(colorMap == uCvQtDepthRedToBlue || colorMap == uCvQtDepthBlueToRed)
194  {
195  my_table.push_back(qRgb(0,0,0));
196  for(int i = 1; i < 256; i++)
197  my_table.push_back(QColor::fromHsv(i, 255, 255, 255).rgb());
198  }
199  else
200  {
201  for(int i = 0; i < 256; i++)
202  my_table.push_back(qRgb(i,i,i));
203  }
204  qtemp.setColorTable(my_table);
205  }
206  else if(!image.empty() && image.depth() != CV_8U)
207  {
208  printf("Wrong image format, must be 8_bits/3channels or (depth) 32bitsFloat/1channel, 16bits/1channel\n");
209  }
210  return qtemp;
211 }
212 
214 {
215 public:
216  UCvMat2QImageThread(const cv::Mat & image, bool isBgr = true) :
217  image_(image),
218  isBgr_(isBgr) {}
219  QImage & getQImage() {return qtImage_;}
220 protected:
221  virtual void mainLoop()
222  {
224  this->kill();
225  }
226 private:
227  cv::Mat image_;
228  bool isBgr_;
229  QImage qtImage_;
230 };
231 
232 #endif /* UCV2QT_H_ */
GLM_FUNC_DECL genType min(genType const &x, genType const &y)
virtual void mainLoop()
Definition: UCv2Qt.h:221
f
unsigned char uchar
Definition: matrix.h:41
void kill()
Definition: UThread.cpp:48
Basic mathematics functions.
cv::Mat image_
Definition: UCv2Qt.h:227
bool uIsFinite(const T &value)
Definition: UMath.h:55
QImage uCvMat2QImage(const cv::Mat &image, bool isBgr=true, uCvQtDepthColorMap colorMap=uCvQtDepthWhiteToBlack)
Definition: UCv2Qt.h:44
UCvMat2QImageThread(const cv::Mat &image, bool isBgr=true)
Definition: UCv2Qt.h:216
GLM_FUNC_DECL genType max(genType const &x, genType const &y)
QImage & getQImage()
Definition: UCv2Qt.h:219
uCvQtDepthColorMap
Definition: UCv2Qt.h:30


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Dec 14 2020 03:37:06