PDA

View Full Version : Qt + OpenCV, simple example



stampede
11th July 2011, 23:24
Hi,

today I've found my old project I wrote some time ago. This is simple application that uses OpenCV to capture images from camera or video, optionally process them and displays in QLabel. I've decided to share it with community, made the code more self-explanatory, added some comments, especially explaining BGR24 IplImage to QImage conversion stuff (it can be done easier, but I think its better this way for "educational" purposes).
This app does not use separate threads for capturing or processing images, I think everything is as simple as possible.
I've seen many posts about using basic OpenCV with Qt, so maybe this will be useful for some "newbies".

I hope this helps someone.

lam-ang
14th July 2011, 05:44
Hi Stampede,
I'm new to QT and opencv, I'm tring to learn to use QT and opencv by following the example given by the book. But I have problem in pointing the directory. So I search and found your free example application. I have installed Opencv2.3 and I tried to edit the project file...and just want to see if it will run..but still I have the similar problem.

TEMPLATE = app
DESTDIR = bin
TARGET = cvexample
DEPENDPATH += . src/cpp src/h src/ui
INCLUDEPATH += . src/h

UI_DIR = build
MOC_DIR = build

build_pass:CONFIG(debug, debug|release){
OBJECTS_DIR = build/debug
} else{
OBJECTS_DIR = build/release
}

win32 {
CV11_INCLUDE = C:/OpenCV2.3
CV11_LIB = C:/OpenCV2.3/lib
CV22_INCLUDE = C:/OpenCV2.3/modules
CV22_LIB = C:/OpenCV2.3/build/bin
} else{
CV11_INCLUDE = /usr/local/include/OpenCV
CV11_LIB = /usr/local/lib/OpenCV
CV22_INCLUDE = /usr/local/include/OpenCV2.3
CV22_LIB = /usr/local/lib/OpenCV2.3
}

contains(cv,2){
message(build with opencv 2)
DEFINES += OPEN_CV_22
INCLUDEPATH += $$CV22_INCLUDE/core/include/opencv2.3/core \
$$CV22_INCLUDE/highgui/include/opencv2.3/highgui \
$$CV22_INCLUDE/imgproc/include/opencv2.3/imgproc \
$$CV22_INCLUDE/core/include \
$$CV22_INCLUDE/imgproc/include
LIBS += -L$$CV22_LIB
LIBS += -lopencv_highgui220 -lopencv_core220 -lopencv_imgproc220
} else{
message(build with opencv 1.1)
DEFINES -= OPEN_CV_22
INCLUDEPATH += $$CV11_INCLUDE/cv/include $$CV11_INCLUDE/otherlibs/highgui $$CV11_INCLUDE/cxcore/include
LIBS += -L$$CV11_LIB
LIBS += -lcv -lhighgui -lcxcore
}

HEADERS += src/h/ImageCapture.h src/h/MainWidget.h
FORMS += src/ui/MainWidget.ui
SOURCES += src/cpp/ImageCapture.cpp src/cpp/MainWidget.cpp main.cpp

When I compiled it says "C:\QtCvExample-build-desktop\..\QtCvExample\src\h\ImageCapture.h:16: error: cv.h: No such file or directory"
I know there are some link in this forum regarding opencv, but still I cant find what I'm looking for.

thanks in advance,
lam-ang

stampede
14th July 2011, 08:09
If you are using OpenCV 2.2 or 2.3, you need to run qmake cv=2 and then make, because by default it will include and link to OpenCV 1.1 (btw. its in README ;)).
What does it print when you run qmake ? Probably "build with opencv 1.1", right ?

lam-ang
14th July 2011, 14:32
Hi, thanks..hmm I think I better do some more reading in order follow your suggestion...I appreciate the help. I'll get back, as soon I got it.

regards,
lam-ang

