opencv.cpp
Go to the documentation of this file.
00001 
00007 /*****************************************************************************
00008 ** Includes
00009 *****************************************************************************/
00010 
00011 #include <QDebug>  // for qWarning
00012 #include <QImage>
00013 #include <QPixmap>
00014 
00015 #include <opencv2/imgproc/imgproc.hpp>
00016 #include <opencv2/imgproc/types_c.h>
00017 
00018 #include "../../include/qglv/opencv.hpp"
00019 
00020 // only for testing
00021 // #include <cv_backports/imshow.hpp>
00022 
00023 /*****************************************************************************
00024 ** Namespaces
00025 *****************************************************************************/
00026 
00027 namespace qglv {
00028 namespace opencv {
00029 
00030 /*****************************************************************************
00031 ** Implementation
00032 *****************************************************************************/
00033 
00034 std::pair<cv::Mat, QImage> matToQImage(const cv::Mat &inMat)
00035 {
00036   switch (inMat.type())
00037   {
00038     // 8-bit, 4 channel
00039     case CV_8UC4:
00040     {
00041       QImage image(inMat.data, inMat.cols, inMat.rows, inMat.step, QImage::Format_RGB32);
00042 
00043       return std::pair<cv::Mat, QImage>(inMat, image);
00044     }
00045 
00046       // 8-bit, 3 channel
00047     case CV_8UC3:
00048     {
00049       QImage image(inMat.data, inMat.cols, inMat.rows, inMat.step, QImage::Format_RGB888);
00050 
00051       return std::pair<cv::Mat, QImage>(inMat, image.rgbSwapped());
00052     }
00053 
00054       // 8-bit, 1 channel
00055     case CV_8UC1:
00056     {
00057       static QVector<QRgb> sColorTable;
00058 
00059       // only create our color table once
00060       if (sColorTable.isEmpty())
00061       {
00062         for (int i = 0; i < 256; ++i)
00063           sColorTable.push_back(qRgb(i, i, i));
00064       }
00065 
00066       QImage image(inMat.data, inMat.cols, inMat.rows, inMat.step, QImage::Format_Indexed8);
00067 
00068       image.setColorTable(sColorTable);
00069 
00070       return std::pair<cv::Mat, QImage>(inMat, image);
00071     }
00072 
00073     case CV_16UC1:  // typically a depth image, convert to 8UC1 and normalize it
00074     {
00075       // no idea how to put a 16bit grayscale into a qimage - thre doesn't seem to be a
00076       // type for that.
00077       cv::Mat downsized_image;
00078 
00079       // normalise
00080       double min;
00081       double max;
00082       cv::minMaxIdx(inMat, &min, &max);
00083       inMat.convertTo(downsized_image, CV_8UC1, 255/(max-min), -min);
00084 
00085       static QVector<QRgb> sColorTable;
00086 
00087       // only create our color table once
00088       if (sColorTable.isEmpty())
00089       {
00090         for (int i = 0; i < 256; ++i)
00091           sColorTable.push_back(qRgb(i, i, i));
00092       }
00093 
00094       QImage image(downsized_image.data, downsized_image.cols, downsized_image.rows, downsized_image.step, QImage::Format_Indexed8);
00095 
00096       image.setColorTable(sColorTable);
00097 
00098       return std::pair<cv::Mat, QImage>(downsized_image, image);
00099     }
00100 
00101     default:
00102       qWarning() << "qglv::cv::matToQImage() - cv::Mat image type not handled in switch:" << inMat.type();
00103       break;
00104   }
00105 
00106   return std::pair<cv::Mat, QImage>(cv::Mat(), QImage());
00107 }
00108 
00109 std::pair<cv::Mat, QPixmap> matToQPixmap(const cv::Mat &inMat)
00110 {
00111   std::pair<cv::Mat, QImage> pair = matToQImage(inMat);
00112   return std::pair<cv::Mat, QPixmap>(pair.first, QPixmap::fromImage(pair.second));
00113 }
00114 
00115 } // namespace cv
00116 } // namespace qglv


qglv_opencv
Author(s): Daniel Stonier
autogenerated on Sat Jun 18 2016 08:19:28