#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.
Related
My problem is that I cannot read from file I tried with .mp4 and .mov using cpp code on windows with library opencv3.4.0. I tried to read from camera it is working.
What could be the reason ?
cv::VideoCapture cap("001.mp4");
// Check if camera opened successfully
if (!cap.isOpened()) {
std::cout << "Error opening video stream or file" << std::endl;
break; //==>hits here
}
/// while below code part works correctly
cv::VideoCapture cap(0);
// Check if camera opened successfully
if (!cap.isOpened()) {
std::cout << "Error opening video stream or file" << std::endl;
break;
}
Copy and try below code, if it doesnt work either. It is completely about your opencv installation or directory mistake.
#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <stdio.h>
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int main(){
VideoCapture cap("videoname.mp4");
if(!cap.isOpened()){
cout << "Error opening video stream or file" << endl;
return -1;
}
while(1){
Mat frame;
cap >> frame;
if (frame.empty())
break;
imshow( "Frame", frame );
char c=(char)waitKey(25);
if(c==27)
break;
}
cap.release();
destroyAllWindows();
return 0;
}
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;
}
}
}
Compiled opencv 3.2.0 to mingw - codeblocks in windows 10 creators
i can write programs in opencv, they run, e.g. Lena.cpp.
but when i need the camera, the program run, making the window but it doesn't show the camera(0) of the laptop.
Here's the code:
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main() {
VideoCapture stream1(0); //0 is the id of video device.0 if you have only one camera.
if (!stream1.isOpened()) { //check if video device has been initialised
cout << "cannot open camera";
}
//unconditional loop
while (true) {
Mat cameraFrame;
stream1.read(cameraFrame);
imshow("cam", cameraFrame);
if (waitKey(30) >= 0)
break;
}
return 0;
}
I run the code (as below) that can play video stream on windows platform(Visual Studio 13 C++).
The code compile successfully on Raspberry Pi but it can not show video.
The video file is in the same document. I had try VideoCapture cap('/home/pi/1.mov') that also doesn't work.
How should I modify the code?
PS. If VideoCapture cap("1.mov") change to VideoCapture cap(0) which open USB camera successfully.
Thanks!
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int, char** argv)
{
Mat frame;
VideoCapture cap("1.mov");
if (!cap.isOpened())
cout << "Error when reading stream";
namedWindow("Frame", 1);
for (;;)
{
cap >> frame;
if (frame.empty())
break;
imshow("Frame", frame);
if (waitKey(10) >= 0)
{
break;
}
}
return(0);
}
Result: Error when reading stream
You need to install proper codecs. See a similar question (and code) here: http://answers.opencv.org/question/9692/mp4-reading/
I have OpenCV-2.4.9 installed in Raspberry Pi. Right now I am trying to load a video from specific path and for that I tried with both C and C++ API
C API: cvCaptureFromFile(path);
C++ API: VideoCapture cap; cap.open(path)
I am getting error and it says could not open file.
It works well in Windows and Linux, but not in Raspberry Pi. Am I missing something?
C++ code:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(){
VideoCapture cap("C:/Users/nava/Videos/file.mp4");
if (!cap.isOpened()){
cout << "Error opening video stream" << endl;
return -1;
}
while (1){
Mat Frame;
if (!cap.read(Frame)){
cout << "No Frame available" << endl;
waitKey();
}
imshow("output", Frame);
if (waitKey(33) == 27) break;
}
}
C Code:
#include "highgui.h"
int main(int argc, char** argv)
{
cvNamedWindow("video",CV_WINDOW_AUTOSIZE);
CvCapture* capture = cvCreateFileCapture("/home/pi/Desktop/test.mp4");
IplImage* frame;
while(1)
{
frame = cvQueryFrame(capture);
if(!frame) break;
cvShowImage("video", frame);
char c = cvWaitKey(33);
if(c == 27) break;
}
}
You have to install UV4L driver.Follow this tutorial :
http://www.linux-projects.org/modules/sections/index.php?op=viewarticle&artid=14