create a video from image sequence in open cv - c++

I am working on open cv with c++.I have around 30 frames of images .I need to combine these frames to obtain a video.Can anyone suggest me method?Is it like reading each frames and storing in videowriter?Please suggest

I would suggest using ffmpeg. You can do it programmatically or using the command line. An example of doing this on the command line is:
ffmpeg -start_number n -i image%d.jpg -vcodec mpeg4 test.avi
Where n is the first image number.

Tutorial how to do Videooutput
You will be asked for the codec you want to use. It's a bit complicated, but there are already many codec related questions on SO.
This code should work (not tested):
#include <iostream> // for standard I/O
#include <string> // for strings
#include <vector>
#include <opencv2/core/core.hpp> // Basic OpenCV structures (cv::Mat)
#include <opencv2/highgui/highgui.hpp> // Video write
using namespace std;
using namespace cv;
int main(int argc, char *argv[], char *window_name){
vector<Mat> images; //fill this somehow
Size S = vector[0].getSize();
VideoWriter outputVideo; // Open the output
outputVideo.open(NAME , ex=-1, 30), S, true); //30 for 30 fps
if (!outputVideo.isOpened()){
cout << "Could not open the output video for write: "<< endl;
return -1;
}
for(int i=0; i<images.size(); i++){
outputVideo << res;
}
cout << "Finished writing" << endl;
return 0;
}

Related

Saved video doesn't have the same duration as streamed video from a camera

I have a question about saving a video with openCV in C++ (I'm using Linux Ubuntu).
I was trying to save a stream from open camera for some time. I finally succeeded, but now I have really not many ideas why the saved stream doesn't have the same duration as I was streaming from my camera. When I am streaming for 10 seconds it has only for example 2-3 seconds and looks like it is accelerated.
Does anybody have some clue what could be the problem? Something wrong in my code or maybe computing performance, maybe the system doesn't save every frame?
Thanks for your help.
My code:
#include <stdio.h>
#include <iostream> // for standard I/O
#include <string> // for strings
#include <opencv2/core.hpp> // Basic OpenCV structures (cv::Mat)
#include <opencv2/videoio.hpp> // Video write
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
Mat capture;
VideoCapture cap(0);
if(!cap.isOpened())
{
cout<<"Cannot connect to camera"<<endl;
return -1;
}
namedWindow("Display",CV_WINDOW_AUTOSIZE);
double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH);
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
Size frameSize(static_cast<int>(dWidth), static_cast<int>(dHeight));
VideoWriter oVideoWriter ("/home/Stream_video/cpp/Cam/out.avi", CV_FOURCC('P','I','M','1'), 20, frameSize,true);
if ( !oVideoWriter.isOpened() ) {
cout << "ERROR: Failed to write the video" << endl;
return -1;
}
while(true){
Mat frame;
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess) {
cout << "ERROR: Cannot read a frame from video file" << endl;
break; //if not success, break loop
}
oVideoWriter.write(frame); //writer the frame into the file
imshow("Display", frame);
if (waitKey(10) == 27) {
cout << "esc key is pressed by user" << endl;
break;
}
}
}

Failed to output video file by OpenCV on MacOS with C++

#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
int main(){
VideoCapture vcap("0");
if(!vcap.isOpened()){
cout << "Error opening video stream or file" << endl;
return -1;
}
int frame_width= vcap.get(CV_CAP_PROP_FRAME_WIDTH);
int frame_height= vcap.get(CV_CAP_PROP_FRAME_HEIGHT);
VideoWriter video("~/out.avi", CV_FOURCC('J','P','E','G'), 10, Size(frame_width,frame_height),true);
for(;;){
Mat frame;
vcap >> frame;
video << frame;
imshow( "Frame", frame );
char c = (char)waitKey(33);
if( c == 'q' ) break;
}
return 0;
}
I have tried the above code, the program can correctly show the camera image through imshow(), but after terminate the program, no video output file has written to the disk. I use MacOS Xcode and OpenCV 2.4.13 as development platform. The same work and output the video file completely on Windows Visual Studio. Please help.

Getting an Image (Stream Data) from the ArDrone 2.0, using opencv2 or opencv

I want to get an image (Stream Data) from the ArDrone, using opencv2 or opencv.
#include <iostream>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
using namespace std;
using namespace cv;
int main()
{
cv::VideoCapture cap;
cv::Mat image;
if (!cap.open("tcp://192.168.1.1:5555"))
{
printf("AR.Drone ERROR CONNECT\n");
return -1;
}
while (1)
{
cap >> image;
if (!image.empty())
{
cv::imshow("AR.Drone", image);
cout << "OK" << endl;
}
else
{
cout << "ERROR" << endl;
cv::waitKey(1);
}
}
return 0;
}
I ve just discovered that great solution which mixes AR Drone 2 and OpenCV.
It may help to have a look to it :
https://github.com/puku0x/cvdrone
The AR.Drone uses a non-standard video format that many libraries and applications cannot handle. For best results, you will need to decode the video as sent by the drone, which is H264 video combined with the drone's custom PaVE headers. For more information, see other posts, such as Ardrone Video Stream decoding in Android

Displaying Images from file using OpenCV VideoCapture, and C++ vectors

I am reading in an image sequence from a file using an OpenCV VidoeCapture - I believe I am doing this part correctly - and then putting them in a c++ vector, for processing at a later point.
To test this, I wrote the following that would read in images, put them in a vector, and then display those images from the vector one by one. However, when I run this, no images appear.
What's wrong?
I am using a raspberry pi, I don't know if that makes any difference.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <vector>
#include <iostream>
using namespace cv;
using namespace std;
vector<Mat> imageQueue;
int main(int argc, char** argv)
{
string arg = ("/home/pi/pictures/ceilingSequence/%02d.jpg");
VidoeCapture sequence(arg);
if(!sequence.isOpened()) {
cout << "Failed to open image sequence" << endl;
return -1;
}
Mat image;
for(;;)
{
sequence >> image;
if(image.empty()) {
cout << "End of sequence" << endl;
break;
}
imageQueue.push_back(image);
}
for(int i = 0; i < 10; i++)
{
//display 10 images
Mat readImage;
readImage = imageQueue[i];
namedWindow("Current Image", CV_WINDOW_AUTOSIZE);
imshow("Current Image", readImage);
sleep(2);
}
return 0;
}
please replace the sleep(2) with a waitKey(2000). // assuming you want to wait for 2 seconds
even if you're not interested in keypresses in general, it is needed to update the opencv / highgui graphics loop correctly.

How to get the average output of two consecutive frames?

I am working on the following code
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
int main()
{
VideoCapture *camera = new VideoCapture();
camera->open(0);
if(!camera->isOpened())
{
cout << "No Camera" << endl;
return -1;
}
Mat image,blur,image2;
namedWindow("Video");
while(true)
{
*camera>>image;
*camera>>image2;
//Show default image
imshow("Video",image);
if(waitKey(30)>=0)
{
break;
}
}
return 0;
}
I got to know I can reduce the video of real time web camera output by 70% if I manage to get the average output of two consecutive frames. I got the two consecutive frames using
*camera>>image;
*camera>>image2;
Now, how can I get the average of this and display it?
For cv::Mat, you can do it like this:
Mat img_mean=0.5*image+0.5*image2;
imshow("Average",img_mean);