sudo apt-get install build-essential libgtk2.0-dev libvtk5-dev libjpeg-dev libjasper-dev libopenexr-dev libtbb-dev
sudo apt-get install cmake
到官网下载OpenCV3.3.0
,并提取到任意位置
在opencv-3.3.0
文件夹下创建build文件夹,并切换到该文件夹
cd opencv-3.3.0
mkdir build
cd build
opencv
到/usr/local/opencv330
路径下cmake -D CMAKE_BUILD_TYPE=RELEASE -D WITH_TBB=ON -D WITH_V4L=ON -D CMAKE_INSTALL_PREFIX=/usr/local/opencv330 ..
sudo make -j16 #并行编译 可以直接 sudo make
sudo make install
sudo gedit /etc/ld.so.conf.d/opencv.conf
/usr/local/lib
sudo ldconfig
opencv
设置环境变量sudo gedit ~/.bashrc
PKG_CONFIG_PATH="/usr/local/opencv330/lib/pkgconfig:$PKG_CONFIG_PATH"
export LD_LIBRARY_PATH="/usr/local/opencv330/lib:$LD_LIBRARY_PATH"
source ~/.bashrc
opencv
版本pkg-config --modversion opencv
opencv-lena
DisplayImage.cpp
,和CMakeLists.txt
,并分别输入内容gedit DisplayImage.cpp
#include
#include
using namespace cv;
int main(int argc, char** argv )
{
if ( argc != 2 )
{
printf("usage: DisplayImage.out \n" );
return -1;
}
Mat image;
image = imread( argv[1], 1 );
if ( !image.data )
{
printf("No image data \n");
return -1;
}
namedWindow("Display Image", WINDOW_AUTOSIZE );
imshow("Display Image", image);
waitKey(0);
return 0;
}
gedit CMakeLists.txt
set(OpenCV_DIR /usr/local/opencv330/share/OpenCV )
cmake_minimum_required(VERSION 3.5)
project(DisplayImage)
find_package(OpenCV 3.3 REQUIRED)
include_directories(SYSTEM ${OpenCV_INCLUDE_DIRS} /usr/local/opencv330/include)
add_executable(DisplayImage DisplayImage.cpp)
target_link_libraries(DisplayImage ${OpenCV_LIBS})
#将相应的路径换成自己的安装路径
cmake .
make
./DisplayImage lena.png
tensorflow
环境source activate tensorflow
conda
安装opencv
(也可使用pip
等其他方式,但是必须安装在tensorflow
的python
中)conda install -c anaconda opencv
python
,成功import cv2
即安装成功有时上一步完成无法成功import cv2
可以如下解决。
将/usr/local/opencv330/lib/python2.7/dist-packages
路径下的 cv2.so
文件,替换
到/anaconda3/envs/tensorflow/lib/python2.7/site-packages
下
最后,opencv的相关的文件安装完毕,可以直接使用了