current position:Home>Manually compile opencv on MacOS and Linux and add it to Python / C + + / Java as a dependency

Manually compile opencv on MacOS and Linux and add it to Python / C + + / Java as a dependency

2022-01-31 04:53:03 Mintimate

「 This is my participation 11 The fourth of the yuegengwen challenge 1 God , Check out the activity details :2021 One last more challenge 」.


OpenCV The full name is Open Source Computer Vision Library, It's a cross platform computer vision library . OpenCV It is also widely used in image recognition 、 Picture processing and other scenes , go by the name of “ Computer vision ”, This tutorial , I'll teach you a variety of ways to deploy OpenCV Ow .

Use scenarios

OpenCV There are a lot of usage scenarios , Including but not limited to :

  • Augmented reality
  • Face recognition
  • Gesture recognition
  • human-computer interaction
  • Action recognition
  • Motion tracking
  • Object recognition
  • Image segmentation

Specific use

OpenCV It's really just a C++ The library of , however Python and Java There is also an interface that can be called . This tutorial , Let's compile and install openCV Of C++ library , You can also according to your own needs , Choose whether to compile Python and Java The interface of .

Python Inside , You can actually use pip Directly installed ow . I won't cover it in this article , It mainly explains manual compilation and configuration .

This demonstration

This demonstration , Is in macOS and Linux Compile and install OpenCV,Windows Not in the scope of this tutorial .

Linux On , Use Debian The server , Whether it's CentOS still Debian Can be installed , But it's best to update the software source in advance :

# Debian/Ubuntu
apt update 
apt upgrade
# CentOS
yum update
yum upgrade
 Copy code 

Environment depends on

compile OpenCV You need to add some dependencies , Otherwise, you cannot compile or Cmkae Automatic configuration . Here I briefly divide it into necessary dependency and module dependency :

  • Necessary dependence : compile OpenCV The dependence that must be needed when .
  • Module dependency : compile OpenCV When , The dependence of choice ; Such as : Need to be right Java Provide support , Need to install ant.

Some optional support , such as :libpng package 、libjepg Bags, etc , There is no detailed explanation here. Ow .

Necessary dependence

The necessary dependencies here are GCC and Cmake, Otherwise, you cannot compile . stay Linux Installation on GCC and Cmake, Depending on the release :

# Debian/Ubuntu edition 
sudo apt-get install build-essential
sudo apt-get install cmake

# CentOS Release version 
sudo yum install gcc gcc-c++
sudo yum install cmake
 Copy code 

Such as : Using the server's CentOS Mirror image :

CentOS Mirror image

about macOS, It's also very simple. , Give me two ways :

  • recommend In the installation Homebrew Under the circumstances , install cmake that will do .
  • install Xcode or XcodeXcode Command Line Tools( So there is GCC), After the installation cmake GUI, And configuration cmake GUI Of cmake Enter environment variables .

 Installed GCC and Cmake

Module dependency

