derekkingston
21st January 2010, 03:30
I've been trying to integrate Qt and OpenCV and ran into a strange problem: some OpenCV functions cause access violations, but only when called during a timerEvent. I simplified the code as much as possible as a proof of concept (see below). The two functions that crash are cvCvtColor (when converting from BGR to Gray) and cvCanny when the sobel aperture has a size greater than 3. Both of these functions work when called anywhere else in the program (e.g. constructor or main). But when called from a timerEvent, an access violation crashes the program. Any ideas?
WinXP
Qt 4.6.1 (mingw)
OpenCV 2.0.0a
CvDebug.pro
TARGET = QtOpenCV
TEMPLATE = app
INCLUDEPATH += C:\OpenCV\include\opencv
LIBS += -L"C:\OpenCV\lib" -lcv200 -lhighgui200 -lcxcore200
SOURCES += main.cpp CvDebug.cpp
HEADERS += CvDebug.h
main.cpp
#include <QtGui/QApplication>
#include "CvDebug.h"
int main(int argc, char **argv)
{
QApplication a(argc, argv);
CvDebug w;
w.show();
return a.exec();
}
CvDebug.h
#ifndef CVDEBUG_H
#define CVDEBUG_H
#include <QtGui/QMainWindow>
#include <cv.h>
#include <highgui.h>
class CvDebug : public QMainWindow
{
Q_OBJECT
public:
CvDebug(QWidget *parent = 0);
~CvDebug();
CvCapture *camera;
protected:
void timerEvent(QTimerEvent*);
};
#endif // CVDEBUG_H
CvDebug.cpp
#include "CvDebug.h"
#include <cv.h>
#include <highgui.h>
#include <cxcore.h>
CvDebug::CvDebug(QWidget *parent) : QMainWindow(parent)
{
cvNamedWindow( "Color", CV_WINDOW_AUTOSIZE );
cvNamedWindow( "Gray", CV_WINDOW_AUTOSIZE );
camera = cvCreateCameraCapture(0);
startTimer(100); // msec
}
CvDebug::~CvDebug()
{
cvDestroyWindow( "Color" );
cvDestroyWindow( "Gray" );
cvReleaseCapture(&camera);
}
void CvDebug::timerEvent(QTimerEvent*)
{
IplImage* image = cvQueryFrame( camera );
cvShowImage( "Color", image);
IplImage* grayImage = cvCreateImage( cvGetSize(image), IPL_DEPTH_8U, 1);
cvCvtColor( image, grayImage, CV_BGR2GRAY );
cvShowImage( "Gray", grayImage );
}
WinXP
Qt 4.6.1 (mingw)
OpenCV 2.0.0a
CvDebug.pro
TARGET = QtOpenCV
TEMPLATE = app
INCLUDEPATH += C:\OpenCV\include\opencv
LIBS += -L"C:\OpenCV\lib" -lcv200 -lhighgui200 -lcxcore200
SOURCES += main.cpp CvDebug.cpp
HEADERS += CvDebug.h
main.cpp
#include <QtGui/QApplication>
#include "CvDebug.h"
int main(int argc, char **argv)
{
QApplication a(argc, argv);
CvDebug w;
w.show();
return a.exec();
}
CvDebug.h
#ifndef CVDEBUG_H
#define CVDEBUG_H
#include <QtGui/QMainWindow>
#include <cv.h>
#include <highgui.h>
class CvDebug : public QMainWindow
{
Q_OBJECT
public:
CvDebug(QWidget *parent = 0);
~CvDebug();
CvCapture *camera;
protected:
void timerEvent(QTimerEvent*);
};
#endif // CVDEBUG_H
CvDebug.cpp
#include "CvDebug.h"
#include <cv.h>
#include <highgui.h>
#include <cxcore.h>
CvDebug::CvDebug(QWidget *parent) : QMainWindow(parent)
{
cvNamedWindow( "Color", CV_WINDOW_AUTOSIZE );
cvNamedWindow( "Gray", CV_WINDOW_AUTOSIZE );
camera = cvCreateCameraCapture(0);
startTimer(100); // msec
}
CvDebug::~CvDebug()
{
cvDestroyWindow( "Color" );
cvDestroyWindow( "Gray" );
cvReleaseCapture(&camera);
}
void CvDebug::timerEvent(QTimerEvent*)
{
IplImage* image = cvQueryFrame( camera );
cvShowImage( "Color", image);
IplImage* grayImage = cvCreateImage( cvGetSize(image), IPL_DEPTH_8U, 1);
cvCvtColor( image, grayImage, CV_BGR2GRAY );
cvShowImage( "Gray", grayImage );
}