UCv2Qt.h
Go to the documentation of this file.
00001 /*
00002 *  utilite is a cross-platform library with
00003 *  useful utilities for fast and small developing.
00004 *  Copyright (C) 2010  Mathieu Labbe
00005 *
00006 *  utilite is free library: you can redistribute it and/or modify
00007 *  it under the terms of the GNU Lesser General Public License as published by
00008 *  the Free Software Foundation, either version 3 of the License, or
00009 *  (at your option) any later version.
00010 *
00011 *  utilite is distributed in the hope that it will be useful,
00012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 *  GNU Lesser General Public License for more details.
00015 *
00016 *  You should have received a copy of the GNU Lesser General Public License
00017 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 #ifndef UCV2QT_H_
00021 #define UCV2QT_H_
00022 
00023 #include <QtGui/QImage>
00024 #include <opencv2/core/core.hpp>
00025 #include <rtabmap/utilite/UMath.h>
00026 #include <rtabmap/utilite/UThread.h>
00027 #include <stdio.h>
00028 
00036 inline QImage uCvMat2QImage(const cv::Mat & image, bool isBgr = true)
00037 {
00038         QImage qtemp;
00039         if(!image.empty() && image.depth() == CV_8U)
00040         {
00041                 if(image.channels()==3)
00042                 {
00043                         const unsigned char * data = image.data;
00044                         if(image.channels() == 3)
00045                         {
00046                                 qtemp = QImage(image.cols, image.rows, QImage::Format_RGB32);
00047                                 for(int y = 0; y < image.rows; ++y, data += image.cols*image.elemSize())
00048                                 {
00049                                         for(int x = 0; x < image.cols; ++x)
00050                                         {
00051                                                 QRgb * p = ((QRgb*)qtemp.scanLine (y)) + x;
00052                                                 if(isBgr)
00053                                                 {
00054                                                         *p = qRgb(data[x * image.channels()+2], data[x * image.channels()+1], data[x * image.channels()]);
00055                                                 }
00056                                                 else
00057                                                 {
00058                                                         *p = qRgb(data[x * image.channels()], data[x * image.channels()+1], data[x * image.channels()+2]);
00059                                                 }
00060                                         }
00061                                 }
00062                         }
00063                 }
00064                 else if(image.channels() == 1)
00065                 {
00066                         // mono grayscale
00067                         qtemp = QImage(image.data, image.cols, image.rows, image.cols, QImage::Format_Indexed8).copy();
00068                         QVector<QRgb> my_table;
00069                         for(int i = 0; i < 256; i++) my_table.push_back(qRgb(i,i,i));
00070                         qtemp.setColorTable(my_table);
00071                 }
00072                 else
00073                 {
00074                         printf("Wrong image format, must have 1 or 3 channels\n");
00075                 }
00076         }
00077         else if(image.depth() == CV_32F && image.channels()==1)
00078     {
00079                 // Assume depth image (float in meters)
00080                 const float * data = (const float *)image.data;
00081                 float min=0, max=0;
00082                 uMinMax(data, image.rows*image.cols, min, max);
00083                 qtemp = QImage(image.cols, image.rows, QImage::Format_Indexed8);
00084                 for(int y = 0; y < image.rows; ++y, data += image.cols)
00085                 {
00086                         for(int x = 0; x < image.cols; ++x)
00087                         {
00088                                 uchar * p = qtemp.scanLine (y) + x;
00089                                 if(data[x] < min || data[x] > max || uIsNan(data[x]))
00090                                 {
00091                                         *p = 0;
00092                                 }
00093                                 else
00094                                 {
00095                                         *p = uchar(255.0f - ((data[x]-min)*255.0f)/(max-min));
00096                                         if(*p == 255)
00097                                         {
00098                                                 *p = 0;
00099                                         }
00100                                 }
00101                         }
00102                 }
00103 
00104                 QVector<QRgb> my_table;
00105                 for(int i = 0; i < 256; i++) my_table.push_back(qRgb(i,i,i));
00106                 qtemp.setColorTable(my_table);
00107     }
00108         else if(image.depth() == CV_16U && image.channels()==1)
00109         {
00110                 // Assume depth image (unsigned short in mm)
00111                 const unsigned short * data = (const unsigned short *)image.data;
00112                 unsigned short min=data[0], max=data[0];
00113                 for(unsigned int i=1; i<image.total(); ++i)
00114                 {
00115                         if(!uIsNan(data[i]) && data[i] > 0)
00116                         {
00117                                 if((uIsNan(min) && data[i] > 0) ||
00118                                    (data[i] > 0 && data[i]<min))
00119                                 {
00120                                         min = data[i];
00121                                 }
00122                                 if((uIsNan(max) && data[i] > 0) ||
00123                                    (data[i] > 0 && data[i]>max))
00124                                 {
00125                                         max = data[i];
00126                                 }
00127                         }
00128                 }
00129 
00130                 qtemp = QImage(image.cols, image.rows, QImage::Format_Indexed8);
00131                 for(int y = 0; y < image.rows; ++y, data += image.cols)
00132                 {
00133                         for(int x = 0; x < image.cols; ++x)
00134                         {
00135                                 uchar * p = qtemp.scanLine (y) + x;
00136                                 if(data[x] < min || data[x] > max || uIsNan(data[x]) || max == min)
00137                                 {
00138                                         *p = 0;
00139                                 }
00140                                 else
00141                                 {
00142                                         *p = uchar(255.0f - (float(data[x]-min)/float(max-min))*255.0f);
00143                                         if(*p == 255)
00144                                         {
00145                                                 *p = 0;
00146                                         }
00147                                 }
00148                         }
00149                 }
00150 
00151                 QVector<QRgb> my_table;
00152                 for(int i = 0; i < 256; i++) my_table.push_back(qRgb(i,i,i));
00153                 qtemp.setColorTable(my_table);
00154         }
00155         else if(!image.empty() && image.depth() != CV_8U)
00156         {
00157                 printf("Wrong image format, must be 8_bits/3channels or (depth) 32bitsFloat/1channel, 16bits/1channel\n");
00158         }
00159         return qtemp;
00160 }
00161 
00162 class UCvMat2QImageThread : public UThread
00163 {
00164 public:
00165         UCvMat2QImageThread(const cv::Mat & image, bool isBgr = true) :
00166                 image_(image),
00167                 isBgr_(isBgr) {}
00168         QImage & getQImage() {return qtImage_;}
00169 protected:
00170         virtual void mainLoop()
00171         {
00172                 qtImage_ = uCvMat2QImage(image_, isBgr_);
00173                 this->kill();
00174         }
00175 private:
00176         cv::Mat image_;
00177         bool isBgr_;
00178         QImage qtImage_;
00179 };
00180 
00181 #endif /* UCV2QT_H_ */


rtabmap
Author(s): Mathieu Labbe
autogenerated on Fri Aug 28 2015 12:51:42