How to get the CImg library working for Qt - c++

I'm currently learning how to use Qt. I want to try out some simple image processing applications using Qt, and since I'm already familiar with CImg I want to use that. I guess it should be possible to do so, if not mark my question for deletion or something.
My question is: how to get CImg working for Qt? CImg is a header file. Lets say its located on my desktop. I import it using Qt creator 4.1.0, by using the "add existing file..." in the rightclick menu on the header folder. Then my menu looks like this:
.
It compiles when I add #include "CImg.h", but I can't use it, even when I'm trying to type using namespace cimg_library it will tell me that cimg_library doesn't exist. I also tried just creating a header file and copying the content of the CImg.h into it but then it simply fails to compile and the Qt Creator freezes.
Edit: I managed to make the situation a bit better by adding the header location to the include code (like this: #include "C:/Users/Marci/Desktop/CImg.h" )I can now "see" CImg related stuff in the dev environment, and it won't bother me with not finding the constructor for CImg or anything like that. However when I try to compile while using anything CImg related it will give me around 20 linker errors. (Error code: LNK2019) My .pro file looks like this:
#-------------------------------------------------
#
# Project created by QtCreator 2016-11-08T17:08:58
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = grayscale
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h \
C:/Users/Marci/Desktop/CImg.h
LIBS += -C:/Users/Marci/Desktop/ -CImg.h
FORMS += mainwindow.ui
Edit2: after implementing the changes that PeterT suggested in his comment my .pro file looks like this:
#-------------------------------------------------
#
# Project created by QtCreator 2016-11-08T17:08:58
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = grayscale
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h \
INCLUDEPATH += C:/Users/Marci/Desktop
FORMS += mainwindow.ui
And my mainwindow.cpp (in which i'm trying to create a CImg object) looks like this:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <CImg.h>
using namespace cimg_library;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
CImg<unsigned char> myimage(100,200);
}
MainWindow::~MainWindow()
{
delete ui;
}
The compiler errors i get are: error: C2871: 'cimg_library': a namespace with this name does not exist
error: C2065: 'CImg': undeclared identifier
error: C2062: type 'unsigned char' unexpected
I hope this is specific enough.

After I few months I figured it out. The problem lies in the fact, that CImg uses a windows .dll file for the visualizing functions of the class cimg_display. Since Qt is platform independent it doesnt like this. However you can make it work with an interesting trick. First you need to include the header file normally, in the project file. After that, whenever you actually #include it, you need to write the following macro:
#define cimg_display 0
In my understanding this makes it work, because the C and C++ compilers simply copy the content of the included file into the source. And thanks to the macro the compiler will ignore the class thats causing trouble for us.

Related

The program has unexpectedly finished in QT opencv

I know that this has been posted many times,but I could not find the solution from previous posts. I followed tutorial on How to setup Qt and openCV on Windows from wiki Qt.
My .pro file and mainwindows.cpp are shown below. I wanted to open image following the example. What is wrong here? Checked the opencv version and it is the same as libs included. The PATH is also correct.
The cpp file
#include "ui_mainwindow.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
cv::Mat image = cv::imread("C://1.jpg", 1);
cv::namedWindow("My Image");
cv::imshow("My Image", image);
}
MainWindow::~MainWindow()
{
delete ui;
}
and
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = opencvtest
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
INCLUDEPATH += C:\opencv\build\include
LIBS += C:\opencv-build\bin\libopencv_core451.dll
LIBS += C:\opencv-build\bin\libopencv_highgui451.dll
LIBS += C:\opencv-build\bin\libopencv_imgcodecs451.dll
LIBS += C:\opencv-build\bin\libopencv_imgproc451.dll
LIBS += C:\opencv-build\bin\libopencv_features2d451.dll
LIBS += C:\opencv-build\bin\libopencv_calib3d451.dll
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
When program crashes like that under Qt Creator, and you have extra libraries, the very likely reason is that the extra libraries are missing from the runtime PATH.
In this case, you need to add C:\opencv-build\bin to the PATH. There are at least 3 ways to go about it.
Edit the system environment, so that the relevant directory is always in the system PATH. You need to restart Qt Creator for this change to take effect. This is not the recommended way, unless you actually want these things in there also for other purposes.
You can edit the Build environment of the project under Qt Creator Project view. There's separate configuration for each build type, so you may need to do this to them all separately, which both good and bad. It is good, because then you can have different directory for different builds (for example debug vs relase, MSVC vs MinGW builds). It's bad because it's extra hassle and makes it easier to have something wrong.
You can add it to the run environment in the Qt Creator Project view. Then it will be the same for all build types.
In this case, 3 is probably the way to go.
Qt Creator annoyingly does not display any information about which DLL is missing, it just says the program crashed. This can be solved by instead string the "Qt command prompt" for the correct toolchain from Windows Start menu (search Qt and you should find it). Then go to the built .exe directory and run the .exe. You should now get an error dialog where Windows tells you which DLL it failed to find. Then you can look where that DLL is and add it to the path and try again, until the program starts. After you know the directories using this method, you can then add them to Qt Creator as explained above.

