Introduction
To install OpenCV on your Linux machine, you need to have the following packages as prerequisites [Ope17g].- GCC 4.4.x or later
- CMake 2.6 or higher
- Git
- GTK+2.x or higher, including headers (libgtk2.0-dev)
- pkg-config
- Python 2.6 or later and Numpy 1.5 or later with developer packages (python-dev, python-numpy)
- ffmpeg or libav development packages: libavcodec-dev, libavformat-dev, libswscale-dev
- [optional] libtbb2 libtbb-dev
- [optional] libdc1394 2.x
- [optional] libjpeg-dev, libpng-dev, libtiff-dev, libjasper-dev, libdc1394-22-dev
sudo apt-get update sudo apt-get install build-essential sudo apt-get install cmake git libgtk-3-dev pkg-config sudo apt-get install libavcodec-dev libavformat-dev libswscale-devListing. Getting required packages for OpenCV.
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev sudo apt-get install libpng-dev libtiff-dev libdc1394-22-dev sudo apt install libv4l-dev libxvidcore-dev libx264-devListing. Getting optional packages for OpenCV.
As a simple way to install OpenCV on Linux, you can enter the following command in terminal.
sudo apt-get install libopencv-dev
If not, you can get the current stable version OpenCV at OpenCV for Linux/Mac (https://opencv.org/). Then, you can use Archive Manager to extract the zip file.
If you want the latest cutting-edge opencv version, you can get it from Git repository OpenCV repository by using following commands instead of using above methods. OpenCV contrib repository can also be obtained as follows.
cd ~ git clone https://github.com/opencv/opencv.git git clone https://github.com/opencv/opencv_contrib.git
Navigate to the resulting opencv directory and make a build directory.
cd ~/opencv mkdir build cd build cmake -D CMAKE_BUILD_TYPE=Release \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules ..Listing. Building OpenCV.
You can use the following option if you want static libs.
-D BUILD_SHARED_LIBS=OFFListing. Option to use static libs.
After a while, you can build and install it as follows.
make sudo make installListing. Installing OpenCV
Check the opencv version as follow.
pkg-config --modversion opencvListing. OpenCV version
Using GCC ၊ CMake
You can use CMake to build your OpenCV applications [Ope17l]. You might want to have a look at CMake tutorial (https://cmake.org/cmake-tutorial/) to learn cmake.As a simple example, DisplayImage.cpp (online) reads an image and displays it. For that, an image file, thiri.jpg, is placed in home folder. A folder can be created to put DisplayImage.cpp.
#include < stdio.h > #include < opencv2/opencv.hpp > using namespace cv; int main(int argc, char** argv ) { Mat image; image = imread( "/home/yan/thiri.jpg", 1 ); if ( !image.data ) { printf("No image data \n"); return -1; } namedWindow("Display Image", WINDOW_AUTOSIZE ); imshow("Display Image", image); waitKey(0); return 0; }Listing. DisplayImage.cpp
In this example, the path for thiri.jpg is "/home/yan/thiri.jpg". You can replace it with your own image path. The function 'imread' loads the image. The value 1 for second argument is to read the image in color. The value can be 0 to read image in grayscale. If the reading of the image is failed, the program will exit with an error message. If it is successful, the image will be displayed using imshow. Create a file called CMakeLists.txt in the same folder as follow.
cmake_minimum_required(VERSION 2.8) project( DisplayImage ) find_package( OpenCV REQUIRED ) add_executable( DisplayImage DisplayImage.cpp ) target_link_libraries( DisplayImage ${OpenCV_LIBS} )Listing. CMakeLists.txt
Enter the following commands in terminal to build and run the program.
cd DisplayImage cmake . make ./DisplayImageListing. Building DisplayImage with CMake, and running it.
pkg-config
You can also use 'pkg-config' instead of CMake to build and run the application as follow.g++ DisplayImage.cpp `pkg-config --cflags --libs opencv` -o DisplayImage ./DisplayImageListing. Using g++, pkg-config to build and run the application
The following Figure illustrates the output the program.
If you get an error of missing shared lib, /etc/ld.so.conf.d/opencv.conf can be created as follow.
sudo nano /etc/ld.so.conf.d/opencv.confThen, input the opencv path depending on your system.
/usr/local/opencv/or
/usr/local/lib/Enter the following command to load the configuration.
sudo ldconfig
Using Qt
Let us discuss about using OpenCV with Qt. The following commands can be used to set up Qt.wget http://download.qt.io/official_releases/qt/5.9/5.9.0/qt-opensource-linux-x64-5.9.0.run chmod +x qt-opensource-linux-x64-5.9.0.run ./qt-opensource-linux-x64-5.9.0.runListing. Installing Qt on Linux.
Other required packages such as g++, font configuration, and OpenGL can be acquired as follows.
sudo apt-get install build-essential sudo apt-get install libfontconfig1 sudo apt-get install mesa-common-dev sudo apt-get install libglu1-mesa-dev -yListing. Getting required packages for Qt.
Open Qt to test a simple OpenCV example. Click New Project, and choose Non-Qt Project and Plain C++ Application as shown in the following Figure.
For the 'Project Location', give a name like DisplayImageQt and create a folder. Leave qmake for Build System and Desktop Qt 5.9.0 GCC 64bit for Kit Selection. Click Next and leave None for Project Management. Then, write a program in Project → Sources → main.cpp (online) as follow.
#include < stdio.h > #include < opencv2/opencv.hpp > using namespace cv; int main(int argc, char** argv ) { Mat image,bw_img; image = imread( "/home/yan/thiri.jpg", 1 ); if ( !image.data ) { printf("No image data \n"); return -1; } namedWindow("Original Image", WINDOW_AUTOSIZE ); imshow("Original Image", image); cvtColor(image, bw_img, COLOR_BGR2GRAY); namedWindow("BW Image", WINDOW_AUTOSIZE ); imshow("BW Image", bw_img); waitKey(0); return 0; }Listing. A program to read an image and convert it to grayscale.
The program read an image and convert it to grayscale using 'cvtColor'. To build the project, open DisplayImageQt.pro and modify it as follows.
TEMPLATE = app CONFIG += console c++11 CONFIG -= app_bundle CONFIG -= qt SOURCES += main.cpp INCLUDEPATH += /usr/local/include/opencv LIBS += -L/usr/local/lib -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_imgcodecsListing. DisplayImageQt.pro
When the program is run, a grayscale image of Thiri will appear beautifully.
Using Code::Blocks
Code::Blocks is a popular IDE for C++. In this section, using Code::Blocks with OpenCV is discussed. To install Code::Blocks IDE, enter the following commands in terminal.sudo apt-get install build-essential sudo apt-get install libgtk-3-dev sudo apt-get install gdb sudo add-apt-repository ppa:damien-moore/codeblocks-stable sudo apt-get update sudo apt-get install codeblocks codeblocks-contribListing. Installing Code::Blocks.
After installing Code::Blocks, open the IDE and create a new project by clicking 'File menu → New → Project...' or 'Start here tab → Create a new project'. A pop up window will appear and choose 'OpenCV project' for Category.
Click next and define project name and location.
Then, choose compiler as 'GNU GCC compiler' as shown and click Finish.
Thereafter, you can see OpenCV sample code and lena.jpg in the project. Modify the header as #include < opencv2/opencv.hpp > in main.cpp as shown in the following Figure and Listing (online).
#include < opencv2/opencv.hpp > using namespace cv; int main(int argc, char *argv[]) { Mat img = imread("lena.jpg", CV_LOAD_IMAGE_COLOR); if(img.empty()) return -1; namedWindow( "lena", CV_WINDOW_AUTOSIZE ); imshow("lena", img); waitKey(0); return 0; }Listing. An example OpenCV program for Code::Blocks.
Defining Build Options
Click Project Menu → Build Options... and define /usr/local/include in Search directories tab → Compiler tab for both Debug and Release configurations as illustrated in the following Figure.Similarly, define /usr/local/lib in Linker tab as in the following Figure.
As a next step, enter Link Libraries in Linker settings tab as illustrated in the following Figure.
After all these steps, click F9 to build and run the program. Its output can be seen as shown in the following Figure.
Using pkg-config
You can also use pkg-config to define build options. Click Project Menu → Build Options... and enter the following line in Compiler settings tab → Other compiler options tab for both Debug and Release configurations.`pkg-config --cflags opencv`
Similarly, enter the following line in Linker settings tab → Other linker options tab.
`pkg-config --libs opencv`
When you build and run the project (online) by clicking F9 , the same output will appear as shown in the previous method.
Related Posts
References
[Ope17g] OpenCV. Installation in Linux. 2017.url: http://docs.opencv.org/2.4/doc/tutorials/introduction/linux_install/linux_install.html.
[Ope17l] OpenCV. Using OpenCV with gcc and CMake. 2017.
url: http://docs.opencv.org/2.4/doc/tutorials/introduction/linux_gcc_cmake/linux_gcc_cmake.html.
No comments:
Post a Comment
Comments are moderated and don't be surprised if your comment does not appear promptly.