1
votes

When I use imread function in basic qt application, the app crashes.

My pro file

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = lab1
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

INCLUDEPATH += /usr/local/include/opencv
INCLUDEPATH += /usr/local/include
INCLUDEPATH += /usr/local/include/opencv2
LIBS += -L/usr/local/lib
LIBS += -lopencv_core
LIBS += -lopencv_imgproc
LIBS += -lopencv_highgui
LIBS += -lopencv_ml
LIBS += -lopencv_video
LIBS += -lopencv_features2d
LIBS += -lopencv_calib3d
LIBS += -lopencv_objdetect
LIBS += -lopencv_contrib
LIBS += -lopencv_legacy
LIBS += -lopencv_flann
LIBS += -lopencv_nonfree
LIBS += `pkg-config opencv --libs`

Code

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

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

void MainWindow::on_actionSave_changed()
{
    cv::Mat img;
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),"",tr("Files (*.*)"));
img = cv::imread("image.jpg", CV_LOAD_IMAGE_UNCHANGED);

} 

Error

Starting /home/yaltug/Desktop/yavuzlab1/build-lab1-Desktop_Qt_5_2_1_GCC_64bit-Debug/lab1... * glibc detected * /home/yaltug/Desktop/yavuzlab1/build-lab1-Desktop_Qt_5_2_1_GCC_64bit-Debug/lab1: realloc(): invalid pointer: 0x00007f752cd1cd80 * ======= Backtrace: ========= /lib/x86_64-linux-gnu/libc.so.6(+0x7eb96)[0x7f752be10b96] /lib/x86_64-linux-gnu/libc.so.6(realloc+0x28e)[0x7f752be1589e] /home/yaltug/Qt5.2.1/5.2.1/gcc_64/lib/libQt5Core.so.5(_ZN9QListData7reallocEi+0x26)[0x7f752c758016] /home/yaltug/Qt5.2.1/5.2.1/gcc_64/lib/libQt5Core.so.5(_ZN9QListData6appendEi+0x65)[0x7f752c7580d5] /home/yaltug/Qt5.2.1/5.2.1/gcc_64/lib/libQt5Core.so.5(+0x8dba6)[0x7f752c6f5ba6] /home/yaltug/Qt5.2.1/5.2.1/gcc_64/lib/libQt5Core.so.5(+0x8bd17)[0x7f752c6f3d17] /home/yaltug/Qt5.2.1/5.2.1/gcc_64/lib/libQt5Core.so.5(+0x8c9c5)[0x7f752c6f49c5] /home/yaltug/Qt5.2.1/5.2.1/gcc_64/lib/libQt5Core.so.5(_Z20qMessageFormatString9QtMsgTypeRK18QMessageLogContextRK7QString+0x742)[0x7f752c6f5252] /home/yaltug/Qt5.2.1/5.2.1/gcc_64/lib/libQt5Core.so.5(+0x8d405)[0x7f752c6f5405] /home/yaltug/Qt5.2.1/5.2.1/gcc_64/lib/libQt5Core.so.5(+0x8a883)[0x7f752c6f2883] /home/yaltug/Qt5.2.1/5.2.1/gcc_64/lib/libQt5Core.so.5(+0x8a9da)[0x7f752c6f29da] /home/yaltug/Qt5.2.1/5.2.1/gcc_64/lib/libQt5Core.so.5(_ZNK14QMessageLogger5fatalEPKcz+0xa9)[0x7f752c6f3109] /home/yaltug/Qt5.2.1/5.2.1/gcc_64/lib/libQt5Core.so.5(_ZN9QMetaType22registerNormalizedTypeERK10QByteArrayPFvPvEPFS3_PKvES5_PFS3_S3_S7_Ei6QFlagsINS_8TypeFlagEEPK11QMetaObject+0x567)[0x7f752c8fec47] /home/yaltug/Qt5.2.1/5.2.1/gcc_64/lib/libQt5Core.so.5(_ZN9QMetaType12registerTypeEPKcPFvPvEPFS2_PKvES4_PFS2_S2_S6_Ei6QFlagsINS_8TypeFlagEEPK11QMetaObject+0x61)[0x7f752c8fed01]

7fffa4005000-7fffa4026000 rw-p 00000000 00:00 0                          [stack]
7fffa4104000-7fffa4106000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
The program has unexpectedly finished.
4

4 Answers

1
votes

I have met the same problem, I search it the whole morning and solve it by re-install the OpenCV. My opencv was the version 2.4.9, and build on the Ubuntu 12.04 system following a post , I installed the QT creator later using the Linux installer. The version of QT creator is 3.1.2 based on the QT 5.3.1. My OpenCV was installed on a customized path. I just deleted the build folder

cd opencv-2.4.9
rm -r build

and I just build the OpenCV again without the -D WITH_QT flag in cmake

mkdir build
cd build
cmake -D WITH_XINE=ON -D WITH_OPENGL=ON -D WITH_TBB=ON -D BUILD_EXAMPLES=ON ..
make -j4
sudo make install

the -j4 flag in the make means that make in 4 cores parallel. and the Magic happens, the code works! the reason why it does not work is that when using the cmake WITH_QT flag the system build the opencv using the system default version of the qt, which is not compatible with the newest version of the qt downloaded from the website. however, to make it work, we must have the right version of the QT

0
votes

in void MainWindow::on_actionSave_changed() you have not declared the img properly.

You have written only cv::Mat which should be cv::Mat img; because after two lines you are using img as img = cv::imread("image.jpg", CV_LOAD_IMAGE_UNCHANGED); where img has no datatype.

So, replace cv::Mat; with cv::Mat img;

0
votes

I encountered the exact same problem.

Here is how I solved it:

when using cmake, be sure to uncheck WITH_QT. The internal version(Qt4.0) used in Opencv might cause conflicts with QT Creator(Qt 5.0) That's how I solved my problem.

just clear the cmake cache, uncheck WITH_QT (and maybe WITH_TBB also), then make sure the previous opencv build is removed, then rebuild opencv. Boom, now it works!

0
votes

I had the same problem and it was because of missing .dll files so when I copy my .dll files to release folder (or debug folder) problem solved. I used opencv for vc15 files and copy opencv_world412d.dll and opencv_world412.dll and opencv_videoio_ffmpeg412_64.dll to release folder.