Configuring Qt for OpenCV on OSX

By | June 8, 2016

In this tutorial we will learn how to configure Qt to use OpenCV. Although the tutorial is targeted for OSX users, you can modify my suggestions for use in Linux and Windows. I assume you have a working knowledge of Qt and you have at least built a “hello world” application using it.

Of the few different ways of configuring Qt for OpenCV, we will use the one that involves pkg-config.

I have created a simple Qt application that uses OpenCV to capture frames from a webcam and display them using QGraphicsView. A screenshot is shown below.

Qt / OpenCV Application Screenshot

To get instant access the code for the application please subscribe to our weekly newsletter here

Install OpenCV

For this tutorial I am assuming you have installed OpenCV 2 or OpenCV 3. If you have not, you can install them using Homebrew. The basic installation commands are shown below.

Install OpenCV 2.4.x on OSX using Homebrew

brew tap homebrew/science
brew install opencv

Install OpenCV 3 on OSX using Homebrew

brew tap homebrew/science
brew install opencv3

You can find detailed instructions for installing OpenCV using Homebrew by clicking here. Using OpenCV 3 in a Qt application is a bit tricky for OSX because opencv and opencv3 packages contain the same libraries and so opencv3 is not deployed to /usr/local/lib like other packages. I will help you navigate through these complexities.

Install Qt Creator

You can download Qt Creator and follow the onscreen instructions for installing it. There are some restrictions on using Qt in a commercial application and you should make sure you know about licensing issues. You may find this discussion helpful.

BuildĀ settings for Qt / OpenCV project

The very first thing to do is instruct Qt where to find pkg-config. The default location for pkg-config is /usr/local/bin.

 
which pkg-config 
# returns /usr/local/bin/pkg-config

We need to add /usr/local/bin to PATH. Go to Project, expand Build Environment and add /usr/local/bin to PATH. Don’t forget to add a colon (:) before appending /usr/local/bin! See the screenshot below.

Qt build settings for OpenCV

Qt build settings for OpenCV

If you are using OpenCV 3 you also need to add a new variable called PKG_CONFIG_PATH and set it to the directory that contains opencv.pc for your OpenCV 3 installation. You can find it using the following command

find /usr/local -name "opencv.pc"
# /usr/local/Cellar/opencv3/3.1.0_3/lib/pkgconfig/opencv.pc

You may not need the above step for OpenCV 2.4.x.

How to modify Qt project file ( .pro ) for OpenCV

Now that the paths are correctly set up, we need to add a few lines to our project file (.pro) to tell qmake to use pkg-config for OpenCV.

 
# The following lines tells Qmake to use pkg-config for opencv
QT_CONFIG -= no-pkg-config
CONFIG  += link_pkgconfig
PKGCONFIG += opencv

If you are using OpenCV 3, the compiler may complain

ld: library not found for -lippicv

How to fix compiler error “ld: library not found for -lippicv”

The ippicv library the compiler is complaining about is actually included in your OpenCV installation, but it is not in the right path. Let’s first locate the library

 
find /usr/local/Cellar -name "libippicv*"
# /usr/local/Cellar/opencv3/3.1.0_3/share/OpenCV/3rdparty/lib/libippicv.a

You can tell the compiler where to look for libippicv.a by adding the following line to the .pro file

 
LIBS += -L/usr/local/Cellar/opencv3/3.1.0_3/share/OpenCV/3rdparty/lib/

Alternatively, you can symlink libippicv.a to /usr/local/lib using the following command on the terminal

ln -s /usr/local/Cellar/opencv3/3.1.0_3/share/OpenCV/3rdparty/lib/libippicv.a /usr/local/lib

and then add the following to the .pro file

 
LIBS += -L/usr/local/lib/
# See cautionary note below 

Caution : When both opencv and opencv3 packages are installed

When both opencv and opencv3 packages are installed, and you are using opencv3 in your Qt application, adding /usr/local/lib to your path ( as shown in the previous section ) can lead to linking errors. The best thing to do in such a case is unlink opencv.

brew unlink opencv 

I have spent an embarrassing amount of time debugging the above problem in El Capitan. It does not show up on Yosemite.

Runtime error on OSX El Capitan

You may receive a runtime error on El Capitan. Note the actual error message may vary sometimes.

 
dyld: Symbol not found: __cg_jpeg_resync_to_restart
  Referenced from: /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO
  Expected in: /usr/local/lib/libJPEG.dylib
 in /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO
