Install and Use OpenCV on Linux
Build & Install
My enviroment:
Architecture: x86_64
OS: Ubuntu 22.04 LTS
Editor: VS Code 1.71.0
Compiler: gcc 11.2.0
Build Tool: CMake 3.22.1
Simply follow the steps in OpenCV Tutorials to install OpenCV on Linux.
I use VS Code as my editor while CMake and gcc to build and compile the program. VSCode support IntelliSense for cross-compiling, but if you need code completions for opencv (and no include path error
when you use opencv!) in your VS Code, some configuration is required.
opencv4
was installed to /usr/local/ by default, and the headers are in /usr/local/include/opencv4 from OpenCV Tutorials. Use ls /usr/local/include/opencv4
to see here is only one directory opencv2 in it.
Press Ctrl+Shift+P
to open the search box, then enter and choose C/C++: Edit Configuration (JSON)
, the editor will open .vscode/c_cpp_properties.json in current directory. Add the include path of opencv4 to your "includePath":
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/local/include/opencv4"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++17",
"intelliSenseMode": "linux-gcc-x64",
"configurationProvider": "ms-vscode.makefile-tools"
}
],
"version": 4
}
Now you can use #include <opecv2/...>
in your code.
[Problem] ❓ When try to run example in Using OpenCV with gcc and CMake, I received the following error message when executing ./DisplayImage lena.jpg
:
terminate called after throwing an instance of 'cv::Exception'
what():
OpenCV(4.6.0-dev)
/home/xyu/Libs/opencv/opencv-4.x/modules/highgui/src/window.cpp:1253:
error: (-2:Unspecified error) The function is not implemented.
Rebuild the library with Windows, GTK+ 2.x or Cocoa support.
If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config,
then re-run cmake or configure script in function 'cvNamedWindow'
Aborted (core dumped)
[Solution] ✅ As the hint says:
sudo apt install libgtk2.0-dev pkg-config
Then re-run cmake
solved this problem.
Last updated