PDA

View Full Version : Using OpenCV with QT creator



gmiller39
3rd July 2010, 19:30
I am trying to create a project using OpenCV with QT for the user interface but I am having some problems.

I am using QT creator 2.0.0 on Ubuntu 10.4 64 bit with OpenCV 2.1.0.

I have each component installed and working independantly, but I can't seem to get them to play nice together.

For instance: using CMake and the following CMakeLists.txt file:


PROJECT( facedetect_proj )
FIND_PACKAGE( OpenCV REQUIRED )
ADD_EXECUTABLE( facedetect facedetect.cpp )
TARGET_LINK_LIBRARIES( facedetect ${OpenCV_LIBS} )

Creator creates a project that compiles and runs just fine, but does not create a .pro file.

When I create just a QT project, it compiles just fine and seems to find the OpenCV libraries just fine in the IDE but then does not build properly. I get "undefined reference" errors for every function call using OpenCV. I am pretty sure that I need to include more in the .pro file for this to work but I am not sure how to do it or what to include. Please help me! Thanks!

gmiller39
4th July 2010, 13:39
OK, so I've done some more research on CMake. Apparently CMakeLists.txt replaces the .pro file altogether and when you use a cmake file, you run cmake to run the MOC instead of QMake. I am still going through the CMake documentation to figure out how to do this. I will post when I figure it out. Also, I can't find the button to edit my posts. Oh well.

gmiller39
6th July 2010, 20:03
I have gotten things to build, but now I am having linker errors. Here is my CMakeLists.txt file:


PROJECT( detect_proj )
CMAKE_MINIMUM_REQUIRED(VERSION 2.6.0)


FIND_PACKAGE( Qt4 REQUIRED )
INCLUDE_DIRECTORIES( ${QT_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR})

FIND_PACKAGE( OpenCV REQUIRED )

#set up optional include packages here:
SET( QT_USE_OPENGL TRUE )

# .cxx sources
SET( DETECT_SRCS_CXX main.cpp myclass.cpp mainwindow.cpp)

# files which need to be moc'd by Qt
SET( DETECT_MOC_SRCS myclass.h mainwindow.h )

SET( DETECT_UI mainwindow.ui )

# build ui_XXX files from the XML-style .ui files
QT4_WRAP_UI( DETECT_SRCS_CXX ${DETECT_UI})

# this moc's the above variable and appends to the cxx sources
QT4_WRAP_CPP( DETECT_SRCS_CXX ${DETECT_MOC_SRCS})

ADD_EXECUTABLE( detect ${DETECT_SRCS_CXX} ${DETECT_MOC_SRCS} ${DETECT_UI} )

TARGET_LINK_LIBRARIES( detect ${OpenCV_LIBS} ${QT_LIBRARIES} )


When I compile, I get undefined reference to QApplication, QObject, and pretty much all the other QT classes. What am I doing wrong?

-Greg

gmiller39
7th July 2010, 16:32
I have solved my issue. Apparently when you install Qt through the .bin file they have on the site it does not change your path, which cmake relies on but QtCreator does not. To do this, edit your bash.bashrc file with superuser permission and add the following lines to the end of the file:


export QTDIR=/opt/qtsdk-2010.04/qt
export PATH=$QTDIR/bin:$PATH
export LD_LIBRARY_PATH=$QTDIR/lib:$LD_LIBRARY_PATH

If you use gnome, use: sudo gedit /etc/bash.bashrc, for KDE I think you would use: sudo kate /etc/bash.bashrc
Also you may have to modify line 1 to match your own QT installation directory.

Modify the following file for your CMakeLists.txt project:


# You must set the minimum version for CMake
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)

# This is the name of your project:
PROJECT(ObjDetect_proj)

SET(CMAKE_BUILD_TYPE Release)
SET(CMAKE_CXX_FLAGS "-Wall")

# Use this for additional packages you need support for. In this case, I am using OpenCV.
# Consult CMake documentation to see which packages are supported and what the command is.
FIND_PACKAGE( OpenCV REQUIRED )

# QT4 Handling
FIND_PACKAGE(Qt4 REQUIRED)
INCLUDE_DIRECTORIES( ${QT_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR})

INCLUDE(${QT_USE_FILE})

# Change these to all your header files
SET( HWQ_Qt4_SRC
src/MainWindow.h
src/ObjDetect.h
)
# Change this to all your .ui designer files
SET( HWQ_Qt4_UI
src/MainWindow.ui
)
# Add resource files if you have them.
SET( HWQ_Qt4_RES
)

# This runs the QT MOC
QT4_WRAP_CPP(HWQ_MOC_CPP ${HWQ_Qt4_SRC})
# This creates your ui_whatever.h files
QT4_WRAP_UI(HWQ_UI_CPP ${HWQ_Qt4_UI})
QT4_ADD_RESOURCES(HWQ_RES_H ${HWQ_Qt4_RES})

INCLUDE_DIRECTORIES( . )

# Put your cpp files here
SET( HWQ_SRC
src/main.cpp
src/MainWindow.cpp
src/ObjDetect.cpp
${HWQ_MOC_CPP}
${HWQ_UI_CPP}
${HWQ_RES_H}
)

SET( HWQ_LIB
${QT_LIBRARIES}
)

ADD_EXECUTABLE(ObjDetect ${HWQ_SRC} )
TARGET_LINK_LIBRARIES(ObjDetect ${HWQ_LIB} ${OpenCV_LIBS} )

# I think this line only gets called when you run 'make install'
INSTALL_TARGETS( /bin Objdetect)


Now I don't know why exactly, but when you run CMake from Qt Creator, it screws up. You have to go to the command line first and run
cmake . After you run this initially, you can open the CMakeLists.txt file with QtCreator and it will open your whole project and build and link correctly.

I hope this helps some other poor sucker trying to figure this stuff out.

-Greg