-1

I just installed Qt 5.15 and is testing how it works with opencv. I downloaded prebuild Opencv4.3, and set up a .pri file for being deployed in Qt.

The include and libs are as follows in the .pri file:

INCLUDEPATH += C:/opencv/opencv-4.3.0-prebuild/include

CONFIG(release, debug|release):{
LIBS += -LC:/opencv/opencv-4.3.0-prebuild/x64/vc14/lib \
        -lopencv_world430
}

CONFIG(debug, debug|release):{
LIBS += -LC:/opencv/opencv-4.3.0-prebuild/x64/vc14/lib \
        -lopencv_world430d
}

Then I run a simple image display domo:

#include "opencv2/opencv.hpp"
using namespace cv;

Mat img = imread("image.png");

if(img.empty())
{
    qDebug()<<"Could not find the image";
}
else
{
    namedWindow("Image");
    imshow("Image", img);
}

The resulting error message: The program has unexpectedly finished. The process was ended forcefully.

Without linking with OpenCV, Qt itself works just fine.

What causes the problem?

jwm
  • 4,832
  • 10
  • 46
  • 78
  • Can you specify where the code is located? Have you tried using a debugger (like gdb) to identify the line where the problem happens? – Doch88 Jun 24 '20 at 19:00

4 Answers4

1

It's not clear where is your example code is located. Is it in main? Generally in OpenCV you also have an event loop (as in Qt) but it is hidden. So if you want to actually see the namedWindow, you need to call cv::waitKey().

You can call it like cv::waitKey(1) if you do it periodically.

But generally you should do only the image processing in OpenCV, convert the cv::Mat to QImage, and show that in Qt.

tr3w
  • 343
  • 4
  • 8
0

I found this issue is due to an improper debugger set in the Qt Creator. Refer to the posts QtCreator no debugger set up (Windows). and Cannot run Qt example in Qt creator: The program has unexpectedly finished, and the official document https://doc.qt.io/qtcreator/creator-debugger-engines.html#supported-native-debugger-versions and https://doc.qt.io/qtcreator/creator-debugger-engines.html#debugging-tools-for-windows

jwm
  • 4,832
  • 10
  • 46
  • 78
0

For the crash,

My first guess is your environment variable is not set properly. Please check you assign the env. variables properly.

Writing imshow() in Qt won't work. As suggested by tr3w, you should convert to a qimage.

Mat img;

QImage img1 = QImage((uchar *) img.data,img.cols,img.rows,img.step,QImage::Format_Indexed8);

You can replace with your supported image format instead of indexed8.

vishnukumar
  • 365
  • 4
  • 17
0

I solved the problem as follows: I have added the paths to opencv's bin and lib files to PATH.

  • 2
    You have a unique style of answering. It makes it very easy to spot that you are posting the same answer identically to multiple questions. Please do not do that. – Yunnosch Oct 02 '20 at 12:01