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.
In this post, we will provide a bash script for installing OpenCV-4.0 (C++ and Python 3.6) on Ubuntu 18.04. 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.
Install OpenCV on Windows for CPP
Step 0: Select OpenCV version to install
echo "OpenCV installation by learnOpenCV.com"
# Define OpenCV Version to install
cvVersion="master"
build
directories and create installation
directory. # Clean build directories
rm -rf opencv/build
rm -rf opencv_contrib/build
# Create directory for installation
mkdir installation
mkdir installation/OpenCV-"$cvVersion"
Finally, we will be storing the current working directory in cwd
variable. We are also going to refer to this directory as OpenCV_Home_Dir throughout this blog.
# Save current working directory
cwd=$(pwd)
Step 1: Update Packages
sudo apt -y update
sudo apt -y upgrade
Step 2: Install OS Libraries
sudo apt -y remove x264 libx264-dev
## Install dependencies
sudo apt -y install build-essential checkinstall cmake pkg-config yasm
sudo apt -y install git gfortran
sudo apt -y install libjpeg8-dev libpng-dev
sudo apt -y install software-properties-common
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt -y update
sudo apt -y install libjasper1
sudo apt -y install libtiff-dev
sudo apt -y install libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev
sudo apt -y install libxine2-dev libv4l-dev
cd /usr/include/linux
sudo ln -s -f ../libv4l1-videodev.h videodev.h
cd "$cwd"
sudo apt -y install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
sudo apt -y install libgtk2.0-dev libtbb-dev qt5-default
sudo apt -y install libatlas-base-dev
sudo apt -y install libfaac-dev libmp3lame-dev libtheora-dev
sudo apt -y install libvorbis-dev libxvidcore-dev
sudo apt -y install libopencore-amrnb-dev libopencore-amrwb-dev
sudo apt -y install libavresample-dev
sudo apt -y install x264 v4l-utils
# Optional dependencies
sudo apt -y install libprotobuf-dev protobuf-compiler
sudo apt -y install libgoogle-glog-dev libgflags-dev
sudo apt -y install libgphoto2-dev libeigen3-dev libhdf5-dev doxygen
Step 3: Install Python Libraries
sudo apt -y install python3-dev python3-pip
sudo -H pip3 install -U pip numpy
sudo apt -y install python3-testresources
virtualenv
and virtualenvwrapper
modules to create Python virtual environments. cd $cwd
############ For Python 3 ############
# create virtual environment
python3 -m venv OpenCV-"$cvVersion"-py3
echo "# Virtual Environment Wrapper" >> ~/.bashrc
echo "alias workoncv-$cvVersion=\"source $cwd/OpenCV-$cvVersion-py3/bin/activate\"" >> ~/.bashrc
source "$cwd"/OpenCV-"$cvVersion"-py3/bin/activate
# now install python libraries within this virtual environment
pip install wheel numpy scipy matplotlib scikit-image scikit-learn ipython dlib
# quit virtual environment
deactivate
[/bash]
<div class="download-code">
<strong> Download Installation Script</strong>
To easily follow along this tutorial, please download installation script by clicking on the button below. It's FREE!
<div style="text-align: center;"><a class="button small loblue" style="text-decoration: none;" href="https://bigvisionllc.lpages.co/leadbox/143948b73f72a2%3A173c9390c346dc/5649050225344512/" target="_blank" rel="noopener">Download Installation Script </a></div>
</div>
<h2>Step 4: Download opencv and opencv_contrib</h2>
git clone https://github.com/opencv/opencv.git
cd opencv
git checkout $cvVersion
cd ..
git clone https://github.com/opencv/opencv_contrib.git
cd opencv_contrib
git checkout $cvVersion
cd ..
Step 4: Download opencv and opencv_contrib
git clone https://github.com/opencv/opencv.git
cd opencv
git checkout $cvVersion
cd ..
git clone https://github.com/opencv/opencv_contrib.git
cd opencv_contrib
git checkout $cvVersion
cd ..
Step 5: Compile and install OpenCV with contrib modules
First we navigate to the build directory
cd opencv
mkdir build
cd build
Next, we start the compilation and installation process.
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_PYTHON3_INSTALL_PATH=$cwd/OpenCV-$cvVersion-py3/lib/python3.5/site-packages \
-D WITH_QT=ON \
-D WITH_OPENGL=ON \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
-D BUILD_EXAMPLES=ON ..
make -j4
make install
2. How to use OpenCV in C++
cmake_minimum_required(VERSION 3.1)
# Enable C++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
The basic stavructure of your CMakeLists.txt will be as follows:
SET(OpenCV_DIR /installation/OpenCV-master/lib/cmake/opencv4)
Make sure that you replace OpenCV_Home_Dir with correct path. For example, in my case:
SET(OpenCV_DIR /home/hp/OpenCV_installation/installation/OpenCV-master/lib/cmake/opencv4)
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.
3. How to use OpenCV in Python
workon OpenCV-master-py3
Once you have activated the virtual environment, you can enter Python shell and test OpenCV version.
ipython
import cv2
print(cv2.__version__)
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.
Hi, thank you a lot for the tutorial. I have some problem when i want to excute a small program that use imshow. I have the following error when i try to excute the program:
QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to ‘/tmp/runtime-root’
qt.qpa.screen: QXcbConnection: Could not connect to display
Could not connect to any X display.
Any idea whaqt this does mean
Thank’s
Hi Assil
Thank you for the kind words.
Are you running this on a server by any chance or any sort of headless installation?
Can you try this and see if it works:
export DISPLAY=:0
Thanks
Hi vishwesh,
i use a docker image
Thank’s in advanced
Assil
Hi Assil
For docker you need to have DISPLAY variable defined. You also need to pass -x while running docker instance.
Please refer to the following blog:
https://learnopencv.com/install-opencv-docker-image-ubuntu-macos-windows/
Hi. I think there’s a bug in installOpenCV-18-04.sh. You overwrite cvVersionChoice, so 3.4.1 is always installed.
I have created a pull request for the same. It will be fixed soon.
Thought I would try this script one line at a time. During the build I get the following output:
[ 70%] Building CXX object modules/datasets/CMakeFiles/example_datasets_is_bsds.dir/samples/is_bsds.cpp.o
[ 70%] Building CXX object modules/calib3d/CMakeFiles/opencv_test_calib3d.dir/test/test_modelest.cpp.o
/devel/software/opencv_contrib/modules/cvv/src/qtutil/filter/diffFilterWidget.cpp: In member function ‘virtual void cvv::qtutil::DiffFilterFunction::applyFilter(cvv::qtutil::DiffFilterFunction::InputArray, cvv::qtutil::DiffFilterFunction::OutputArray) const’:
/devel/software/opencv_contrib/modules/cvv/src/qtutil/filter/diffFilterWidget.cpp:72:44: error: ‘COLOR_BGR2HSV’ was not declared in this scope
cv::cvtColor(in.at(0).get(), originalHSV, COLOR_BGR2HSV);
^~~~~~~~~~~~~
/devel/software/opencv_contrib/modules/cvv/src/qtutil/filter/diffFilterWidget.cpp:72:44: note: suggested alternative:
In file included from /devel/software/opencv_contrib/modules/cvv/src/qtutil/filter/diffFilterWidget.cpp:2:0:
/devel/software/opencv/modules/imgproc/include/opencv2/imgproc.hpp:579:5: note: ‘COLOR_BGR2HSV’
COLOR_BGR2HSV = 40, //!< convert RGB/BGR to HSV (hue saturation value), @ref color_convert_rgb_hsv "color conversions"
^~~~~~~~~~~~~
[ 70%] Building CXX object modules/calib3d/CMakeFiles/opencv_test_calib3d.dir/test/test_posit.cpp.o
modules/cvv/CMakeFiles/opencv_cvv.dir/build.make:686: recipe for target 'modules/cvv/CMakeFiles/opencv_cvv.dir/src/qtutil/filter/diffFilterWidget.cpp.o' failed
make[2]: *** [modules/cvv/CMakeFiles/opencv_cvv.dir/src/qtutil/filter/diffFilterWidget.cpp.o] Error 1
CMakeFiles/Makefile2:12756: recipe for target 'modules/cvv/CMakeFiles/opencv_cvv.dir/all' failed
make[1]: *** [modules/cvv/CMakeFiles/opencv_cvv.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs….
[ 70%] Building CXX object modules/calib3d/CMakeFiles/opencv_test_calib3d.dir/test/test_reproject_image_to_3d.cpp.o
[ 70%] Building CXX object modules/ccalib/CMakeFiles/opencv_ccalib.dir/src/omnidir.cpp.o
[ 70%] Linking CXX executable ../../bin/example_datasets_is_bsds
[ 70%] Building CXX object modules/calib3d/CMakeFiles/opencv_test_calib3d.dir/test/test_solvepnp_ransac.cpp.o
[ 70%] Built target example_datasets_is_bsds
[ 70%] Building CXX object modules/ccalib/CMakeFiles/opencv_ccalib.dir/src/randpattern.cpp.o
[ 70%] Building CXX object modules/calib3d/CMakeFiles/opencv_test_calib3d.dir/test/test_stereomatching.cpp.o
[ 70%] Building CXX object modules/calib3d/CMakeFiles/opencv_test_calib3d.dir/test/test_undistort.cpp.o
[ 70%] Building CXX object modules/calib3d/CMakeFiles/opencv_test_calib3d.dir/test/test_undistort_badarg.cpp.o
[ 70%] Building CXX object modules/calib3d/CMakeFiles/opencv_test_calib3d.dir/test/test_undistort_points.cpp.o
[ 70%] Linking CXX executable ../../bin/opencv_test_calib3d
[ 70%] Built target opencv_test_calib3d
[ 70%] Linking CXX shared library ../../lib/libopencv_ccalib.so
[ 70%] Built target opencv_ccalib
Makefile:162: recipe for target 'all' failed
make: *** [all] Error 2
Seems like a rather odd error. So I checked the CMakeError.Log and found this:
Run Build Command:"/usr/bin/make" "cmTC_9e0e3/fast"
/usr/bin/make -f CMakeFiles/cmTC_9e0e3.dir/build.make CMakeFiles/cmTC_9e0e3.dir/build
make[1]: Entering directory '/devel/software/opencv/build/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_9e0e3.dir/CheckIncludeFile.c.o
/usr/bin/cc -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG -fPIE -o CMakeFiles/cmTC_9e0e3.dir/CheckIncludeFile.c.o -c /devel/software/opencv/build/CMakeFiles/CMakeTmp/CheckIncludeFile.c
/devel/software/opencv/build/CMakeFiles/CMakeTmp/CheckIncludeFile.c:1:10: fatal error: sys/videoio.h: No such file or directory
#include
^~~~~~~~~~~~~~~
compilation terminated.
CMakeFiles/cmTC_9e0e3.dir/build.make:65: recipe for target ‘CMakeFiles/cmTC_9e0e3.dir/CheckIncludeFile.c.o’ failed
make[1]: *** [CMakeFiles/cmTC_9e0e3.dir/CheckIncludeFile.c.o] Error 1
make[1]: Leaving directory ‘/devel/software/opencv/build/CMakeFiles/CMakeTmp’
Makefile:126: recipe for target ‘cmTC_9e0e3/fast’ failed
make: *** [cmTC_9e0e3/fast] Error 2
and this
Determining if the CL_VERSION_2_2 exist failed with the following output:
Change Dir: /devel/software/opencv/build/CMakeFiles/CMakeTmp
Run Build Command:”/usr/bin/make” “cmTC_49d82/fast”
/usr/bin/make -f CMakeFiles/cmTC_49d82.dir/build.make CMakeFiles/cmTC_49d82.dir/build
make[1]: Entering directory ‘/devel/software/opencv/build/CMakeFiles/CMakeTmp’
Building C object CMakeFiles/cmTC_49d82.dir/CheckSymbolExists.c.o
/usr/bin/cc -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG -fPIE -o CMakeFiles/cmTC_49d82.dir/CheckSymbolExists.c.o -c /devel/software/opencv/build/CMakeFiles/CMakeTmp/CheckSymbolExists.c
/devel/software/opencv/build/CMakeFiles/CMakeTmp/CheckSymbolExists.c:2:10: fatal error: OpenCL_INCLUDE_DIR-NOTFOUND/CL/cl.h: No such file or directory
#include
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
CMakeFiles/cmTC_49d82.dir/build.make:65: recipe for target ‘CMakeFiles/cmTC_49d82.dir/CheckSymbolExists.c.o’ failed
multiple times. I am having trouble seeing what what is in the Error log has to do with the error on the output.
Really could use a hand here.
OS Ubuntu server 18.04
Cuda 10.0
dLib 19.16
gcc (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0
OpenCV Master (4.0)
Thanks!
I used the steps to build the OpenCV-4.0.0 release candidate. Your cmake line contains WITH_QT=ON. I found that I had to add a cv:: to Line 72 and Line 73 in
opencv_contrib-4.0.0/modules/cvv/src/qtutil/filter/diffFilterWidget.cpp.
The lines should read:
cv::cvtColor(in.at(0).get(), originalHSV, cv::COLOR_BGR2HSV);
cv::cvtColor(in.at(1).get(), filteredHSV, cv::COLOR_BGR2HSV);
Hi!
Please create a PR for the same in OpenCV github repo.
Hi. I think I finished installing everything going according to the bash script.
But when I try to run the sample c++ code, it returns these errors.
##code
##GNU nano 2.9.3 main.cpp
##
#include
#include
int main( int argc, char** argv ) {
cv::Mat image;
image = cv::imread(“sample.jpg” , CV_LOAD_IMAGE_COLOR);
if(! image.data ) {
std::cout << "Could not open or find the image" << std::endl ;
return -1;
}
cv::namedWindow( "Display window", cv::WINDOW_AUTOSIZE );
cv::imshow( "Display window", image );
cv::waitKey(0);
return 0;
}
Output:
$ g++ `pkg-config –cflags –libs /installation/OpenCV-3.4.1/lib/pkgconfig/opencv.pc` main.cpp -o main
bash: /installation/OpenCV-3.4.1/lib/pkgconfig/opencv.pc: No such file or directory
main.cpp:1:10: fatal error: opencv/highgui.hpp: No such file or directory
#include
^~~~~~~~~~~~~~~~~~~~
compilation terminated.
I followed the same procedure, after running this its showing no such file or directory.
should i need to create 1. please help me with this
I’m new to ubuntu and opencv
Well, yes but one could try this (python): sudo pip install opencv-contrib-python
It may help 😉
Hi, thank you. I want to use Mosse Tracking. But the compiler cannot find the tracking.hpp.