1

I am trying to read avi video and write it again as it is without any change using openCV 2.4.0 on MAC 10.6.8

My videos is grayscale with frame_rate = 25 and Codec = 827737670 which is FFV1 (I guess)

The problem is .... when I read and write the video as it is .... I see many changes in size and in color ... After 3 or 4 times of writing I can see the video start to be (Pink) color !!!

I am not sure what is the problem !!!

this is my code for the people who interest

Appreciate your help in advance :D

Seereen

Note : I have on my computer FFMPEG V 0.11 (I do not know if this important)

{    

int main (int argc, char * const argv[]) {
    char name[50];

    if (argc==1)
    {
        printf("\nEnter the name of the video:");
        scanf("%s",name); 

    } else if (argc == 2)
        strcpy(name, argv[1]);

    else 
    {
        printf("To run this program you should enter the name of the program at least, or you can enter the name of the program then the file name");
        return 0; 
    }

    cvNamedWindow( "Read the video", CV_WINDOW_AUTOSIZE );

    // GET video
    CvCapture* capture = cvCreateFileCapture( name );
    if (!capture ) 
    {
        printf( "Unable to read input video." );
        return 0;
    }

    double fps = cvGetCaptureProperty( capture,CV_CAP_PROP_FPS);
     printf( "fps %f ",fps );
    int codec = cvGetCaptureProperty( capture,CV_CAP_PROP_FOURCC);
    printf( "codec %d ",codec );
    // Read frame
    IplImage* frame = cvQueryFrame( capture );
    // INIT the video writer
    CvVideoWriter *writer = cvCreateVideoWriter( "x7.avi", codec, fps, cvGetSize(frame),1);
    while(1) 
    {
        cvWriteFrame( writer, frame );
        cvShowImage( "Read the video", frame );
        // READ next frame
        frame = cvQueryFrame( capture );
        if( !frame ) 
            break;
        char c = cvWaitKey(33);
        if( c == 27 ) 
            break;
    }

    // CLEAN everything
    cvReleaseImage( &frame );
    cvReleaseCapture( &capture );
    cvReleaseVideoWriter( &writer );
    cvDestroyWindow( "Read the video" );    
    return 0;}

   } 
Jay Walker
  • 4,654
  • 5
  • 47
  • 53
seereen
  • 193
  • 1
  • 4
  • 20
  • I suggest you upgrade to the newest OpenCV and see if it solves the problem. – karlphillip Feb 07 '13 at 17:28
  • I am little worry to update the version ! ,,, because I write my project work in this version ... I will die if it does not work in the new version ,,,, also mine is not that old it is 2.4.0 .. is there any problem in this line ? CvVideoWriter *writer = cvCreateVideoWriter( "x7.avi", codec, fps, cvGetSize(frame),1); is there any problem in FFV1 codec ? the video originally writing on windows using ffmpeg library..I do not know if this will affect ! – seereen Feb 07 '13 at 18:22
  • I recently compiled OpencV 2.4.3 on my Mac OS X 10.7.5. No worries. Your problem could be related to faulty codecs, or some problem on the OpenCV side. I would update both. I suspect you might be using FFmpeg under the hood of OpenCV. Remember: you can always download OpenCV 2.4.0 and install it again. Just make sure you write down what version of ffmpeg, libavcodec and libavformat you currently have before updating them. – karlphillip Feb 07 '13 at 18:25
  • ok .. do you know how can I update it ? ... do I need to remove the last one then install it again ? I remember when I download ffmpeg then openCV I was almost ge crazy :S – seereen Feb 07 '13 at 18:37
  • by the way , I do not care about the audio part in the video ... is this affect ? – seereen Feb 07 '13 at 18:38
  • is there any other codec can be used (lossless compression) .. that should give me same result ? – seereen Feb 07 '13 at 18:52
  • As far as I know, you can simply overwrite the installed files. You can try to use `CV_FOURCC('M','J','P','G')`. – karlphillip Feb 07 '13 at 21:35
  • MJPG is lossy codec ... I want lossless if you know appreciate your replay Thank you – seereen Feb 08 '13 at 03:43

2 Answers2

3

Check this list of fourcc codes, and search for the uncompressed ones, like HFYU.

You also might find this article interesting: Truly lossless video recording with OpenCV.

EDIT:

I have a Mac OS X 10.7.5 at my disposal and since you gave us the video for testing I decided to share my findings.

I wrote the following source code for testing purposes: it loads your video file and writes it to a new file out.avi while preserving the codec information:

#include <cv.h>
#include <highgui.h>

#include <iostream>

