Download, compile and install opencv under ubuntu

1. Download the opencv source code

On the opencv official website, the opencv source code download path is as follows: https://opencv.org/releases/. The web page is as follows, just select the required opencv version and download the corresponding sources file.
opencv_download_page

2. Configure dependencies

At the terminal command line, run the following 3 commands:

sudo apt-get install build-essential 

sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

3. Compile and install opencv

First unzip the sources source folder of opencv just downloaded, and copy the source folder to a random place. Then the terminal command line enters the source code folder directory, creates the compiled file build directory, and the command line operations are as follows:

cd opencv
mkdir build
cd build

Then, compile the opencv source code under the build folder, and the command line operations are as follows:

sudo cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..

sudo make -j8(8可以改,看电脑cpu线程数)

sudo make install

4. Modify the system configuration file

After opencv is installed, you need to modify some configuration files of the ubuntu system. The specific operations are as follows.
(1) Add path.
Open the file ld.so.conf, the command line operation is as follows:

sudo gedit /etc/ld.so.conf

Add the following code at the end of the opened file:

/usr/loacal/lib

After the addition is complete, save and close the file ld.so.conf, and then run the command line as follows:

sudo ldconfig

(2) Configuration environment
Open the .bashrc file, and the command line operation is as follows:

sudo gedit /etc/bash.bashrc 

At the end of the opened file, add the following two lines of code and put them at the end:

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH

Save the file and exit, then run the command line as follows:

source /etc/bash.bashrc

Finally, enter the following command to view the version of the installed opencv (the display will be a bit wrong, the installed version of opencv4 or above may display the version of opencv3):

pkg-config opencv --modversion

5. Test opencv operation

Enter the opencv/samples/cpp/example_cmake directory, open the terminal, and enter in sequence:

mkdir build
cd build
cmake ..
make -j8
./opencv_example

After running, the window screen of opencv will be displayed. At this point the opencv installation is complete.

Guess you like

Origin blog.csdn.net/hy2014x/article/details/127189486