[Reproduced] Install OpenCV-Python in Fedora (2)

This tutorial is for Fedora 18 (64-bit) and Fedora 19 (32-bit). Other versions currently do not rule out other bugs.

Introduction

We install OpenCV-Python in Fedora in two ways:

1) Install
from pre-built binaries available in the Fedora repository; 2) Compile from source code.

In addition, OpenCV-Python requires the Numpy library, and it is strongly recommended to install Matplotlib. Of course, the installation needs to be done through IPython (an interactive Python terminal). Let’s use the first method to install it.

Pre-built binaries installation

In order to make the installation more convenient, we use the following command in the terminal as root (system administrator) to install all the software packages.

 	view plaincopy to clipboardprint? 
 1. $ yum install numpy opencv *

Open the IPython just introduced, and then enter the code in the Python terminal:

	view plaincopy to clipboardprint?
 1. >>> import cv2 as cv  
 2. >>> print( cv.__version__ )

If there is no error after the carriage return and printing, then the installation has been successful, and there may be a problem with this method of installation:

The Yum repository may not always contain the latest version of OpenCV. For example, at the time of writing this tutorial, the yum library contains 3.4.1, and the latest OpenCV version is 4.2. For the Python API, the latest version always includes better support. In addition, depending on the driver, ffmpeg, gstreamer software package, etc. used, camera support, video playback, etc. may have problems.

So in fact, another method is more effective, which is to compile from source code.

Compile from source

Compiling from source code may seem a bit complicated at first, but once it succeeds, there is nothing complicated.

First, we will install some dependencies. Some are mandatory, some are optional. Optional dependencies, if not needed, you can skip them.

Mandatory dependencies:

First, you need CMake to configure and install, use GCC to compile, and Python-devel and Numpy to create Python extensions.

	view plaincopy to clipboardprint?

 1. yum install cmake   
 2. yum install python-devel numpy   
 3. yum install gcc gcc-c++

Next, GTK support for GUI functions, camera support (libdc1394, v4l), media support (ffmpeg, gstreamer), etc. are needed.

	view plaincopy to clipboardprint?
 1. yum install gtk2-devel   
 2. yum install libdc1394-devel   
 3. yum install ffmpeg-devel   
 4. yum install gstreamer-plugins-base-devel

Optional dependencies:

OpenCV comes with supporting files for image formats (such as PNG, JPEG, JPEG2000, TIFF, WebP, etc.). But it may be a bit old. If you want to get the latest library, you can install development files in these formats.

	view plaincopy to clipboardprint?
 1. yum install libpng-devel   
 2. yum install libjpeg-turbo-devel   
 3. yum install jasper-devel  
 4. yum install openexr-devel   
 5. yum installlibtiff-devel   
 6. yum install libwebp-devel

Several OpenCV functions are parallel to Intel's Thread Building Block (TBB). However, if you want to enable it, you need to install TBB first. (Also when using CMake to configure the installation, please don't forget to set -D WITH_TBB = ON. More details below.)

	view plaincopy to clipboardprint?
 1. yum install tbb-devel

