Results 1 to 6 of 6

Thread: OpenCV causes access violation only during timerEvent

  1. #1
    Join Date
    Jan 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default OpenCV causes access violation only during timerEvent

    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
    Qt Code:
    1. TARGET = QtOpenCV
    2. TEMPLATE = app
    3.  
    4. INCLUDEPATH += C:\OpenCV\include\opencv
    5. LIBS += -L"C:\OpenCV\lib" -lcv200 -lhighgui200 -lcxcore200
    6. SOURCES += main.cpp CvDebug.cpp
    7. HEADERS += CvDebug.h
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "CvDebug.h"
    3.  
    4. int main(int argc, char **argv)
    5. {
    6. QApplication a(argc, argv);
    7. CvDebug w;
    8. w.show();
    9. return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    CvDebug.h
    Qt Code:
    1. #ifndef CVDEBUG_H
    2. #define CVDEBUG_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include <cv.h>
    6. #include <highgui.h>
    7.  
    8. class CvDebug : public QMainWindow
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. CvDebug(QWidget *parent = 0);
    14. ~CvDebug();
    15. CvCapture *camera;
    16.  
    17. protected:
    18. void timerEvent(QTimerEvent*);
    19. };
    20.  
    21. #endif // CVDEBUG_H
    To copy to clipboard, switch view to plain text mode 

    CvDebug.cpp
    Qt Code:
    1. #include "CvDebug.h"
    2. #include <cv.h>
    3. #include <highgui.h>
    4. #include <cxcore.h>
    5.  
    6. CvDebug::CvDebug(QWidget *parent) : QMainWindow(parent)
    7. {
    8. cvNamedWindow( "Color", CV_WINDOW_AUTOSIZE );
    9. cvNamedWindow( "Gray", CV_WINDOW_AUTOSIZE );
    10. camera = cvCreateCameraCapture(0);
    11.  
    12. startTimer(100); // msec
    13. }
    14.  
    15. CvDebug::~CvDebug()
    16. {
    17. cvDestroyWindow( "Color" );
    18. cvDestroyWindow( "Gray" );
    19. cvReleaseCapture(&camera);
    20. }
    21.  
    22. void CvDebug::timerEvent(QTimerEvent*)
    23. {
    24. IplImage* image = cvQueryFrame( camera );
    25. cvShowImage( "Color", image);
    26. IplImage* grayImage = cvCreateImage( cvGetSize(image), IPL_DEPTH_8U, 1);
    27. cvCvtColor( image, grayImage, CV_BGR2GRAY );
    28. cvShowImage( "Gray", grayImage );
    29. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: OpenCV causes access violation only during timerEvent

    You're going to have to choose if you want to use the Qt Gui system or the one provided in highgui(cvNamedWindow cvShowImage). Both run a message pump and can't be used simultaneously. If you want to stick to Qt, you should convert the IplImage to a QImage and display it on a QLabel or a QGraphicsView or similar.

    If you search the forum, you will find many posts on this exact topic.

  3. #3
    Join Date
    Jan 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: OpenCV causes access violation only during timerEvent

    Thanks for the reply spud.

    I actually started this project by searching these forums and used ideas from here. I have a working project where opencv does quite a number of image processing functions and displays them using a pixmap on a label. I noticed the access violation when I started adjusting parameters in cvCanny (note that with default parameters cvCanny does *not* crash). The example code in the above post was simply to get the code to a minimal size so others could run and see the same error. If you comment out line 27 of CvDebug.cpp, then that code runs fine. Put line 27 back in (cvCvtColor) and it crashes.

    As far as I understand, the only event manager in opencv is the highgui function cvWaitKey - there is no other way to start/receive events. That being said, I'll try to strip all highgui functions from my program and see if I can still crash it with a call to cvCvtColor in the timerEvent.

  4. #4
    Join Date
    Jan 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: OpenCV causes access violation only during timerEvent

    I modified the proof-of-concept code to remove any of OpenCV's highgui functions. Even after the change, I am experiencing crashes when cvCvtColor is called in the timerEvent. I also tried eliminating the timer event and using a single shot timer, but I am having the same results: without a call to cvCvtColor, the program operates correctly, as soon as cvCvtColor is called when triggered by a timer, an access violation occurs. Really baffled by all of this.

    Here is the simple code w/o any highgui functions:

    CvDebug.pro
    Qt Code:
    1. INCLUDEPATH += C:\OpenCV\include\opencv
    2. LIBS += -L"C:\OpenCV\lib" -lcv200 -lcxcore200
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <qtimer.h>
    3. #include "CvDebug.h"
    4.  
    5. int main(int argc, char **argv)
    6. {
    7. QApplication a(argc, argv);
    8. CvDebug w;
    9. QTimer::singleShot( 1000, &w, SLOT(ConvertColor()) );
    10. w.show();
    11. return a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    CvDebug.h
    Qt Code:
    1. #ifndef CVDEBUG_H
    2. #define CVDEBUG_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include <QImage>
    6. #include <cv.h>
    7.  
    8. class CvDebug : public QMainWindow
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. CvDebug(QWidget *parent = 0);
    14. ~CvDebug();
    15. QImage* qimage;
    16.  
    17. IplImage* ConvertToIplImage();
    18.  
    19. public slots:
    20. void ConvertColor();
    21. };
    22.  
    23. #endif // CVDEBUG_H
    To copy to clipboard, switch view to plain text mode 

    CvDebug.cpp
    Qt Code:
    1. #include "CvDebug.h"
    2. #include <cv.h>
    3. #include <cxcore.h>
    4.  
    5. CvDebug::CvDebug(QWidget *parent) : QMainWindow(parent)
    6. {
    7. qimage = new QImage("test_pic.jpg");
    8. }
    9.  
    10. CvDebug::~CvDebug()
    11. {
    12. delete qimage;
    13. }
    14.  
    15. void CvDebug::ConvertColor()
    16. {
    17. IplImage* image = ConvertToIplImage();
    18. IplImage* grayImage = cvCreateImage( cvGetSize(image), IPL_DEPTH_8U, 1);
    19. cvCvtColor( image, grayImage, CV_BGR2GRAY );
    20.  
    21. cvReleaseImage( &image );
    22. cvReleaseImage( &grayImage );
    23. }
    24.  
    25. IplImage* CvDebug::ConvertToIplImage()
    26. {
    27. IplImage* image = cvCreateImage( cvSize(qimage->width(), qimage->height()), IPL_DEPTH_8U, 3 );
    28.  
    29. int cvIndex = 0, cvLineStart = 0;
    30. for(int y=0; y<qimage->height(); y++)
    31. {
    32. cvIndex = cvLineStart;
    33. for(int x=0; x<qimage->width(); x++)
    34. {
    35. image->imageData[cvIndex+0] = qBlue(qimage->pixel(x,y));
    36. image->imageData[cvIndex+1] = qGreen(qimage->pixel(x,y));
    37. image->imageData[cvIndex+2] = qRed(qimage->pixel(x,y));
    38. cvIndex += 3;
    39. }
    40. cvLineStart += image->widthStep;
    41. }
    42. return image;
    43. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: OpenCV causes access violation only during timerEvent

    Changed compilers to Visual Studio and the program is running fine - I cannot duplicate the crashing behavior. Guess I'll stick with Visual Studio for now then.

  6. #6
    Join Date
    Feb 2010
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: OpenCV causes access violation only during timerEvent

    Hi, I'm having a similar problem.

    Using: Qt4.6 OpenCV2.0 MinGW

    If I call OpenCV functions (e.g. cvThreshold) inside Qt slots, the application crashes.

    If I call the same functions anywhere else (main or constructors), it works.
    If I call the functions using QtConcurrent::run() inside the slot, it works.
    If I use OpenCV1.0, it works.

    Any ideas about what may be causing this conflict? (...other than having to switch compilers )

    Cheers

Similar Threads

  1. Replies: 5
    Last Post: 17th June 2009, 14:01
  2. Access violation when reading big text files
    By robertson1 in forum General Programming
    Replies: 0
    Last Post: 18th September 2008, 06:59
  3. timerEvent is not raising
    By navi1084 in forum Qt Programming
    Replies: 2
    Last Post: 2nd September 2008, 22:07
  4. Access Violation on Signal Emit
    By khagzan in forum Qt Programming
    Replies: 2
    Last Post: 25th September 2007, 22:51
  5. timerEvent for each QGraphicsItem
    By tts80 in forum Qt Programming
    Replies: 1
    Last Post: 18th January 2007, 09:33

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.