Search code examples
c++opencvvideovideo-processingavi

Recording Video in UYVY codec in Opencv


I have a camera from e-con systems which supports UYVU codec video recording. When I use their own software (QTCam) to record a video it records in avi format with YUY2 Codec, which the video opens and runs in VLC perfectly.

enter image description here

Now I tried recording the video through Opencv VideoWrtiter(). I used this command to set the Camera property to read UYVY Codec video.

camera1.set(CV_CAP_PROP_FOURCC,CV_FOURCC('U','Y','V','Y'));

and also used VideoWriter to record the video in an AVI file format.

video1.open("/home/camera1UYVY.avi",CV_FOURCC('Y','U','Y','2'),30,s1,true);

The feed from the camera is working, I checked with imshow(). But the recored video is not playing in VLC as it worked for the one recorded from QTCam.

Even the recoded opencv recorded has the same codec

enter image description here

My Code goes below

#include <opencv2/core/core.hpp>
#include <opencv2/video/video.hpp>
#include <opencv2/imgcodecs/imgcodecs.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/videoio/videoio.hpp>
#include <iostream>

using namespace std;
using namespace cv;

int main(int argc, char **argv) {

VideoCapture camera1;
Mat frame1;

camera1.open(0);

camera1.set(CV_CAP_PROP_FOURCC,CV_FOURCC('U','Y','V','Y'));


camera1.set(CV_CAP_PROP_FRAME_WIDTH,1280);
camera1.set(CV_CAP_PROP_FRAME_HEIGHT,720);



cout << "FPS:" << camera1.get(CV_CAP_PROP_FPS) << endl;

camera1.set(CV_CAP_PROP_FPS,30);

cout << "FPS:" << camera1.get(CV_CAP_PROP_FPS) << endl;

cout << "Camera -1 Codec: " << (int)camera1.get(CV_CAP_PROP_FOURCC) << endl;


VideoWriter video1;

cout << camera1.get(CV_CAP_PROP_FRAME_WIDTH) << endl;
cout << camera1.get(CV_CAP_PROP_FRAME_HEIGHT) << endl;
Size s1 = Size((int)camera1.get(CV_CAP_PROP_FRAME_WIDTH),(int)camera1.get(CV_CAP_PROP_FRAME_HEIGHT));


video1.open("/home/camera1UYVY.avi",CV_FOURCC('Y','U','Y','2'),30,s1,true);


while(!camera1.isOpened()){
    cout << "Camera not opened" << endl;
    continue;
}
while(1){

    if(!video1.isOpened()){
        cout << "Error opening video" << endl;
    }
    camera1.read(frame1);
    imshow("Display1",frame1);
    video1.write(frame1);
    cout << frame1.data << endl;
    if(waitKey(1) == 27){
        break;
    }
}
video1.release();
camera1.release();
return 0;

} please tell me where I'm going wrong. I want to record a uncompressed video from the camera and save it as a video file(which opens in a VLC or any other video player)


Solution

  • OpenCV seems to have an issue writing yuv422p formats to avi. Try this instead:

    video1.open("/home/camera1UYVY.avi",CV_FOURCC('I', 'Y', 'U', 'V'),30,s1,true);
    

    This is a yuv420p pixel format, which means you lose some precision in the vertical U and V planes, but it will still be uncompressed video.