Hey peeps so far i manage OpenCV to play a video.avi but what should i do now to extract frames...?
below is the code i written so far that got my video playing:
#include<opencv\cv.h>
#include<opencv\highgui.h>
#include<opencv\ml.h>
#include<opencv\cxcore.h>
int main( int argc, char** argv ) {
cvNamedWindow( "DisplayVideo", CV_WINDOW_AUTOSIZE );
CvCapture* capture = cvCreateFileCapture( argv[1] );
IplImage* frame;
while(1) {
frame = cvQueryFrame( capture );
if( !frame ) break;
cvShowImage( "DisplayVideo", frame );
char c = cvWaitKey(33);
if( c == 27 ) break;
}
cvReleaseCapture( &capture );
cvDestroyWindow("DisplayVideo" );
}
frame is the frame you are extracting. If you want to convert that to a cv::Mat you can do that by creating a mat with that IplImage:
Mat myImage(IplImage);
There is a nice tutorial on it here.
However, you are doing it the old way. The newest version of OpenCV has the latest camera capture abilities, and you should do something like this:
#include "cv.h"
#include "highgui.h"
using namespace cv;
int main()
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
namedWindow("Output",1);
while(true)
{
Mat frame;
cap >> frame; // get a new frame from camera
//Do your processing here
...
//Show the image
imshow("Output", frame);
if(waitKey(30) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
Related
Im relativly new to OpenCV. In this case I tried to save an image using cvSaveImage after making some processing, but this error was thrown
Assertion failed ((flags & FIXED_TYPE) != 0) in cv::_InputArray::type, file C:\build\master_winpack-build-win64-vc14\opencv\modules\core\src\matrix_wrap.cpp, line 807
It seems, like it has some problem with type of input array, but i have no idea why?.
Here is what my code look like
int main(int argc, char** argv) {
IplImage* img = cvLoadImage("HOLES_CAM1_NG.bmp", CV_LOAD_IMAGE_GRAYSCALE);
IplImage* houghImg = cvCloneImage(img);
/*
SOME PROCESSING
*/
cvSaveImage("HOLES_CAM1_NG_processed.png", houghImg);
cvReleaseImage(&img);
cvReleaseImage(&houghImg);
}
You are using the deprecated C API.
Please try doing something like this:
Reference: https://docs.opencv.org/2.4/doc/tutorials/introduction/load_save_image/load_save_image.html
#include <cv.h>
#include <highgui.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <stdio.h>
using namespace cv;
int main( int argc, char** argv )
{
Mat img;
img = imread( "HOLES_CAM1_NG.bmp", CV_LOAD_IMAGE_GRAYSCALE );
if(!img.data )
{
printf( " No image data \n " );
return -1;
}
/*
SOME PROCESSING
*/
imwrite( "HOLES_CAM1_NG_processed.png", houghImg );
namedWindow( "Original image", CV_WINDOW_AUTOSIZE );
namedWindow( "Gray image", CV_WINDOW_AUTOSIZE );
imshow( "Original image", img );
imshow( "Hough image", houghImg );
waitKey(0);
return 0;
}
If cvSaveImage() is not working, it would have been better to remove it like cvCopyImage :)
I am working on visual studio 2010 C++ and Opencv 2.3.1. Using HP laptop windows 7 32 bit. I really have tried many times to solve this problem but it still happening. My built-in webcam with some codes (some times works and some times not ) and with some other codes it always displays a gray window instead of camera feed?
could anyone help?
Thanks in advance.
For Example The first code sometimes display cam feed and the second one always display gray window
#include "opencv2/opencv.hpp"
using namespace cv;
using namespace std;
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
if (!cap.isOpened()) // check if we succeeded
return -1;
Ptr<BackgroundSubtractor> pMOG = new BackgroundSubtractorMOG2();
Mat fg_mask;
Mat frame;
int count = -1;
for (;;)
{
// Get frame
cap >> frame; // get a new frame from camera
// Update counter
++count;
// Background subtraction
pMOG->operator()(frame, fg_mask);
imshow("frame", frame);
imshow("fg_mask", fg_mask);
// Save foreground mask
string name = "mask_" + std::to_string(static_cast<long long>(count)) + ".png";
imwrite("D:\\SO\\temp\\" + name, fg_mask);
if (waitKey(1) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
////////////////////////
second code:
// WriteVideo.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
VideoCapture cap(0); // open the video camera no. 0
if (!cap.isOpened()) // if not success, exit program
{
cout << "ERROR: Cannot open the video file" << endl;
return -1;
}
namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video
cout << "Frame Size = " << dWidth << "x" << dHeight << endl;
Size frameSize(static_cast<int>(dWidth), static_cast<int>(dHeight));
VideoWriter oVideoWriter ("D:/visual outs/mix/WriteVideo.avi", CV_FOURCC('P','I','M','1'), 20, frameSize, true); //initialize the VideoWriter object
if ( !oVideoWriter.isOpened() ) //if not initialize the VideoWriter successfully, exit the program
{
cout << "ERROR: Failed to write the video" << endl;
return -1;
}
while (1)
{
Mat frame;
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "ERROR: Cannot read a frame from video file" << endl;
break;
}
oVideoWriter.write(frame); //writer the frame into the file
imshow("MyVideo", frame); //show the frame in "MyVideo" window
if (waitKey(10) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}
Without the actual code, there's nothing we can really do in order to pinpoint the issue. Could you reformulate your question please? Anyway, in order to instantiate a VideoCapture object in OpenCV, the C++ code would be pretty much something like this
#include "opencv2/opencv.hpp"
using namespace cv;
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
Mat edges;
namedWindow("edges",1);
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
cvtColor(frame, edges, CV_BGR2GRAY);
GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
Canny(edges, edges, 0, 30, 3);
imshow("edges", edges);
if(waitKey(30) >= 0) break; // Wait 30 ms for a key feed, then break out of the code if a keypress is found in the message queue
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
As for the standard C version, you'd have to leverage the old yet still reliable CvCapture struct which is contained in the old OpenCV API. In this case, you'd act upon the cvCreateCameraCapture(0) which simply returns a pointer to the newly created camera instance (CvCapture). In the latter case, the code would evaluate to the snipper hereunder.
# include "highgui.h"
# include "cv.h"
int main( int argc, char** argv )
{
CvCapture* capture;
capture = cvCreateCameraCapture(0);
if (!capture)
return -1;
IplImage* bgr_frame = cvQueryFrame(capture); // Query the next frame
CvSize size = cvSize(
(int)cvGetCaptureProperty(capture,
CV_CAP_PROP_FRAME_WIDTH),
(int)cvGetCaptureProperty(capture,
CV_CAP_PROP_FRAME_HEIGHT)
);
cvNamedWindow("OpenCV via CvCapture", CV_WINDOW_AUTOSIZE); // Create a window and assign it a title
/* Open a CvVideoWriter to save the video to a file. Think of it as a stream */
CvVideoWriter *writer = cvCreateVideoWriter(argv[1],
CV_FOURCC('D','I','V','X'),
30,
size
);
while((bgr_frame = cvQueryFrame(capture)) != NULL)
{
cvWriteFrame(writer, bgr_frame);
cvShowImage("OpenCV via CvCapture", bgr_frame);
char c = cvWaitKey(30); // Same as the snippet above. Wait for 30 ms
if(c == 27) break; // If the key code is 0x27 (ESC), break
}
cvReleaseVideoWriter(&writer); // Dispose the CvVideoWriter instance
cvReleaseCapture(&capture); // Dispose the CvCapture instance
cvDestroyWindow("OpenCV via CvCapture"); // Destroy the window
return 0;
}
I'm trying to play a video file using the following code.
When run it only shows a black screen with the window name (Video), can anyone help me fix it.
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <opencv2\core\core.hpp>
#include "opencv2/opencv.hpp"
using namespace cv;
int main( int argc, char** argv )
{
CvCapture* capture = cvCreateFileCapture( "1.avi" );
Mat frame= cvQueryFrame(capture);
imshow("Video", frame);
waitKey();
cvReleaseCapture(&capture);
}
Try this if you only want to play a video ::::::::::::::::::::::::::
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <opencv2\core\core.hpp>
#include "opencv2/opencv.hpp"
int main(int argc, char** argv)
{
cvNamedWindow("Example3", CV_WINDOW_AUTOSIZE);
//CvCapture* capture = cvCreateFileCapture("20051210-w50s.flv");
CvCapture* capture = cvCreateFileCapture("1.wmv");
/* if(!capture)
{
std::cout <<"Video Not Opened\n";
return -1;
}*/
IplImage* frame = NULL;
while(1) {
frame = cvQueryFrame(capture);
//std::cout << "Inside loop\n";
if (!frame)
break;
cvShowImage("Example3", frame);
char c = cvWaitKey(33);
if (c == 27) break;
}
cvReleaseCapture(&capture);
cvDestroyWindow("Example3");
std::cout << "Hello!";
return 0;
}
Actually the code you posted won't even compile.
Just have a look at OpenCV documentation: Reading and Writing images and video
#include "opencv2/opencv.hpp"
using namespace cv;
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
//Video Capture cap(path_to_video); // open the video file
if(!cap.isOpened()) // check if we succeeded
return -1;
namedWindow("Video",1);
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
imshow("Video", frame);
if(waitKey(30) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
I am a beginner to OpenCV and I wish to play a video in OpenCV. I've made a code but it's displaying a single image only.
I am using OpenCV 2.1 and Visual Studio 2008.
I would really appreciate it if someone guided me where am I going wrong.
Here is my pasted code:
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
int main()
{
CvCapture* capture = cvCaptureFromAVI("C:/OpenCV2.1/samples/c/tree.avi");
IplImage* img = 0;
if(!cvGrabFrame(capture)){ // capture a frame
printf("Could not grab a frame\n\7");
exit(0);}
cvQueryFrame(capture); // this call is necessary to get correct
// capture properties
int frameH = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT);
int frameW = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH);
int fps = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
int numFrames = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_COUNT);
///numFrames=total number of frames
printf("Number of rows %d\n",frameH);
printf("Number of columns %d\n",frameW,"\n");
printf("frames per second %d\n",fps,"\n");
printf("Number of frames %d\n",numFrames,"\n");
for(int i=0;i<numFrames;i++)
{
IplImage* img = 0;
img=cvRetrieveFrame(capture);
cvNamedWindow( "img" );
cvShowImage("img", img);
}
cvWaitKey(0);
cvDestroyWindow( "img" );
cvReleaseImage( &img );
cvReleaseCapture(&capture);
return 0;
}
You have to use cvQueryFrame instead of cvRetrieveFrame. Also as pointed out by #Chipmunk, you have to add a delay after cvShowImage.
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
cvNamedWindow( "img" );
for(int i=0;i<numFrames;i++)
{
IplImage* img = cvQueryFrame(capture);
cvShowImage("img", img);
cvWaitKey(10);
}
Here is the complete method to play a video using OpenCV:
int main()
{
CvCapture* capture = cvCreateFileCapture("C:/OpenCV2.1/samples/c/tree.avi");
IplImage* frame = NULL;
if(!capture)
{
printf("Video Not Opened\n");
return -1;
}
int width = (int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_WIDTH);
int height = (int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_HEIGHT);
double fps = cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
int frame_count = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_COUNT);
printf("Video Size = %d x %d\n",width,height);
printf("FPS = %f\nTotal Frames = %d\n",fps,frame_count);
while(1)
{
frame = cvQueryFrame(capture);
if(!frame)
{
printf("Capture Finished\n");
break;
}
cvShowImage("video",frame);
cvWaitKey(10);
}
cvReleaseCapture(&capture);
return 0;
}
After showing a image on the window, there has to be a delay or a wait before the next image can be show, I think you can guess why that is. Okay so for that delay we use cvWaitKey() .
And thats what I have added in the code in the loop.
cvNamedWindow( "img" );
for(int i=0;i<numFrames;i++)
{
IplImage* img = 0;
img=cvRetrieveFrame(capture);
cvShowImage("img", img);
cvWaitKey(10);
}
i have a little problem according "displaying a video with opencv". The code is written in c++ with visual studio 2008.
here is the code:
int main( int argc, char** argv )
{
cvNamedWindow( "xample2", CV_WINDOW_AUTOSIZE );
CvCapture* capture = cvCreateFileCapture( "Micro-dance_2_.avi" );
IplImage* frame;
while(1) {
frame = cvQueryFrame( capture );
if( !frame ) break;
cvShowImage( "xample2", frame );
char c = cvWaitKey(33);
if( c == 27 ) break;
}
cvReleaseCapture( &capture );
cvDestroyWindow( "xample2" );
}
when debugging, the programm launches and i can see the command window and a grey window (wher the video should be displayed i suppose) for a few milliseconds. Then both windows close.
the output from debug window in visual shows the following:
..
. (a lot of loaded and unloaded dlls)
.
.
.
The program '[3684] 2aufg4).exe: Native' has exited with code 0 (0x0).
i dont know what i am doing wrong...
i would appreciate your help a lot!
as allways thank you guys
You need to check the return of cvCreateFileCapture() and make sure it loaded the file successfully:
#include <cv.h>
#include <highgui.h>
int main(int argc, char** argv)
{
cvNamedWindow("xample2", CV_WINDOW_AUTOSIZE);
CvCapture* capture = cvCreateFileCapture( "Micro-dance_2_.avi" );
if (!capture)
{
std::cout << "!!! cvCreateFileCapture didn't found the file !!!\n";
return -1;
}
IplImage* frame;
while (1)
{
frame = cvQueryFrame(capture);
if(!frame)
break;
cvShowImage("xample2", frame);
char c = cvWaitKey(33);
if (c == 27)
break;
}
cvReleaseCapture(&capture);
cvDestroyWindow("xample2");
}
Try this
int main( int argc, char** argv )
{
cvNamedWindow( "xample2", CV_WINDOW_AUTOSIZE );
CvCapture* capture = cvCreateFileCapture( "Micro-dance_2_.avi" );
IplImage* frame;
if(!cvQueryFrame( capture )){
std::cout << "Could not open file\n";
return -1;
}
while(1) {
frame = cvQueryFrame( capture );
if( !frame ) break;
cvShowImage( "xample2", frame );
char c = cvWaitKey(33);
if( c == 27 ) break;
}
cvReleaseCapture( &capture );
cvDestroyWindow( "xample2" );
}