Unable to create/save output avi file in OpenCV - c++

I Have following source code to detect BLOB and i am using MS 2008 , OpenVC 2.1
#include "stdafx.h"
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
/*You may change the values of the sthreshold and hlower and hupper to get different results....*/
const int sthreshold=210;
const double hlower=178;
const double hupper=3;
int main(int argc, char* argv[]) {
int i,j,k;//for iterations
int height,width,step,channels;/*HSV means the frame after color conversion*/
int heightmono,widthmono,stepmono,channelsmono;/*mono means the frame which has the monochrome image*/
const char string1[]="monoimg.avi";/*This is the name of the video which would be the outcome of the blob detection program..*/
uchar *data,*datamono;
i=j=k=0;
IplImage *frame = 0;
int key = 0;/*Initializing the capture from the video...*/
CvCapture* capture = cvCreateFileCapture( "partofvideo3.avi" );
double fps = cvGetCaptureProperty (/*getting the capture properties......the frame rate..*/
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(string1, CV_FOURCC('D', 'I', 'V', 'X') ,fps,size) ;
if(writer !=NULL)
printf("Loaded\n");
else
printf("Not Loaded\n");
/* always check */
if (!capture) {
fprintf (stderr, "Cannot open video file!\n");
return(1);
}
height = frame->height;
width = frame->width;
step = frame->widthStep;
channels = frame->nChannels;
data = (uchar *)frame->imageData;
cvNamedWindow("monoimage", CV_WINDOW_AUTOSIZE);
cvNamedWindow("original frame", CV_WINDOW_AUTOSIZE);
for (;;) {/*keep looping till we are out of frames...*/
if (!cvGrabFrame(capture)) {
break;
}
frame = cvRetrieveFrame(capture);
IplImage *colimgbot = cvCreateImage( cvGetSize(frame), 8, 3 );
IplImage *monoimgbot = cvCreateImage( cvGetSize(frame), 8, 1 );
cvCvtColor(frame,frame,CV_RGB2HSV);
for(i=0;i< (height);i++)
{
for(j=0;j<(width);j++)
{
if((data[(height-i)*step+j*channels]<=hlower) && (data[(height-i)*step+j*channels]>=hupper))
{
if((data[(height-i)*step+j*(channels)+1])>sthreshold)
/*"height-i" because if we use only "i" we were getting vertically inverted result...hence reinverting the same
would do the required....*/
datamono[i*stepmono+j*channelsmono]=255;
else
datamono[i*stepmono+j*channelsmono]=0;}
else datamono[i*stepmono+j*channelsmono]=0;
}
}
cvErode(monoimgbot,monoimgbot,0,14);
cvDilate( monoimgbot,monoimgbot,0,15);
cvWriteFrame(writer, monoimgbot);
cvShowImage("original frame", frame);
cvShowImage("monoimage", monoimgbot);
if( (cvWaitKey(10) & 255) == 27 ) break;
}
cvReleaseVideoWriter(&writer) ;
cvDestroyWindow("monoimage");
cvReleaseCapture(&capture);
return 0;
}
when i run the program i am getting following run time error
when following line encounters
CvVideoWriter* writer=cvCreateVideoWriter(string1, CV_FOURCC( ‘D’,'I’,'V’,'X’),fps,size) ;
Output #0 , avi , to ‘monoimg.avi’ :
Stream #0.0: Video mgeg4, yuv420p, q=2-31, 90k tbn
[mpeg4 # 0x37e5c0] framerate not set
OpenCV Error: Bad Argument (Could not open codec ‘mpeg 4′:Unspecified Error) in unknown function , file
C:\User\VP\ocv\opencv\src\highgui\cvcap_ffmpeg.cpp, line 1306

First off getCaptureProperties kinda sucks at actually getting anything, so you should check that fps actually has what you think it does. Some codecs can't encode at certain framerates so try just explicitly setting fps to 30 and see if it works.
otherwise you are missing the mpeg 4 codec as it says. I'd recommend:
1.) download some codecs and try again.
http://www.divx.com/en/software/divx-plus/codec-pack probably has what you're looking for.
2.) you can change the
CvVideoWriter *writer=cvCreateVideoWriter(string1, CV_FOURCC('D', 'I', 'V', 'X') ,fps,size) ;
line to use some other codec. I played around with a couple of codecs and put the amount of time for encoding a 7 min video on my system.
(\P,\I,\M,\1) ;= MPEG-1 codec (112913.386195 msecs) (104 MB)
(\M,\J,\P,\G) ;= motion-jpeg codec (crashed)
(\M,\P,\4,\2) ;= MPEG-4.2 codec (107184.186774 msecs) (82 MB)
(\D,\I,\V,\3) ;= MPEG-4.3 codec (118308.933328 msecs) (83 MB)
(\D,\I,\V,\X) ;= MPEG-4 codec (99037.738131 msecs) (85 MB)
(\U,\2,\6,\3) ;= H263 codec (101141.993551 msecs) (89 MB)
(\I,\2,\6,\3) ;= H263I codec (crashed)
(\F,\L,\V,\1) ;= FLV1 codec (104307.567802 msecs) (93 MB)
In particular I would recommend trying the FLV1 codec as I've had a lot of luck with that. So in summary try:
CvVideoWriter *writer=cvCreateVideoWriter(string1, CV_FOURCC('F', 'L', 'V', '1') ,fps,size) ;
Good luck!

Related

Reading h264 or h265 stream with ffmpeg/OpenCV: Which is faster?

I'm using OpenCV with ffmpeg support to read a RTSP stream coming from an IP camera and then to write the frames to a video. The problem is that the frame size is 2816x2816 at 20 fps i.e. there's a lot of data coming in.
I noticed that there was a significant delay in the stream, so I set the buffer size of the cv::VideoCapture object to 1, because I thought that the frames might just get stuck in the buffer instead of being grabbed and processed. This however just caused for frames to be dropped instead.
My next move was to experiment a bit with the frame size/fps and the encoding of the video that I'm writing. All of those things helped to improve the situation, but in the long run I still have to use a frame size of 2816x2816 and support up to 20 fps, so I can't set it lower sadly.
That's where my question comes in: given the fact that the camera stream is going to be either h264 or h265, which one would be read faster by the cv::VideoCapture object? And how should I encode the video I'm writing in order to minimize the time spent decoding/encoding frames?
That's the code I'm using for reference:
using namespace cv;
int main(int argc, char** argv)
{
VideoCapture cap;
cap.set(CAP_PROP_BUFFERSIZE, 1); // internal buffer will now store only 1 frames
if (!cap.open("rtsp://admin:admin#1.1.1.1:554/stream")) {
return -1;
}
VideoWriter videoWr;
Mat frame;
cap >> frame;
//int x264 = cv::VideoWriter::fourcc('x', '2', '6', '4'); //I was trying different options
int x264 = cv::VideoWriter::fourcc('M', 'J', 'P', 'G');
videoWr = cv::VideoWriter("test_video.avi", 0, 0, 20, frame.size(), true);
namedWindow("test", WINDOW_NORMAL);
cv::resizeWindow("test", 1024, 768);
for (;;)
{
cap >> frame;
if (frame.empty()) break; // end of video stream
imshow("test", frame);
if (waitKey(10) == 27) break;
videoWr << frame;
}
return 0;
}

Recording desktop to file, using openCV

I'm attempting to utilize OpenCV, C++, on my windows 10 system to record the screen as part of a larger program I am writing. I need the ability to record the display and save the recording for later review.
I was able to find this link on stackoverflow
How to capture the desktop in OpenCV (ie. turn a bitmap into a Mat)?
User john ktejik created a function that in essence completed exactly what I am looking to accomplish, short of saving the stream to file.
Now what I have always done in the past was once I've opened a connection to my webcam or a video file, I could simply create a VideoWriter Object and write the individual frames to file. I have attempted to do just that utilizing John's function to act as a video source.
int main (int argc, char **argv)
{
HWND hwndDesktop = GetDesktopWindow ();
int key = 0;
int frame_width = 1920;
int frame_height = 1080;
VideoWriter video ("screenCap.avi", CV_FOURCC ('M', 'J', 'P', 'G'), 15, Size (frame_width, frame_height));
while (key != 27)
{
Mat src = hwnd2mat (hwndDesktop);
video.write (src);
imshow ("Screen Capture", src);
key = waitKey (27);
}
video.release ();
destroyAllWindows ();
return 0;
}
What I'm seeing as the output, is the file labeled "screenCap.avi", however the file is empty of video. The file saves as 16KB storage space.
John's function is on point, as it displays the frames just fine via imshow(), but doesn't seem to allow me to save them.
So over the weekend I played with the software some more. And as I really don't have a firm grasp on it, I figured that there had to be a problem with settings between the screen capture and the file writer.
So I started looking at each of the lines in John's function. I came across
src.create(height, width, CV_8UC4);
It seems that the Mat object is being created as with 4 color channels. Did a bit more digging and I found a couple references that point to Videowriter expecting 3 channels.
So a simple change was to convert the output of Johns function from 4 channels to 3 channels. This fixed the problem and I can now write the frames to file.
int main (int argc, char **argv)
{
HWND hwndDesktop = GetDesktopWindow ();
int key = 0;
int frame_width = 1920;
int frame_height = 1080;
VideoWriter video ("screenCap.avi", CV_FOURCC ('M', 'J', 'P', 'G'), 15, Size
(frame_width, frame_height));
while (key != 27)
{
Mat src = hwnd2mat (hwndDesktop);
Mat dst;
cvtColor (src, dst, COLOR_BGRA2RGB);
video.write (dst);
imshow ("Screen Capture", dst);
key = waitKey (27);
}
video.release ();
destroyAllWindows ();
return 0;
}

OpenCV webcam MJPG low FPS

The problem is that I am keep getting low FPS from Logitech C270 webcam capture in OpenCV3.
Things i've tried are described in code comments
Mat frame;
int main(int argc, char *argv[])
{
// i've tried it this way
//int apiBackend = cv::CAP_DSHOW;
//cv::VideoCapture cap(0+apiBackend);
//and tis way
VideoCapture cap(0);
cap.open(0);
cap.set(CAP_PROP_FOURCC ,cv::VideoWriter::fourcc('M', 'J', 'P', 'G') );
//cap.set(CAP_PROP_EXPOSURE , 1); //changing this gives no result
//cap.set(CAP_PROP_GAIN , 10); // same with this
cap.set(CAP_PROP_FPS, 100);
cap.set(CAP_PROP_FRAME_WIDTH, 640);
cap.set(CAP_PROP_FRAME_HEIGHT, 480);
while(1)
{
float e1 = cv::getTickCount();
cap >> frame; // get a new frame from camera
imshow("frame", frame);
float e2 = cv::getTickCount();
float t = (e2 - e1)/cv::getTickFrequency();
float fps = 1.0 / t;
std::cout << fps << std::endl;
if(waitKey(1) >= 0) break;
}
return 0;
}
Changing CAP_PROP_FPS to 5 works, and FPS drops ok.
Playing with resolution didn't help: from 320*240 to 1280*720 i keep getting about 16 FPS.
Webcam drivers are latest.
Am i missing something?
Thanks fo suggestions everybody!
Looks like the answer is camera-specific: i had to install Logitech Webcam Software and disable RightLight feature, now FPS is about 30.
Maybe there is some way to disable RightLight from OpenCV using cap.set(...), but this is subject for further investigation.

Cannot play video created by OpenCV (using Ubuntu 12.04)

I tried to write a video from a sequence of images created by OpenCV. However I cannot open the videos after writing them. I guess there is a codec issue. I find it extremely difficult where the error exactly comes from. Here is my code:
Size size = Size(vecOfMats[0].rows,vecOfMats[0].cols);
int codec = CV_FOURCC('D', 'I', 'V', 'X');
VideoWriter videoWriter;
videoWriter.open(outputFilename,codec,15.0,size,true);
for(int z=0; z < vecOfMats.size(); z++)
{
videoWriter.write(vecOfMats[z]);
}
videoWriter.release();
I also tried all of these codecs without success (either OpenCv could not find the codec or the video could not be opened):
int codec = CV_FOURCC('P','I','M','1'); // = MPEG-1 codec
int codec = CV_FOURCC('M','J','P','G'); // = motion-jpeg codec - cannot be played by VLC
int codec = CV_FOURCC('M', 'P', '4', '2');// = MPEG-4.2 codec - not found
int codec = CV_FOURCC('D', 'I', 'V', '3');// = MPEG-4.3 codec - not found
int codec = CV_FOURCC('D', 'I', 'V', 'X');// = MPEG-4 codec - cannot be played by VLC
int codec = CV_FOURCC('U', '2', '6', '3');// = H263 codec - must have w/h = 4
int codec = CV_FOURCC('I', '2', '6', '3');// = H263I codec - not found
I even took the codec of a video opened via OpenCV previously (without success):
string filename = "/path/to/the/video/myVideo.avi";
VideoCapture capture(filename);
int ex = static_cast<int>(capture.get(CV_CAP_PROP_FOURCC)); // Get Codec Type- Int form
char EXT[] = {(char)(ex & 0XFF) , (char)((ex & 0XFF00) >> 8),(char)((ex & 0XFF0000) >> 16),(char)((ex & 0XFF000000) >> 24), 0};// Transform from int to char via Bitwise operators
cout<<"Codec: "<<ex<<endl;
VideoWriter videoWriter;
videoWriter.open(outputFilename,ex,15.0,size,true);
I'm not even sure if the problem lies with my OpenCV or my Ubuntu :/.
(I tried to open them using the default video player and vlc)
Since I gave up on writing videos using Opencv I just want to mention how I resolved my problem. After saving every image as a png file, I'm using ffmpeg from linux command line to write the sequence of images as a video.
Pictures are named sequentially like this:
00001.png,
00002.png,
00003.png,
[…]
Then I use this command
ffmpeg -i %5d.png -vcodec mpeg4 test.avi

Writing video with openCV - no key frame set for track 0

I'm trying to modify and write some video using openCV 2.4.6.1 using the following code:
cv::VideoCapture capture( video_filename );
// Check if the capture object successfully initialized
if ( !capture.isOpened() )
{
printf( "Failed to load video, exiting.\n" );
return -1;
}
cv::Mat frame, cropped_img;
cv::Rect ROI( OFFSET_X, OFFSET_Y, WIDTH, HEIGHT );
int fourcc = static_cast<int>(capture.get(CV_CAP_PROP_FOURCC));
double fps = 30;
cv::Size frame_size( RADIUS, (int) 2*PI*RADIUS );
video_filename = "test.avi";
cv::VideoWriter writer( video_filename, fourcc, fps, frame_size );
if ( !writer.isOpened() && save )
{
printf("Failed to initialize video writer, unable to save video!\n");
}
while(true)
{
if ( !capture.read(frame) )
{
printf("Failed to read next frame, exiting.\n");
break;
}
// select the region of interest in the frame
cropped_img = frame( ROI );
// display the image and wait
imshow("cropped", cropped_img);
// if we are saving video, write the unwrapped image
if (save)
{
writer.write( cropped_img );
}
char key = cv::waitKey(30);
When I try to run the output video 'test.avi' with VLC I get the following error: avidemux error: no key frame set for track 0. I'm using Ubuntu 13.04, and I've tried using videos encoded with MPEG-4 and libx264. I think the fix should be straightforward but can't find any guidance. The actual code is available at https://github.com/benselby/robot_nav/tree/master/video_unwrap. Thanks in advance!
[PYTHON] Apart from the resolution mismatch, there can also be a frames-per-second mismatch. In my case, the resolution was correctly set, but the problem was with fps. Checking the frames per second at which VideoCapture object was reading, it showed to be 30.0, but if I set the fps of VideoWriter object to 30.0, the same error was being thrown in VLC. Instead of setting it to 30.0, you can get by with the error by setting it to 30.
P.S. You can check the resolution and the fps at which you are recording by using the cap.get(3) for width, cap.get(4) for height and cap.get(5) for fps inside the capturing while/for loop.
The full code is as follows:
import numpy as np
import cv2 as cv2
cap = cv2.VideoCapture(0)
#Define Codec and create a VideoWriter Object
fourcc = cv2.VideoWriter_fourcc('X','V','I','D')
#30.0 in the below line doesn't work while 30 does work.
out = cv2.VideoWriter('output.mp4', fourcc, 30, (640, 480))
while(True):
ret, frame = cap.read()
colored_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
print('Width = ', cap.get(3),' Height = ', cap.get(4),' fps = ', cap.get(5))
out.write(colored_frame)
cv2.imshow('frame', colored_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
The full documentation (C++) for what all properties can be checked is available here : propId OpenCV Documentation
This appears to be an issue of size mismatch between the frames written and the VideoWriter object opened. I was running into this issue when trying to write a series of resized images from my webcam into a video output. When I removed the resizing step and just grabbed the size from an initial test frame, everything worked perfectly.
To fix my resizing code, I essentially ran a single test frame through my processing and then pulled its size when creating the VideoWriter object:
#include <cassert>
#include <iostream>
#include <time.h>
#include "opencv2/opencv.hpp"
using namespace cv;
int main()
{
VideoCapture cap(0);
assert(cap.isOpened());
Mat testFrame;
cap >> testFrame;
Mat testDown;
resize(testFrame, testDown, Size(), 0.5, 0.5, INTER_NEAREST);
bool ret = imwrite("test.png", testDown);
assert(ret);
Size outSize = Size(testDown.cols, testDown.rows);
VideoWriter outVid("test.avi", CV_FOURCC('M','P','4','2'),1,outSize,true);
assert(outVid.isOpened());
for (int i = 0; i < 10; ++i) {
Mat frame;
cap >> frame;
std::cout << "Grabbed frame" << std::endl;
Mat down;
resize(frame, down, Size(), 0.5, 0.5, INTER_NEAREST);
//bool ret = imwrite("test.png", down);
//assert(ret);
outVid << down;
std::cout << "Wrote frame" << std::endl;
struct timespec tim, tim2;
tim.tv_sec = 1;
tim.tv_nsec = 0;
nanosleep(&tim, &tim2);
}
}
My guess is that your problem is in the size calculation:
cv::Size frame_size( RADIUS, (int) 2*PI*RADIUS );
I'm not sure where your frames are coming from (i.e. how the capture is set up), but likely in rounding or somewhere else your size gets messed up. I would suggest doing something similar to my solution above.