Saicharan Kumar
Saicharan Kumar

Reputation: 11

Program in Qt Creator using opencv crashes unexpectedly

I am a beginner in both Qt Programming and Opencv and I am just trying to run a basic program of showing an image in a window.

I went through a lot of tutorials online and I was finally able to install Opencv and link the libraries with Qt without any error. But when I run the program, it just gets unexpectedly finished without showing any error.

Here are my code snippets:

My Pro file:

    QT       += core gui

    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

    TARGET = opencv2
    TEMPLATE = app


    INCLUDEPATH += C:/opencv/opencv/build/include
    LIBS += -LC:/opencv/opencv/build/x86/vc11/lib

    LIBS +=  -lopencv_core2413d \
     -lopencv_highgui2413d \
     -lopencv_imgproc2413d \
     -lopencv_features2d2413d \
     -lopencv_contrib2413d \
     -lopencv_flann2413d \
     -lopencv_gpu2413d \
     -lopencv_legacy2413d \
     -lopencv_ml2413d \
     -lopencv_nonfree2413d \
     -lopencv_objdetect2413d \
     -lopencv_photo2413d \
     -lopencv_stitching2413d \
     -lopencv_superres2413d \
     -lopencv_ts2413d \
     -lopencv_video2413d \
     -lopencv_videostab2413d \
     -lopencv_calib3d2413d \


    SOURCES += main.cpp\
            mainwindow.cpp

    HEADERS  += mainwindow.h

    FORMS    += mainwindow.ui

My mainwindow.h:

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>

   namespace Ui {
   class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
        Q_OBJECT

    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
        QImage imdisplay;  
        QTimer* Timer;   


    public slots:    
    void DisplayImage();

    private:
        Ui::MainWindow *ui;
    };

    #endif // MAINWINDOW_H

My main.cpp:

    #include "mainwindow.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();

        return a.exec();
    }

My mainwindow.cpp:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QTimer>     
    #include "opencv2/imgproc.hpp"
    #include "opencv2/highgui.hpp"
    #include "opencv2/core.hpp"

    using namespace cv;

    MainWindow::MainWindow(QWidget *parent) 
       QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        Timer = new QTimer(this);
        connect(Timer, SIGNAL(timeout()), this, SLOT(DisplayImage()));
        Timer->start(); //Will start the timer
    }

    MainWindow::~MainWindow()
    {
        delete ui;
    }

    void MainWindow::DisplayImage()
    {
        Mat img;
        img = imread("D:\\Kumar\\amazon.jpeg");
        cv::resize(img, img, Size(512, 384), 0, 0, INTER_LINEAR);
        cv::cvtColor(img,img,CV_BGR2RGB);
        QImage imdisplay((uchar*)img.data, img.cols, img.rows, img.step, QImage::Format_RGB888);
        ui->display_image->setPixmap(QPixmap::fromImage(imdisplay));
    }

And the screenshot from depends:

This lists the dlls that are missing

And the locations I have added in my Path environment variable are:

C:\opencv\opencv\build\x86\vc11\bin;
C:\Program Files (x86)\Qt\Tools\mingw492_32\bin\;
C:\opencv\opencv\build\bin\;

I have been trying to figure this out for the past two days, but I am totally stuck. If someone went through the same problem or if someone knows the solution, it would be really nice of you, if you could share it with me!

EDIT: Okay, I rebuilt opencv with a vc10 toolchain like mvidelgauz suggested, and I still get the same problem, although this time, the missing dlls are different.

This is the new screenshot

Any other suggestions?

Upvotes: 1

Views: 1347

Answers (2)

maidamai
maidamai

Reputation: 712

I got the same problem, add path of dynamic library path to PATH solved my problem. In your case, you may need add the following path to PATH environment variable.

C:/opencv/opencv/build/x86/vc11/bin

Upvotes: 1

mvidelgauz
mvidelgauz

Reputation: 2214

As it is seen in your screenshot you are building your project with VS2010 compiler (vc10) but your opencv libraries are built with vc11, which makes C++ run-time incompatible. You either need to build your project with vc11 (VS2012) or get opencv built with vc10 toolchain

And BTW you don't need C:\Program Files (x86)\Qt\Tools\mingw492_32\bin\ in your path at all if you use Visual Studio compiler

Upvotes: 4

Related Questions