OpenCV - CvVideoWriter codec error in raspbian - c

I'm making something like black box in raspberry pi.
I set OpenCV 2.4.3 and many video libraries.
( I referred this site - Opencv cannot acces my webcam )
And I compiled this sample code.
#include <stdio.h>
#include "opencv/cv.h"
#include "opencv/highgui.h"
#include "opencv/cxcore.h"
int main(void){
CvCapture* capture = cvCaptureFromCAM(0);
cvNameWindow("video", 1);
double fps = cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
CvSize frame_size = cvSize((int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH), (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT));
CvVideoWriter* writer = cvCreateVideoWriter("out.avi", -1, fps, frame_size, 1);
IpImage* frame;
while(1){
frame = cvQueryFrame(capture);
cvShowImage("video", frame);
if(cvWaitKey(38) == 27){
break;
}
}
cvReleaseVideoWriter(&writer);
cvReleaseCapture(&capture);
cvDestroyWindow("video");
return 0;
}
This code compiled successfully.
But when i run this process, there are some error.
OpenCV Error: Unsupported format or combination of formats (Gstreamer Opencv backend doesn't support this codec acutally.) in CvVideoWriter_GStreamer::open, file /home/pi/OpenCV-2.4.3/modules/highgui/src/cap_gstreamer.cpp, line 479
terminate called after throwing an instance of 'cv::Exception'
what(): /home/pi/OpenCV-2.4.3/modules/highgui/src/cap_gstreamer.cpp:479: error: (-210) Gstreamer Opencv backend doesn't support this codec acutally. in function CvVideoWriter_GStreamer::open
Aborted
So, i changed codec part in 'cvCreateVideoWriter' instead of -1.
I tried many types of codec like 'CV_FOURCC('M','J','P','G')' and so on..
but I cannot fix this problem.
How can i solve this problem? Please help me..

Related

OpenCV fails to recognize webcam, but mplayer succeeds

As a first step on a larger project I was trying to display the imagem from my webcam using OpenCV:
#include <stdlib.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
int
main()
{
cv::VideoCapture cap(-1);
if (!cap.isOpened())
exit(EXIT_FAILURE);
cv::Mat frame;
bool done = false;
while (!done) {
cap >> frame;
cv::imshow("webcam", frame);
done = (cv::waitKey(30) >= 0);
}
return EXIT_SUCCESS;
}
This returns an error code (!cap.isOpened() passes ,confirmed with gdb). Initially I had 0 instead of -1. When searching this site -1 was suggested, but it was to no avail. I also tried 1 through 3, as another user suggested it.
I can display my webcam using mplayer, more specifically mplayer tv:// -tv driver=v4l2.
v4l2 is the "video for linux" driver. I noticed OpenCV can be installed with such driver by compiling it with -DWITH_V4L and -DWITH_LIBV4L (v4l USE flag in Gentoo). After recompiling OpenCV with it, it successfully recognized the webcam. GTK support seems to be needed to display the image.

Capture image from webcam and display it - OpenCV - Eclipse - Windows

I'm new to OpenCV and I want to display what my webcam sees with OpenCV. I'm using the C Coding Language.
I've tried with this code:
#include <stdio.h>
#include <cv.h> // Include the OpenCV library
#include <highgui.h> // Include interfaces for video capturing
int main()
{
cvNamedWindow("Window", CV_WINDOW_AUTOSIZE);
CvCapture* capture =cvCreateCameraCapture(-1);
if (!capture){
printf("Error. Cannot capture.");
}
else{
cvNamedWindow("Window", CV_WINDOW_AUTOSIZE);
while (1){
IplImage* frame = cvQueryFrame(capture);
if(!frame){
printf("Error. Cannot get the frame.");
break;
}
cvShowImage("Window",frame);
}
cvReleaseCapture(&capture);
cvDestroyWindow("Window");
}
return 0;
}
My webcam's light turns on, but the result is a completely grey window, with no image.
Can you help me?
You need to add
cvWaitKey(30);
to the end of while-loop.
cvWaitKey(x) / cv::waitKey(x) does two things:
It waits for x milliseconds for a key press. If a key was pressed during that time, it returns the key's ASCII code. Otherwise, it returns -1.
It handles any windowing events, such as creating windows with cvNamedWindow(), or showing images with cvShowImage().
A common mistake for opencv newcomers is to call cvShowImage() in a loop through video frames, without following up each draw with cvWaitKey(30). In this case, nothing appears on screen, because highgui is never given time to process the draw requests from cvShowImage().
See What does OpenCV's cvWaitKey( ) function do? for more info.

read/write avi video on MAC using openCV

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;}
}
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.
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