undefined reference SDL_Init with Qt

I've already gone through questions similar to mine, but none solved my problem.
So I'm trying to use SDL in a Qt Widget (for educational purpose), and I always get and undefined reference on every SDL fonction that I call.
Here is the (minimalist) code I've been using for testing :
#include "mainview.h"
#include <QApplication>
#include "SDL.h"
#undef main
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainView w;
w.show();
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = SDL_CreateWindowFrom((void*)w.centralWidget->winId());
SDL_Renderer* render = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
SDL_SetRenderDrawColor(render, 255, 0, 0, 255);
SDL_RenderFillRect(render, NULL);
SDL_RenderPresent(render);
SDL_DestroyWindow(window);
SDL_DestroyRenderer(render);
SDL_Quit();
return a.exec();
}
And here is th .pro file :
#-------------------------------------------------
#
# Project created by QtCreator 2014-10-08T22:37:55
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = MapEditor
TEMPLATE = app
SOURCES += main.cpp\
mainview.cpp
HEADERS += mainview.h
FORMS += mainview.ui
INCLUDEPATH += SDL/include/
LIBS += -L SDL/lib/x86/SDL2.lib
According to other answers i've read, it seems the problem comes from the link to the library.
I have never really used this file, or other qmake before, so I am not sure about the path I am using. I work on windows, and according to this article, I should be writing the full path, but then I get access authorization problems (damn Windows).
Furthermore, changing the path to an false one doesn't seems to upset the compiler.
However, when I try to use
LIBS += -L SDL/lib/x86/ -lSDL2.lib
the compiler cannot find the file.
The compiler I'm using is a MinGW 32bits, so I've downloaded the corresponding file on lbsdb.org, and I've tried every library on very sub-folder there was (x86_64, i686,...) but none worked.
And sorry if the way I'm writing is strange, I'm french.
When you have a .lib file, it's easiest to just add the file for linker. You have it subdirectory of your project it seems, but you are probably using a separate build directory (as you should), so the problem is probably that. To fix, use PWD qmake variable:
LIBS += $${PWD}/SDL/lib/x86/SDL2.lib
You might also want to add PWD to include path, because relative path there is relative to directory of the .c file, and you might not always have them all in the same directory.
In a comment you mention something about not being able to use absolute paths... Not sure what you mean by that, but then a way out is to not use shadow build (which is no problem, as long as you remember to do full rebuild when changing build types). If you are using Qt Creator, you can find the project's build settings for each build type under Projects view, and there you have a check box for shadow build.

Debug Assertion Failed (unsigned)(c+1) <= 256 (in VideoCapture::open [Qt Creator])

