1. Install Opencv
Step 1: Download opencv
Download address: https://github.com/opencv/opencv
Step 2: Unzip and install
unzip opencv-4.6.0zip
Step 3: Install compiled files
sudo apt-get install cmake
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
Step 4: Start compiling
# Create build directory
mkdir -p build && cd build
# Configure
cmake ../opencv-4.x
# Build
cmake --build .
Step 5: Installation
sudo make install
Default installation address: /user/local/lib
Step 6: Configure environment variables
sudo gedit /etc/ld.so.conf
Add a line to the file
then:
sudo ldconfig
Modify the bash.bashrc file
command line:
sudo gedit /etc/bash.bashrc
Add at the end of the file
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
Effective:
source /etc/bash.bashrc
test:
pkg-config opencv –modversion
It seems that every time an error is reported, the version number should appear.
Use the following official procedures:
Open in Terminal under this installation package
cd samples
cd cpp
cd example_cmake
cmake .
make
./opencv_example
Test success!
2. Configure VSCODE below
Step 1: Create a new CV2 folder on the desktop
Step 2: Open it in VSCODE and create a new test program
Code:
#include<opencv2/opencv.hpp>
using namespace cv;
int main()
{
Mat srcImage=imread("1.jpg");
imshow("Origin",srcImage);
waitKey(0);
return 0;
}
Find a random picture, name it 1.jpg and put it in the CV2 folder.
Step 3: Start configuring launch.json
Select the first one and modify it according to the following procedure, or you can copy it directly:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/${fileBasenameNoExtension}.main.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build"
}
]
}
Step 4: Start configuring tasks.json
Press F5 to run the program just now
Copy the program below:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": [
"-g",
"-std=c++11",
"${file}",
"-o",
"${fileBasenameNoExtension}.main.out",
"-I", "/usr/local/include",
"-I", "/usr/local/include/opencv4",
"-I", "/usr/local/include/opencv4/opencv2",
"-L", "/usr/local/lib",
"-l", "opencv_core",
"-l", "opencv_imgproc",
"-l", "opencv_imgcodecs",
"-l", "opencv_video",
"-l", "opencv_ml",
"-l", "opencv_highgui",
"-l", "opencv_objdetect",
"-l", "opencv_flann",
"-l", "opencv_imgcodecs",
"-l", "opencv_photo",
"-l", "opencv_videoio"
],
"problemMatcher":{
"owner": "cpp",
"fileLocation":[
"relative",
"${workspaceFolder}"
],
"pattern":[
{
"regexp": "^([^\\\\s].*)\\\\((\\\\d+,\\\\d+)\\\\):\\\\s*(.*)$",
"file": 1,
"location": 2,
"message": 3
}
]
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
You may also need to check the opencv directory. You may need to install another plocate here: sudo apt install plocate
Step 5: Start configuring c_cpp_properties.json
Ctrl+Shift+P to find Edit Configurations (JSON)
Copy the following program over:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/include",
"/usr/local/include/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "/usr/bin/cpp",
"cStandard": "c11",
"cppStandard": "c++11",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
Press F5:
smoothly!