Mac命令行配置opencv

我的目的是使用g++手动编辑c++文件。

Mac版本 10.14.6

安装homebrew,去官网,复制粘贴

安装opencv

brew install opencv

安装的路径一般为

/usr/local/Cellar/opencv

安装pkg-config,它是用来反弹出opencv的cpp文件和lib的 .o文件的路径,使用g++编译的时候就不用输入一长串配置了。

brew install pkg-config

运行

pkg-config --cflags --libs opencv4

这一步会有各种错误,通常就是找不到opencv,或者opencv4,或者opencv4.pc,来回尝试就可以了。这是因为之前的文章使用的是opencv,所以可以找到,这里我们默认的brew安装的是opencv4,所以找不到了。执行这个命令后显示

-I/usr/local/Cellar/opencv/4.1.2/include/opencv4/opencv -I/usr/local/Cellar/opencv/4.1.2/include/opencv4 -L/usr/local/Cellar/opencv/4.1.2/lib -lopencv_gapi -lopencv_stitching -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_highgui -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_quality -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_sfm -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_videostab -lopencv_video -lopencv_videoio -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core

接下来编写c++文件,命名为1.cpp

#include <opencv2/opencv.hpp>


//g++ -Wall -std=c++17 `pkg-config --cflags --libs opencv4` 1.cpp  -o 1

int main(int argc, char** argv) 
{ 
//  Read the image file 
 cv::Mat image = cv::imread("1.jpg");
 return 0; 
}

注释部分为执行编译的命令,我这里用的是c++17,可自行选择。

参考文章