索引地址:系列索引
测试
编译完成之后,需要确定编译结果是可以使用的。(安装完成之后也想要测试一下)
最简单的就是写个测试代码验证一下。
本部分代码不需要理解,只需要按照要求可以运行。
为了方便编译运行,我们使用Makefile文件进行快速编译
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| file = none CXX = g++ INCPATH = -I. -isystem /usr/include/opencv4 LIBS = -L/usr/lib \ -lopencv_alphamat -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired\ -lopencv_calib3d -lopencv_ccalib -lopencv_core -lopencv_cvv -lopencv_datasets \ -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dnn -lopencv_dpm \ -lopencv_face -lopencv_features2d -lopencv_flann -lopencv_freetype -lopencv_fuzzy \ -lopencv_gapi -lopencv_hdf -lopencv_hfs -lopencv_highgui -lopencv_img_hash -lopencv_imgcodecs\ -lopencv_imgproc -lopencv_intensity_transform -lopencv_line_descriptor -lopencv_ml \ -lopencv_objdetect -lopencv_optflow -lopencv_phase_unwrapping -lopencv_photo -lopencv_plot \ -lopencv_quality -lopencv_rapid -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_shape \ -lopencv_stitching -lopencv_structured_light -lopencv_superres -lopencv_surface_matching \ -lopencv_text -lopencv_tracking -lopencv_video -lopencv_videoio -lopencv_videostab -lopencv_viz \ -lopencv_xfeatures2d -lopencv_ximgproc -lopencv_xobjdetect -lopencv_xphoto
all: $(CXX) -c $(INCPATH) $(file).cpp $(CXX) -o $(file) $(file).o $(LIBS) rm *.o
|
将上述文本保存为MakeFile文件,使用时输入make file=filename
就可以了。
file
可以写死想要编译的文件名,也可以编译时指定。库文件位置根据实际指定。
或者使用CMake
1 2 3 4 5 6 7 8 9 10 11
| cmake_minimum_required(VERSION 3.10)
project(yolov3)
set(OpenCV_DIR $ENV{OPENCV_PATH}) find_package(OpenCV REQUIRED) include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(${PROJECT_NAME} "main.cpp")
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})
|
输出版本号
1 2 3 4 5 6 7 8 9
| #include <iostream> #include <opencv2/opencv.hpp> using namespace cv;
int main() { std::cout<<OPENCV_VERSION<<std::endl return 0; }
|
将以上代码保存为filename.cpp(比如说version.cpp),然后用MakeFile编译
1 2 3 4
| $ make file=version g++ -c -I. -isystem /usr/include/opencv4 version.cpp g++ -o version version.o -L/usr/lib -lopencv_core -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_imgproc -lopencv_ml rm *.o
|
这里就是编译时指定需要编译的文件名,不需要文件后缀。
执行以后
显示图片
1 2 3 4 5 6 7 8 9
| #include <opencv2/opencv.hpp> using namespace cv;
int main() { Mat img=imread("./img/1.jpg"); imshow("test",img); waitKey(6000); }
|
同理,效果如下。
确认库可以正常使用之后就可以正式进行OpenCV4的开发之路了。
首先我们先介绍一下OpenCV常用的数据结构和函数。
准备好在线参考文档。