OpenCV study notes (1): install under ubuntu

installation

1. You can download the same version of opencv and opencv_contrib from the github website (https://github.com/opencv). It can also be downloaded via the command line, as shown below:

git clone https://github.com/opencv/opencv.git
cd opencv	#进入opencv目录,再接着下载opencv_contrib
git clone https://github.com/opencv/opencv_contrib.git

2. Install related dependencies.

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. Safe cmake-gui.

sudo apt-get install cmake3-qt-gui

4. In the opencv folder, create a new build folder, and then run cmake-gui.

  • Click to Browse Source…select the download path of opencv;
  • Click to Browse Build…select the build folder path;
  • Click Configure, generally do not modify by default, click directly Finish, and then wait for the default configuration to load;
  • Check BUILD_opencv_world, modify the value of CMAKE_BUILD_TYPE to RELEASE, and set the OPENCV_EXTRA_MODULES_PATH path to the modules folder path under the opencv_contrib directory;
  • Finally, click GenerateGenerate Configuration File.

5. Enter the build folder and enter make -j7 (7-thread parallel compilation) to start the compilation; if the compilation is successful, enter sudo make install to complete the installation.

make -j7
sudo make install

Configure environment variables

1. In the /etc/ld.so.conf.d directory, create a new file opencv.conf, and then add /usr/local/lib in it.
2. Enter the sudo ldconfig command to configure the library.
3. Add the following statement at the end of the bash.bashrc file in the /etc directory:

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

test

1. Write a simple image display program display.cpp.

#include <opencv2/opencv.hpp>
using namespace cv;

void main()
{
    
    
	Mat image = imread("1.jpg");	//载入指定图像
	imshow("图片", image);	//显示图像
	waitKey(0);				//等待任意按键按下,退出
}

2. Write the CMakeLists.txt file.

cmake_minimum_required(VERSION 3.5)
project(displayImage)
find_package(OpenCV REQUIRED)
add_executable(display display.cpp)
target_link_libraries(display ${
    
    OpenCV_LIBS})

3. For the first compilation, you need to enter the cmake command to generate Makefile and other files, and then directly enter make to compile.

cmake .
make

Of course, there is another way to use the g++ compiler: g++ display.cpp -o display `pkg-config --cflags --libs opencv`
4. Run, the picture can be displayed to prove that the installation is successful, and you can press any button drop out.

./display

Guess you like

Origin blog.csdn.net/qq_42386127/article/details/99332327