Ask Your Question

Why does findcontours crash the program in Qt?

asked 2017-02-18 08:51:41 -0500

updated 2017-02-18 13:35:22 -0500

I wrote a program that finds contours-rectangles- in a video stream from web-cam. I tried it with visual studio 2015 and worked well. But, when i try it on Qt - to design a GUI- it crashes at findcontours with no error explanation, only gives me

The program has unexpectedly finished. C:\Users\Amr\Documents\build-untitled10-Desktop_Qt_5_8_0_MSVC2015_64bit-Release\release\untitled10.exe crashed.

i have searched a lot,but unfortunately didn't find any thing helpful.

project.h file

#include <iostream>
#include <QMainWindow>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <QDebug>
using namespace std;
using namespace cv;

namespace Ui {
class MainWindow;

class MainWindow : public QMainWindow

    explicit MainWindow(QWidget *parent = 0);
    Ui::MainWindow *ui;
    cv::VideoCapture vid;
    cv::Mat original;
    cv::Mat gray;
    cv::Mat dst ;

    QImage qoriginal;
    QImage qgray;
    QImage qdst;

    QTimer *timer;

    vector<Vec4i> hierarchy;

public slots:
    void camerashow();

private slots:
    void on_pushButton_clicked();


main window

MainWindow::MainWindow(QWidget *parent) :
    ui(new Ui::MainWindow)
    if (!vid.isOpened()){qDebug()<<"error";}
    timer = new QTimer(this);
    connect (timer,SIGNAL(timeout()),this,SLOT(camerashow()));


and the camerashow function; // read camera feed

    cvtColor(original,gray,CV_BGR2GRAY); // convert to a format that qt understand

    blur(gray, gray, Size(3, 3));
    Canny(gray, gray, 120, 255,3);

    findContours(gray, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);

        for (int i = 0; i < contours.size(); i++)

       drawContours(original, contours, i, Scalar(0,0,255), 2, 8, hierarchy);             
   QImage qoriginal((uchar*),original.cols,original.rows,original.step,QImage::Format_RGB888);
   QImage qgray((uchar*),gray.cols,gray.rows,gray.step,QImage::Format_Grayscale8);

        // equivilant to imshow
    ui->label ->setPixmap(QPixmap::fromImage(qoriginal));
    ui->label_2 ->setPixmap(QPixmap::fromImage(qgray));


when I comment the for loop, it also crashes. when commenting findcontour, it works and shows original and canny outputs.

i'm using OpenCV 2.4.9 visual studio 2015 Qt creator 5.8.0 and working in release mode

Edit: when i run it in debug mode, it works with no error but gives me

Debug assertion failed "(_ptr_user&(_big_allocation_alignment -1)==0" &&0

edit retag flag offensive close merge delete


I don't know QT at all when you create QImage is it same memory that gray or not? If it same try to clone gray :

Mat forQImage=gray.clone();
QImage qgray((uchar*),forQImage.cols,forQImage.rows,forQImage.step,QImage::Format_Grayscale8);

same remark for original

may be you update opencv to last version (2.4.12 or 3.2)

LBerger gravatar imageLBerger ( 2017-02-18 14:18:07 -0500 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2017-05-15 15:52:54 -0500

findContours is doing something rather odd casting your std::vector<std::vector<cv::point> > contours to std::vector<std::vector<uchar> >& and then calling resize(). This does not work on Windows if you compile with Debug and run Release bits or vice versa. But if you compile release opencv and release app bits then it works ok. I find this casting rather bad, they should fix findContours so it doesn't do that.

edit flag offensive delete link more

answered 2017-02-20 04:43:23 -0500

pi-null-mezon gravatar image

There is several stanges/issues/bugs in your code:

  1. Why when you create QImage you pass 4-th argument as Mat.step()? To create QImage you should provide the number of bytes per line. So the calls should be:

    QImage qoriginal((const uchar*),original.cols,original.rows,original.cols*3,QImage::Format_RGB888);
    QImage qgray((const uchar*),gray.cols,gray.rows,gray.cols,QImage::Format_Grayscale8);
  2. Qimage could be created from the raw data only if raw data is continuous, so you need to check:

     if(original.isContinuous() && gray.isContinuous()) {
         // create QImages
  3. Update your opencv version to the last one

  4. Use QPainter to draw QImage on the QWidget instead of QLabel with QPixmap, research of how to here

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-02-18 08:51:41 -0500

Seen: 477 times

Last updated: Feb 20 '17