OpenCV: Displaying video files from media/camera

Displaying video from media

#include "stdafx.h"
#include "opencv2/core/core.hpp"
#include "opencv2/flann/miniflann.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/photo/photo.hpp"
#include "opencv2/video/video.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/ml/ml.hpp"
#include "opencv2/highgui/highgui.hpp"
//#include "opencv2/contrib/contrib.hpp"
#include "opencv2/core/core_c.h"
#include "opencv2/highgui/highgui_c.h"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/core/core.hpp"

using namespace cv;
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, const char** argv )
{
VideoCapture cap("C:/Users/shubham/Downloads/android/coursera/vedam.MP4"); // opens the video file

if (!cap.isOpened()) // if not success, exit program
{
cout << "Cannot open the video file" << endl;
return -1;
}

double frame = cap.get(CAP_PROP_POS_FRAMES);
double fmsRate = cap.get(CAP_PROP_FPS);

namedWindow("myVideo", CV_WINDOW_AUTOSIZE);

while (1)
{
Mat frame;
if (cap.read(frame))
{
imshow("myVideo", frame);
}
else
{
cout << "error reading frames \n";
}

if (waitKey(fmsRate) == 27)
{
break;
}
}
return 0;
}

Displaying video from camera

 

#include "stdafx.h"
#include "opencv2/core/core.hpp"
#include "opencv2/flann/miniflann.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/photo/photo.hpp"
#include "opencv2/video/video.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/ml/ml.hpp"
#include "opencv2/highgui/highgui.hpp"
//#include "opencv2/contrib/contrib.hpp"
#include "opencv2/core/core_c.h"
#include "opencv2/highgui/highgui_c.h"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/core/core.hpp"

using namespace cv;
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, const char** argv )
{
VideoCapture cap(0); // opens the video camera 0

if (!cap.isOpened()) // if not success, exit program
{
cout << "Cannot open the video file" << endl;
return -1;
}

double frame = cap.get(CAP_PROP_POS_FRAMES);

//playing with other arguments of namedWindow to see the effect.

namedWindow("myVideo", CV_WINDOW_NORMAL);

while (1)
{
Mat frame;
if (cap.read(frame))
{
imshow("myVideo", frame);
}
else
{
cout << "error reading frames \n";
}

if (waitKey(30) == 27)
{
break;
}
}
return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *