I am working with GigaE Camera and it is a grayscale image and I want to record the videos. So I have tried initially with webcam and below is my code:
#include "opencv2\highgui\highgui.hpp"
#include "iostream"
#include "opencv2/opencv.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/imgcodecs/imgcodecs.hpp"
#include "opencv2/videoio/videoio.hpp"
#include<string>
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
VideoCapture cap(0);
VideoWriter writer;
if (!cap.isOpened())
{
cout << "not opened" << endl;
return -1;
}
char* windowName = "Webcam Feed";
namedWindow(windowName, CV_WINDOW_AUTOSIZE);
string filename = "D:\videos\myVideo12.avi";
int fcc = CV_FOURCC('M', 'J', 'P', 'J');
int fps = 30;
Size frameSize(cap.get(CV_CAP_PROP_FRAME_WIDTH), cap.get(CV_CAP_PROP_FRAME_HEIGHT));
bool isColor = false;
writer = VideoWriter(filename, fcc, fps, frameSize, isColor);
if (!writer.isOpened())
{
cout << "Error not opened" << endl;
getchar();
return -1;
}
while (1)
{
Mat frame;
bool bSuccess = cap.read(frame);
if (!bSuccess)
{
cout << "ERROR READING FRAME FROM CAMERA FEED" << endl;
break;
}
cvtColor(frame, frame, CV_BGR2GRAY);
writer.write(frame);
imshow(windowName, frame);
return 0;
}
There is no video created and I don't get any error too. But it works fine with OpenCV-2.4.10.
Most likely, the video is not written because of the codec. OpenCV tends to stay silent in case of encoding (and many other) problems. Try setting fcc to -1 to choose from a list of available codecs.
Solved! The error is in the giving the filename path where I used '\' instead of '/'. The codecs are MPEG or DIV3 for grayscale images.
Related
My name is Toan. Currently, I'm using OpenCV-C++ to write video *.mp4 type. I can write video .avi type but It's take a lot of storage. About 1Mb/1s with 640x480 resolution and 15 FPS. I'm using iMX6UL-EVK board(Linux).
I built without error but no output .mp4 file. And in python code (OpenCV-Python), this board can write .mp4 video with "mp4v".
I tried with "mp4v", "xvid", "divx", "h264", "x264" but not working. So what can I do now? Or may you show me others type of video which not take much of storage ?
This is my code:
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
cout << "Built with OpenCV " << CV_VERSION << endl;
Mat image;
Mat src;
VideoCapture capture;
capture.open(2);
capture >> src;
bool isColor = (src.type() == CV_8UC3);
VideoWriter writer;
int codec = VideoWriter::fourcc('M', 'P', '4', 'V');
double fps = 15.0;
string filename = "live.mp4";
Size sizeFrame(640,480);
writer.open(filename, codec, fps, sizeFrame, isColor);
cout << "Started writing video... " << endl;
for (int i = 0 ; i < 60 ; i ++)
{
capture >> image;
Mat xframe;
resize(image,xframe,sizeFrame);
writer.write(xframe);
// imshow("Sample", image);
// char c = (char)waitKey(1);
// if(c == 27) break;
}
cout << "Write complete !" << endl;
capture.release();
writer.release();
return 0;
}
Thank you so much,
Toan
VideoWriter::fourcc('a', 'v', 'c', '1')
work fine for me to write mp4 file.
I want to crop a certain area of my raw video before extracting the frames. I don't want to use external cropping video software because I need the video to be raw for processing. Is it possible to crop a certain area before extraction ? I read about ROI for images, is it possible for video too ?
Here's my extraction code that's already working :
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <string>
#include <sstream>
using namespace cv;
using namespace std;
int c = 0;
string int2str(int &);
int main(int argc, char **argv) {
string s;
VideoCapture cap("test_video.mov");
if (!cap.isOpened())
{
cout << "Cannot open the video file" << endl;
return -1;
}
double fps = cap.get(CV_CAP_PROP_FPS);
cout << "Frame per seconds : " << fps << endl;
namedWindow("MyVideo", CV_WINDOW_NORMAL);
resizeWindow("MyVideo", 600, 600);
while (1)
{
Mat frame;
Mat Gray_frame;
bool bSuccess = cap.read(frame);
if (!bSuccess)
{
cout << "Cannot read the frame from video file" << endl;
break;
}
s = int2str(c);
//cout<<("%d\n",frame )<<endl;
c++;
imshow("MyVideo", frame);
imwrite("frame" + s + ".jpg", frame);
if (waitKey(30) == 27)
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}
string int2str(int &i) {
string s;
stringstream ss(s);
ss << i;
return ss.str();
}
Still need to crop the video, any advice ?
I'm following a tutorial to extract video frames. I've read this question, it doesn't work, also queationfrom Open CV Answer, but the solution is for capturing current frame. I have a 120fps video and want to extract all of them. Here's my code
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <string>
#include <sstream>
using namespace cv;
using namespace std;
int c = 0;
string int2str(int &);
int main(int argc, char **argv) {
string s;
VideoCapture cap("test.mp4"); // video
if (!cap.isOpened())
{
cout << "Cannot open the video file" << endl;
return -1;
}
double fps = cap.get(CV_CAP_PROP_FPS); //get the frames per seconds of the video
cout << "Frame per seconds : " << fps << endl;
namedWindow("MyVideo", CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
while (1)
{
Mat frame;
Mat Gray_frame;
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess)
{
cout << "Cannot read the frame from video file" << endl;
break;
}
s = int2str(c);
//cout<<("%d\n",frame )<<endl;
c++;
imshow("MyVideo", frame); //show the frame in "MyVideo" window
imwrite("ig" + s + ".jpg", frame);
if (waitKey(30) == 27) //esc key
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}
//int to string function
string int2str(int &i) {
string s;
stringstream ss(s);
ss << i;
return ss.str();
}
My problem is, I have a minute video at 120fps. I expected to be able to extract 120 frames. But the extraction went wrong, the video last for 1 minute but I got more than 200 frames stored in my folder. Did I do something wrong in my code ?
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;
}
}
}
Hi im trying to write a video from my webcam to my computer but I keep getting the error that my writer isnt opened. Im using windows 8 64 bit, VS 2013 & OpenCV 2.4.10. Here is the code that I am using:
#include <opencv\highgui.h>
#include <opencv\cv.h>
#include <iostream>
using namespace cv;
using namespace std;
string intToString(int number){
std::stringstream ss;
ss << number;
return ss.str();
}
int main(int argc, char* argv[])
{
VideoCapture cap(0); // open the video camera no. 0
VideoWriter writer;
if (!cap.isOpened()) // if not success, exit program
{
cout << "ERROR INITIALIZING VIDEO CAPTURE" << endl;
return -1;
}
char* windowName = "Webcam Feed";
namedWindow(windowName, CV_WINDOW_AUTOSIZE); //create a window to display our webcam feed
string filename = "C:\\thevideo.avi";
int fcc = CV_FOURCC('D', 'I', 'V', '3');
double fps = 20;
cv::Size frameSize(cap.get(CV_CAP_PROP_FRAME_WIDTH), cap.get(CV_CAP_PROP_FRAME_HEIGHT));
writer = VideoWriter(filename, fcc, fps, frameSize);
if (!writer.isOpened())
{
cout << "the writer isnt opened" << endl;
getchar();
return -1;
}
while (1) {
Mat frame;
bool bSuccess = cap.read(frame); // read a new frame from camera feed
if (!bSuccess) //test if frame successfully read
{
cout << "ERROR READING FRAME FROM CAMERA FEED" << endl;
break;
}
writer.write(frame);
imshow(windowName, frame); //show the frame in "MyVideo" window
//listen for 10ms for a key to be pressed
switch (waitKey(10)){
case 27:
//'esc' has been pressed (ASCII value for 'esc' is 27)
//exit program.
return 0;
}
}
return 0;
}
Can anyone help me?
I find the use a bit confusing. You do:
VideoWriter writer;
and then
writer = VideoWriter(filename, fcc, fps, frameSize);
Either do:
VideoWriter writer = VideoWriter(filename, fcc, fps, frameSize);
or
VideoWriter writer;
writer.open(filename, 0, fps, frameSize, 1);
Perhaps that is the issue?
Also, in writer.open(), the last parameter sis the colour setting. Set it accordingly. I have assumed you have colour input.
Also, a more complicated thing could be codec issue. I read that OPENCV can only write AVI files. So, I am not sure if it can use the DIV3 codec for writing. Call the writer with:
writer = VideoWriter(filename, -1, fps, frameSize);
and see what codecs can be used.
Try this codec:
int fcc = CV_FOURCC('M', 'J', 'P', 'G')
instead of:
int fcc = CV_FOURCC('D', 'I', 'V', '3');