I've been trying to use cv::VideoCapture::open("< path to video file >") in QtCreator (opencv added). Even though the program runs without errors in "bebug mode" (debug build), it gives below runtime error in "release mode" (release build).
Debug Assertion Failed File:
f:/dd/vctools/crt_bld/self_x86/src/isctype.c Line: 56 Expression:
(unsigned)(c+1) <= 256
It is a simple program which uses only cv::VideoCapture::open() [for testing purposes]
Below is the .pro file
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = untitled
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
INCLUDEPATH += C:/C/opencv/build/include
INCLUDEPATH += C:/C/opencv/build/include/opencv
LIBS += C:/C/opencv/build/x86/vc10/lib/opencv_highgui240d.lib
LIBS += C:/C/opencv/build/x86/vc10/lib/opencv_highgui240.lib
LIBS += C:/C/opencv/build/x86/vc10/bin/opencv_highgui240d.dll
LIBS += C:/C/opencv/build/x86/vc10/bin/opencv_highgui240.dll
Below is the Header file
#include <QMainWindow>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
cv::VideoCapture vcap;
};
Below is the .cpp file
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
vcap.open("C:/Users/ANURUDDHA/pedestrians/ThreePastShop2cor.mpg");
}
MainWindow::~MainWindow()
{
delete ui;
}
When I pass an int as the argument to cv::VideoCapture::open() [eg: vcap.open(0)] it runs without errors in both debug and release build and opens webcam successfully. Problem comes only when I pass a String to arguments.
Someone please shed some light on this. Really appreciated.
It looks like you're linking in both debug and release versions of the libs (twice?). That's caused me problems in the past before. Try using only LIBS += C:/C/opencv/build/x86/vc10/bin/opencv_highgui240.dll for release builds and LIBS += C:/C/opencv/build/x86/vc10/bin/opencv_highgui240d.dll for debug builds.
Also, 0 is the same as NULL. Probably the library aborts doing whatever it was doing (whether in debug or release) prior to the assert fail. You shouldn't be getting assert fails in release mode, but the fact that you are tells me that the debug library was loaded in your program that you compiled for release mode.
Edit:
Looks like somebody already answered you here:
http://answers.opencv.org/question/15838/videocaptureopenqt-creatordebug-assertion-failed/
I guess I found the answer though it seems kinda ambiguous to me. Anyway, now the program works in release mode.
Hope this might help someone in someway.
I copied dll files inside C:/C/opencv/build/x86/vc10/bin/ to Windows system folder. That is C:\Windows\SysWOW64. In case if someone is using a 32 bit version of Windows it should be System32.

SYSTEMTIME in qt

