videomanger.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include "videomanger.h"
  2. #include <QImage>
  3. #include <QList>
  4. #include <QString>
  5. #include <random>
  6. VideoManger::VideoManger(QObject *parent/* = nullptr*/) : QObject(parent),
  7. framesPerSecound(25)
  8. {
  9. }
  10. bool VideoManger::makeVideo(/*QList<QImage> *frames, */QString outputName, int count)
  11. {
  12. // if(frames->isEmpty())
  13. // return false;
  14. if(outputName.isEmpty()){
  15. srand(time(nullptr));
  16. outputName = QString(QString::number(rand() % 200 + 10));
  17. }
  18. // counts the size of one frame
  19. frameSize = cv::imread(outputName.toStdString() + std::to_string(1) + ".png").size();
  20. // init videoWriter
  21. videoWriter = cv::VideoWriter(outputName.toStdString() + "1.avi", cv::VideoWriter::fourcc('M', 'J', 'P', 'G'),
  22. framesPerSecound, frameSize, true);
  23. // record frames in a video file
  24. for(int i = 0; i < count; ++i){
  25. // frame = QImageToCvMat(frames->at(i), CV_8UC3);
  26. // cv::imshow("qwe", frame);
  27. frame = cv::imread(outputName.toStdString() + std::to_string(i + 1) + ".png");
  28. videoWriter.write(frame);
  29. }
  30. emit videoSaved();
  31. return true;
  32. }
  33. // convert QImage to cv;;mat
  34. cv::Mat VideoManger::QImageToCvMat(QImage frame, int format)
  35. {
  36. return cv::Mat(frame.height(), frame.width(), format,
  37. const_cast<uchar*>(frame.bits()),
  38. frame.bytesPerLine()).clone();
  39. }