OpenCV in Ubuntu does not show window - c++

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

Related

How to calculate Frame Per Second in opencv?

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

opencv - cvCaptureFromCAM(CV_CAP_DSHOW) capture blank frame

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.

OpenCv Visual C++ 2010 Express problem

Hi I am trying to run the code below but having problems with the cvCreateFileCapture function.
#include "stdafx.h"
#include <iostream>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
using namespace std;
int main( int argc, char** argv ) {
cvNamedWindow( "Example2", CV_WINDOW_AUTOSIZE );
// CvCapture* capture = cvCaptureFromAVI( argv[1] ); // either one will work
CvCapture* capture = cvCreateFileCapture( "test.avi");
IplImage* frame;
while(1) {
frame = cvQueryFrame( capture );
if( !frame ) break;
cvShowImage( "Example2", frame );
char c = cvWaitKey(33);
if( c == 27 ) break;
}
cvReleaseCapture( &capture );
cvDestroyWindow( "Example2" );
}
Initially I was getting an error about a missing msvcr90d.dll file. I had to download vs 2008 and change the platform toolset configuration settings. After I did this I got the error below. Any help would be much appreciated.
I bet cvCreateFileCapture() is failing because it didn't found the file. You just don't know because you are not checking the return of the function.
It returns NULL if it can't load the video file.
CvCapture* capture = cvCreateFileCapture("test.avi");
if (capture == NULL)
{
std::cout << "!!! cvCreateFileCapture failed !!!" << std::endl;
exit(0);
}
I have no idea what openCv is, but if memory serves me, msvcr90d.dll is the debug dll for VC++2008.
Have you checked your project's configuration and properties to see what mode and what dlls it is using/importing ?

trouble with output directory for OpenCV VideoWriters

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

OpenCV cvNamedWindow not appearing under Fedora

As the title suggests I'm simply trying to get a named window to come up. I've been working with OpenCV for over a year now, and never had this problem before. For some reason, the window never opens. I've tried running some of my old scripts and everything works fine.
As a very cut down example, see below
#include "cv.h"
#include "highgui.h"
int main(int argc, char** argv) {
cvNamedWindow( "video", 0 );
IplImage *im = cvCreateImage( cvSize(200,200), 8, 3 );
while(1) {
cvShowImage( "video", im );
}
return 0;
}
I can see no reason why that wouldn't work, but for some reason the window never appears.
Has anyone else experienced this? It's doing my head in!
Simply call cvWaitKey(int milliseconds) within the loop. This function notifies the GUI system to run graphics pending events.
Your code should be something like:
int main(int argc, char** argv) {
cvNamedWindow( "video", 0 );
IplImage *im = cvCreateImage( cvSize(200,200), 8, 3 );
while(1) {
cvShowImage( "video", im );
cvWaitKey(100); //wait for 100 ms for user to hit some key in the window
}
return 0;
}