opencv相关记录

1.cmakelist找不到opencv

#find_package(OpenCV 2.4.3 REQUIRED)
find_package(OpenCV 3.0 QUIET)
if(NOT OpenCV_FOUND)
   find_package(OpenCV 2.4.3 QUIET)
   if(NOT OpenCV_FOUND)
      message(FATAL_ERROR "OpenCV > 2.4.3 not found.")
   endif()
endif()

opencv的配置信息

# Package Information for pkg-config
prefix=/usr/local
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir_old=${prefix}/include/opencv
includedir_new=${prefix}/include

Name: OpenCV
Description: Open Source Computer Vision Library
Version: 3.2.0
Libs: -L${exec_prefix}/lib -lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_viz -lopencv_core
Libs.private: -L/usr/lib/x86_64-linux-gnu -lwebp -ljasper -lImath -lIlmImf -lIex -lHalf -lIlmThread -lgtk-x11-2.0 -lgdk-x11-2.0 -lpangocairo-1.0 -latk-1.0 -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lpangoft2-1.0 -lpango-1.0 -lgobject-2.0 -lglib-2.0 -lfontconfig -lfreetype -lgthread-2.0 -lavcodec-ffmpeg -lavformat-ffmpeg -lavutil-ffmpeg -lswscale-ffmpeg -lvtkRenderingOpenGL-6 -lvtkImagingHybrid-6 -lvtkIOImage-6 -lvtkCommonDataModel-6 -lvtkCommonMath-6 -lvtkCommonCore-6 -lvtksys-6 -lvtkCommonMisc-6 -lvtkCommonSystem-6 -lvtkCommonTransforms-6 -lvtkCommonExecutionModel-6 -lvtkDICOMParser-6 -lvtkIOCore-6 -lz -lvtkmetaio-6 -ljpeg -lpng -ltiff -lvtkImagingCore-6 -lvtkRenderingCore-6 -lvtkCommonColor-6 -lvtkFiltersExtraction-6 -lvtkFiltersCore-6 -lvtkFiltersGeneral-6 -lvtkCommonComputationalGeometry-6 -lvtkFiltersStatistics-6 -lvtkImagingFourier-6 -lvtkalglib-6 -lvtkFiltersGeometry-6 -lvtkFiltersSources-6 -lvtkInteractionStyle-6 -lvtkRenderingLOD-6 -lvtkFiltersModeling-6 -lvtkIOPLY-6 -lvtkIOGeometry-6 -ljsoncpp -lvtkFiltersTexture-6 -lvtkRenderingFreeType-6 -lvtkftgl-6 -lvtkIOExport-6 -lvtkRenderingAnnotation-6 -lvtkImagingColor-6 -lvtkRenderingContext2D-6 -lvtkRenderingGL2PS-6 -lvtkRenderingContextOpenGL-6 -L/usr/lib -lgl2ps -lvtkRenderingLabel-6 -ldl -lm -lpthread -lrt
Cflags: -I${includedir_old} -I${includedir_new}
yjx@yjx-ThinkPad-Edge-E431:~$ pkg-config --modversion opencv
3.2.0
yjx@yjx-ThinkPad-Edge-E431:~$ pkg-config --cflags opencv
-I/usr/local/include/opencv -I/usr/local/include
yjx@yjx-ThinkPad-Edge-E431:~$ pkg-config --libs opencv
-L/usr/local/lib -lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_viz -lopencv_core

实在解决不了,就在set上进行修改

#斧底抽薪
set (OpenCV_DIR "/home/yjx/3rdparty/opencv-2.4.13.4/build")  

2.

20170812
得到opencv的lib配置信息(在ubuntu16.04)

yjx@yjx-ThinkPad-Edge-E431:~$ pkg-config --libs opencv
-L/usr/local/lib -lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_viz -lopencv_core

并将显示的信息添加在QT的配置文件中, 如下
LIBS += “此处添加显示的配置信息”


20170815

double t = (double)cvGetTickCount();
InitTime = ( (double)cvGetTickCount() - t )/(cvGetTickFrequency()*1000000.0);
cout<<"FirstTime: "<

3.

卸载opencv

sudo apt-get remove libopencv-*

查看opencv版本

pkg-config --modversion opencv

查看opencv安装的目录

pkg-config --cflags opencv

4. 安装多个版本的opencv

20171027
参考博客"http://blog.csdn.net/cumt08113684/article/details/53006376"

这几天在用代码提取光流的时候,总是会发生opencv VideoCapture打不开视频文件的错误,因此怀疑是opencv有问题,于是决定换个版本重新安装
我所使用的是Ubuntu15.04,通常Ubuntu系统是自带opencv的,使用命令

pkg-config --modversion opencv  

可以查看当前的opencv版本,而我的系统自带的是opencv-2.4.9
因为不知道如何卸载自带的opencv,所以我决定直接安装另一个版本的opencv,即让两个版本opencv共存。首先下载新版本的opencv安装包,我因为实验需要,下载的是opencv-2.4.12,之后解压
安装一些opencv的依赖项

sudo apt-get -qq install libopencv-dev build-essential checkinstall cmake pkg-config yasm libjpeg-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev python-dev python-numpy libtbb-dev libqt4-dev libgtk2.0-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils  

进入解压后的opencv安装包,新建目录,目录名为build,并进入该目录

mkdir build && cd build  

输入命令

