Why does findcontours crash the program in Qt?
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>
#include<QtCore>
using namespace std;
using namespace cv;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
cv::VideoCapture vid;
cv::Mat original;
cv::Mat gray;
cv::Mat dst ;
QImage qoriginal;
QImage qgray;
QImage qdst;
QTimer *timer;
vector<vector<Point>>contours;
vector<Vec4i> hierarchy;
public slots:
void camerashow();
private slots:
void on_pushButton_clicked();
};
main window
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
vid.open(0);
if (!vid.isOpened()){qDebug()<<"error";}
timer = new QTimer(this);
connect (timer,SIGNAL(timeout()),this,SLOT(camerashow()));
}
and the camerashow function
vid.read(original); // 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);
}
cv::cvtColor(original,original,CV_BGR2RGB);
QImage qoriginal((uchar*)original.data,original.cols,original.rows,original.step,QImage::Format_RGB888);
QImage qgray((uchar*)gray.data,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
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 :
same remark for original
may be you update opencv to last version (2.4.12 or 3.2)