当前位置: 首页 > 工具软件 > Synaptic.js > 使用案例 >

opencvjs 安装 windows ubuntu

易俊友
2023-12-01

原文链接: opencvjs 安装 windows ubuntu

上一篇: opencvjs 磨皮美颜

下一篇: js 显示矩阵形式的图片

opencv-node

https://github.com/peterbraden/node-opencv

windows 未成功

https://docs.opencv.org/2.4/doc/tutorials/introduction/windows_install/windows_install.html#windows-installation

mac 没有...

https://www.v2ex.com/t/348922

ubuntu 1804 虚拟机未成功

https://docs.opencv.org/2.4/doc/tutorials/introduction/linux_install/linux_install.html#linux-installation

https://www.learnopencv.com/install-opencv-4-on-ubuntu-18-04/

需要完整安装两个博客列出的所有包,才能进行make,否则报错

1. Install OpenCV 4.0

Step 0: Select OpenCV version to install

1

2

3

echo "OpenCV installation by learnOpenCV.com"

# Define OpenCV Version to install

cvVersion= "master"

We are also going to clean build directories and create installation directory.

1

2

3

# Clean build directories

rm -rf opencv /build

rm -rf opencv_contrib /build

1

2

3

# 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.

1

2

# Save current working directory

cwd=$( pwd )

Step 1: Update Packages

1

2

sudo apt -y update

sudo apt -y upgrade

If you are still not able to install OpenCV on your system, but want to get started with it, we suggest using our docker images with pre-installed OpenCV, Dlib, miniconda and jupyter notebooks along with other dependencies as described in this blog .

Step 2: Install OS Libraries

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

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

Looking for installation script for OpenCV 3.4.4 on Ubuntu 18.04 ? Have a look at this blog .

Step 3: Install Python Libraries

1

2

3

sudo apt -y install python3-dev python3-pip

sudo -H pip3 install -U pip numpy

sudo apt -y install python3-testresources

We are also going to install virtualenv and virtualenvwrapper modules to create Python virtual environments.

1

2

3

4

5

6

7

8

9

10

11

12

13

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

Download Installation Script
To easily follow along this tutorial, please download installation script by clicking on the button below. It’s FREE!

DOWNLOAD INSTALLATION SCRIPT

Step 4: Download opencv and opencv_contrib

1

2

3

4

5

6

7

8

9

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.

1

2

3

cd opencv

mkdir build

cd build

Next, we start the compilation and installation process.

1

2

3

4

5

6

7

8

9

10

11

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 ..

For system wide installation of OpenCV, change CMAKE_INSTALL_PREFIX to CMAKE_INSTALL_PREFIX=/usr/local \ .

1

2

make -j4

make install

2. How to use OpenCV in C++

Using CMakeLists.txt

The basic structure of your CMakeLists.txt will be as follows:

1

2

3

4

cmake_minimum_required(VERSION 3.1)

# Enable C++11

set (CMAKE_CXX_STANDARD 11)

set (CMAKE_CXX_STANDARD_REQUIRED TRUE)

You will have to set OpenCV_DIR as shown below.

1

SET(OpenCV_DIR <OpenCV_Home_Dir> /installation/OpenCV-master/lib/cmake/opencv4 )

Make sure that you replace OpenCV_Home_Dir with correct path. For example, in my case:

1

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.

1

2

3

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

To use the OpenCV version installed using Python script, first we activate the correct Python Virtual Environment.

For OpenCV-4 : Python 3

1

workon OpenCV-master-py3

Once you have activated the virtual environment, you can enter Python shell and test OpenCV version.

1

2

3

ipython

import cv2

print(cv2.__version__)

Required Packages

  • GCC 4.4.x or later
  • CMake 2.6 or higher
  • Git
  • GTK+2.x or higher, including headers (libgtk2.0-dev)
  • pkg-config
  • Python 2.6 or later and Numpy 1.5 or later with developer packages (python-dev, python-numpy)
  • ffmpeg or libav development packages: libavcodec-dev, libavformat-dev, libswscale-dev
  • [optional] libtbb2 libtbb-dev
  • [optional] libdc1394 2.x
  • [optional] libjpeg-dev, libpng-dev, libtiff-dev, libjasper-dev, libdc1394-22-dev

The packages can be installed using a terminal and the following commands or by using Synaptic Manager:

[compiler] sudo apt-get install build-essential
[required] sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
[optional] sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

Getting OpenCV Source Code

You can use the latest stable OpenCV version available in sourceforge or you can grab the latest snapshot from our Git repository .

Getting the Latest Stable OpenCV Version

Getting the Cutting-edge OpenCV from the Git Repository

Launch Git client and clone OpenCV repository

In Linux it can be achieved with the following command in Terminal:

cd ~/<my_working _directory>
git clone https://github.com/opencv/opencv.git

Building OpenCV from Source Using CMake, Using the Command Line

  1. Create a temporary directory, which we denote as <cmake_binary_dir>, where you want to put the generated Makefiles, project files as well the object files and output binaries.

  2. Enter the <cmake_binary_dir> and type

    cmake [<some optional parameters>] <path to the OpenCV source directory>

    For example

    cd ~/opencv
    mkdir release
    cd release
    cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
  3. Enter the created temporary directory (<cmake_binary_dir>) and proceed with:

    make
    sudo make install

 类似资料: