OpenCV cvNamedWindow not appearing under Fedora - c++

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

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 in Ubuntu does not show window

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

imshow window freeze in opencv

I'm doing opencv with leap motion. when I try to create a window with black screen but the window freezes and not respond. anyone have this problem? this is my code.
Mat PaintShow( 480,640,CV_8UC3);
int main(int argc, char* argv[])
{
rectangle( PaintShow,Point( 0, 0),Point( 2000, 2000),Scalar( 0, 0, 0 ),-1,8 );
// Create a sample listener and controller
SampleListener listener;
Leap::Controller controller;
.
.
.
}
the following is the leap motion method which update every few ms. so I put imshow here
void SampleListener::onFrame(const Leap::Controller& controller)
{
const Leap::Frame frame = controller.frame();
imshow("PaintShow",PaintShow);
.
.
.
}
you need to call waitKey(some_millis); somewhere after imshow(), else your window won't get updated.

opencv qt display Video

This code to display a video using opencv with Visual studio
i have been looking everywhere for a tutorial how to use Qt with opencv to display video
but i couldn't find any :/
is there anyone here knows how to do that?
#include <opencv\highgui.h>
#include <opencv\cv.h>
int main(int argc, char** argv)
{
CvCapture* capture1 = cvCreateFileCapture("c:\\VideoSamples\\song.avi");
IplImage* frame1;
cvNamedWindow( "display video1", CV_WINDOW_AUTOSIZE );
while(1)
{
frame1 = cvQueryFrame( capture1 );
cvSmooth( frame1, out, CV_GAUSSIAN, 17, 17 );
if( !frame1 ) break;
cvShowImage( "display video1", frame1 );
char c = cvWaitKey(33);
if( c == 27 ) break;
}
cvReleaseCapture( &capture1 );
cvDestroyWindow( "display video1" );
}
You can easily display a cv::Mat in a QLabel:
Assuming frame is your current RGB-videoframe with 8bit depth as cv::Mat-object and label is a pointer to your QLabel:
//convert to QPixmap:
QPixmap pixmap = QPixmap::fromImage(QImage((uchar*)frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888));
//set scaled pixmap as content:
label->setPixmap(pixmap.scaled(frame.cols, frame.rows, Qt::KeepAspectRatio));
For starters, you've got to make sure that the OpenCV libraries you are using have been built with Qt support.
You will probably need to download the source code (available on Github), configure the build using CMake, and re-build them yourself. Here is the link to the guide on how to build the OpenCV libraries from source.
Once that is done, this is an example of how to capture frames from a camera (just swap camera with file for your case) and display the frames to a window, making use of the Qt framework.
Hope this helps you.

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