[macOS Mojave 10.14.3] Set up environment for Opencv4.0.1 C++ development

Nhan Cao
Nhan Cao
Mar 31, 2019 · 4 min read

1. Install Xcode v10.2

2. Install Command Line Tools

xcode-select --install

3. Install homebrew

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

4. Install OpenCV

brew install opencv

OpenCV located at “/usr/local/Cellar/opencv/”

  • To remove
brew rm opencv
  • To update all installed packages
brew update && brew upgrade && brew cleanup

5. Install pkg-config

brew install pkg-config

pkg-config located at /usr/local/lib/pkgconfig/ where containing symlink to package, checking opencv4.pc exist or not.

If opencv4.pc does not exist, make a symlink:

  • Put to ./bash_profile
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/
  • Make a symlink
ln -s /usr/local/Cellar/opencv/4.0.1/lib/pkgconfig/opencv4.pc $PKG_CONFIG_PATH

6. Check OpenCV linker flags

pkg-config --cflags --libs opencv4

Look like this:

-I/usr/local/Cellar/opencv/4.0.1/include/opencv4/opencv -I/usr/local/Cellar/opencv/4.0.1/include/opencv4 -L/usr/local/Cellar/opencv/4.0.1/lib -lopencv_gapi -lopencv_stitching -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dpm -lopencv_face -lopencv_fuzzy -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_dnn -lopencv_plot -lopencv_videostab -lopencv_video -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core
  • If you get an error, then change opencv4 to opencv
pkg-config --cflags --libs opencv

- If still error, you might need to specify the location of opencv.pc file

pkg-config --cflags --libs path/to/opencv.pc

Ex:

pkg-config --cflags --libs /usr/local/Cellar/opencv/4.0.1/lib/pkgconfig/opencv4.pc

7. Test OpenCV compilation with simple code

  • Download lena.jpg on the internet and put it to project, place it to the same with the source file.
lena.jpg
  • Make try.cpp source file
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main()
{
Mat img = imread("lena.jpg");
namedWindow("image", WINDOW_NORMAL);
imshow("image", img);
waitKey(0);
return 0;
}
  • Run command to compile
g++ $(pkg-config --cflags --libs opencv4) -std=c++11 try.cpp -o try

or

g++ $(pkg-config --cflags --libs /usr/local/Cellar/opencv/4.0.1/lib/pkgconfig/opencv4.pc) -std=c++11 try.cpp -o try
  • Run your program
./try

Done all, go to Xcode part


  • Open Xcode -> New Project -> macOS -> Command Line Tool -> Next -> Select language C++

Run the project and get Hello world

  • Add lena.jpg to project

— Copy lena.jpg to source files folder

— Edit project running scheme -> Run -> Options -> Check on Working Directory to Use custom working directory with the path of current source files folder. -> Close

Edit scheme
  • Select Project -> Build Settings ->

— Header Search Paths: /usr/local/Cellar/opencv/4.0.1/include (recursive)

— Library Search Paths: /usr/local/Cellar/opencv/4.0.1/lib (recursive)

— Other Linker Flags: Insert all content from ‘pkg-config — cflags — libs opencv4’

Content like:

-I/usr/local/Cellar/opencv/4.0.1/include/opencv4/opencv -I/usr/local/Cellar/opencv/4.0.1/include/opencv4 -L/usr/local/Cellar/opencv/4.0.1/lib -lopencv_gapi -lopencv_stitching -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dpm -lopencv_face -lopencv_fuzzy -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_dnn -lopencv_plot -lopencv_videostab -lopencv_video -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core

After you paste and enter.

— Update main.cpp file

#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main()
{
Mat img = imread("lena.jpg");
namedWindow("image", WINDOW_NORMAL);
imshow("image", img);
waitKey(0);
return 0;
}

— Build and get success

beesightsoft

Founded since 2011 by young and passionate technologists. Beesight Soft has started by innovative passion "together makes perfect". We stay up late together to ensure project deadline. And we are a family.

Nhan Cao

Written by

Nhan Cao

nhancv.com

beesightsoft

Founded since 2011 by young and passionate technologists. Beesight Soft has started by innovative passion "together makes perfect". We stay up late together to ensure project deadline. And we are a family.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade