Displaying a video using opencv - c++

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" );
}

Related

OpenCV C++ error using cvSaveImage Error:Assertion failed ((flags & FIXED_TYPE) != 0) in cv::_InputArray::type

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 :)

When using namedWindow from OpenCV it is not updating after I use VideoCapture::open(filename), why?

So I have been experimenting with OpenCV and I don't understand the update rules in regards to namedWindow. I would expect to be able to place the lines as either:
cv::namedWindow( "Example-in", cv::WINDOW_AUTOSIZE );
cv::namedWindow( "Example-out", cv::WINDOW_AUTOSIZE );
g_cap.open( string(argv[1]) );
or
g_cap.open( string(argv[1]) );
cv::namedWindow( "Example-in", cv::WINDOW_AUTOSIZE );
cv::namedWindow( "Example-out", cv::WINDOW_AUTOSIZE );
The first way works fine and updates my windows. However, the second way only produces a still image with and without the filter.
Full code here:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <fstream>
#include <opencv2/opencv.hpp>
using namespace std;
void example2_5( const cv::Mat & image ) {
// Create some windows to show the input
// and output images in.
//
//cv::namedWindow( "Example2_5-in", cv::WINDOW_AUTOSIZE );
//cv::namedWindow( "Example2_5-out", cv::WINDOW_AUTOSIZE );
// Create a window to show our input image
//
cv::imshow( "Example2_5-in", image );
// Create an image to hold the smoothed output
//
cv::Mat out;
// Do the smoothing
// ( Note: Could use GaussianBlur(), blur(), medianBlur() or bilateralFilter(). )
//
cv::GaussianBlur( image, out, cv::Size(5,5), 3, 3);
cv::GaussianBlur( out, out, cv::Size(5,5), 3, 3);
// Show the smoothed image in the output window
//
cv::imshow( "Example2_5-out", out );
// Wait for the user to hit a key, windows will self destruct
//
cv::waitKey( 10 );
}
int g_run = 1, g_dontset = 0; //start out in single step mode
cv::Size size(640,360);
cv::VideoCapture g_cap;
void onTrackbarSlide( int pos, void *) {
g_cap.set( cv::CAP_PROP_POS_FRAMES, pos );
if( !g_dontset )
g_run = 1;
g_dontset = 0;
}
int main( int argc, char** argv ) {
cout<<"g_run :"<<g_run<<"\n g_dontset"<<g_dontset<<endl;
cv::namedWindow( "Example2_5-in", cv::WINDOW_AUTOSIZE );
cv::namedWindow( "Example2_5-out", cv::WINDOW_AUTOSIZE );
g_cap.open( string(argv[1]) );
g_cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
g_cap.set(CV_CAP_PROP_FRAME_HEIGHT, 360);
cv::Mat frame;
for(;;) {
cout<<"g_run :"<<g_run<<"\n g_dontset"<<g_dontset<<endl;
if( g_run != 0 ) {
g_cap >> frame; if(frame.empty()) break;
cv::resize(frame,frame,size,0,0,CV_INTER_CUBIC);
example2_5(frame);
g_dontset = 1;
g_run-=1;
}
char c = (char) cv::waitKey(10);
if( c == 's' ) // single step
{g_run = 1; cout << "Single step, run = " << g_run << endl;}
if( c == 'r' ) // run mode
{g_run = -1; cout << "Run mode, run = " << g_run <<endl;}
if( c == 27 )
break;
}
return(0);
}
Apologies for ugliness

Playing a video in OpenCV

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);
}

How to extract Frames from AVI video

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;
}

How to know the size of a frame or image

This may seem trivial for most people but I am getting problems in determining the exact size i.e. the exact width and height of my video frames. I used cvGetSize but I am probably coding it inaccurately because I am getting an error. Is it possible to output the values of the width and height of my frames as I have included in my code below? Please I would appreciate it if someone could advise me on this.
#include "cv.h"
#include "highgui.h"
#include "iostream"
using namespace std;
int main( int argc, char* argv ) {
CvCapture *capture = NULL;
capture = cvCaptureFromAVI("C:\\walking\\lady walking.avi");
if(!capture){
return -1;
}
IplImage* color_frame = NULL;
IplImage* gray_frame = NULL ;
int thresh_frame = 17;
int frameCount=0;//Counts every 5 frames
cvNamedWindow( "contours", CV_WINDOW_AUTOSIZE );
while(1) {
color_frame = cvQueryFrame( capture );//Grabs the frame from a file
if( !color_frame ) break;
gray_frame = cvCreateImage(cvSize(color_frame->width, color_frame->height), color_frame->depth, 1);
if( !color_frame ) break;// If the frame does not exist, quit the loop
frameCount++;
if(frameCount==5)
{
cvCvtColor(color_frame, gray_frame, CV_BGR2GRAY);
cvThreshold(gray_frame, gray_frame, thresh_frame, 255, CV_THRESH_TOZERO_INV);
cvGetSize(gray_frame);
int w;
int h;
cvSize(w,h);
cout <<" dimensions " << cvSize(w, h) << endl;
cvShowImage("contours", gray_frame);
frameCount=0;
}
char c = cvWaitKey(33);
if( c == 27 ) break;
}
cvReleaseImage(&color_frame);
cvReleaseImage(&gray_frame);
cvReleaseCapture( &capture );
cvDestroyWindow( "contours" );
return 0;
}
Try the following code :)
The key point is the usage of cvGetSize function and CvSize structure.
#include "cv.h"
#include "highgui.h"
#include "iostream"
using namespace std;
int main( int argc, char* argv ) {
CvCapture *capture = NULL;
capture = cvCaptureFromAVI("C:\\walking\\lady walking.avi");
if(!capture){
return -1;
}
IplImage* color_frame = NULL;
IplImage* gray_frame = NULL ;
int thresh_frame = 17;
int frameCount=0;//Counts every 5 frames
cvNamedWindow( "contours", CV_WINDOW_AUTOSIZE );
while(1) {
color_frame = cvQueryFrame( capture );//Grabs the frame from a file
if( !color_frame ) break;
gray_frame = cvCreateImage(cvSize(color_frame->width, color_frame->height), color_frame->depth, 1);
if( !color_frame ) break;// If the frame does not exist, quit the loop
frameCount++;
if(frameCount==5)
{
cvCvtColor(color_frame, gray_frame, CV_BGR2GRAY);
cvThreshold(gray_frame, gray_frame, thresh_frame, 255, CV_THRESH_TOZERO_INV);
CvSize dim = cvGetSize(gray_frame);
cout <<" dimensions:: height:" << dim.height<<" width:"<< dim.width<< endl;
cvShowImage("contours", gray_frame);
frameCount=0;
}
char c = cvWaitKey(33);
if( c == 27 ) break;
}
cvReleaseImage(&color_frame);
cvReleaseImage(&gray_frame);
cvReleaseCapture( &capture );
cvDestroyWindow( "contours" );
return 0;
}