Why after linking the OpenCV libraries, Mat objects are still 'undefined'?
-
Hi, I'm new to QT and I'm currently trying to create GUI for my C++ program that uses OpenCV to create an image collage from multiple images. The program compiles and runs as it should from a shell and I want to figure out how to change it to work on Qt as well. I defined the libraries and include path in the .pro file :
INCLUDEPATH += -I\usr\include\opencv4 LIBS += -L\usr\local\lib\libopencv_core.so \ \usr\local\lib\libopencv_highgui.so \ \usr\local\lib\libopencv_imgproc.so
and in the main.cpp:
#include "mainwindow.h" #include <QApplication> #include <QPushButton> #include<math.h> #include<stdlib.h> #include <stdio.h> #include <cmath> #include <opencv4/opencv2/core.hpp> #include <opencv4/opencv2/highgui.hpp> #include <opencv4/opencv2/imgproc.hpp> using namespace cv; #include<iostream> using namespace std;
When I try to run the code I get errors like <unknown type name 'Mat' > How can I fix this ?
This is a function from the code for example:struct RGBvalues{ long R; long G; long B; }; //splits the input image into tiles and stores them in a vector vector<Mat> split ( Mat & image, int M, int N ) { // All images should be the same size ... int width = image.cols / M; int height = image.rows / N; // ... except for the Mth column and the Nth row int width_last_column = width + ( image.cols % width ); int height_last_row = height + ( image.rows % height ); vector<Mat> result; for( int i = 0; i < N; ++i ) { for( int j = 0; j < M; ++j ) { // Compute the region to crop from Rect roi( width * j, height * i, ( j == ( M - 1 ) ) ? width_last_column : width, ( i == ( N - 1 ) ) ? height_last_row : height ); result.push_back( image( roi ) ); } } return result; }
-
Hi and welcome to devnet,
Because you are not linking to the OpenCV libraries. The "-L" option is to add search paths for the linker. You are looking for the "-l" as well. This is the one that instructs to link to a library.
-
Hi
Have you tried with
INCLUDEPATH += -I\usr\includeas the includes already have the opencv4 name.
#include <opencv4/opencv2/core.hpp>also if you look inside the include folder, does it have the opencv2 sub folder ?
ahh. captial L. i need glasses :)
-
Hi
Have you tried with
INCLUDEPATH += -I\usr\includeas the includes already have the opencv4 name.
#include <opencv4/opencv2/core.hpp>also if you look inside the include folder, does it have the opencv2 sub folder ?
ahh. captial L. i need glasses :)
-
@mrjj I tried to change it the way you suggested and using -l instead of -L but I still get those errors.
-
Your paths are invalid, use forward slashes for them.
-
@SGaist @mrjj I changed the paths to this
INCLUDEPATH += -I/usr/include
LIBS += -l/usr/local/lib/libopencv_core.so
LIBS+= -l/usr/local/lib/libopencv_highgui.so
LIBS+= -l/usr/local/lib/libopencv_imgproc.so
Should it work now?
and did the cleaning process and now I have more errors but all because the libraries still seem to be unrecognised. -
How did you install OpenCV ?
Did you check that it is from the correct architecture ? -
Call "file /usr/local/lib/libopencv_core.so". What do you get ?
As for your .pro file:
LIBS += -L/usr/local/lib \ -lopencv_core \ -lopencv_highgui \ -lopencv_imgproc
should be enough to link to these OpenCV libraries.
-
@SGaist It was a 'symbolic link' to libopencv_imgproc.so.4.5.0, so I called it again for that and got:
file /usr/local/lib/libopencv_imgproc.so.4.5.0
/usr/local/lib/libopencv_imgproc.so.4.5.0: ELF 64-bit LSB shared object, x86-64, version 1 (GNU/Linux), dynamically linked, BuildID[sha1]=a9e4463d8bcc7f5051819e1ab34a131be7685cb3, not stripped -
Can you show your current .pro file content ?
-
@SGaist yes :
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++11 # You can make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ core.cpp \ main.cpp \ mainwindow.cpp HEADERS += \ mainwindow.h FORMS += \ mainwindow.ui INCLUDEPATH += -I/usr/local/include LIBS += -L/usr/local/lib \ -lopencv_core \ -lopencv_highgui \ -lopencv_imgproc # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target
-
That looks good. What exact error are you getting now, after doing a full rebuild ?
-
That's not a linking problem you have there.
Where is opencv2/core.hpp located exactly in your system ?
-
Did you notice the "opencv4" folder that is missing from your INCLUDEPATH statement ?
Either add it there or to your include statements.
-
Please provide the information directly.
What exactly did you try ?
What is the error your get ?