cmake -D CMAKE_BUILD_TYPE=RELEASE -D WITH_TBB=ON  -D WITH_V4L=ON -D CMAKE_INSTALL_PREFIX=/usr/local/opencv/2.4.12 ..  
make -j12  
sudo make install -j12  

这样opencv-2.4.12就被安装在了/usr/local/opencv/2.4.12目录下.
接下来要对新安装的opencv-2.4.12进行配置,否则系统默认的还是opencv-2.4.9版本。
通过修改bashrc文件来设置PKG_ CONFIG_PATH(后来发现好像还需要改一下LD_LIBRARY_PATH)

sudo gedit ~/.bashrc  

在文件末尾加上

export PKG_CONFIG_PATH=/usr/local/opencv/2.4.12/lib/pkgconfig  
export LD_LIBRARY_PATH=/usr/local/opencv/2.4.12/lib  

保存之后,再使用命令

source ~/.bashrc  

这时候使用命令

pkg-config --modversion opencv  
pkg-config --cflags opencv  
pkg-config --libs opencv  

可以看到全都已经变成了opencv-2.4.12版本了。如果还想使用原来的opencv-2.4.9,只需要再修改PKG_CONFIG_PATH为opencv-2.4.9的pkgconfig路径即可。

然后如果碰上,opencv的版本在cmake时没有更换编译的版本的时候.修改cmakelist
参考博文"http://blog.csdn.net/kekong0713/article/details/53688131"

ubuntu下opencv2.4.10 和 opencv 3.10 多版本切换问题
原创 2016年12月16日 09:09:08 2142
1.由于需要,之前系统中是opencv2.4.10版本,后来安装opencv3.10版本,并支持多版本切换。通过以下命令可以查看opencv版本

pkg-config --modversion opencv  

2.如果编译好并安装了opencv3.10版本,想切换到3.10版本,可以通过如下的方式,
通过修改bashrc文件来设置PKG_ CONFIG_PATH 和 LD_LIBRARY_PATH的路径来选择对应的opencv版本

sudo gedit ~/.bashrc  

并在文件的默认追加新编译的3.10版本的路径,也就是cmake时:CMAKE_INSTALL_PREFIX=/usr/local/opencv/3.10

export PKG_CONFIG_PATH=/usr/local/opencv/3.10/lib/pkgconfig  
export LD_LIBRARY_PATH=/usr/local/opencv/3.10/lib  

然后执行命令,后面用pkg-config --modversion opencv 查看opencv版本的时候就是3.10版本

source ~/.bashrc  

3.上面的这些过程并不能保证在写makefile中调用的是3.10版本的库,可能还是调用之前的2.4.10的库,具体还是要在CMakeList.txt中指定要找的opencv的目录,
opencv3.10的编译目录/usr/local/opencv-3.1.0/build 下找到OpenCVConfig.cmake 文件,并在CMakeList.txt中添加对应的路径信息。这样cmake的时候调用的就是3.10的库,想切换成原来的2.4.10版本只需将第二步的bashrc文件和CMakeList.txt文件中修改的内容改成对应的opencv2.4.10的位置就好了。

# cmake needs this line  
cmake_minimum_required(VERSION 2.8)  
set (OpenCV_DIR "/usr/local/opencv-3.1.0/build")  
# Define project name  
project(myTest)  
# Find OpenCV, you may need to set OpenCV_DIR variable  
# to the absolute path to the directory containing OpenCVConfig.cmake file  
# via the command line or GUI  
find_package(OpenCV REQUIRED)  
# If the package has been found, several variables will  
# be set, you can find the full list with descriptions  
# in the OpenCVConfig.cmake file.  
# Print some message showing some of them  
message(STATUS "OpenCV library status:")  
message(STATUS "    version: ${OpenCV_VERSION}")  
message(STATUS "    libraries: ${OpenCV_LIBS}")  
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")  
if(CMAKE_VERSION VERSION_LESS "2.8.11")  
  # Add OpenCV headers location to your include paths  
  include_directories(${OpenCV_INCLUDE_DIRS})  
endif()  
# Declare the executable target built from your sources  
add_executable(demo demo.cpp)  
# Link your application with OpenCV libraries  
target_link_libraries(demo ${OpenCV_LIBS})  

5. 额外编译(使用window的ubuntu应用)

  • 出现问题1:Error compiling OpenCV, fatal error: stdlib.h: No such file or directory.
cd /build
cmake  -DENABLE_PRECOMPILED_HEADERS=OFF 
make
  • 出现问题2:fatal error: opencv2/xfeatures2d.hpp: No such file or directory.
    缺少 opencv_contrib,下载所需版本,之后重新编译 OpenCV.
cd /build
cmake -DOPENCV_EXTRA_MODULES_PATH=/modules 
make
  • 出现问题3:fatal error: opencv2/viz.hpp: No such file or directory
    可以发现在opencv的编译信息为
    VTK is not found:
-- VTK is not found. Please set -DVTK_DIR in CMake to VTK build directory, or to VTK install subdirectory with VTKConfig.cmake file

因此先安装vtk:

sudo apt-get install libvtk6-dev
cd /build
cmake -DOPENCV_EXTRA_MODULES_PATH=/modules WITH_VTK=ON  
make

编译样例:

cmake -DENABLE_PRECOMPILED_HEADERS=OFF -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.1.0/modules ../../opencv-3.1.0

你可能感兴趣的:(opencv相关记录)