如何编译运行opencv的sample
前言:之前在树莓派上配置了opencv,但还不知道怎么使用,知道opencv有很多的sample,所以现在记录一下如何使用opencv的sample。
设备:华硕FX50V
环境:已经配置好opencv的ubuntu 14.04(如果还没配置opencv可以参照前面的博客,有讲怎么配置,看树莓派那篇也行)
进入到samples文件夹,这里有各种语言的,如果你配置好python与opencv的话,进入python那个文件夹直接 python 文件名.py就能运行opencv的python样例了。
但cpp文件的编译运行就有些不一样了。
首先,要有cmake:
sudo apt-get install cmake
然后,要用到example_cmake里面的 Makefile 和 CMakeLists.txt,官方的介绍是把需要编译的 样例.cpp 文件拷贝到这个文件夹下面,再到CMakeLists.txt修改相应的文件名然后编译即可,但我发现这样做源代码里面的路径被不正确了,就像人脸识别那个样例没办法载入文件,而且新建一个build文件夹来编译的方式也会造成路径不正确,所以为了使它们能够正常运行,我的做法是直接在cpp文件夹里面编译,运行,就不新建了。
具体做法如下:
把example_cmake里面的 Makefile 和 CMakeLists.txt 拷贝到cpp文件夹下(将原先的 CMakeLists.txt 拷贝到tutorial_code备份一下),打开 CMakeLists.txt,先把最下面那两行example的删掉:
# Declare the executable target built from your sources
add_executable(example example.cpp)
# Link your application with OpenCV libraries
target_link_libraries(example ${OpenCV_LIBS})
然后添加全部样例的信息:
add_executable(facedetect facedetect.cpp)
target_link_libraries(facedetect ${OpenCV_LIBS})
add_executable(facial_features facial_features.cpp)
target_link_libraries(facial_features ${OpenCV_LIBS})
add_executable(falsecolor falsecolor.cpp)
target_link_libraries(falsecolor ${OpenCV_LIBS})
add_executable(fback fback.cpp)
target_link_libraries(fback ${OpenCV_LIBS})
add_executable(ffilldemo ffilldemo.cpp)
target_link_libraries(ffilldemo ${OpenCV_LIBS})
add_executable(filestorage_base64 filestorage_base64.cpp)
target_link_libraries(filestorage_base64 ${OpenCV_LIBS})
add_executable(filestorage filestorage.cpp)
target_link_libraries(filestorage ${OpenCV_LIBS})
add_executable(fitellipse fitellipse.cpp)
target_link_libraries(fi