惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

MongoDB | Blog
MongoDB | Blog
V
V2EX
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
有赞技术团队
有赞技术团队
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
罗磊的独立博客
月光博客
月光博客
爱范儿
爱范儿
D
Docker
U
Unit 42
P
Proofpoint News Feed
I
InfoQ
腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
L
LangChain Blog
V
Visual Studio Blog
IT之家
IT之家
Vercel News
Vercel News
G
Google Developers Blog
M
MIT News - Artificial intelligence
美团技术团队
The GitHub Blog
The GitHub Blog
阮一峰的网络日志
阮一峰的网络日志
MyScale Blog
MyScale Blog

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
Building OpenCV from Source on Linux
2020-12-07 · via jdhao's digital space

In this post, I will summarize how to build OpenCV from source, and introduce how to use it when writing C++ programs.

Build from source#

On Ubuntu, run the following command to build OpenCV from source.

sudo apt update && sudo apt install -y cmake g++ wget unzip
wget -O opencv.zip https://github.com/opencv/opencv/archive/master.zip
unzip opencv.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/master.zip
unzip opencv_contrib.zip
# Create build directory
mkdir -p opencv_build && cd opencv_build
cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo \
      -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-master/modules \
      -DCMAKE_INSTALL_PREFIX=$HOME/local/ \
      ../opencv-master/
# using multiprocessing to compile can save a lot of time
make -j
make install

The package will be installed to $HOME/local/, with binary file go to $HOME/local/bin, header file go to $HOME/local/include/opencv4, dynamic shared library file go to $HOME/local/lib.

A speical note on header files, the structure of OpenCV header files is someting like the following:

# generated using tree -L 2 -d $HOME/local/include/opencv4
/home/jdhao/local/include/opencv4/
└── opencv2
    ├── calib3d
    ├── core
    ├── dnn
    ├── features2d
    ├── flann
    ├── gapi
    ├── highgui
    ├── imgcodecs
    ├── imgproc
    ├── ml
    ├── objdetect
    ├── photo
    ├── stitching
    ├── video
    └── videoio

The header files are grouped into subdirectories based on their functionality.

Ref:

Run a simple program utilizing OpenCV#

The following program reads an image and resize it to size 300x300 using OpenCV:

 #include "opencv2/opencv.hpp"
 #include "opencv2/core/core.hpp"
 #include "opencv2/highgui/highgui.hpp"
 #include "opencv2/imgproc/imgproc.hpp"
 #include <iostream>

 int main(){
     cv::Mat img, dst;
     img = cv::imread("wind-turbine.jpg", cv::IMREAD_COLOR);
     cv::resize(img, dst,  cv::Size(300, 300));
     std::cout << "img width and height: " << img.size().width << ", " << img.size().height << std::endl;

     cv::imwrite("resize_img.jpg", dst);

     return 0;
 }

In the above code, we have included some OpenCV header files. If you look at the header file opencv.hpp, you will find that it actually includes other OpenCV header files, if they are not included in your code already. So in most cases, include opencv.hpp may be enough if you do not care about compilation time (see also here and here for more discussions on this).

We can compile the above program with the following command:

clang++ -Wall \
    -I$HOME/local/include/opencv4/ \
    -L$HOME/local/lib/  \
    -lopencv_core -lopencv_imgcodecs -lopencv_imgproc \
    read_img.cc  -o read_img

Note that cv::imresize() requires libopencv_imgproc.so be linked against.

Issues met when compiling#

I met the following error:

undefined references to cv::imread()

It is because if we use functions from some OpenCV module, we need to link our C++ program against the corresponding shared object file. We can specify the Library to link against with -l. For example, -lopencv_imgproc means to link against libopencv_imgproc.so.

Ref: