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


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Jan 23 2023 03:38:58