lam-ang
15th July 2011, 14:48
Hi, I have a question, I appoligize if this is so obvious, but I'm really struggling to learn QT. And I came back to post it here because I feel I'm not going anywhere..I was reading "OpenCV 2 Computer application Porgramming cookbook". The first exercise in the book is to display an image. But I found out my QT and Opencv configuration is not correct. I tried to search into this forum and found this useful example(by stampede) that I will gain from but obviously I dont have any idea how to do the procedure as suggested, I must use qmake and make ( tried to read some document) but I was getting lost :(..So here is my question.
1. How can I configure QT and OpenCv using qmake and make?
Before trying the "QtCvexample" I want to try to display an image to the screen, please see my attachemnt. I have Qt 4.7.3 and OpenCv2.3 installed

Thanks in advance,
lam-ang

stampede
15th July 2011, 17:01
I don't know what is the directory structure of OpenCV 2.3, but in general you need to specify the path to include directories in INCLUDEPATH variable in .pro file. So if in your code you write


#include <opencv2.3/core/core.hpp>

and the file "core.hpp" is in (for example) "C:/OpenCV2.3/include/opencv2.3/core" directory, then make sure INCLUDEPATH contains "C:/OpenCV2.3/include" directory.
This is just an example, you need to check the actual paths to your files on disk.

Added after 1 44 minutes:

Looks like including and linking to OpenCV libraries can be problematic, so here is a simple how-to. It will work only on 32 bit Windows system, with MinGW g++ compiler. We are going to keep things as simple as possible:
1) download OpenCV 2.3 "superpack" (http://sourceforge.net/projects/opencvlibrary/files/opencv-win/2.3/OpenCV-2.3.0-win-superpack.exe/download)
2) double click, extract to : C:\ (it will make OpenCV2.3 subfolder automatically)
3) use the following main.cpp and .pro files:


// main.cpp
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>

int main(){
cv::Mat image = cv::imread("test.jpg");
cv::namedWindow("My Image");
cv::imshow("My Image", image);
cv::waitKey(-1);
return 0;
}



// project.pro
TEMPLATE = app
QT -= gui
TARGET = SimpleCvApp

SOURCES += main.cpp

INCLUDEPATH += C:/OpenCV2.3/opencv/modules/core/include \
C:/OpenCV2.3/opencv/modules/highgui/include

# you can put the opencv .dll files where you want, this is where they are by default
# if you want to use different compiler or architecture, look for the libraries for your configuration in "OpenCV2.3/build" subfolder
LIBS += -L"C:/OpenCV2.3/build/x86/mingw/bin"
LIBS += -lopencv_core230 -lopencv_highgui230

4) cd to the project folder, run qmake && make
5) place test.jpg in the project folder
6) run debug\SimpleCvApp.exe (or release\SimpleCvApp.exe, depends on what version you compile)

lam-ang
16th July 2011, 03:30
Hi, thanks for the time helping me out I appreciate it very much. I test compiling the project to check if any problem will arise, but no error concerning the library location. But still have some issues.
result when compiling:

Starting C:\OpenCVProjects\MyQtConsoleProject-build-desktop\debug\MyQtConsoleProject.exe...
C:\OpenCVProjects\MyQtConsoleProject-build-desktop\debug\MyQtConsoleProject.exe exited with code -1073741511
4. cd to the project folder, run qmake && make - I tried to issue qmake and it gives "qmake is not recognized as internal or external command". Am I missing something?


regards,
lam-ang

stampede
16th July 2011, 09:11
Looks like you need to place the needed opencv dll files in folder where the app .exe is located, or in system PATH.

I tried to issue qmake and it gives "qmake is not recognized as internal or external command"
Again, path to folder with qmake.exe shoud be in PATH env. variable.

lam-ang
16th July 2011, 13:39
Looks like you need to place the needed opencv dll files in folder where the app .exe is located, or in system PATH.
Again, path to folder with qmake.exe shoud be in PATH env. variable.
Here is what I did..
I copied the opencv dll files to this location "C:\OpenCVProjects\MyQtConsoleProject-build-desktop\debug"
I think I got the qmake setup in the PATH env. variable right. And this how I type the command "C:\OpenCVProjects\MyQtConsoleProject>qmake" no error output.
But I when I compile the project I still have this message "exited with code -1073741511"

Regards,
lam-ang

stampede
16th July 2011, 15:42
Err.exe -1073741511:
# for decimal -1073741511 / hex 0xc0000139 :
STATUS_ENTRYPOINT_NOT_FOUND ntstatus.h
# {Entry Point Not Found}
# The procedure entry point %hs could not be located in the
# dynamic link library %hs.

Are you sure you copy the same dll files that you link against ?

lam-ang
17th July 2011, 13:36
Err.exe -1073741511:
# for decimal -1073741511 / hex 0xc0000139 :
STATUS_ENTRYPOINT_NOT_FOUND ntstatus.h
# {Entry Point Not Found}
# The procedure entry point %hs could not be located in the
# dynamic link library %hs.