The program has unexpectedly finished.

To fix it go to Project -> Run -> Run Environment -> Unset DYLD_LIBRARY_PATH. See image below.

Qt Opencv El Capitan- Runtime Error

That’s all you need to know to build a Qt application with OpenCV, but let me throw in another goodie.

How to convert OpenCV Mat to QImage

The following piece of code converts OpenCV Mat to QImage. This will come in handy when you build your first Qt based OpenCV application.

# Create an OpenCV image. 
Mat image(320, 240, CV_8UC3, Scalar(0,0,0));

# Convert it to QImage 
QImage qImage = QImage((const unsigned char*)(image.data), 
                        image.cols,image.rows, 
                        QImage::Format_RGB888).rgbSwapped());

Subscribe & Download Code

If you liked this article and would like to download code, please subscribe to our newsletter. You will also receive a free Computer Vision Resource guide. In our newsletter we share OpenCV tutorials and examples written in C++/Python, and Computer Vision and Machine Learning algorithms and news.

Subscribe Now

Category: Compile how-to Install OpenCV 3 Tutorial Tags: , ,

About Satya Mallick

I am an entrepreneur with a love for Computer Vision and Machine Learning with a dozen years of experience (and a Ph.D.) in the field. In 2007, right after finishing my Ph.D., I co-founded TAAZ Inc. with my advisor Dr. David Kriegman and Kevin Barnes. The scalability, and robustness of our computer vision and machine learning algorithms have been put to rigorous test by more than 100M users who have tried our products.

  • Logic

    I have encountered this problem : “dyld: Symbol not found: __cg_jpeg_resync_to_restart”, and solved this problem by the method above.
    But when I running it in debugging mode, it receive the same runtime error. How can I solve this problem? Thank you for your selfless help!

    • mvh

      Did you solve the problem when running in debugging mode?

  • s ss

    Hi ,Tnx for the video,I wrote all that you said in .pro and I added #include
    cv::Mat inputImage = cv::imread(“address of my pic”);
    cv::imshow(“Display Image”,inputImage);
    cv:waitkey(5000); To my mainwindow.cpp when I push build button I get many errors what should I do?

  • mvh

    Thank you for this post. It saved me a lot of time. In my case, the paths were different. Maybe because i’m using MacPorts instead of homebrew.
    The difference: /opt/local/bin vs /usr/local/bin

    • Thanks. Yes in case of MacPorts the path will be different. Thanks for pointing it out.

  • mvh

    I have problems when i want to debug my application. When debugging, the dynamic libs can’t be load. Can this be because the DYLD_LIBRARY_PATH is unset?

    • Thanks a bunch. I am sure someone will greatly benefit from this comment.

  • euseila Qualeh

    Hi I have installed the opencv3 using homebrew and qt5.8 in a macOS Sierra. Its very strange cause when I try to use the wich command to find pkg-config I have no answer …, so I try to use the path that you suggest in the video but if I try to build the project I found the error “opencv development package not found”.

    • Are you sure OpenCV3 is linked ? By default it is not. You can link it by using

      brew link opencv3 --force

      • euseila Qualeh

        Yeh, the error persists..

      • euseila Qualeh

        The process “/Users/charlie/Qt5.8.0/5.8/clang_64/bin/qmake” exited with code 3.

        Error while building/deploying project CameraTest (kit: Desktop Qt 5.8.0 clang 64bit)

        When executing step “qmake”

      • euseila Qualeh

        I find the error, I just forget a / in the build environment!!

        • Yay! Thanks for letting us know. I am sure someone in future will save a lot of time because of this thread.

      • euseila Qualeh

        I already subscribe for the OpenCV books and this script and do note have any answers..

  • SCY

    Hi, I’m trying to follow this post with the example code downloaded and the camviewer widget is not showing under the central widget. Any explanation for this? Thanks.

    • Sorry for the late reply. I can’t think of an explanation. I hope you were able to solve it.

  • Arqam Ahmad

    I am using openCV3 but I didn’t get the error `ld: library not found for -lippicv`

    May be its not needed now.

    • Then you are all good. It depends on your installation.

  • Carlos Vidrios

    Hi, great tutorial, I finally could installed opencv with QT thanks to this tutorial. I have a question, How can I get the code from the example that is in the video? I am already subscribed to learnopencv.com

    • Thanks. When you subscribe, we send send you access to all code in this blog including this post.

      • Carlos Vidrios

        Thanks