博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
QImage Mat IplImage 之间的相互转换
阅读量:6510 次
发布时间:2019-06-24

本文共 2817 字,大约阅读时间需要 9 分钟。

hot3.png

  • Mat(opencv) 与 QImage(Qt) 之间互转
// Mat to QImageQImage cvMat2QImage(const cv::Mat& mat)  {      // 8-bits unsigned, NO. OF CHANNELS = 1      if(mat.type() == CV_8UC1)      {          QImage image(mat.cols, mat.rows, QImage::Format_Indexed8);          // Set the color table (used to translate colour indexes to qRgb values)          image.setColorCount(256);          for(int i = 0; i < 256; i++)          {              image.setColor(i, qRgb(i, i, i));          }          // Copy input Mat          uchar *pSrc = mat.data;          for(int row = 0; row < mat.rows; row ++)          {              uchar *pDest = image.scanLine(row);              memcpy(pDest, pSrc, mat.cols);              pSrc += mat.step;          }          return image;      }      // 8-bits unsigned, NO. OF CHANNELS = 3      else if(mat.type() == CV_8UC3)      {          // Copy input Mat          const uchar *pSrc = (const uchar*)mat.data;          // Create QImage with same dimensions as input Mat          QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);          return image.rgbSwapped();      }      else if(mat.type() == CV_8UC4)      {          qDebug() << "CV_8UC4";          // Copy input Mat          const uchar *pSrc = (const uchar*)mat.data;          // Create QImage with same dimensions as input Mat          QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_ARGB32);          return image.copy();      }      else      {          qDebug() << "ERROR: Mat could not be converted to QImage.";          return QImage();      }  }// QImage to Mat  cv::Mat QImage2cvMat(QImage image)  {      cv::Mat mat;      qDebug() << image.format();      switch(image.format())      {      case QImage::Format_ARGB32:      case QImage::Format_RGB32:      case QImage::Format_ARGB32_Premultiplied:          mat = cv::Mat(image.height(), image.width(), CV_8UC4, (void*)image.constBits(),                      image.bytesPerLine());          break;      case QImage::Format_RGB888:          mat = cv::Mat(image.height(), image.width(), CV_8UC3, (void*)image.constBits(),                       image.bytesPerLine());          cv::cvtColor(mat, mat, CV_BGR2RGB);          break;      case QImage::Format_Indexed8:          mat = cv::Mat(image.height(), image.width(), CV_8UC1, (void*)image.constBits(),                       image.bytesPerLine());          break;      }      return mat;  }
  • Mat 与 IplImage 之间相互转换  
// cv::Mat to IplImagecv::Mat matimg = cv::imread ("heels.jpg");IplImage* iplimg;*iplimg = IplImage(matimg);// IplImage to cv::MatIplImage* iplimg = cvLoadImage("heels.jpg");cv::Mat matimg;matimg = cv::Mat(iplimg);
  • Reference
  1. http://blog.csdn.net/liyuanbhu/article/details/46662115
  2. http://blog.csdn.net/caroline_wendy/article/details/16940895

转载于:https://my.oschina.net/wellsoschina/blog/1570848

你可能感兴趣的文章