I am a bit stuck at the moment with a little sample project that I would like to run to test some cryptology that I want to use in a main project.
Basically I am using the latest Qt Creator and I have created a simple window dialog. Furthermore, I would like to test the PBKDF2 implementation through CkCrypt2
So what I have done is downloading the X64 version of the library and added it to my project folder. I then told my Qt project to use an external library, the final .pro file looks like this:
#-------------------------------------------------
#
# Project created by QtCreator 2013-06-09T18:09:44
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = crypt2test
TEMPLATE = app
SOURCES += main.cpp\
m
ainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/libs/ -lChilkatDbgDll_x64
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/libs/ -lChilkatDbgDll_x64d
else:unix: LIBS += -L$$PWD/libs/ -lChilkatDbgDll_x64
INCLUDEPATH += $$PWD/include
DEPENDPATH += $$PWD/include
I can successfully load the library but I cannot start the application.
My mainwindow.cpp looks like this:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "CkCrypt2.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
CkCrypt2 crypt;
bool success;
success = crypt.UnlockComponent("Just some random text ");
if ( !success )
{
qDebug() << "Not successfully unlocking the library";
}
}
MainWindow::~MainWindow()
{
delete ui;
}
The error message I get in the compiler is:
c:\qt\qt5.0.2\tools\qtcreator\bin\crypt2test\include\CkString.h:127: error: C2061: syntax error : identifier 'SYSTEMTIME'
c:\qt\qt5.0.2\tools\qtcreator\bin\crypt2test\include\CkString.h:129: error: C2061: syntax error : identifier 'SYSTEMTIME'
C:\Qt\Qt5.0.2\Tools\QtCreator\bin\crypt2test\include\CkCrypt2.h:429: error: C2061: syntax error : identifier 'SYSTEMTIME'
Looking into the files I see:
void appendDateRfc822(SYSTEMTIME &sysTime);
void appendDateRfc822Gmt(SYSTEMTIME &sysTime);
// GETSIGNATURESIGNINGTIME_BEGIN
bool GetSignatureSigningTime(int index, SYSTEMTIME &outSysTime);
// GETSIGNATURESIGNINGTIME_END
Okay, so it is complaining about the SYSTEMTIME construct. So I look up the error C2061
Basically it says:
The compiler found an identifier where it wasn't expected. Make sure
that identifier is declared before you use it.
Which makes sense, so I look up the SYSTEMTIME and try to do :
#include <windows.h>
But that leads to many more errors in the windows.h file itself.
I use the MS Visual C++ compiler in Qt. Even though I use qmake. I am very new to this and I do not understand it all yet. Furthermore, I have no idea how to fix this, because including the windows.h does not help.
What seems to be the problem here ? Is this an issue regarding my compiler or the constulation that I use a third party app which library is compiled with MS Visual C++ and I am now trying to use this on my Windows machine in Qt with a Windows Visual C++ compiler ?
For any help I am gratefully thankful!
EDIT1:
Actually, after a clean all and qmake and build project I have now different errors and none were found in the Windows.h as previously stated. Since there were so many I have made a screenshot: http://i.imgur.com/B8EoENB.png
EDIT2:
I have adjusted the library that I include. Before this I was using the multi-threaded library of CkCrypt in the Debug mode. I have now included the single realease library. Which is located in the same directory.
When including windows.h I got the errors that things were already defined. So I removed the line again. With this result: http://i.imgur.com/z415txR.png
This shows at the bottom that MSVCRT conflichts with other library. It mentions to use NODEFAULTLIB:library but I am not to sure how to do that. Will google and keep this up to date as I process.
Many years ago, Chilkat was originally developed for the Windows platform only, and used SYSTEMTIME for this reason. In the last 5 years (approx) Chilkat is cross-platform, and SYSTEMTIME no longer makes sense. To cope with the issue, there is a "SystemTime.h" header in the same directory as the CkCrypt2.h header file. You could include this to solve the problem. (However, if WIN32 is defined, you'll probably need to edit SystemTime.h to remove the #ifdef.)
In any case, the methods using SYSTEMTIME are going to be deprecated. For any method or property that uses SYSTEMTIME, there should be a newer alternative method/property that instead uses CkDateTime.
Finally, Chilkat will test with Qt so that for the next version, (hopefully) Qt out-of-the-box will compile without any pitfalls.

Using Dlls in Qt C++

I'm trying to build a project with a library (dll) I made. I've never attempted to either load or make a library before and I'm getting the following error.
error: undefined reference to `imp__ZN6NeuronC1Ev'
In Qt, the error is shown in the following line.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow) <--------------------------- Error indicated here.
{
ui->setupUi(this);
}
Project File
QT += core gui
TARGET = Jane
TEMPLATE = app
LIBS += -L quote(C:\Programming\Jane\Jane\Source\Neuron.dll)
SOURCES += main.cpp\
MainWindow.cpp
HEADERS += MainWindow.h
FORMS += MainWindow.ui
Here is one of the classes that I've exported
#ifndef NEURON_H
#define NEURON_H
#include <QList>
#include "Neuron_global.h"
#include <Sensor.h>
class NEURONSHARED_EXPORT Neuron
{
public:
explicit Neuron();
const double getOutput() const;
const double & getWeight() const;
void setWeight(const double& weight);
private:
double weight; // The weight of this neuron.
QList<Neuron*> neurons; // This Neuron's children.
QList<Sensor*> sensors; // This Neuron's Sensors.
};
#endif // NEURON_H
The NEURONSHARED_EXPORT Marco is defined in "Neuron_global.h"
#ifndef NEURON_GLOBAL_H
#define NEURON_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(NEURON_LIBRARY)
# define NEURONSHARED_EXPORT Q_DECL_EXPORT
#else
# define NEURONSHARED_EXPORT Q_DECL_IMPORT
#endif
#endif // NEURON_GLOBAL_H
If anyone has any advice on how to fix this, I would greatly appreciate it.
Edit:
I've added the libNeuron.a file to the LIBS argument in pro file. However, I'm now getting the following error.
LIBS += libNeuron.a
cannot find -lNeuron.a
any ideas?
What are you trying to do?
LIBS += -L quote(C:\Programming\Jane\Jane\Source\Neuron.dll)
It variable contains a lib-files, which project will be linked with, not a library itself!
You should find a lib for the dll or use WINAPI LoadLibrary/GetProcAddres functions to load dll dynamically.
This is only a quick guess of mine: your trouble is caused c++ name mangling. Google
"qt dll c++ name mangling"
and find some examples of working dll / client projects.
In this case everything looks right (assuming NEURON_LIBRARY is not defined since you're building under the app template, although Windows v. Linux act differently in this regard).
qmake is known not to pick up all the changes that it ought to so I'd recommend re-running qmake and then your make variant (e.g. make, gmake, nmake):
$ qmake
$ nmake
In some cases, you'll actually need to do a clean (or delete the relevant object files) before everything will be able to link correctly.