convert_image.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #ifndef CONVERT_IMAGE_H_
  2. #define CONVERT_IMAGE_H_
  3. #include <QImage>
  4. #include <QPixmap>
  5. #include <opencv/cv.h>
  6. #include <opencv/highgui.h>
  7. namespace convert_image {
  8. inline QImage CvMatToQImage(const cv::Mat &input) {
  9. switch (input.type()) {
  10. case CV_8UC4: { // 8-bit, 4 channel
  11. QImage image(input.data, input.cols, input.rows, input.step, QImage::Format_RGB32);
  12. return image;
  13. }
  14. case CV_8UC3: { // 8-bit, 3 channel
  15. QImage image(input.data, input.cols, input.rows, input.step, QImage::Format_RGB888);
  16. return image.rgbSwapped();
  17. }
  18. case CV_8UC1: { // 8-bit, 1 channel
  19. static QVector<QRgb> color_table;
  20. // only create color table once
  21. if (color_table.isEmpty()) {
  22. for (int i = 0; i < 256; i++) {
  23. color_table.push_back(qRgb(i, i, i));
  24. }
  25. }
  26. QImage image(input.data, input.cols, input.rows, input.step, QImage::Format_Indexed8);
  27. image.setColorTable(color_table);
  28. return image;
  29. }
  30. }
  31. return QImage();
  32. }
  33. inline QPixmap CvMatToQPixmap(const cv::Mat &input) {
  34. return QPixmap::fromImage(CvMatToQImage(input));
  35. }
  36. }
  37. #endif