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(!uIsNan(data[i]) && data[i] > 0)
94  {
95  if((uIsNan(min) && data[i] > 0) ||
96  (data[i] > 0 && data[i]<min))
97  {
98  min = data[i];
99  }
100  if((uIsNan(max) && data[i] > 0) ||
101  (data[i] > 0 && data[i]>max))
102  {
103  max = data[i];
104  }
105  }
106  }
107  qtemp = QImage(image.cols, image.rows, QImage::Format_Indexed8);
108  for(int y = 0; y < image.rows; ++y, data += image.cols)
109  {
110  for(int x = 0; x < image.cols; ++x)
111  {
112  uchar * p = qtemp.scanLine (y) + x;
113  if(data[x] < min || data[x] > max || uIsNan(data[x]) || max == min)
114  {
115  *p = 0;
116  }
117  else
118  {
119  *p = uchar(255.0f - ((data[x]-min)*255.0f)/(max-min));
120  if(*p == 255)
121  {
122  *p = 0;
123  }
124  }
125  if(*p!=0 && (colorMap == uCvQtDepthBlackToWhite || colorMap == uCvQtDepthRedToBlue))
126  {
127  *p = 255-*p;
128  }
129  }
130  }
131 
132  QVector<QRgb> my_table;
133  my_table.reserve(256);
134  if(colorMap == uCvQtDepthRedToBlue || colorMap == uCvQtDepthBlueToRed)
135  {
136  my_table.push_back(qRgb(0,0,0));
137  for(int i = 1; i < 256; i++)
138  my_table.push_back(QColor::fromHsv(i, 255, 255, 255).rgb());
139  }
140  else
141  {
142  for(int i = 0; i < 256; i++)
143  my_table.push_back(qRgb(i,i,i));
144  }
145  qtemp.setColorTable(my_table);
146  }
147  else if(image.depth() == CV_16U && image.channels()==1)
148  {
149  // Assume depth image (unsigned short in mm)
150  const unsigned short * data = (const unsigned short *)image.data;
151  unsigned short min=data[0], max=data[0];
152  for(unsigned int i=1; i<image.total(); ++i)
153  {
154  if(!uIsNan(data[i]) && data[i] > 0)
155  {
156  if((uIsNan(min) && data[i] > 0) ||
157  (data[i] > 0 && data[i]<min))
158  {
159  min = data[i];
160  }
161  if((uIsNan(max) && data[i] > 0) ||
162  (data[i] > 0 && data[i]>max))
163  {
164  max = data[i];
165  }
166  }
167  }
168 
169  qtemp = QImage(image.cols, image.rows, QImage::Format_Indexed8);
170  for(int y = 0; y < image.rows; ++y, data += image.cols)
171  {
172  for(int x = 0; x < image.cols; ++x)
173  {
174  uchar * p = qtemp.scanLine (y) + x;
175  if(data[x] < min || data[x] > max || uIsNan(data[x]) || max == min)
176  {
177  *p = 0;
178  }
179  else
180  {
181  *p = uchar(255.0f - (float(data[x]-min)/float(max-min))*255.0f);
182  if(*p == 255)
183  {
184  *p = 0;
185  }
186  }
187  if(*p!=0 && (colorMap == uCvQtDepthBlackToWhite || colorMap == uCvQtDepthRedToBlue))
188  {
189  *p = 255-*p;
190  }
191  }
192  }
193 
194  QVector<QRgb> my_table;
195  my_table.reserve(256);
196  if(colorMap == uCvQtDepthRedToBlue || colorMap == uCvQtDepthBlueToRed)
197  {
198  my_table.push_back(qRgb(0,0,0));
199  for(int i = 1; i < 256; i++)
200  my_table.push_back(QColor::fromHsv(i, 255, 255, 255).rgb());
201  }
202  else
203  {
204  for(int i = 0; i < 256; i++)
205  my_table.push_back(qRgb(i,i,i));
206  }
207  qtemp.setColorTable(my_table);
208  }
209  else if(!image.empty() && image.depth() != CV_8U)
210  {
211  printf("Wrong image format, must be 8_bits/3channels or (depth) 32bitsFloat/1channel, 16bits/1channel\n");
212  }
213  return qtemp;
214 }
215 
217 {
218 public:
219  UCvMat2QImageThread(const cv::Mat & image, bool isBgr = true) :
220  image_(image),
221  isBgr_(isBgr) {}
222  QImage & getQImage() {return qtImage_;}
223 protected:
224  virtual void mainLoop()
225  {
227  this->kill();
228  }
229 private:
230  cv::Mat image_;
231  bool isBgr_;
232  QImage qtImage_;
233 };
234 
235 #endif /* UCV2QT_H_ */
GLM_FUNC_DECL genType min(genType const &x, genType const &y)
virtual void mainLoop()
Definition: UCv2Qt.h:224
f
unsigned char uchar
Definition: matrix.h:41
void kill()
Definition: UThread.cpp:48
Basic mathematics functions.
cv::Mat image_
Definition: UCv2Qt.h:230
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:219
GLM_FUNC_DECL genType max(genType const &x, genType const &y)
QImage & getQImage()
Definition: UCv2Qt.h:222
bool uIsNan(const T &value)
Definition: UMath.h:42
uCvQtDepthColorMap
Definition: UCv2Qt.h:30


rtabmap
Author(s): Mathieu Labbe
autogenerated on Wed Jun 5 2019 22:43:40