如何在 cv::Mat 中用 QwT 表示颜色图数据?

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

我正在使用 Qt 和 Qwt 框架用 C++ 开发一个用于科学绘图的应用程序。我将矩阵数据存储为 cv::Mat,表示具有标量数据 (MxN) 的图像,需要将其可视化为颜色图。

对于 OpenCV,它是使用 cv::applyColorMap(img,cm_img,cv::COLORMAP_JET)cv::imshow("img name", img) 执行的,如 here

所述

我已尝试将 cv::Mat 转换为 QImage,如 here and here 所述,但它似乎无法正常工作。当我尝试显示生成的图像时,它没有任何意义。

来自 Qwt,有一些 类 看起来很有趣:QwtMatrixRasterData, QwtPlotSpectrogram or QwtPlotRasterItem.

我需要的最终输出是这样的。 给定一个具有双精度值的矩阵 (MxN),调用类似 imshow 的东西,我得到一个这样的彩色图图像

我们使用 QCustomPlot 来解决问题,发送 QVector<double>

想法是从 cv::Mat:

创建 QVector
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;
}

然后我们创建一个 QCPColorMap* colorMap 并用 QVector vec 数据填充它:

  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);
    }
  }