Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to choose Full Frames (Uncompressed) as codec for VideoWriter

i want to store uncompressed frames from a device as a video, but i need to know how to choose " Full Frames (Uncompressed) " as a codec for the VideoWriter (in emgu aka openCV).

Iam able to choose it from the dropdown menu when iam passing -1 like this

VideoWriter myVideoWriter = new VideoWriter ("myVieoColor.avi", -1 , 15, videoSize, true);

But i want to choose the Full Frames (Uncompressed) codec automatically. For example i know i can choose the Lagarith Lossless Video Codec by

VideoWriter myVideoWriter = new VideoWriter ("myVieoColor.avi", Emgu.CV.VideoWriter.Fourcc('L','A','G','S') , 15, videoSize, true);

but iam not able to figure out which fourcc i need to use.

Perhaps someone can help me out please

like image 458
Rantir Avatar asked Nov 26 '16 18:11

Rantir


People also ask

What is Fourcc codec?

A FOURCC code is a 32-bit unsigned integer that is created by concatenating four ASCII characters. For example, the FOURCC code for YUY2 video is 'YUY2'. For compressed video formats and non-RGB video formats (such as YUV), the biCompression member of the BITMAPINFOHEADER structure should be set to the FOURCC code.

What is cv2 VideoWriter?

VideoWriter is the desired FPS of the output video file. We then have the width and height of output video. It's important that you set these values correctly, otherwise OpenCV will throw an error if you try to write a frame to file that has different dimensions than the ones supplied to cv2.


2 Answers

From Opencv Reference

Tips:

->With some backends fourcc=-1 pops up the codec selection dialog from the system.

->To save image sequence use a proper filename (eg. img_%02d.jpg) and fourcc=0 OR fps=0. Use uncompressed image format (eg. img_%02d.BMP) to save raw frames.

->Most codecs are lossy. If you want lossless video file you need to use a lossless codecs (eg. FFMPEG FFV1, Huffman HFYU, Lagarith LAGS, etc...)

->If FFMPEG is enabled, using codec=0; fps=0; you can create an uncompressed (raw) video file.

http://docs.opencv.org/trunk/dd/d9e/classcv_1_1VideoWriter.html#afec93f94dc6c0b3e28f4dd153bc5a7f0

like image 29
shawndfernandes Avatar answered Sep 18 '22 07:09

shawndfernandes


If we break into debugger when this codec selection dialog box is open, we can see that it's the result of this call to AVISaveOptions(...). So, one way to find out what you picked would be to set a breakpoint on say line 799 and inspect the contents of fourcc.

However, there is even simpler way:

  1. Create a dummy video foo.avi with just 1 black frame, selecting the codec using the GUI.
  2. Open foo.avi using cv::VideoCapture.
  3. Get the CAP_PROP_FOURCC from the cv::VideoCapture instance.
  4. Decode and print it.
  5. [Optional] Create a dummy video bar.avi with just 1 black frame, and use the FOURCC code you determined in step 3. Compare foo.avi and bar.avi to verify they are the same.

Sorry, I don't use C#/EmguCV, so I can't provide you an exact example, but the following should be easy enough to port.

C++ Sample

#include <opencv2/opencv.hpp>
#include <iostream>

int main()
{
    {
        cv::VideoWriter outputVideo;
        outputVideo.open("foo.avi", -1, 25.0, cv::Size(640, 480), true);

        cv::Mat frame(480, 640, CV_8UC3);
        outputVideo.write(frame);
    }

    cv::VideoCapture inputVideo("foo.avi");
    int fourcc = static_cast<int>(inputVideo.get(CV_CAP_PROP_FOURCC));

    char FOURCC_STR[] = {
        (char)(fourcc & 0XFF)
        , (char)((fourcc & 0XFF00) >> 8)
        , (char)((fourcc & 0XFF0000) >> 16)
        , (char)((fourcc & 0XFF000000) >> 24)
        , 0
    };
    std::cout << "FOURCC is '" << FOURCC_STR << "'\n";

    return 0;
}

Console output:

FOURCC is 'DIB '

Python Sample

import cv2
import numpy as np
import struct

outputVideo = cv2.VideoWriter()
outputVideo.open("foo.avi", -1, 25, (640,480), True)

frame = np.zeros((480,640,3), dtype=np.uint8)
outputVideo.write(frame)
outputVideo.release()

inputVideo = cv2.VideoCapture("foo.avi")
fourcc = int(inputVideo.get(cv2.cv.CV_CAP_PROP_FOURCC))

print "FOURCC is '%s'" % struct.pack("<I", fourcc)

Console output:

FOURCC is 'DIB '
like image 129
Dan Mašek Avatar answered Sep 18 '22 07:09

Dan Mašek