编译运行opencv的sample

原文链接:https://blog.csdn.net/Wuzebiao2016/article/details/79252731
前言:之前在树莓派上配置了opencv,但还不知道怎么使用,知道opencv有很多的sample,所以现在记录一下如何使用opencv的sample。

进入到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代码里面的路径信息。
--------------------- 
作者:Wuzebiao2016 
来源:CSDN 
原文:https://blog.csdn.net/Wuzebiao2016/article/details/79252731 
 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在介绍pip编译安装OpenCV之前,我需要先说明一下,pip是Python的包管理工具,而OpenCV是一个开源的计算机视觉库。通常情况下,我们使用pip来安装Python包,而不是编译安装。 如果你想使用pip来安装OpenCV,可以直接运行以下命令: ``` pip install opencv-python ``` 这将会自动下载并安装最新版本的OpenCV。 如果你需要编译安装OpenCV,可以按照以下步骤进行操作: 1. 首先,确保你已经安装了CMake和GCC编译器。你可以通过运行以下命令来检查它们是否已经安装: ``` cmake --version gcc --version ``` 2. 下载OpenCV的源代码。你可以从OpenCV官方网站(https://opencv.org/releases/)下载最新版本的源代码。 3. 解压下载的源代码文件,并进入解压后的目录。 4. 创建一个用于编译的目录,并进入该目录: ``` mkdir build cd build ``` 5. 运行CMake来配置编译过程。你可以根据自己的需求添加一些选项,例如指定安装路径等。以下是一个示例命令: ``` cmake .. ``` 6. 运行make命令进行编译: ``` make ``` 这个过程可能需要一些时间,取决于你的计算机性能。 7. 完成编译后,运行make install命令进行安装: ``` make install ``` 8. 安装完成后,你可以通过导入cv2模块来验证OpenCV是否成功安装: ``` import cv2 print(cv2.__version__) ``` 如果能够成功导入cv2模块并打印出版本号,说明OpenCV已经成功安装。 希望以上步骤对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值