Are you sure you copy the same dll files that you link against ?
Please see the attachement if I have the correct dlls to the required folder.

Thanks and regards,
lam-ang

stampede
18th July 2011, 08:38
Actually you need only two: libopencv_core230.dll and libopencv_highgui230.dll, place them in one folder with .exe file.
Make sure you have test.jpg in project folder, or maybe instead of cv::imread("test.jpg"); try with cv::imread("C:/test.jpg");.
Btw. what is your gcc version (type gcc -v ) ? OpenCV 2.3 needs gcc ver. 4.4 or higher.

lam-ang
18th July 2011, 10:08
Hi, I followed your suggestion,

// main.cpp
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>

int main(){
cv::Mat image = cv::imread("C:/OpenCVProjects/MyQtConsoleProject/penguins.jpg");
cv::namedWindow("My Image");
cv::imshow("My Image", image);
cv::waitKey(-1);
return 0;
}

Again thanks for your time, I appreciate it much.
I think I still have some settings that I need to fix, because when I typed gcc -v I still have some issue...please see the attachement

regards,
lam-ang

stampede
18th July 2011, 11:38
Where did you get your mingw installation from ?

lam-ang
18th July 2011, 13:41
I got Qt 4.7.3, MinGW 4.4, please see my development environment..

stampede
18th July 2011, 15:12
I'm running out of ideas. Have you tried doing a clean build ? Is your operating system 32 bit ?
Try using the system console:
cd to project folder
make clean
qmake
make release
post the output.

lam-ang
18th July 2011, 16:08
I have windows 7 32bit..when I issue "make clean" to the system console I have some issue..probably this is causing the problem.

lokkoLori
19th July 2011, 14:31
Hi!

It seems I've found the thread what I need. I know the first step is the hardest one. I have to program a cross platform stereo camera based application, so the Qt + OpenCV should be an essential environment for my task.

this thread is a good starting point, but i've stucked

