Commit 0b581c79a375df587109732abd74ca86d73681f1
1 parent
d0e03b50
Add a format class for reading video files
Opening videos via the open transform will result in all frames of a video being loaded into memory, and stored as matrices of a single template. Modify defaultFormat to try to read a file as a video if it can't be read as an image (since video files can also have various file extensions). Signed-off-by: caotto <ottochar@gmail.com>
Showing
1 changed file
with
67 additions
and
2 deletions
sdk/plugins/format.cpp
| ... | ... | @@ -32,6 +32,57 @@ using namespace cv; |
| 32 | 32 | namespace br |
| 33 | 33 | { |
| 34 | 34 | |
| 35 | + /*! | |
| 36 | + * \ingroup formats | |
| 37 | + * \brief Read all frames of a video using OpenCV | |
| 38 | + * \author Charles Otto \cite caotto | |
| 39 | + */ | |
| 40 | +class videoFormat : public Format | |
| 41 | +{ | |
| 42 | + Q_OBJECT | |
| 43 | + | |
| 44 | +public: | |
| 45 | + Template read() const | |
| 46 | + { | |
| 47 | + VideoCapture videoSource(file.name.toStdString()); | |
| 48 | + videoSource.open(file.name.toStdString() ); | |
| 49 | + | |
| 50 | + | |
| 51 | + Template frames; | |
| 52 | + if (!videoSource.isOpened()) { | |
| 53 | + qWarning("video file open failed"); | |
| 54 | + return frames; | |
| 55 | + } | |
| 56 | + int res = (int) videoSource.get(CV_CAP_PROP_FOURCC); | |
| 57 | + | |
| 58 | + bool open = true; | |
| 59 | + while(open) { | |
| 60 | + cv::Mat frame; | |
| 61 | + open = videoSource.read(frame); | |
| 62 | + if (!open) break; | |
| 63 | + | |
| 64 | + frames.append(cv::Mat()); | |
| 65 | + frames.back() = frame.clone(); | |
| 66 | + } | |
| 67 | + | |
| 68 | + return frames; | |
| 69 | + } | |
| 70 | + | |
| 71 | + void write(const Template &t) const | |
| 72 | + { | |
| 73 | + VideoWriter videoSink(file.name.toStdString(), CV_FOURCC('x','2','6','4'), 30, t.begin()->size()); | |
| 74 | + | |
| 75 | + // Did we successfully open the output file? | |
| 76 | + if (!videoSink.isOpened() ) qFatal("Failed to open output file"); | |
| 77 | + | |
| 78 | + for (Template::const_iterator it = t.begin(); it!= t.end(); ++it) { | |
| 79 | + videoSink << *it; | |
| 80 | + } | |
| 81 | + } | |
| 82 | +}; | |
| 83 | + | |
| 84 | +BR_REGISTER(Format, videoFormat) | |
| 85 | + | |
| 35 | 86 | /*! |
| 36 | 87 | * \ingroup formats |
| 37 | 88 | * \brief A simple binary matrix format. |
| ... | ... | @@ -164,7 +215,14 @@ class DefaultFormat : public Format |
| 164 | 215 | QString prefix = ""; |
| 165 | 216 | if (!QFileInfo(file.name).exists()) prefix = file.getString("path") + "/"; |
| 166 | 217 | Mat m = imread((prefix+file.name).toStdString()); |
| 167 | - if (m.data) t.append(m); | |
| 218 | + if (m.data) { | |
| 219 | + t.append(m); | |
| 220 | + } | |
| 221 | + else { | |
| 222 | + videoFormat videoReader; | |
| 223 | + videoReader.file = file; | |
| 224 | + t = videoReader.read(); | |
| 225 | + } | |
| 168 | 226 | } |
| 169 | 227 | |
| 170 | 228 | return t; |
| ... | ... | @@ -172,7 +230,14 @@ class DefaultFormat : public Format |
| 172 | 230 | |
| 173 | 231 | void write(const Template &t) const |
| 174 | 232 | { |
| 175 | - imwrite(file.name.toStdString(), t); | |
| 233 | + if (t.size() != 1) { | |
| 234 | + videoFormat videoWriter; | |
| 235 | + videoWriter.file = file; | |
| 236 | + videoWriter.write(t); | |
| 237 | + } | |
| 238 | + else { | |
| 239 | + imwrite(file.name.toStdString(), t); | |
| 240 | + } | |
| 176 | 241 | } |
| 177 | 242 | }; |
| 178 | 243 | ... | ... |