( I wanted to finish it in a small Directory . After writing it , I still think it's easy to say , The following steps ……)

Module dependency , Is that you need to OpenCV As a dependency , Add to the specific language development environment . give an example :

  • Python2&Python3: You need to specify the Python Installation path for , And in the system variable , To specify lib Address .
  • Java: Need to specify JAVA_HOME, need ANT Support for .

For its function , Some specific dependencies will not be repeated .( such as :ffmpeg)

OpenCV Source code

We enter OpenCV Its official website, find Source Forge Options :

Source Forge

And download the source code . You can also go directly to GitHub Download the source code or historical version 、 label .

We need to pay attention to ️: download GitHub On OpenCV Very slowly , You can try Gitee Mirror image , Here's a demonstration , Download from the server 4.5.3 edition OpenCV Source code :

git clone -b 4.5.3
 Copy code 

 Download source code to /usr/local/src

Cmake precompile

Download it OpenCV Source code , So let's use Cmake Precompile , Get into OpenCV Source directory , Input :

cmake -S . -B build -DCMAKE_INSTALL_PREFIX=/usr/local/opencv
 Copy code 

among :

  • DCMAKE_INSTALL_PREFIX:OpenCV Installation address of , Choose to install to /sur/local/opencv in .

after , It will start “ Run yard ” Precompiled generation build The folder and Markfile file :

cmake precompile

But this way , Our subsequent compilation can only install Python2( System ) and C++ Support for , Unable to support Python3 and java.

Next , Just teach you how to add Python3 and java rely on .

 Here comes the big play, ow

Add dependency

To add a pair of Java perhaps Python Dependence , Just Need to recompile . therefore , You need to delete the just precompiled build Folder :

rm -rf build
 Copy code 


Java The configuration of is very simple , The first is to install and configure JDK, Especially the configuration JAVA_HOME, Such as :

# Java11
 Copy code 

 I configured JDK

If you don't know how to Linux and macOS On the configuration JDK, You can refer to my previous video tutorial :

Special attention required ️, If your macOS perhaps Linux, Configured with jenv, that jenv The configuration needs to be in JDK Before configuration , For example, I macOS There is a local configuration jenv, You need to write environment variables like this :

jenv Configure in JDK Before configuration

after , Installation is needed. ANT, There are many ways , The simplest is to install with the package manager :

# macOS 
brew install ant
# Debian/Ubuntu
sudo apt-get install ant
# CentOS
sudo yum install ant
 Copy code 

such as : I'm on the server Debian Installation on system ANT:

Debian Installation on ANT

After installed , We still need to set ANT_HOME, otherwise OpenCV Unable to read at compile time , Package installation ANT:

  • /usr/share/ant: Linux Install... Through a package ant Address (Debian The server )
  • /usr/local/Cellar/ant/{ant_version}:macOS adopt Homebrew install

 To configure ANT(Linux)

notes :macOS On , adopt Homebrew Installed ant, There should be no configuration ANT_HOME,Homebrew It will be configured automatically .

after , Until then OpenCV Within the source code , Recompile :


You can see , Such precompiling , There is JDK Support .


Python3 Deployment of , It will be more complicated . I suggest not moving the system Python3,macOS and Linux All installed Anaconda, use Anaconda Of Python3 better .macOS Of Anaconda, Same as above , It can be used Homebrew install :

# macOS
brew install anaconda
 Copy code 

Of course , Can also be combined with Linux equally , Use Anaconda Script for :

The official website may not be very friendly to mainland servers , You can use Tsinghua image download source :

 Tsinghua mirror source

The installation method is very simple : After downloading the script , By default Shell Can run ; Such as :

 install anaconda

My installation address is /root/anaconda3, So configure the environment variables again :

anaconda To configure

 Configuration is successful

after , We'll match /root/anaconda3/bin/python3 This Python3:

Rerun cmake:

cmake -S . -B build -DCMAKE_INSTALL_PREFIX=/usr/local/opencv -D BUILD_opencv_python3=ON -DPYTHON_DEFAULT_EXECUTABLE=/root/anaconda3/bin/python3
 Copy code 

among :

  • DCMAKE_INSTALL_PREFIX:OpenCV Installation address .
  • BUILD_opencv_python3: Activate Python3
  • DPYTHON_DEFAULT_EXECUTABLE: Appoint Python3 Address

cmake precompile

Make compile

Now? , We can officially start compiling :

#  Get into build Catalog 
cd build
# make compile 
make -j{N}
 Copy code 

among ,{N} It depends on how many you have CPU、 Several threads , such as : my MacBook Pro:

12 Thread compilation

however , The server I bought , yes 2C 4G Of ; therefore , Use here :

2 Thread compilation

Wait until it's compiled , Just install it :

make install
 Copy code 


Another one “ Run yard “,OpenCV Just install it .

 installation is complete

This compiles , How do we use it in the development environment ? Let's see how to use .

Import Python

First, let's see how to import into Python, Before importing , That's true :

 Import failed

Mainly , our OpenCV Corresponding to compiled Python3 The library file address is :/usr/local/opencv/lib/python3.8/site-packages/cv2( That is to say OpenCV Install under directory lib Folder ):

 The library files

And we Python3 The library file is in /root/anaconda3/lib/python3.8/site-packages

Python The library of

therefore , We need to build soft links :

#  Copy so file 
cp /usr/local/opencv/lib/python3.8/site-packages/cv2/python-3.8/ /usr/local/opencv/lib/python3.8/site-packages/cv2/python-3.8/
#  Soft link so file 
ln -s /usr/local/opencv/lib/python3.8/site-packages/cv2/python-3.8/ /root/anaconda3/lib/
#  Soft link cv2 package 
ln -s /usr/local/opencv/lib/python3.8/site-packages/cv2 /root/anaconda3/lib/python3.8/site-packages/cv2
 Copy code 

after , stay Python It can be used in :

Python3 Use in

Of course ,macOS You can also compile and install in this way OpenCV. But if it's local PyCharm Developed , So we compiled OpenCV, No, data Documentation ,PyChram There is no prompt file Of course ~

Import C++

Because I haven't done it on the server C++ Project development , So I was there macOS Show on , Use Clion, Create an empty project here :


after , Entry project , edit CMakeLists file , It was like this :

 Not added yet OpenCV

Then add :

 Add dependency

It is convenient for everyone to copy and change :

cmake_minimum_required(VERSION 3.19)

add_executable(HelloOpenCV main.cpp)



target_link_libraries(HelloOpenCV ${OpenCV_LIBS})
 Copy code 

after , Let's write a test file to see the effect :

 Test code

 Test success

Import Java

Last , Let's see how to import into Java, precondition :

  • Preediting , Need to pass through Java compile


If the preconditions are not met , Need to delete uninstall OpenCV And delete build Reconfigure folders and compile .

When the preconditions are met , stay OpenCV Installation directory , There will be share Folder , Among them java Folders are what we need .

open java Folder :

 In the folder

Extract the inside .jar And libraries , And import into the project , Such as :

 Import maven

Last , Click on the project structure , add to libopencv_java453.dylib This document serves as lib library .

however , Here's the odd thing , The information I reported here is a little strange , It could be me JDK Version problem , I suggest you use JDK8:

 Operation information

uninstall OpenCV

uninstall OpenCV It's simple , We re-enter OpenCV Source directory build Folder , function :

make uninstall all
 Copy code 

after ,OpenCV Will automatically uninstall :

 uninstall OpenCV

If you have deleted the source folder , You can delete OpenCV Compile installation directory , This article is about :/usr/local/opencv


ha-ha , You slipped to the bottom ? What about? ,OpenCV The installation of is not very simple ? If you have questions, you can leave a message in the comment area ~ Also welcome to my blog to learn more Unix and GUN About ~

 Hey , What the hell is going on

copyright notice
author[Mintimate],Please bring the original link to reprint, thank you.

Random recommended