Search code examples
c++qtopencvcolormapqwt

How can I represent colormap data in QwT from cv::Mat?


I'm developing an application in c++ with Qt and Qwt framework for scientific plots. I have matrix data stored as cv::Mat, representing an image with scalar data (MxN), which needs to be visualized as a colormap.

With OpenCV it is performed using cv::applyColorMap(img,cm_img,cv::COLORMAP_JET) and cv::imshow("img name", img), as described here

I have tried converting cv::Mat to QImage, as described here and here but it seems not to be working properly. When I try to show the resulting images, it doesn't make sense.

From Qwt, there are some classes that looks interesting for that matter: QwtMatrixRasterData, QwtPlotSpectrogram or QwtPlotRasterItem.

What I need as final output would be something like this. Given a matrix (MxN) with double values, calling something like imshow I get an colormap image like this

colormap


Solution

  • We came around using QCustomPlot, sending it a QVector<double>.

    The idea is to create the QVector from cv::Mat:

    QVector<double> cvMatToQVector(const cv::Mat& mat) {
      QVector<double> image;
      auto img_ptr = img.begin<double>();
    
      for (; img_ptr < img.end(); img_ptr++) {
        image.append(*img_ptr) = element;
      }
      return image;
    }
    

    Then we create a QCPColorMap* colorMap and populate it with QVector vec data:

      for (int yIndex = 0; yIndex < col; ++yIndex) {
        y_col = yIndex * col;
        for (int xIndex = 0; xIndex < row; ++xIndex) {
          z = vec.at(xIndex + y_col);
          colorMap->data()->setCell(xIndex, yIndex, z);
        }
      }