I'm a math undergrad and have little programming experience. I'm interested in computer vision however. Tried to follow the Learning OpenCV book but its slightly outdated. How do i save the resulting video file in my linux home directory? for eg "/home/user/..", thanks in advance, this is my first post and i know i won't be disappointed. I'm compiling on eclipse btw, and i'm not too familiar with the arguments setting.
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>
int main(int argc, char *argv[]) {
int isColor = 1;
int frameW = 640;
int frameH = 480;
int fps = 25;
CvCapture* capture = cvCaptureFromCAM(0);
assert( capture != NULL );
cvNamedWindow( "Webcam", CV_WINDOW_AUTOSIZE);
CvVideoWriter *writer = cvCreateVideoWriter(
"out.avi",
CV_FOURCC('M','J','P','G'),
fps,
cvSize(frameW,frameH),
isColor
);
IplImage* frame = cvQueryFrame( capture );
while( (frame = cvQueryFrame( capture )) != NULL ) {
cvWriteFrame(writer, frame);
cvShowImage("Webcam", frame);
char c = cvWaitKey( 33 );
if ( c == 27 ) break;
}
cvReleaseVideoWriter( &writer );
cvReleaseCapture( &capture );
return(0);
}
Have you tried passing the full path to cvCreateVideoWriter?
CvVideoWriter *writer = cvCreateVideoWriter(
"/home/user/out.avi",
CV_FOURCC('M','J','P','G'),
fps,
cvSize(frameW,frameH),
isColor
);
Related
Here is my code it display video but at high fps. I want original fps here but don't know how to do it. Watching some tutorials , they are using VideoCapture , I tried to use it but this is giving me linker error undefined reference to 'cv::VideoCapture::VideoCapture(std::string const&)'.. though I am linking all libraries but error is same. I am using Dev-C++ 5.11 (GCC 4.9.2) , so any idea how to use (CV_CAP_PROP_FPS)here -
#include <windows.h>
#include <opencv/cv.hpp>
#include <opencv/highgui.h>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
double fps=0;
cvNamedWindow( "Movie", CV_WINDOW_NORMAL );
CvCapture* capture = cvCreateFileCapture( "G:\\movie\\Journey.2.The.Mysterious.Island.2012.avi" );
IplImage* frame;
//cv::VideoCapture cap("G:\\movie\\Journey.2.The.Mysterious.Island.2012.avi" ); [giving me error]
//fps=cap.get(CV_CAP_PROP_FPS); [How to use this]
while(1)
{
frame = cvQueryFrame( capture );
if( !frame ) break;
cvShowImage( "Movie", frame );
char c = cvWaitKey(27);
if( c == 27 ) break; //esc
}
cvReleaseCapture( &capture );
cvDestroyWindow( "Movie" );
}
Thnx :)
double fps=cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
I installed a fresh Ubuntu. Downloaded Eclipse via the Shop, installed the CDT plugin via the Plugin Manager in Eclipse (Kepler). I used the Shop to download the OpenCV dev package. After adding the paths in eclipse I wrote a short program.
#include <iostream>
#include "opencv2/opencv.hpp"
int main(int argc, const char * argv[])
{
cvNamedWindow( "result", CV_WINDOW_AUTOSIZE );
CvCapture* capture = cvCaptureFromCAM(-1);
IplImage *newImg;
while(true)
{
newImg = cvQueryFrame( capture );
if( newImg==0 )
break;
cvShowImage( "result", newImg );
}
return 0;
}
The program compiles and the debugger shows some values in newImg. But there is no window coming up and shows the result. The camera LED lights, a step through the loop seem to work perfect. Only the output window is missing. The same program runs perfect in XCode on OS X.
Just add small wait between execution of subsequent loops. Use cv::waitKey for this purpose.
#include <iostream>
#include "opencv2/opencv.hpp"
int main(int argc, const char * argv[])
{
cvNamedWindow( "result", CV_WINDOW_AUTOSIZE );
CvCapture* capture = cvCaptureFromCAM(-1);
IplImage *newImg;
while(true)
{
newImg = cvQueryFrame( capture );
if( newImg==0 )
break;
cvShowImage( "result", newImg );
cv::waitKey(100); //Wait of 100 ms
}
return 0;
}
How to execute some shell script (e.g 1.sh) after opencv detect circle?
I have used exec, it works but opencv program close after circle detected, and what I want is that the program didn't close until I press "q" key.
Here is my code:
#include<cv.h>
#include<highgui.h>
#include <math.h>
#include <stdlib.h>
#include <unistd.h>
using namespace std;
int main( int argc, char **argv )
{
CvCapture *capture = 0;
IplImage *img = 0;
int key = 0;
CvFont font;
cvInitFont(&font, CV_FONT_HERSHEY_PLAIN,1.0,1.0,0,1,CV_AA);
capture = cvCaptureFromCAM( 0 );
if ( !capture ) {
fprintf( stderr, "Cannot open initialize webcam!\n" );
return 1;
}
cvNamedWindow( "result", CV_WINDOW_AUTOSIZE );
img = cvQueryFrame( capture );
if (!img)
exit(1);
IplImage* gray = cvCreateImage( cvGetSize(img), 8, 1 );
CvMemStorage* storage = cvCreateMemStorage(0);
while( key != 'q' ) {
img = cvQueryFrame( capture );
if( !img ) break;
cvCvtColor( img, gray, CV_BGR2GRAY );
cvSmooth( gray, gray, CV_GAUSSIAN, 5, 5 );
CvSeq* circles = cvHoughCircles( gray, storage, CV_HOUGH_GRADIENT, 2, >gray->height/40, 200, 100/*, 20, 100*/ );
int i;
for( i = 0; i < circles->total; i++ )
{
float* p = (float*)cvGetSeqElem( circles, i );
cvCircle( img, cvPoint(cvRound(p[0]),cvRound(p[1])), cvRound(p[2]), >CV_RGB(50,255,30), 5, 8, 0 );
cvPutText(img, "CIRCLE",cvPoint(cvRound(p[0]+45),cvRound(p[1]+45)), &font, >CV_RGB(50,10,255));
if ( circles ) {
execl("./1.sh", (char *)0);
}
}
cvShowImage( "result", img );
cvShowImage("gray", gray);
key = cvWaitKey( 1 );
}
// cvReleaseMemStorage(storage);
// cvReleaseImage(gray);
cvDestroyAllWindows();
cvDestroyWindow( "result" );
cvReleaseCapture( &capture );
return 0;
}
I used codeblocks on ubuntu.
After exec*, none of the code (in that process) will be reached. You can fork, exec if you want the program to continue without waiting for the script to complete, otherwise add a wait. Alternatively, you could use system or popen.
Examples:
example function to fork a command and wait:
#include <unistd.h>
/*as a macro*/
#define FORK_EXEC_WAIT(a) ({int s,p;if((p=fork())==0) \
{execvp(a[0],a);}else{while(wait(&s)!= p);}})
/*as a function*/
void fork_exec_wait(char** a) {
int s,p;
if((p=fork())==0){
execvp(a[0],a);
}else{
while(wait(&s)!= p);
}
}
to fork a command and continue
#include <unistd.h>
/*as a macro*/
#define FORK_EXEC(a) ({if((fork())==0) execvp(a[0],a);})
/*as a function*/
void fork_exec(char** a) {
int s,p;
if((p=fork())==0)
execvp(a[0],a);
}
the system command is ~ fork-exec-wait of "sh -c command args"
#include <stdlib.h>
system("command args");
the popen command is similar without the sh -c and will give you the output as a stream (think pipes, fifo, etc)
#include <stdio.h>
FILE *fp;
fp = popen("command args", "r");
...
pclose(fp);
I am using OpenCV 2.1 and Visual Studio 2008 in Windows. I am trying to grab the frames from CCD camera and want to display on Windows. Camera is with PAL format. Camera is detecting but showing the blank grey screen.
I found many post related to blank screen but no one is work in my case. So post I post this question.
Below is my code:
#include "stdafx.h"
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
#include <iostream>
int main(int argc, char* argv[])
{
cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE );
CvCapture* capture = cvCaptureFromCAM(CV_CAP_DSHOW);
if ( !capture ) {
fprintf( stderr, "ERROR: capture is NULL \n" );
getchar();
return -1;
}
while ( 1 ) {
IplImage* frame = cvQueryFrame( capture );
if ( !frame ) {
fprintf( stderr, "ERROR: frame is null...\n" );
getchar();
break;
}
else
{
fprintf( stderr, "Size of camera frame %d X %d\n",frame->width,frame->height );
}
cvShowImage( "mywindow", frame );
if ( (cvWaitKey(10) & 255) == 27 ) break;
}
// Release the capture device housekeeping
cvReleaseCapture( &capture );
cvDestroyWindow("mywindow");
return 0;
}
Above code return frame size 320 X 240 but blank screen.
Code is working fine for usb webcam with code CvCapture* capture = cvCaptureFromCAM(1);
I am using Avermedia Gold Camera Card on my board. So Do I need SDK to use this camera or is there any option to use CCD camera??
Driver is installed correctly and check with EzCaptureVC application.
OpenCV needs to support your camera else there's no guarantee its going to work: check the compatibility list.
Also 2.1 it's very outdated. I suggest you try again with the 2.3.1 since there has been some improvements in this area.
I've picked up 'Learning OpenCV' and have been trying some of the code examples/exercises. In this code snippet, I want to get the slider to update its position with each video frame change, but for some reason it won't work (the picture freezes with the following code):
#include "cv.h"
#include "highgui.h"
int g_slider_position = 0;
CvCapture* g_capture = NULL;
void onTrackbarSlide(int pos)
{
cvSetCaptureProperty(g_capture, CV_CAP_PROP_POS_FRAMES, pos);
}
int main(int argc, char** argv)
{
cvNamedWindow("The Tom 'n Jerry Show", CV_WINDOW_AUTOSIZE);
g_capture = cvCreateFileCapture(argv[1]);
int frames = (int) cvGetCaptureProperty(
g_capture,
CV_CAP_PROP_FRAME_COUNT
);
if (frames != 0)
{
cvCreateTrackbar(
"Position",
"The Tom 'n Jerry Show",
&g_slider_position,
frames,
onTrackbarSlide
);
}
IplImage* frame;
while (1)
{
frame = cvQueryFrame(g_capture);
if (!frame)
break;
cvSetTrackbarPos(
"Position",
"The Tom 'n Jerry Show",
++g_slider_position
);
cvShowImage("The Tom 'n Jerry Show", frame);
char c = cvWaitKey(33);
if (c == 27)
break;
}
cvReleaseCapture(&g_capture);
cvDestroyWindow("The Tom 'n Jerry Show");
return 0;
}
Any idea how to get the slider and video to work as intended?
This is the actual working code
// PROGRAM TO ADD A UPDATING TRACKBAR TO A VIDEO
#include <cv.h>
#include <highgui.h>
int g_slider_position = 0;
CvCapture* video_capture = NULL;
void onTrackbarSlide(int current_frame)
{
current_frame = g_slider_position;
cvSetCaptureProperty(video_capture,CV_CAP_PROP_POS_FRAMES,current_frame);
}
int main( int argc, char** argv )
{
cvNamedWindow( "Video", CV_WINDOW_AUTOSIZE );
video_capture = cvCreateFileCapture( "Crowdy.avi");
int no_of_frames = (int) cvGetCaptureProperty(video_capture,CV_CAP_PROP_FRAME_COUNT);
if( no_of_frames!= 0 )
{
cvCreateTrackbar("Slider","Video",&g_slider_position,no_of_frames,onTrackbarSlide);
}
IplImage* frame;
while(1)
{
frame = cvQueryFrame( video_capture );
if( !frame ) break;
cvShowImage( "Video", frame );
cvSetTrackbarPos("Slider","Video",++g_slider_position);
char c = cvWaitKey(33);
if( c == 27 ) break;
}
cvReleaseCapture( &video_capture );
cvDestroyWindow( "Video" );
return(0);
}
You are incrementing g_slider_position twice in the code, so it will increment beyond its limit (set in cvCreateTrackbar as frames). This is likely causing your picture to freeze.
To fix, change this
g_slider_position++;
cvSetTrackbarPos(
"Position",
"The Tom 'n Jerry Show",
++g_slider_position
);
to
cvSetTrackbarPos(
"Position",
"The Tom 'n Jerry Show",
++g_slider_position
);
Accounting for the edited code, I would check that OpenCV is properly reading the number of frames from your file. Look at Learning OpenCV's Chapter 2, example 2.3 for a method of generically retrieving the number of frames from your AVI (if that is what you are using).
In your code above, if the number of frames is 0, the trackbar is not created but the code still enters a loop that attempts to update the trackbar position (if it finds a frame). I would use this instead:
if (frames != 0)
{
cvCreateTrackbar(
"Position",
"The Tom 'n Jerry Show",
&g_slider_position,
frames,
onTrackbarSlide
);
}
else
{
exit(1);
}
This seems a bit complicated to me. I used the cvGetCaptureProperty(g_capture, CV_CAP_PROP_POS_FRAMES) call to retrieve the current frame and used this to update the slider.
The callback function is then used just to change the position within g_capture.
So the call back is:
//Call back for slider bar
void onTrackbarSlide(int pos) {
cvSetCaptureProperty(g_capture, CV_CAP_PROP_POS_FRAMES, pos);
}
And the loop is:
IplImage* frame; //Frame grabbed from video
while(1) {
frame = cvQueryFrame( g_capture );
if (!frame ) break;
cvShowImage( "Example2", frame );
g_frame_count = (int) cvGetCaptureProperty(g_capture, CV_CAP_PROP_POS_FRAMES);
cvSetTrackbarPos("Position","Example2", g_frame_count);
char c = cvWaitKey(33);
if ( c == 27 ) break;
}
Where the g_ variables are global.
You can try the solution below.
change this
void onTrackbarSlide(int pos)
{
cvSetCaptureProperty(g_capture, CV_CAP_PROP_POS_FRAMES, pos);
}
to
void onTrackbarSlide( int pos )
{
if( pos > g_slider_position + 1 )
cvSetCaptureProperty(
g_capture,
CV_CAP_PROP_POS_FRAMES,
pos);
}
and also change this
cvSetTrackbarPos(
"Position",
"The Tom 'n Jerry Show",
++g_slider_position
);
to
cvSetTrackbarPos(
"Position",
"The Tom 'n Jerry Show",
g_slider_position + 1
);
Hi I have simlar code and I did the following:
void onTrackbarSlide(int pos)
{
if(pos > g_lastPosition+1 || pos < g_lastPosition)
cvSetCaptureProperty(g_capture,CV_CAP_PROP_POS_FRAMES,pos);
g_lastPosition = pos;
}
.............
while(1)
{
frame = cvQueryFrame( g_capture );
if( !frame ) break;
cvShowImage( "Example3", frame );
cvSetTrackbarPos("Position", "Example3", g_slider_position+1);
char c = cvWaitKey(33);
if( c == 27 ) break;
}
So you can grab the slide bar to any direction , I hope this can help
OK I finally solved this problem of updating the slider
and also if you want to move the slider the video will update
there is no problem of picture freezing now
#include "stdafx.h"
#include<cv.h>
#include<cxcore.h>
#include<highgui.h>
int g_slider_position = 0;
CvCapture* g_capture = NULL;
int count=0; //initiate a global counter
void onTrackbarSlide( int pos )
{// if you are moving the slider for more than two frames then this loop will initiate to
// to update the video
if(pos>count+2 || pos<count-2){
cvSetCaptureProperty(
g_capture,
CV_CAP_PROP_POS_FRAMES,
pos);}
count=pos;
}
int main(int argc, _TCHAR* argv[])
{
//int count=0;
cvNamedWindow("Example3",CV_WINDOW_AUTOSIZE);
g_capture=cvCreateFileCapture("video.avi");
int frames = (int) cvGetCaptureProperty(
g_capture,
CV_CAP_PROP_FRAME_COUNT
);
if(frames!= 0) {
cvCreateTrackbar(
"Position",
"Example3",
&g_slider_position,
frames,
onTrackbarSlide
);
}
IplImage* frame;
while (1)
{
count++; // the counter will move along with the frame
frame = cvQueryFrame( g_capture );
if (!frame) break;
cvShowImage ("Example3", frame);
cvSetTrackbarPos("Position", "Example3", g_slider_position+1);
char c = cvWaitKey(33);
if(c==27) break;
}
cvReleaseCapture(&g_capture);
cvDestroyWindow("Example3");
return 0;
}
ok now what i have done is that i have created a global counter which will be updated alongside with the frames
now when we use the slider with the mouse to a different position then in onTrackbarSlider routine the if loop will be initiated and it will set the video to the new position