OpenCV uses another Eigen library to optimize mathematical operations. Therefore, if Eigen is installed in the system, you can use it. (Also when using CMake to configure the installation, please don't forget to set WITH_EIGEN = ON)

	view plaincopy to clipboardprint?
 1. yum install eigen3-devel

If you need to build documentation, you need to install Doxygen (document generation tool).

	view plaincopy to clipboardprint?
 1. yum install doxygen

Download OpenCV

Next, we can start downloading OpenCV:

1. Download the latest version of OpenCV from the sourceforge website: http://sourceforge.net/projects/opencvlibrary/ . Then unzip the folder.

2. Download the latest source code from OpenCV's github repository. To do this, you need to install Git first.

	view plaincopy to clipboardprint?
 1. yum install git   
 2. git clone https://github.com/opencv/opencv.git

It will create a folder OpenCV in the home directory, copying will take some time.

Now open a terminal window and navigate to the downloaded OpenCV folder. Create a new build folder and navigate to it.

	view plaincopy to clipboardprint?
 1. mkdir build   
 2. cd build

Configuration and installation

Now that all the necessary dependencies have been installed, let's install OpenCV. Of course, the installation must be configured using CMake. It specifies the modules to be installed, the installation path, other libraries to be used, whether to compile documents and examples, etc. The following commands are usually used for configuration (executed from the build folder)

	view plaincopy to clipboardprint?
 1. cmake -D CMAKE_BUILD_TYPE = RELEASE -D CMAKE_INSTALL_PREFIX = / usr
    / local ..

It specifies the build type as "release mode" and the installation path is /usr/local. Mark -D before each option, observe the sign at the end.... In short, this is a format:

	view plaincopy to clipboardprint?
 1. cmake [-D <flag>] [-D <flag>] ..

You can specify any number of flags, but each flag should be preceded by -D.

In this tutorial, we will install OpenCV with TBB and Eigen support. We also built the documentation, but did not include performance testing and build examples. We will also disable GPU-related modules (because we are using OpenCV-Python, so we don’t need GPU-related modules. This saves us some time).

1. Enable TBB and Eigen support:

	view plaincopy to clipboardprint?
 1. cmake -D WITH_TBB=ON -D WITH_EIGEN=ON ..

2. Enable documentation and disable tests and examples:

	view plaincopy to clipboardprint?
 2. Cmake -D BUILD_DOCS=ON -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D
    BUILD_EXAMPLES=OFF ..

3. Disable all GPU-related modules:

	view plaincopy to clipboardprint?
 3. cmake -D WITH_OPENCL=OFF -D BUILD_opencv_gpu=OFF -D
 4. BUILD_opencv_gpuarithm=OFF -D BUILD_opencv_gpubgsegm=OFF -D
 5. BUILD_opencv_gpucodec=OFF -D BUILD_opencv_gpufeatures2d=OFF -D
 6. BUILD_opencv_gpufilters=OFF -D BUILD_opencv_gpuimgproc=OFF -D
 7. BUILD_opencv_gpulegacy=OFF -D BUILD_opencv_gpuoptflow=OFF -D
 8. BUILD_opencv_gpustereo=OFF -D BUILD_opencv_gpuwarping=OFF ..

4. Set the installation path and build type:

	view plaincopy to clipboardprint?
 1. Cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local
    ..

Every time you enter a cmake statement, it will print out the resulting configuration settings. In the final setup, please make sure to fill in the following fields (the following are some important configurations I got). These fields should also be properly filled in in your system, otherwise some problems will occur. Therefore, you need to check whether you have performed the above steps correctly.

	view plaincopy to clipboardprint?
 1. ...  
 2. --   GUI:  
 3. --     GTK+ 2.x:                    YES (ver 2.24.19)  
 4. --     GThread :                    YES (ver 2.36.3)  
 5. --   Video I/O:  
 6. --     DC1394 2.x:                  YES (ver 2.2.0)  
 7. --     FFMPEG:                      YES  
 8. --       codec:                     YES (ver 54.92.100)  
 9. --       format:                    YES (ver 54.63.104)  
 10. --       util:                      YES (ver 52.18.100)  
 11. --       swscale:                   YES (ver 2.2.100)  
 12. --       gentoo-style:              YES  
 13. --     GStreamer:  
 14. --       base:                      YES (ver 0.10.36)  
 15. --       video:                     YES (ver 0.10.36)  
 16. --       app:                       YES (ver 0.10.36)  
 17. --       riff:                      YES (ver 0.10.36)  
 18. --       pbutils:                   YES (ver 0.10.36)  
 19. --     V4L/V4L2:                    Using libv4l (ver 1.0.0)  
 20. --   Other third-party libraries:  
 21. --     Use Eigen:                   YES (ver 3.1.4)  
 22. --     Use TBB:                     YES (ver 4.0 interface 6004)  
 23. --   Python:  
 24. --     Interpreter:                 /usr/bin/python2 (ver 2.7.5)  
 25. --     Libraries:                   /lib/libpython2.7.so (ver 2.7.5)  
 26. --     numpy:                       /usr/lib/python2.7/site-packages/numpy/core/include (ver 1.7.1)  
 27. --     packages path:               lib/python2.7/site-packages   
 28. ...

Now use the make command to build the file, and use the make install command to install, make install is still executed as root (system administrator).

	view plaincopy to clipboardprint?
 1. make   
 2. su   
 3. make install

The installation is over. All files are installed in the /usr/local/ folder. But to use it, Python should be able to find the OpenCV module. There are two ways:

1. Move the module to any folder in the Python path: You can find the Python path by typing import sys;print(sys.path) in the Python terminal. It will print out many locations. Move /usr/local/lib/python2.7/site-packages/cv2.so to any one of this folder. E.g,

sumv/usr/local/lib/python2.7/site-packages/cv2.so/usr/lib/python2.7/site-packages

However, you must do this every time you install OpenCV.

2. Add /usr/local/lib/python2.7/site-packages to PYTHON_PATH: it only needs to be executed once. Just open /.bashrc and add the following line to it, then log out and return.

	view plaincopy to clipboardprint?
 1. export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages

At this point, the OpenCV installation is complete. Open the terminal and try:

	view plaincopy to clipboardprint?
 1. import cv2 as cv。

Possible problems

(1) Errors often appear when make compiles opencv. What I encountered is:

	view plaincopy to clipboardprint?
 1. Linking CXX executable ../../bin/opencv_perf_core /lib/libEGL.so.1: undefined reference to `wl_display_dispatch_queue_pending' collect2: error: ld returned 1 exit status make[2]: * [bin/opencv_perf_core] Error 1 make[1]: *[modules/core/CMakeFiles/opencv_perf_core.dir/all] Error 2  
 2.
 3. make: * [all] Error 2

Enter:

	view plaincopy to clipboardprint?
 1. yum update libwayland*

Then make it through again.

(2) Test can be done after installation

cd ~/opencv-3.4.1 I encountered the following problems:

	view plaincopy to clipboardprint?
 1. Package opencv was not found in the pkg-config search path.  
 2. Perhaps you should add the directory containing `opencv.pc'   
 3. to thePKG_CONFIG_PATH environment variable   
 4. No package 'opencv' found

The solution is:

cp /usr/local/lib/pkgconfig/opencv.pc /usr/lib/pkgconfig

If it still can't be solved, the possible reason is that root privileges are used when compiling and installing opencv and export environment variables, and the program is called here as a normal user, so the relevant files cannot be found.

Add at the end of ~/.bash_profile:

	view plaincopy to clipboardprint?
 1. PKG_CONFIG_PATH=/usr/local/lib/pkgconfig   
 2. export PKG_CONFIG_PATH

Log in to the system again, compile the program, and succeed.
Then installing OpenCV-Python in Fredora is basically done, and you're done.
Check the article summary page https://blog.csdn.net/weixin_44237705/article/details/107864965
More openvino technical information can be exchanged in the group~
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44237705/article/details/107905064