Install OpenCV in Ubuntu 16.04 LTS for C / C++

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_24990189/article/details/89788383

参考链接:http://www.codebind.com/cpp-tutorial/install-opencv-ubuntu-cpp/

这个印度小哥下面有视频教程安装的:

经历了三次报错 == 

目录

Step 1 – Updating Ubuntu

Step 2 – Install dependencies

Step 3 –  Get OpenCV

Step 4 – build and install OpenCV

Step 5 – check


Step 1 – Updating Ubuntu

wlsh@wlsh-ThinkStation:~$ sudo apt-get update
wlsh@wlsh-ThinkStation:~$ sudo apt-get upgrade

Configuration file '/etc/modprobe.d/blacklist.conf'
 ==> Modified (by you or by a script) since installation.
 ==> Package distributor has shipped an updated version.
   What would you like to do about it ?  Your options are:
    Y or I  : install the package maintainer's version
    N or O  : keep your currently-installed version
      D     : show the differences between the versions
      Z     : start a shell to examine the situation
 The default action is to keep your current version.
*** blacklist.conf (Y/I/N/O/D/Z) [default=N] ?  N

Step 2 – Install dependencies

wlsh@wlsh-ThinkStation:~$ sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

wlsh@wlsh-ThinkStation:~$ sudo apt-get install python3.5-dev python3-numpy libtbb2 libtbb-dev

wlsh@wlsh-ThinkStation:~$ sudo apt-get install libjpeg-dev libpng-dev libtiff5-dev libjasper-dev libdc1394-22-dev libeigen3-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev sphinx-common libtbb-dev yasm libfaac-dev libopencore-amrnb-dev libopencore-amrwb-dev libopenexr-dev libgstreamer-plugins-base1.0-dev libavutil-dev libavfilter-dev libavresample-dev

Y

报错:

/sbin/ldconfig.real: /usr/local/cuda-10.1/targets/x86_64-linux/lib/libcudnn.so.7 is not a symbolic link

解决方法:https://devtalk.nvidia.com/default/topic/1032114/tensorrt/tensorrt-4-installation-libcudnn-so-7-is-not-a-symbolic-link/

根据上面的方法,调整自己的连接

wlsh@wlsh-ThinkStation:~$ cd /usr/local/cuda/lib64
wlsh@wlsh-ThinkStation:/usr/local/cuda/lib64$ sudo ln -sf libcudnn.so.7.5.0 libcudnn.so.7
wlsh@wlsh-ThinkStation:/usr/local/cuda/lib64$ sudo ln -sf libcudnn.so.7 libcudnn.so

Step 3 –  Get OpenCV

wlsh@wlsh-ThinkStation:~$ sudo su
root@wlsh-ThinkStation:/home/wlsh# cd /opt
root@wlsh-ThinkStation:/opt# git clone https://github.com/Itseez/opencv.git
Cloning into 'opencv'...
remote: Enumerating objects: 2, done.
remote: Counting objects: 100% (2/2), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 253082 (delta 0), reused 0 (delta 0), pack-reused 253080
Receiving objects: 100% (253082/253082), 457.82 MiB | 9.54 MiB/s, done.
Resolving deltas: 100% (176658/176658), done.
Checking connectivity... done.

root@wlsh-ThinkStation:/opt# git clone https://github.com/Itseez/opencv_contrib.git
Cloning into 'opencv_contrib'...
remote: Enumerating objects: 14, done.
remote: Counting objects: 100% (14/14), done.
remote: Compressing objects: 100% (12/12), done.
remote: Total 29514 (delta 3), reused 6 (delta 1), pack-reused 29500
Receiving objects: 100% (29514/29514), 126.12 MiB | 4.10 MiB/s, done.
Resolving deltas: 100% (18261/18261), done.
Checking connectivity... done.

Step 4 – build and install OpenCV

root@wlsh-ThinkStation:/opt# ls
google  kingsoft  opencv  opencv_contrib  teamviewer

root@wlsh-ThinkStation:/opt# cd opencv
root@wlsh-ThinkStation:/opt/opencv# mkdir release
root@wlsh-ThinkStation:/opt/opencv# cd release

root@wlsh-ThinkStation:/opt/opencv/release# cmake -D BUILD_TIFF=ON -D WITH_CUDA=OFF -D ENABLE_AVX=OFF -D WITH_OPENGL=OFF -D WITH_OPENCL=OFF -D WITH_IPP=OFF -D WITH_TBB=ON -D BUILD_TBB=ON -D WITH_EIGEN=OFF -D WITH_V4L=OFF -D WITH_VTK=OFF -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=/opt/opencv_contrib/modules /opt/opencv/

root@wlsh-ThinkStation:/opt/opencv/release# make -j4
root@wlsh-ThinkStation:/opt/opencv/release# make install

root@wlsh-ThinkStation:/opt/opencv/release# ldconfig
root@wlsh-ThinkStation:/opt/opencv/release# exit
exit

Step 5 – check

卧槽,报错

lsh@wlsh-ThinkStation:~$ pkg-config --modversion opencv
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found

下面的评论也遇到了问题,解决方法

wlsh@wlsh-ThinkStation:~$ sudo apt-get install libopencv-dev

解决,接下测

wlsh@wlsh-ThinkStation:~$ pkg-config --modversion opencv
2.4.9.1
wlsh@wlsh-ThinkStation:~/Project$ mkdir cpp_test
wlsh@wlsh-ThinkStation:~/Project$ cd cpp_test/
wlsh@wlsh-ThinkStation:~/Project/cpp_test$ touch main.cpp
#include <opencv2/highgui.hpp>
#include <iostream>
 
int main( int argc, char** argv ) {
  
  cv::Mat image;
  image = cv::imread("sample.jpg" , CV_LOAD_IMAGE_COLOR);
  
  if(! image.data ) {
      std::cout <<  "Could not open or find the image" << std::endl ;
      return -1;
    }
  
  cv::namedWindow( "Display window", cv::WINDOW_AUTOSIZE );
  cv::imshow( "Display window", image );
  
  cv::waitKey(0);
  return 0;
}
wlsh@wlsh-ThinkStation:~/Project/cpp_test$ g++ main.cpp -o output `pkg-config --cflags --libs opencv`
main.cpp:1:31: fatal error: opencv2/highgui.hpp: No such file or directory
compilation terminated.

卧槽,第三次报错...

解决:

wlsh@wlsh-ThinkStation:/usr/include/opencv2/highgui$ ls
cap_ios.h  highgui_c.h  highgui.hpp  ios.h

然后把头文件改为

#include <opencv2/highgui/highgui.hpp>

找一张图片命名为sample.jpg放在主函数的同一个目录下

运行成功

wlsh@wlsh-ThinkStation:~/Project/cpp_test$ g++ main.cpp -o output `pkg-config --cflags --libs opencv`
wlsh@wlsh-ThinkStation:~/Project/cpp_test$ ./output 

** (Display window:19296): WARNING **: Couldn't register with accessibility bus: Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired, or the network connection was broken.

猜你喜欢

转载自blog.csdn.net/qq_24990189/article/details/89788383