00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef UCV2QT_H_
00021 #define UCV2QT_H_
00022
00023 #include <QtGui/QImage>
00024 #include <QtGui/QColor>
00025 #include <opencv2/core/core.hpp>
00026 #include <rtabmap/utilite/UMath.h>
00027 #include <rtabmap/utilite/UThread.h>
00028 #include <stdio.h>
00029
00030 enum uCvQtDepthColorMap{
00031 uCvQtDepthWhiteToBlack,
00032 uCvQtDepthBlackToWhite,
00033 uCvQtDepthRedToBlue,
00034 uCvQtDepthBlueToRed
00035 };
00036
00044 inline QImage uCvMat2QImage(const cv::Mat & image, bool isBgr = true, uCvQtDepthColorMap colorMap = uCvQtDepthWhiteToBlack)
00045 {
00046 QImage qtemp;
00047 if(!image.empty() && image.depth() == CV_8U)
00048 {
00049 if(image.channels()==3)
00050 {
00051 const unsigned char * data = image.data;
00052 if(image.channels() == 3)
00053 {
00054 qtemp = QImage(image.cols, image.rows, QImage::Format_RGB32);
00055 for(int y = 0; y < image.rows; ++y, data += image.cols*image.elemSize())
00056 {
00057 for(int x = 0; x < image.cols; ++x)
00058 {
00059 QRgb * p = ((QRgb*)qtemp.scanLine (y)) + x;
00060 if(isBgr)
00061 {
00062 *p = qRgb(data[x * image.channels()+2], data[x * image.channels()+1], data[x * image.channels()]);
00063 }
00064 else
00065 {
00066 *p = qRgb(data[x * image.channels()], data[x * image.channels()+1], data[x * image.channels()+2]);
00067 }
00068 }
00069 }
00070 }
00071 }
00072 else if(image.channels() == 1)
00073 {
00074
00075 qtemp = QImage(image.data, image.cols, image.rows, image.cols, QImage::Format_Indexed8).copy();
00076 QVector<QRgb> my_table;
00077 for(int i = 0; i < 256; i++)
00078 my_table.push_back(qRgb(i,i,i));
00079 qtemp.setColorTable(my_table);
00080 }
00081 else
00082 {
00083 printf("Wrong image format, must have 1 or 3 channels\n");
00084 }
00085 }
00086 else if(image.depth() == CV_32F && image.channels()==1)
00087 {
00088
00089 const float * data = (const float *)image.data;
00090 float min=data[0], max=data[0];
00091 for(unsigned int i=1; i<image.total(); ++i)
00092 {
00093 if(!uIsNan(data[i]) && data[i] > 0)
00094 {
00095 if((uIsNan(min) && data[i] > 0) ||
00096 (data[i] > 0 && data[i]<min))
00097 {
00098 min = data[i];
00099 }
00100 if((uIsNan(max) && data[i] > 0) ||
00101 (data[i] > 0 && data[i]>max))
00102 {
00103 max = data[i];
00104 }
00105 }
00106 }
00107 qtemp = QImage(image.cols, image.rows, QImage::Format_Indexed8);
00108 for(int y = 0; y < image.rows; ++y, data += image.cols)
00109 {
00110 for(int x = 0; x < image.cols; ++x)
00111 {
00112 uchar * p = qtemp.scanLine (y) + x;
00113 if(data[x] < min || data[x] > max || uIsNan(data[x]) || max == min)
00114 {
00115 *p = 0;
00116 }
00117 else
00118 {
00119 *p = uchar(255.0f - ((data[x]-min)*255.0f)/(max-min));
00120 if(*p == 255)
00121 {
00122 *p = 0;
00123 }
00124 }
00125 if(*p!=0 && (colorMap == uCvQtDepthBlackToWhite || colorMap == uCvQtDepthRedToBlue))
00126 {
00127 *p = 255-*p;
00128 }
00129 }
00130 }
00131
00132 QVector<QRgb> my_table;
00133 my_table.reserve(256);
00134 if(colorMap == uCvQtDepthRedToBlue || colorMap == uCvQtDepthBlueToRed)
00135 {
00136 my_table.push_back(qRgb(0,0,0));
00137 for(int i = 1; i < 256; i++)
00138 my_table.push_back(QColor::fromHsv(i, 255, 255, 255).rgb());
00139 }
00140 else
00141 {
00142 for(int i = 0; i < 256; i++)
00143 my_table.push_back(qRgb(i,i,i));
00144 }
00145 qtemp.setColorTable(my_table);
00146 }
00147 else if(image.depth() == CV_16U && image.channels()==1)
00148 {
00149
00150 const unsigned short * data = (const unsigned short *)image.data;
00151 unsigned short min=data[0], max=data[0];
00152 for(unsigned int i=1; i<image.total(); ++i)
00153 {
00154 if(!uIsNan(data[i]) && data[i] > 0)
00155 {
00156 if((uIsNan(min) && data[i] > 0) ||
00157 (data[i] > 0 && data[i]<min))
00158 {
00159 min = data[i];
00160 }
00161 if((uIsNan(max) && data[i] > 0) ||
00162 (data[i] > 0 && data[i]>max))
00163 {
00164 max = data[i];
00165 }
00166 }
00167 }
00168
00169 qtemp = QImage(image.cols, image.rows, QImage::Format_Indexed8);
00170 for(int y = 0; y < image.rows; ++y, data += image.cols)
00171 {
00172 for(int x = 0; x < image.cols; ++x)
00173 {
00174 uchar * p = qtemp.scanLine (y) + x;
00175 if(data[x] < min || data[x] > max || uIsNan(data[x]) || max == min)
00176 {
00177 *p = 0;
00178 }
00179 else
00180 {
00181 *p = uchar(255.0f - (float(data[x]-min)/float(max-min))*255.0f);
00182 if(*p == 255)
00183 {
00184 *p = 0;
00185 }
00186 }
00187 if(*p!=0 && (colorMap == uCvQtDepthBlackToWhite || colorMap == uCvQtDepthRedToBlue))
00188 {
00189 *p = 255-*p;
00190 }
00191 }
00192 }
00193
00194 QVector<QRgb> my_table;
00195 my_table.reserve(256);
00196 if(colorMap == uCvQtDepthRedToBlue || colorMap == uCvQtDepthBlueToRed)
00197 {
00198 my_table.push_back(qRgb(0,0,0));
00199 for(int i = 1; i < 256; i++)
00200 my_table.push_back(QColor::fromHsv(i, 255, 255, 255).rgb());
00201 }
00202 else
00203 {
00204 for(int i = 0; i < 256; i++)
00205 my_table.push_back(qRgb(i,i,i));
00206 }
00207 qtemp.setColorTable(my_table);
00208 }
00209 else if(!image.empty() && image.depth() != CV_8U)
00210 {
00211 printf("Wrong image format, must be 8_bits/3channels or (depth) 32bitsFloat/1channel, 16bits/1channel\n");
00212 }
00213 return qtemp;
00214 }
00215
00216 class UCvMat2QImageThread : public UThread
00217 {
00218 public:
00219 UCvMat2QImageThread(const cv::Mat & image, bool isBgr = true) :
00220 image_(image),
00221 isBgr_(isBgr) {}
00222 QImage & getQImage() {return qtImage_;}
00223 protected:
00224 virtual void mainLoop()
00225 {
00226 qtImage_ = uCvMat2QImage(image_, isBgr_);
00227 this->kill();
00228 }
00229 private:
00230 cv::Mat image_;
00231 bool isBgr_;
00232 QImage qtImage_;
00233 };
00234
00235 #endif