int main(int argc, char* argv[])
{
    // Load input video
    cv::VideoCapture input_cap(argv[1]);
    if (!input_cap.isOpened())
    {
        std::cout << "!!! Input video could not be opened" << std::endl;
        return -1;
    }

    // Setup output video
    cv::VideoWriter output_cap("out.avi", 
                               input_cap.get(CV_CAP_PROP_FOURCC),
                               input_cap.get(CV_CAP_PROP_FPS),
                               cv::Size(input_cap.get(CV_CAP_PROP_FRAME_WIDTH), input_cap.get(CV_CAP_PROP_FRAME_HEIGHT)));                          
    if (!output_cap.isOpened())
    {
        std::cout << "!!! Output video could not be opened" << std::endl;
        return -1;
    }

    // Loop to read from input and write to output
    cv::Mat frame;
    while (true)
    {       
        if (!input_cap.read(frame))             
            break;

        output_cap.write(frame);
    }

    input_cap.release();
    output_cap.release();

    return 0;
}

The output video presented the same characteristics of the input:

  • Codec: FFMpeg Video 1 (FFV1)
  • Resolution: 720x480
  • Frame rate: 25
  • Decoded format: Planar 4:2:0 YUV

and it looked fine when playing.

I'm using OpenCV 2.4.3.

karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • in any codec if I pass 0 in last argument in cvCreateVideoWriter ... I got 8KB only and nothing work ! – seereen Feb 08 '13 at 23:46
  • HFYU add noise to the video ! ... and the other codec in the link does not work at all (it give me 8KB even without last argument) – seereen Feb 09 '13 at 01:19
  • OpenCV uses either Ffmpeg or Gstreamer to handle videos. If you've updated Ffmpeg and it didnt solve the problem, there might be a chance that OpenCV is using Gstreamer. – karlphillip Feb 09 '13 at 01:57
  • do I need to install Gstreamer before openCV or I can install it after and it gonna work ? – seereen Feb 09 '13 at 02:38
  • The only approach that will work is to install GStreamer (binaries and development files) and compile OpenCV yourself. Make sure CMake's output shows that it will use gstreamer instead of ffmpeg when building pencv. – karlphillip Feb 09 '13 at 22:38
  • are you working on MAC ? ... do you face problem when writing grayscale videos ? – seereen Feb 10 '13 at 18:14
  • yes, I use Mac OS X all the time. I dont have problems writing videos. – karlphillip Feb 10 '13 at 19:55
  • can I know what version are you working on in (ffmpeg , openCV , OSX)?... I would like to know where is my problem !! – seereen Feb 10 '13 at 20:19
  • OpenCV 2.4.3. libavcodec 52.66.0. libavutil 50.14.0. libavformat 52.61.0. libavdevice 52.2.0. libswscale 0.10.0. Besides OpenCV, all of the others where installed through homebrew. I don't know what version of ffmpeg I have installed. More important than that are the versions of the libraries it uses, and those I just gave you. Mac OS X 10.7.5. – karlphillip Feb 10 '13 at 20:59
  • is it ok if I want to give you one of the videos that I am working on ... so you can try it on your computer and let me know if the problem from the video or not ... I have it on Dropbox .. I can share it with you if you give me your email :D – seereen Feb 10 '13 at 21:36
  • I just find that ... the original video written in YUV 240 format not RGB or normal grayscale ! I do not know If I need special method to read it and write .... any help ? :D – seereen Feb 12 '13 at 17:52
  • Don't you mean YUV 420? Just share a link to the video so I can download it here. [OpenCV doesn't support YUV](http://stackoverflow.com/a/9873222/176769). – karlphillip Feb 12 '13 at 18:03
  • this video has been writting by FFMPEG library on windows using this format PIX_FMT_YUV420P for the pixels – seereen Feb 12 '13 at 18:23
  • The link works but my Internet connection is very slow, and it will take a couple of hours to download it. – karlphillip Feb 12 '13 at 18:38
  • take your time ... waiting for your replay :D – seereen Feb 12 '13 at 18:45
  • Your code above displays the video on a window. Does it look ok? – karlphillip Feb 12 '13 at 18:53
  • I am not working on windows ! I am working on MAC ... if it is YUV is it easy to convert it to RGB then to GRAY ! – seereen Feb 12 '13 at 19:04
  • I'll ask one last time... isn't it YUV 420? I've never heard of YUV 240. I just want to make sure. – karlphillip Feb 12 '13 at 19:08
  • I am not sure believe me ,,, I know it is written by this format PIX_FMT_YUV420P ... that why I thought it is YUV 240 ! ... if it is so , how my code can read it ! – seereen Feb 12 '13 at 19:24
  • ffmpeg -pix_fmts type this on Terminal and you will see ... IO... yuv420p 3 12 – seereen Feb 12 '13 at 19:30
  • I updated my answer. OpenCV can read videos in this format because the underlaying libraries (FFMPEG or GStreamer) do the proper conversion from YUV 420 to a format that OpenCV can understand. I run the application I shared above and I had no problems creating a new video file with the same type/fps/codec/resolution of the video you sent me. – karlphillip Feb 12 '13 at 19:34
  • I don't think there's anything else I can do for you. If my application doesn't work for you, I suggest you try your best to install the specific versions of the libraries I mentioned on the comments because they work on my system. Please **up vote** my answer if it helped you somehow. Also, consider clicking on the checkbox near it to select it as the official answer if it solved your problem. By doing these things you will be helping future visitors just like yourself. – karlphillip Feb 12 '13 at 19:36
  • can you just try to read it and write 3 times to see if it is become pink! ... please – seereen Feb 12 '13 at 19:41
  • I wrote the entire video into a new file. No problems at all. You should try my code. – karlphillip Feb 12 '13 at 19:46
  • by the way , using your code the file size in increased after I write it ... I think there is a problem when openCV read YUV240 format .... – seereen Feb 13 '13 at 02:50
  • I do not think the problem is my version !!! .... using your code and your version .... if you read and write the same video more than 4 time you will see it is become pink !!! .... try it and you will see (take the output video read it and write it ..... then take the new output and read then write it) I think the cvQueryframe read the frame as RGB not as YUV240 !!! try this and let me know ! .... – seereen Feb 13 '13 at 18:24
  • I've done it 5 times and I see no such thing in the output video. My tests are performed with the first 61MB of your video, which creates an output of 66MB. I wasn't able to reproduce the problem, sorry. – karlphillip Feb 13 '13 at 18:33
  • I updated my library and I got openCV 2.4.3 ,,,, I can see the both library in the usr/local/lib !!!! how can I delete the old one ... and both of them work !!... I tried the code with updated library ... it give me the same result !!!! (my FFMPEG is already updated) – seereen Feb 14 '13 at 17:15
  • `rm /usr/local/lib/libopencv*` and then install the 2.4.3 again :) After that, recompile your application so it gets linked with the latest version installed. – karlphillip Feb 14 '13 at 17:36
  • I really appreciate you patient and help (Thanks a lot)... :D – seereen Feb 14 '13 at 19:22
  • using new library I got this at the end of the program !!! Program received signal: “SIGABRT”. sharedlibrary apply-load-rules all !!! – seereen Feb 14 '13 at 19:25
  • To answer the color format question, check [detect color space with openCV](http://stackoverflow.com/q/2122706/176769). About the error, **recompile** your application: that means `make clean` before `make`. Notice how you are having questions that are far away from the original question. Stackoverflow is Q&A forum, and that means 1 question per thread. I'm sure I've already answered a few in this comment section. – karlphillip Feb 14 '13 at 19:33
  • After the application is successfully compile, make sure the binary is using the right opencv libs. To list the libraries that a binary application is linked to, on Max OS X execute `otool -L app`. – karlphillip Feb 14 '13 at 19:34
  • I did not get what do you mean by "recompile your application: that means make clean before make."... what is my application !! the library openCV or the small program that read and write !! – seereen Feb 14 '13 at 19:40
  • Your [application](http://en.wikipedia.org/wiki/Application_software) is the small program that you developed to *read & write*, as you call it. – karlphillip Feb 14 '13 at 20:19
  • recompile , clean everything ,,,, but does not fix the problem ! – seereen Feb 14 '13 at 20:46
  • Are you paying attention to my comments? What about the `otool -L app` output? Did you verified that your application is linked with the right opencv libs (pay attention to the version displayed)? – karlphillip Feb 14 '13 at 20:49
  • I'll finish this right here and say that there's no problem in my source code, and a standard installation of OpencV works beautifully on Mac OS X, provided that you have installed it's dependencies. The problem you are facing right now is related to environment settings, not OpenCV programming. If you have new questions, feel free to ask them in new threads. Just don't keep asking different questions in the same thread, like it happened in the comment section. – karlphillip Feb 14 '13 at 20:52
  • this problem does not solve yet ! ... the increasing size happen because the changed in the pixel values (colors) , after many read and write process you can see big different between the last one and the original one ... – seereen Feb 22 '13 at 17:22
  • I'm afraid I can't help you any further, since my example code works with the video you shared. Good luck! – karlphillip Feb 22 '13 at 17:40
1

I figure out the problem ,,,,, The original videos written in YUV240 pixel format (and it is gray)

the openCV read the video on BGR by default , so each time when I read it the openCV convert the pixel values to BGR

after few time of reading and writing , the error start to be bigger (because the conversion operation) that why the pixels values change .....and I see the video pink !

The Solution is , read and write this kind of videos by FFMPEG project which provide YUV240 and many other format there is a code can do this operation in the tutorial of FFMPEG

I hope this can help the others who face similar problem

seereen
  • 193
  • 1
  • 4
  • 20