Get an error bitrate tolerance too small for bitrate when recording using opencv

I'm new to opencv and I have a problem when writing into a video file. Basically I'm reading from a HD webcam and write to an avi. The runable codes:
#include "cv.h"
#include "highgui.h"
int main(int argc, char** argv){
CvCapture* capture=NULL;
capture=cvCreateCameraCapture(0);
if(!capture){
return -1;
}
IplImage *bgr_frame=cvQueryFrame(capture);
double fps=cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
CvSize size=cvSize((int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH),
(int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT));
CvVideoWriter* writer=cvCreateVideoWriter(argv[1],
CV_FOURCC('M','J','P','G'),
fps,
size);
cvNamedWindow("Video", CV_WINDOW_AUTOSIZE);
while((bgr_frame=cvQueryFrame(capture))){
cvWriteFrame(writer, bgr_frame);
cvShowImage("Video", bgr_frame);
char c=cvWaitKey(60);
if(c==27){
break;
}
}
cvReleaseVideoWriter(&writer);
cvReleaseImage(&bgr_frame);
cvReleaseCapture(&capture);
return 0;
}
When running, I get an error
Output #0, avi, to 'test.avi':
Stream #0.0: Video: mjpeg, yuvj420p, 1280x720, q=2-31, 117964 kb/s, 90k tbn
[mjpeg # 0x7fd55b805600] bitrate tolerance too small for bitrate
WARNING: Could not create empty movie file container.
OpenCV Error: Assertion failed (dst.data == dst0.data) in cvCvtColor, file /opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_tarballs_ports_graphics_opencv/opencv/work/OpenCV-2.3.1/modules/imgproc/src/color.cpp, line 3175
terminate called throwing an exceptionAbort trap: 6
The camera is an HD webcam on a Macbook. Is that camera that causes problem? If so, can I set the bitrate tolerance lower? I'm new to opencv. Thanks!
By the way, can the CvVideoWriter create a new file when argv[1].avi doesn't exist?
Yes, cvCreateVideoWriter will create new file when does not exist yet.
And you get problems because FPS detection code is not implemented with every webcam driver. Tried your code on Mac with one assert added:
double fps=(double)cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
assert(fps>0.0);
And running it:
./w x.avi
Assertion failed: (fps>0.0), function main, file w.cpp, line 11.
Abort trap: 6
AVI file with FPS does not run, this is why it is not created.

Trying to write a video file using OpenCV

I’m trying to use OpenCV to write a video file. I have a simple program that loads frames from a video file then accepts to save them
At first the cvCreateVideoWrite always return NULL. I got a answer from your group saying it returns separate images and to try to change the file name to test0001.png, this worked.
But now the cvWriteFrame function always fails, the code is
CString path;
path="d:\\mice\\Test_Day26_2.avi";
CvCapture* capture = cvCaptureFromAVI(path);
IplImage* img = 0;
CvVideoWriter *writer = 0;
int isColor = 1;
int fps = 25; // or 30
int frameW = 640; // 744 for firewire cameras
int frameH = 480; // 480 for firewire cameras
writer=cvCreateVideoWriter("d:\\mice\\test0001.png",CV_FOURCC('P','I','M','1'),
fps,cvSize(frameW,frameH),isColor);
if (writer==0)
MessageBox("could not open writter");
int nFrames = 50;
for(int i=0;i<nFrames;i++){
if (!cvGrabFrame(capture))
MessageBox("could not grab frame");
img=cvRetrieveFrame(capture); // retrieve the captured frame
if (img==0)
MessageBox("could not retrive data");
if (!cvWriteFrame(writer,img) )
MessageBox("could not write frame");
}
cvReleaseVideoWriter(&writer);
Try CV_FOURCC('D', 'I', 'V', 'X'), CV_FOURCC('f', 'f', 'd', 's') (with *.avi filename) or CV_FOURCC_DEFAULT (with *.mpg). Video writing is still quite messy in opencv >_>
I've seen many issues with writing video as well in OpenCV. I found intel iYUV format worked well for what I needed.
Was your library built with HAVE_FFMPEG defined?
If it wasn't,you might need to recompile opencv with that option.You should see something like this in the configure step:
...
Video I/O--------------
Use QuickTime no
Use xine no
Use ffmpeg: yes
Use v4l yes
...
If you don't have ffmpeg,you can get it from here.

Resources