前言:之前在树莓派上配置了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(fitellipse ${OpenCV_LIBS})
add_executable(grabcut grabcut.cpp)
target_link_libraries(grabcut ${OpenCV_LIBS})
add_executable(image_alignment image_alignment.cpp)
target_link_libraries(image_alignment ${OpenCV_LIBS})
add_executable(image image.cpp)
target_link_libraries(image ${OpenCV_LIBS})
add_executable(imagelist_creator imagelist_creator.cpp)
target_link_libraries(imagelist_creator ${OpenCV_LIBS})
add_executable(image_sequence image_sequence.cpp)
target_link_libraries(image_sequence ${OpenCV_LIBS})
add_executable(inpaint inpaint.cpp)
target_link_libraries(inpaint ${OpenCV_LIBS})
add_executable(intelperc_capture intelperc_capture.cpp)
target_link_libraries(intelperc_capture ${OpenCV_LIBS})
add_executable(kalman kalman.cpp)
target_link_libraries(kalman ${OpenCV_LIBS})
add_executable(kmeans kmeans.cpp)
target_link_libraries(kmeans ${OpenCV_LIBS})
add_executable(laplace laplace.cpp)
target_link_libraries(laplace ${OpenCV_LIBS})
add_executable(letter_recog letter_recog.cpp)
target_link_libraries(letter_recog ${OpenCV_LIBS})
add_executable(lkdemo lkdemo.cpp)
target_link_libraries(lkdemo ${OpenCV_LIBS})
add_executable(logistic_regression logistic_regression.cpp)
target_link_libraries(logistic_regression ${OpenCV_LIBS})
add_executable(lsd_lines lsd_lines.cpp)
target_link_libraries(lsd_lines ${OpenCV_LIBS})
add_executable(mask_tmpl mask_tmpl.cpp)
target_link_libraries(mask_tmpl ${OpenCV_LIBS})
add_executable(matchmethod_orb_akaze_brisk matchmethod_orb_akaze_brisk.cpp)
target_link_libraries(matchmethod_orb_akaze_brisk ${OpenCV_LIBS})
add_executable(minarea minarea.cpp)
target_link_libraries(minarea ${OpenCV_LIBS})
add_executable(morphology2 morphology2.cpp)
target_link_libraries(morphology2 ${OpenCV_LIBS})
add_executable(neural_network neural_network.cpp)
target_link_libraries(neural_network ${OpenCV_LIBS})
add_executable(npr_demo npr_demo.cpp)
target_link_libraries(npr_demo ${OpenCV_LIBS})
add_executable(opencv_version opencv_version.cpp)
target_link_libraries(opencv_version ${OpenCV_LIBS})
add_executable(openni_capture openni_capture.cpp)
target_link_libraries(openni_capture ${OpenCV_LIBS})
add_executable(pca pca.cpp)
target_link_libraries(pca ${OpenCV_LIBS})
add_executable(peopledetect peopledetect.cpp)
target_link_libraries(peopledetect ${OpenCV_LIBS})
add_executable(phase_corr phase_corr.cpp)
target_link_libraries(phase_corr ${OpenCV_LIBS})
add_executable(points_classifier points_classifier.cpp)
target_link_libraries(points_classifier ${OpenCV_LIBS})
add_executable(polar_transforms polar_transforms.cpp)
target_link_libraries(polar_transforms ${OpenCV_LIBS})
add_executable(segment_objects segment_objects.cpp)
target_link_libraries(segment_objects ${OpenCV_LIBS})
add_executable(select3dobj select3dobj.cpp)
target_link_libraries(select3dobj ${OpenCV_LIBS})
add_executable(shape_example shape_example.cpp)
target_link_libraries(shape_example ${OpenCV_LIBS})
add_executable(smiledetect smiledetect.cpp)
target_link_libraries(smiledetect ${OpenCV_LIBS})
add_executable(squares squares.cpp)
target_link_libraries(squares ${OpenCV_LIBS})
add_executable(starter_imagelist starter_imagelist.cpp)
target_link_libraries(starter_imagelist ${OpenCV_LIBS})
add_executable(stereo_calib stereo_calib.cpp)
target_link_libraries(stereo_calib ${OpenCV_LIBS})
add_executable(stereo_match stereo_match.cpp)
target_link_libraries(stereo_match ${OpenCV_LIBS})
add_executable(stitching stitching.cpp)
target_link_libraries(stitching ${OpenCV_LIBS})
add_executable(stitching_detailed stitching_detailed.cpp)
target_link_libraries(stitching_detailed ${OpenCV_LIBS})
add_executable(train_HOG train_HOG.cpp)
target_link_libraries(train_HOG ${OpenCV_LIBS})
add_executable(train_svmsgd train_svmsgd.cpp)
target_link_libraries(train_svmsgd ${OpenCV_LIBS})
add_executable(travelsalesman travelsalesman.cpp)
target_link_libraries(travelsalesman ${OpenCV_LIBS})
add_executable(tree_engine tree_engine.cpp)
target_link_libraries(tree_engine ${OpenCV_LIBS})
add_executable(tvl1_optical_flow tvl1_optical_flow.cpp)
target_link_libraries(tvl1_optical_flow ${OpenCV_LIBS})
add_executable(videocapture_basic videocapture_basic.cpp)
target_link_libraries(videocapture_basic ${OpenCV_LIBS})
add_executable(videocapture_starter videocapture_starter.cpp)
target_link_libraries(videocapture_starter ${OpenCV_LIBS})
add_executable(videostab videostab.cpp)
target_link_libraries(videostab ${OpenCV_LIBS})
add_executable(videowriter_basic videowriter_basic.cpp)
target_link_libraries(videowriter_basic ${OpenCV_LIBS})
add_executable(warpPerspective_demo warpPerspective_demo.cpp)
target_link_libraries(warpPerspective_demo ${OpenCV_LIBS})
add_executable(watershed watershed.cpp)
target_link_libraries(watershed ${OpenCV_LIBS})
add_executable(3calibration 3calibration.cpp)
target_link_libraries(3calibration ${OpenCV_LIBS})
add_executable(application_trace application_trace.cpp)
target_link_libraries(application_trace ${OpenCV_LIBS})
add_executable(autofocus autofocus.cpp)
target_link_libraries(autofocus ${OpenCV_LIBS})
add_executable(bgfg_segm bgfg_segm.cpp)
target_link_libraries(bgfg_segm ${OpenCV_LIBS})
add_executable(calibration calibration.cpp)
target_link_libraries(calibration ${OpenCV_LIBS})
add_executable(camshiftdemo camshiftdemo.cpp)
target_link_libraries(camshiftdemo ${OpenCV_LIBS})
add_executable(cloning_demo cloning_demo.cpp)
target_link_libraries(cloning_demo ${OpenCV_LIBS})
add_executable(cloning_gui cloning_gui.cpp)
target_link_libraries(cloning_gui ${OpenCV_LIBS})
add_executable(connected_components connected_components.cpp)
target_link_libraries(connected_components ${OpenCV_LIBS})
add_executable(contours2 contours2.cpp)
target_link_libraries(contours2 ${OpenCV_LIBS})
add_executable(convexhull convexhull.cpp)
target_link_libraries(convexhull ${OpenCV_LIBS})
add_executable(cout_mat cout_mat.cpp)
target_link_libraries(cout_mat ${OpenCV_LIBS})
add_executable(create_mask create_mask.cpp)
target_link_libraries(create_mask ${OpenCV_LIBS})
add_executable(dbt_face_detection dbt_face_detection.cpp)
target_link_libraries(dbt_face_detection ${OpenCV_LIBS})
add_executable(delaunay2 delaunay2.cpp)
target_link_libraries(delaunay2 ${OpenCV_LIBS})
add_executable(demhist demhist.cpp)
target_link_libraries(demhist ${OpenCV_LIBS})
add_executable(detect_blob detect_blob.cpp)
target_link_libraries(detect_blob ${OpenCV_LIBS})
add_executable(dft dft.cpp)
target_link_libraries(dft ${OpenCV_LIBS})
add_executable(distrans distrans.cpp)
target_link_libraries(distrans ${OpenCV_LIBS})
add_executable(drawing drawing.cpp)
target_link_libraries(drawing ${OpenCV_LIBS})
add_executable(edge edge.cpp)
target_link_libraries(edge ${OpenCV_LIBS})
add_executable(em em.cpp)
target_link_libraries(em ${OpenCV_LIBS})
保存,回到我们的cpp文件夹这里,运行:
cmake . #注意是一个点,代表当前路径
make
当看到编译到100%的时候就可以了。
这时候直接运行我们的样例是都可以的。例如运行:
./facedetect
如果需要的话,可以将 CMakeLists.txt ,Makefile 和想要单独编译的cpp拷贝出来,修改CMakeLists.txt然后在其他地方cmake和make,可以单独使用,但要注意cpp代码里面调用到的路径不能出错,所以编译前先检查cpp代码里面的路径信息。