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 
47 inline QImage uCvMat2QImage(
48  const cv::Mat & image,
49  bool isBgr = true,
51  float depthMin = 0,
52  float depthMax = 0)
53 {
54  QImage qtemp;
55  if(!image.empty() && image.depth() == CV_8U)
56  {
57  if(image.channels()==3)
58  {
59  const unsigned char * data = image.data;
60  if(image.channels() == 3)
61  {
62  qtemp = QImage(image.cols, image.rows, QImage::Format_RGB32);
63  for(int y = 0; y < image.rows; ++y, data += image.cols*image.elemSize())
64  {
65  for(int x = 0; x < image.cols; ++x)
66  {
67  QRgb * p = ((QRgb*)qtemp.scanLine (y)) + x;
68  if(isBgr)
69  {
70  *p = qRgb(data[x * image.channels()+2], data[x * image.channels()+1], data[x * image.channels()]);
71  }
72  else
73  {
74  *p = qRgb(data[x * image.channels()], data[x * image.channels()+1], data[x * image.channels()+2]);
75  }
76  }
77  }
78  }
79  }
80  else if(image.channels() == 1)
81  {
82  // mono grayscale
83  qtemp = QImage(image.data, image.cols, image.rows, image.cols, QImage::Format_Indexed8).copy();
84  QVector<QRgb> my_table;
85  for(int i = 0; i < 256; i++)
86  my_table.push_back(qRgb(i,i,i));
87  qtemp.setColorTable(my_table);
88  }
89  else
90  {
91  printf("Wrong image format, must have 1 or 3 channels\n");
92  }
93  }
94  else if(image.depth() == CV_32F && image.channels()==1)
95  {
96  // Assume depth image (float in meters)
97  const float * data = (const float *)image.data;
98  float min,max;
99  if(depthMax>depthMin)
100  {
101  min = depthMin;
102  max = depthMax;
103  }
104  else
105  {
106  min = max = data[0];
107  for(unsigned int i=1; i<image.total(); ++i)
108  {
109  if(uIsFinite(data[i]) && data[i] > 0)
110  {
111  if(!uIsFinite(min) || (data[i] > 0 && data[i]<min))
112  {
113  min = data[i];
114  }
115  if(!uIsFinite(max) || (data[i] > 0 && data[i]>max))
116  {
117  max = data[i];
118  }
119  }
120  }
121  if(depthMax > 0 && depthMax > depthMin)
122  {
123  max = depthMax;
124  }
125  if(depthMin>0 && (depthMin < depthMax || depthMin < max))
126  {
127  min = depthMin;
128  }
129  }
130 
131  qtemp = QImage(image.cols, image.rows, QImage::Format_Indexed8);
132  for(int y = 0; y < image.rows; ++y, data += image.cols)
133  {
134  for(int x = 0; x < image.cols; ++x)
135  {
136  uchar * p = qtemp.scanLine (y) + x;
137  if(!uIsFinite(data[x]) || max == min || data[x] == 0)
138  {
139  *p = 0;
140  }
141  else if(data[x] < min)
142  {
143  *p = 255;
144  }
145  else if(data[x] > max)
146  {
147  *p=1;
148  }
149  else
150  {
151  *p = uchar(std::max(1.0f, std::min(255.0f, 255.0f - ((data[x]-min)*255.0f)/(max-min))));
152  }
153  if(*p!=0 && (colorMap == uCvQtDepthBlackToWhite || colorMap == uCvQtDepthRedToBlue))
154  {
155  *p = 255-*p+1;
156  }
157  }
158  }
159 
160  QVector<QRgb> my_table;
161  my_table.reserve(256);
162  if(colorMap == uCvQtDepthRedToBlue || colorMap == uCvQtDepthBlueToRed)
163  {
164  my_table.push_back(qRgb(0,0,0));
165  for(int i = 1; i < 256; i++)
166  my_table.push_back(QColor::fromHsv(i, 255, 255, 255).rgb());
167  }
168  else
169  {
170  for(int i = 0; i < 256; i++)
171  my_table.push_back(qRgb(i,i,i));
172  }
173  qtemp.setColorTable(my_table);
174  }
175  else if(image.depth() == CV_16U && image.channels()==1)
176  {
177  // Assume depth image (unsigned short in mm)
178  const unsigned short * data = (const unsigned short *)image.data;
179  unsigned short min,max;
180  if(depthMax>depthMin)
181  {
182  min = depthMin*1000;
183  max = depthMax*1000;
184  }
185  else
186  {
187  min = max = data[0];
188  for(unsigned int i=1; i<image.total(); ++i)
189  {
190  if(uIsFinite(data[i]) && data[i] > 0)
191  {
192  if(!uIsFinite(min) || (data[i] > 0 && data[i]<min))
193  {
194  min = data[i];
195  }
196  if(!uIsFinite(max) || (data[i] > 0 && data[i]>max))
197  {
198  max = data[i];
199  }
200  }
201  }
202  if(depthMax > 0 && depthMax > depthMin)
203  {
204  max = depthMax*1000;
205  }
206  if(depthMin>0 && (depthMin < depthMax || depthMin*1000 < max))
207  {
208  min = depthMin*1000;
209  }
210  }
211 
212  qtemp = QImage(image.cols, image.rows, QImage::Format_Indexed8);
213  for(int y = 0; y < image.rows; ++y, data += image.cols)
214  {
215  for(int x = 0; x < image.cols; ++x)
216  {
217  uchar * p = qtemp.scanLine (y) + x;
218  if(!uIsFinite(data[x]) || max == min || data[x]==0)
219  {
220  *p = 0;
221  }
222  else if(data[x] < min)
223  {
224  *p = 255;
225  }
226  else if(data[x] > max)
227  {
228  *p = 1;
229  }
230  else
231  {
232  *p = uchar(std::max(1.0f, std::min(255.0f, 255.0f - (float(data[x]-min)/float(max-min))*255.0f)));
233  }
234  if(*p!=0 && (colorMap == uCvQtDepthBlackToWhite || colorMap == uCvQtDepthRedToBlue))
235  {
236  *p = 255-*p+1;
237  }
238  }
239  }
240 
241  QVector<QRgb> my_table;
242  my_table.reserve(256);
243  if(colorMap == uCvQtDepthRedToBlue || colorMap == uCvQtDepthBlueToRed)
244  {
245  my_table.push_back(qRgb(0,0,0));
246  for(int i = 1; i < 256; i++)
247  my_table.push_back(QColor::fromHsv(i, 255, 255, 255).rgb());
248  }
249  else
250  {
251  for(int i = 0; i < 256; i++)
252  my_table.push_back(qRgb(i,i,i));
253  }
254  qtemp.setColorTable(my_table);
255  }
256  else if(!image.empty() && image.depth() != CV_8U)
257  {
258  printf("Wrong image format, must be 8_bits/3channels or (depth) 32bitsFloat/1channel, 16bits/1channel\n");
259  }
260  return qtemp;
261 }
262 
264 {
265 public:
266  UCvMat2QImageThread(const cv::Mat & image, bool isBgr = true) :
267  image_(image),
268  isBgr_(isBgr) {}
269  QImage & getQImage() {return qtImage_;}
270 protected:
271  virtual void mainLoop()
272  {
274  this->kill();
275  }
276 private:
277  cv::Mat image_;
278  bool isBgr_;
279  QImage qtImage_;
280 };
281 
282 #endif /* UCV2QT_H_ */
glm::min
GLM_FUNC_DECL genType min(genType const &x, genType const &y)
rtflann::uchar
unsigned char uchar
Definition: matrix.h:69
uCvQtDepthRedToBlue
@ uCvQtDepthRedToBlue
Definition: UCv2Qt.h:33
UCvMat2QImageThread::qtImage_
QImage qtImage_
Definition: UCv2Qt.h:279
uCvQtDepthColorMap
uCvQtDepthColorMap
Definition: UCv2Qt.h:30
y
Matrix3f y
UCvMat2QImageThread::image_
cv::Mat image_
Definition: UCv2Qt.h:277
uCvQtDepthBlueToRed
@ uCvQtDepthBlueToRed
Definition: UCv2Qt.h:34
UMath.h
Basic mathematics functions.
data
int data[]
glm::max
GLM_FUNC_DECL genType max(genType const &x, genType const &y)
UThread::kill
void kill()
Definition: UThread.cpp:48
UCvMat2QImageThread::getQImage
QImage & getQImage()
Definition: UCv2Qt.h:269
UCvMat2QImageThread::isBgr_
bool isBgr_
Definition: UCv2Qt.h:278
x
x
p
Point3_ p(2)
UCvMat2QImageThread::mainLoop
virtual void mainLoop()
Definition: UCv2Qt.h:271
f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
uCvQtDepthWhiteToBlack
@ uCvQtDepthWhiteToBlack
Definition: UCv2Qt.h:31
UCvMat2QImageThread::UCvMat2QImageThread
UCvMat2QImageThread(const cv::Mat &image, bool isBgr=true)
Definition: UCv2Qt.h:266
UThread
Definition: UThread.h:86
UThread.h
uCvMat2QImage
QImage uCvMat2QImage(const cv::Mat &image, bool isBgr=true, uCvQtDepthColorMap colorMap=uCvQtDepthWhiteToBlack, float depthMin=0, float depthMax=0)
Definition: UCv2Qt.h:47
uCvQtDepthBlackToWhite
@ uCvQtDepthBlackToWhite
Definition: UCv2Qt.h:32
UCvMat2QImageThread
Definition: UCv2Qt.h:263
i
int i
uIsFinite
bool uIsFinite(const T &value)
Definition: UMath.h:53


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jul 25 2024 02:50:23