OpenCV released OpenCV-3.4.4 and OpenCV-4.0.0 on 20th November. There have been a lot of bug fixes and other changes in these versions. The release highlights are as follows:
- OpenCV is now C++11 library and requires C++11-compliant compiler. Minimum required CMake version has been raised to 3.5.1.
- A lot of C API from OpenCV 1.x has been removed.
- Persistence (storing and loading structured data to/from XML, YAML or JSON) in the core module has been completely reimplemented in C++ and lost the C API as well.
- New module G-API has been added, it acts as an engine for very efficient graph-based image procesing pipelines.
- dnn module now includes experimental Vulkan backend and supports networks in ONNX format.
- The popular Kinect Fusion algorithm has been implemented and optimized for CPU and GPU (OpenCL)
QR code detector and decoder have been added to the objdetect module.
- Very efficient and yet high-quality DIS dense optical flow algorithm has been moved from opencv_contrib to the video module.
Master Generative AI for CV
In this post, we will provide a bash script for installing OpenCV-3.4.4 (C++ and Python 3.7) on macOS – High Sierra and Mojave. We will also briefly study the script to understand what’s going in it. Note that this script will install OpenCV in a local directory and not on the entire system. Let’s jump in 🙂
1. Install XCode
Install XCode from App Store.
If XCode available on App Store is not compatible with your OS:
- Find XCode version compatible to your OS from this table https://en.wikipedia.org/w/index.php?title=Xcode#Version_comparison_table
- Go to this webpage https://developer.apple.com/download/more/
- Login if you have apple developer account else create your account and login.
- Search for xcode and download the version compatible to your OS.
- Install XCode.
- After installation open XCode, and accept xcode-build license when it asks.
2. Install OpenCV
Now that XCode has been installed, we will move on to OpenCV installation.
First, let’s install Homebrew.
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" brew update
We will also add Homebrew to PATH.
# Add Homebrew path in PATH echo "# Homebrew" >> ~/.bash_profile echo "export PATH=/usr/local/bin:$PATH" >> ~/.bash_profile
Next we will install the requirements – Python 3, CMake and Qt 5.
brew install python3 brew install cmake brew install qt5 QT5PATH=/usr/local/Cellar/qt/5.11.2_1
We will also save current working directory in cwd variable and OpenCV version (3.4.4) in cvVersion.
cwd=$(pwd) cvVersion="3.4.4" # Clean build directories rm -rf opencv/build rm -rf opencv_contrib/build # Create directory for installation mkdir installation mkdir installation/OpenCV-"$cvVersion"
Now, let’s install the Python libraries and create the Python environment.
sudo -H pip3 install -U pip numpy # Install virtual environment sudo -H python3 -m pip install virtualenv virtualenvwrapper VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3 echo "VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3" >> ~/.bash_profile echo "# Virtual Environment Wrapper" >> ~/.bash_profile echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bash_profile cd $cwd source /usr/local/bin/virtualenvwrapper.sh ############ For Python 3 ############ # create virtual environment mkvirtualenv OpenCV-"$cvVersion"-py3 -p python3 workon OpenCV-"$cvVersion"-py3 # now install python libraries within this virtual environment pip install cmake numpy scipy matplotlib scikit-image scikit-learn ipython dlib # quit virtual environment deactivate ######################################
If you are solely a Python user, it is easier to use the following and skip the next steps
pip install opencv-contrib-python==18.104.22.168
Next, let’s clone the OpenCV github repositories.
git clone https://github.com/opencv/opencv.git cd opencv git checkout 3.4 cd .. git clone https://github.com/opencv/opencv_contrib.git cd opencv_contrib git checkout 3.4 cd .. cd opencv mkdir build cd build
Finally, we will use CMake to build OpenCV.
cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=$cwd/installation/OpenCV-"$cvVersion" \ -D INSTALL_C_EXAMPLES=ON \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D WITH_TBB=ON \ -D WITH_V4L=ON \ -D OPENCV_SKIP_PYTHON_LOADER=ON \ -D CMAKE_PREFIX_PATH=$QT5PATH \ -D CMAKE_MODULE_PATH="$QT5PATH"/lib/cmake \ -D OPENCV_PYTHON3_INSTALL_PATH=~/.virtualenvs/OpenCV-"$cvVersion"-py3/lib/python3.7/site-packages \ -D WITH_QT=ON \ -D WITH_OPENGL=ON \ -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \ -D BUILD_EXAMPLES=ON .. make -j$(sysctl -n hw.physicalcpu) make install cd $cwd
And that’s it! By now you should have OpenCV installed successfully in your system.
3. Test OpenCV Installation
3.1. OpenCV in Python
To use cv2 module in Python, we will first activate the Python environment.
Next, let’s import the module and verify the OpenCV Version installed.
import cv2 cv2.__version__
3.2. OpenCV in C++
To use OpenCV in C++, we can simply use CMakeLists.txt and specify the OpenCV_DIR variable. The format is as follows:
Make sure that you replace OpenCV_Home_Dir with correct path. For example, in my case:
Once you have made your CMakeLists.txt, follow the steps given below.
mkdir build && cd build cmake .. cmake --build . --config Release
This will generate your executable file in build directory.
Hope this script proves to be useful for you :). Stay tuned for more interesting stuff. In case of any queries, feel free to comment below and we will get back to you as soon as possible.