I'm trying to use opencv in a QT project for a raspberry pi 4. I'm on a Linux machine
Here is my .pro file and errors I get. And my mainwindow.cpp
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++17
# 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 += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
TRANSLATIONS += \
SCWWS_fr_FR.ts
CONFIG += lrelease
CONFIG += embed_translations
# Default rules for deployment.
qnx: target.pHere is my mainwindow.cppath = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
RESOURCES += \
resources.qrc
unix:!macx: LIBS += -L$$PWD/../../Documents/Qt-CrossCompile-RaspberryPi/raspberrypi4/sysroot/usr/lib/arm-linux-gnueabihf -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_imgcodecs -lopencv_shape -lopencv_videoio
INCLUDEPATH += $$PWD/../../Documents/Qt-CrossCompile-RaspberryPi/raspberrypi4/sysroot/usr/include
DEPENDPATH += $$PWD/../../Documents/Qt-CrossCompile-RaspberryPi/raspberrypi4/sysroot/usr/include
Here is my mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtCore>
#include <QFontDatabase>
#include <QDebug>
#include <QImage>
//#include "opencv2/core.hpp"
//#include "opencv2/highgui.hpp"
//using namespace cv;
mainwindow::mainwindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::mainwindow)
{
QFontDatabase::addApplicationFont(":/fonts/roboto-light.ttf");
ui->setupUi(this);
qDebug() << "Hello World !";
//cv::VideoCapture camera = VideoCapture(0);
//
//Mat frame;
//camera >> frame;
//QImage img(frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888);
//ui->label->setPixmap(QPixmap::fromImage(img));
}
mainwindow::~mainwindow()
{
delete ui;
}
And here are errors I get
libXinerama.so.1, needed by /home/lolix/Documents/Qt-CrossCompile-RaspberryPi/raspberrypi4/sysroot/usr/lib/arm-linux-gnueabihf/libgdk-3.so.0, not found (try using -rpath or -rpath-link)
libmmal_core.so, needed by /home/lolix/Documents/Qt-CrossCompile-RaspberryPi/raspberrypi4/sysroot/usr/lib/arm-linux-gnueabihf/libavcodec.so.58, not found (try using -rpath or -rpath-link)
libmmal_vc_client.so, needed by /home/lolix/Documents/Qt-CrossCompile-RaspberryPi/raspberrypi4/sysroot/usr/lib/arm-linux-gnueabihf/libavcodec.so.58, not found (try using -rpath or -rpath-link)
.
.
.
liblapack.so.3, needed by /home/lolix/Documents/Qt-CrossCompile-RaspberryPi/raspberrypi4/sysroot/lib/libarmadillo.so.9, not found (try using -rpath or -rpath-link)
/home/lolix/Documents/Qt-CrossCompile-RaspberryPi/raspberrypi4/sysroot/usr/lib/arm-linux-gnueabihf/libopenmpt.so.0:-1: error: undefined reference to `std::random_device::_M_getentropy() const#GLIBCXX_3.4.25'
Errors arrives has soon has I uncomment my code. The version of open cv on the RP4 is 3.2 and when I install it on my machine it's 4.5. Event if I use the same command sudo apt-get install libopencv-dev
Thanks for your help
**EDIT**
I was able to get rid of these errors with this response. But a new one appeared
undefined reference to std::random_device::_M_getentropy() const#GLIBCXX_3.4.25
Regarding OpenCV I suggest to build it from source, for the RPi I always cross-compile using docker, as described here (just remove the references to openvino).
At this point you also need to cross-compile Qt for the RPi, but I guess you already did it.
Related
I need your help for a problem in OpenCV cv::StereoBM because I got an error when I tried bo compile the code below:
std::string myImgLeftString = myImgLeft.toUtf8().constData();
std::string myImgRightString = myImgRight.toUtf8().constData();
cv::Mat img1 = cv::imread(myImgLeftString,CV_LOAD_IMAGE_COLOR);
cv::Mat img2 = cv::imread(myImgRightString,CV_LOAD_IMAGE_COLOR);
cv::Mat img3;
cv::Mat img1grey, img2grey;
cv::cvtColor(img1,img1grey,CV_BGR2GRAY);
cv::cvtColor(img2,img2grey,CV_BGR2GRAY);
cv::Ptr<cv::StereoBM> match = cv::StereoBM::create(0,21);
match->compute(img1grey,img2grey,img3);
It gives me : undefiend reference to cv::StereoBM::create(int,int)
But I already include all the headers necessary and not necessary
#include <opencv2/core.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/calib3d.hpp>
#include <opencv2/imgproc.hpp>
#include <stdio.h>
#include <iostream>
#include <opencv2/core/affine.hpp>
#include "opencv2/core/hal/intrin.hpp"
#include "opencv2/core/utility.hpp"
#include "opencv2/ximgproc.hpp"
And I've added two lignes of code in my .pro file
LIBS += -L ./opt/local/lib -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_imgcodecs
INCLUDEPATH += -I /opt/local/stow/opencv-3.4.3
I don't know what is wrong, please kindly help, thanks.
cv::StereoBM is in calib3d module of OpenCV, so you need to link to the lib file of that module, to do that add -lopencv_calib3d to the LIBS in your .pro file:
LIBS += -L ./opt/local/lib -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_imgcodecs -lopencv_calib3d
Don't forget to run qmake after you modify your .pro file.
I created a c++ dynamic library from qt creator. and when i try to implement it in a qwidget application for testing. I get some errors.
Include and library paths must all be correct. as i have the .a file qt needs and include path is also correct.
my project's .pro file is;
QT += core gui multimedia
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = testlib2
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
CONFIG += c++11
SOURCES +=
main.cpp
mainwindow.cpp
HEADERS +=
mainwindow.h
FORMS +=
mainwindow.ui
Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
unix|win32: LIBS += -L$$PWD/../build-tonegenlib-Desktop_Qt_5_12_1_MinGW_64_bit-Debug/debug/ -ltonegenlib
INCLUDEPATH += $$PWD/../tonegenlib
DEPENDPATH += $$PWD/../tonegenlib
and the compile error is this;
debug/mainwindow.o: In function MainWindow::MainWindow(QWidget*)': C:\QT_workspace\build-testlib2-Desktop_Qt_5_12_1_MinGW_64_bit-Debug/../testlib2/mainwindow.cpp:6: undefined reference to__imp__ZN10TonegenlibC1Ev'
collect2.exe: error: ld returned 1 exit status
mingw32-make[1]: *** [Makefile.Debug:69: debug/testlib2.exe] Error 1
mingw32-make: *** [Makefile:38: debug] Error 2
mingw32-make[1]: Leaving directory 'C:/QT_workspace/build-testlib2-Desktop_Qt_5_12_1_MinGW_64_bit-Debug'
01:29:22: The process "C:\Qt\Qt5.12.1\Tools\mingw730_64\bin\mingw32-make.exe" exited with code 2.
Error while building/deploying project testlib2 (kit: Desktop Qt 5.12.1 MinGW 64-bit)
When executing step "Make"
01:29:22: Elapsed time: 00:01.
this is the header file of the project;
#include <QMainWindow>
#include <tonegenlib.h>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
Tonegenlib tonegen;
void test();
private:
Ui::MainWindow *ui;
};
and this is the .cpp
void MainWindow::test()
{
tonegen.m_freq = 240;
tonegen.playAudio();
}
when i add the library and build the project without a code about the library it compiles it without error. but when i try to call a function it breaks.
Thank you so much for your help
i have 2 machines with Ubuntu 12.04.2 LTS (64 mb) and the other is a snow leopard OS.
In my mac i work with ffmpeg without any trouble but then i try to use ffmpeg in ubuntu and 6700 (!) errors appear:
error: undefined reference to `av_freep'
error: undefined reference to `av_strstart'
error: undefined reference to `av_freep'
...
I use QT 4.8.5 and g++ x86 64b
I installed ffmpeg following http://ffmpeg.org/trac/ffmpeg/wiki/UbuntuCompilationGuide without errors or warnings.
My test code is:
.h
QT += core gui
INCLUDEPATH += \
/usr/local/Include
LIBS += /usr/local/lib/libavcodec.a
LIBS += /usr/local/lib/libavutil.a
LIBS += /usr/local/lib/libavformat.a
LIBS += /usr/local/lib/libswscale.a
SOURCES += main.cpp
.cpp:
extern "C"
{
#ifndef __STDC_CONSTANT_MACROS
# define __STDC_CONSTANT_MACROS
#endif
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
}
#include <stdio.h>
int main()
{
AVFormatContext *pFormatCtx;
av_register_all();
const char* fil = "/home/cits/Desktop/movFile.mov";
if(avformat_open_input(&pFormatCtx, fil, NULL, NULL)!=0)
{
return -1; // Couldn't open file
}
if(avformat_find_stream_info(pFormatCtx, NULL)<0)
{
return -1; // Couldn't find stream information
}
return 0;
}
Any ideas??
Thanks in advance.
Your .pro file should read something like:
QT += core gui
INCLUDEPATH += \
/usr/local/Include
LIBS += -L/usr/local/lib/ -llibavcodec -llibavutil -llibavformat -llibswscale
SOURCES += main.cpp
I tried to control my camera using OpenCV functions. The building progress is fine, however, when I tried to run my program it crashes and Qt only gives the information about the exit code -1073741515. I tried to comment all of the code using OpenCV function and the program seems to be OK. Are the libraries I included the wrong ones?
opencv.pro
QT += core gui
TARGET = opencv
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
INCLUDEPATH += F:/opencv/build/include
INCLUDEPATH += F:/opencv/build/include/opencv
LIBS += F:/opencv/build/x86/mingw/lib/libopencv_calib3d231.dll.a
LIBS += F:/opencv/build/x86/mingw/lib/libopencv_contrib231.dll.a
LIBS += F:/opencv/build/x86/mingw/lib/libopencv_core231.dll.a
LIBS += F:/opencv/build/x86/mingw/lib/libopencv_features2d231.dll.a
LIBS += F:/opencv/build/x86/mingw/lib/libopencv_flann231.dll.a
LIBS += F:/opencv/build/x86/mingw/lib/libopencv_gpu231.dll.a
LIBS += F:/opencv/build/x86/mingw/lib/libopencv_highgui231.dll.a
LIBS += F:/opencv/build/x86/mingw/lib/libopencv_imgproc231.dll.a
LIBS += F:/opencv/build/x86/mingw/lib/libopencv_legacy231.dll.a
LIBS += F:/opencv/build/x86/mingw/lib/libopencv_ml231.dll.a
LIBS += F:/opencv/build/x86/mingw/lib/libopencv_objdetect231.dll.a
LIBS += F:/opencv/build/x86/mingw/lib/libopencv_video231.dll.a
mainwindow.cpp
Since MainWindow.cpp is long, I will just paste the constructor here and add the rest of them if needed.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
cam = NULL;
frame = NULL;
timer = new QTimer;
imag = new QImage();
connect(timer,SIGNAL(timeout()),this,SLOT(readFrame()));
connect(ui->open,SIGNAL(clicked()),this,SLOT(openCamara()));
connect(ui->pic,SIGNAL(clicked()),this,SLOT(takingPictures()));
connect(ui->closeCam,SIGNAL(clicked()),this,SLOT(closeCamara()));
}
Finally GET IT WORK! When I try to run the app from Windows Explorer, it complains about lack of libstdc++-6.dll. Although there exist on under C:\QtSDK\mingw\bin, its version is incorrect for OpenCV 2.3 (or any other reason?) and will cause an error. I got one from here and the program runs correctly.
I've just installed Qt 4.7.4 and trying to make a simple Qt App that uses Qt DLL. I added export/import in a class in DLL through ifdef as usual but when compiling App I het unresolved references. I also set that App depends on the DLL.
Here are the main files:
Lib.pro
QT -= gui
TARGET = Lib
TEMPLATE = lib
DEFINES += LIB_LIBRARY
SOURCES += lib.cpp
HEADERS += lib.h\
Lib_global.h
symbian {
MMP_RULES += EXPORTUNFROZEN
TARGET.UID3 = 0xE10C4E25
TARGET.CAPABILITY =
TARGET.EPOCALLOWDLLDATA = 1
addFiles.sources = Lib.dll
addFiles.path = !:/sys/bin
DEPLOYMENT += addFiles
}
unix:!symbian {
maemo5 {
target.path = /opt/usr/lib
} else {
target.path = /usr/lib
}
INSTALLS += target
}
App.pro
QT += core gui
TARGET = App
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
lib.h
#ifndef LIB_H
#define LIB_H
#include "Lib_global.h"
#include <QString>
class LIBSHARED_EXPORT Lib {
public:
Lib();
~Lib();
QString Hello(QString a);
};
#endif // LIB_H
Lib_global.h
#ifndef LIB_GLOBAL_H
#define LIB_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(LIB_LIBRARY)
# define LIBSHARED_EXPORT Q_DECL_EXPORT
#else
# define LIBSHARED_EXPORT Q_DECL_IMPORT
#endif
#endif // LIB_GLOBAL_H
What's wrong with this code? Why is it unresolved reference?
In App I just include "../Lib/lib.h" and try to use the class.
What looks to be missing is the link between your application and your dynamic library.
I usually do something like this for my desktop (linux) apps:
INCLUDEPATH += ./include
LIBS += -L./lib -lLib
Where:
INCLUDEPATH defines the path to where your shared library .h files are located.
LIBS defines the path to where your built library is.
You can find the relevant documentation here: QMake - Declaring Other Libraries
Updated:
I was able to build the project (Linux Qt v4.7.4) and link to the library using the code you posted. I will try to clarify as my response may not be entirely clear.
The INCLUDEPATH and LIBS variables must be added to the App.pro file.
For the linker to properly find the library the paths must be adjusted to the paths you are using for your project:
LIBS += -L PATH -l LIBNAME
Supposing your folder structure is similar to the following:
Project
|_ Lib (your sharded library project files)
|_ App (your application project files)
You would adjust the variables like so:
INCLUDEPATH += ../Lib
LIBS += -L../Lib -lLib
Note that you link differently depending on your platform, you can do something like this to cover Windows and Linux:
unix {
INCLUDEPATH += ../Lib
LIBS += -L../Lib -lLib
}
win32 {
INCLUDEPATH += ../Lib
LIBS += ../Lib.lib
}