6

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)

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
  • Not sure if I understand this - but I am no expert on video. You appear to be reading `UYVY` data and writing the exact same unmodified data and suddenly claiming it is `YUY2`...? – Mark Setchell Oct 06 '17 at 13:52
  • Actually, I'm reading UYVY data and writing in Avi file using UYVY and also using YUV2 codec. Both gave the same result. It doesn't play on any media player. – prabhakar-sivanesan Oct 06 '17 at 15:30
  • I would start by checking what ``CAP_PROP_FORMAT`` and ``CAP_PROP_CONVERT_RGB`` are set to. – zeFrenchy Oct 09 '17 at 09:57
  • Could you create your `Mat frame1` inside the `while(1)` and also move the `imshow` after the `write` and see if that helps? – Samer Tufail Oct 10 '17 at 15:52

1 Answers1

5

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.

Mike
  • 562
  • 3
  • 15