the first example has a reference to "C:/OpenCV2.2/build/bin" but the downloadable OpenCV2.2 has no binaries. I tried to compile it from console with the newest cmake and WinGW, but had no success, I've got only a flood of errors from cmake ...
so i tried the "OpenCV2.3 superpack" example ... but when I try to run or debug the program under the Qt I always got this error message:
6687
but if i launch the exe file (with the requested OpenCV's DLLs in the same dir) it works perfectly ... i really don't understand why is this happening, and what should i do :(

i don't want to lose the convenient of the Qt debugging functionality

6688

add. infos:
gcc ver: 4.5.2
Qt ver: 4.7.4
win XP Prof SP3 32 bit

Added after 10 minutes:

yes!! I've found the solution ... in the Bulid settings I've changed the Tool chain to MinGw (x86 32bit) ... and that's it ... i can debug my app now ... cool :)

lam-ang
19th July 2011, 15:02
Hi lokkoLori, I'm happy for you that you got your app running, I'm still stuck with my own setup. Probably I need to re install my opencv or perhaps my QTSDK.. :(
I notice when I type gcc -v in the command line I got an error..
"sorry, \epoc32\gcc\bin\gcc.exe is not supoorted in this release"
Any idea how to resolve this?
regards,
lam-ang

stampede
20th July 2011, 09:03
Whats in this C:\Symbian\ directory ? Looks like it contains gcc and make that is used instead of your mingw binaries. Can you remove this directory from PATH and try again ?

lokkoLori
20th July 2011, 13:17
I've got the gcc from the original MinGW ( http://www.mingw.org/ ) ... install it, and put it's bin into the sys path

Added after 12 minutes:

but I have to admit that I really don't understand why it works with the other toolchain, and why dosen't with the original Qt's toolchain ...

lam-ang
20th July 2011, 14:47
Whats in this C:\Symbian\ directory ? Looks like it contains gcc and make that is used instead of your mingw binaries. Can you remove this directory from PATH and try again ?

Here is the content of my path variable
C:\Program Files\PC Connectivity Solution\;C:\Program Files\Common Files\Symbian\Tools;C:\Python25\Lib\site-packages\PyQt4\bin;C:\Perl\site\bin;C:\Perl\bin;%S ystemRoot%\system32;%SystemRoot%;%SystemRoot%\Syst em32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\ v1.0\;C:\Windows\system32\Wbem;C:\Program Files\CSL Arm Toolchain\bin;C:\Program Files\QuickTime\QTSystem\;c:\qtsdk\desktop\qt\4.7. 2\mingw\bin;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\DTS\Binn;C:\QtSDK\Desktop\Qt\4.7.3\ming w\bin\;C:\Python2.7;C:\OpenCV2.2\bin

I tried to remove it and check if will solve the problem, but still the same output.

regards,
lam-ang

stampede
21st July 2011, 10:11
Ok, so try to rename this folder or even remove it if you dont need it, looks like its messing up your configuration.
It very simple, you just have to download mingw (linked above) or use mingw from SDK, add mingw/bin folder to PATH and you're done. Just try to remove/remove from PATH/rename all other folders that contains some crappy mingw executables.

lam-ang
21st July 2011, 15:05
Ok, so try to rename this folder or even remove it if you dont need it, looks like its messing up your configuration.
It very simple, you just have to download mingw (linked above) or use mingw from SDK, add mingw/bin folder to PATH and you're done. Just try to remove/remove from PATH/rename all other folders that contains some crappy mingw executables.

Hi, I got the gcc -v running on the teminal..so I tried to to run
1. qmake (no error)
2. make (error displayed)
I still have problem issuing the make command..please check attachement.

regards,
lam-ang

lam-ang
21st July 2011, 23:37
I made a mistake in issuing the qmake command it should be c:\opencvprojects\myqtconsoleproject\qmake - results has no error. But again issuing the make command give me "make is not recognized as internal or external command"

ChrisW67
22nd July 2011, 02:37
Start the command prompt from the item under Qt SDK in the Windows Start menu. Then you have everything you need in the path:


D:\Work> qmake
D:\Work> mingw32-make

lam-ang
22nd July 2011, 12:17
Hi, thanks for the time and support everyone...I appreciate it very much..I'll try something else and post it here my results.

andre_teprom
31st July 2011, 03:15
Hi stampede,


I wish ask for your support to a little issue regarding a feature not allowed to your ( excelent ) sample project.
That´s the Go to slot...

Could you please provide some guideline about how I could solve that ?


+++

stampede
31st July 2011, 08:40
To be honest I don't know what you mean by "Go to slot", can you provide more details ?

andre_teprom
31st July 2011, 16:11
Hi,


Sorry if I was not so clear.
I meant that once an object is selected ai UI form, occurs an error :
http://www.teprom.eng.br/arquivos/qt/h.bmp
http://www.teprom.eng.br/arquivos/qt/g.bmp

I search about the solution here at the forum.
However, the solution can be reached automactly just creating a new project with Wizard tool.
But, the structure directory is different from that in design.
I tryed to undestand what declaration is missing.

Thanks.


+++

stampede
31st July 2011, 16:33
I dont know anything about that, I've used Geany + command line while creating and testing the project.
If creating new project works ok, then create new project and copy the files, directory structure is not important when you run the program.

andre_teprom
31st July 2011, 17:01
The problem is there are many files with different contents , and I wold have to search line-by-line.
I don´t want to kill the directory structure you had created, that is very well organized.
For while, I will keep the structure and choice to loose the function.


Thanks.

astodolski
21st June 2013, 19:26
This is a huge start - pretty comprehensive too. However, the current version (2.4.5) for windows contains different tree structure. For example, the paths in the win32 block in the .pro file is listed as:



win32 {
CV11_INCLUDE = C:/OpenCV
CV11_LIB = C:/OpenCV/lib
CV22_INCLUDE = C:/OpenCV2.2/modules
CV22_LIB = C:/OpenCV2.2/build/bin
}


In the current version the bin folder is c:\opencv\build\x86\vc10\bin. There are also a lib and staticlib folder there. Accordingly, there is also folders for other compilers as well as an x64 + compilers. The include folder is located off the build folder. These paths have to be changed in order to use the latest version.

With that accomplished though, there are numerous build errors. If you were to grab the latest version, you would see that as well

chfakht
4th August 2015, 17:06
thanks for this great work , the camera button worked perfectly but when i click on load file i get nothing just a blank window , what could be the problem , i'm working on ubuntu 14.04 with opencv 2.4.10
thanks a lot