ubuntu 16.04 不用root权限安装 opencv 3.1.0

1、下载安装包:

 http://opencv.org/releases.html

选择3.1.0版本的source下载

2、安装

unzip opencv-3.1.0.zip

cd opencv-3.1.0

mkdir build

cd build

cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/home/xiongcx/files/opencv_install ..

make -j20  #可能遇到的问题见第三部分

make install

如果没有报错,此时opencv应该安装在了/home/xiongcx/files/opencv_install/lib/python2.7/site-packages/cv2.so,查看使用的是哪个python

>>> which python

/home/xiongcx/anaconda2/bin/python

将自己的python与opencv建立联系

cd /home/xiongcx/anaconda2/lib/python2.7/site-packages/

ln -s /home/xiongcx/files/opencv_install/lib/python2.7/site-packages/cv2.so cv2.so

3、验证

pkg-config --modversion opencv

或在python中import cv2,并查看cv2.__version__。此处应注意,如果opencv没有安装成功,但装了opencv-python的包的话,import cv2也不会报错,所以要注意看版本对不对。

4、遇到的问题

(1) make -j20时报错:

/home/xiongcx/files/opencv-3.1.0/modules/cudalegacy/src/graphcuts.cpp:120:54: error: ‘NppiGraphcutState’ has not been declared
     typedef NppStatus (*init_func_t)(NppiSize oSize, NppiGraphcutState** ppState, Npp8u* pDeviceMem);

解决:将/opencv-3.1.0/modules/cudalegacy/src/graphcuts.cpp中的

#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)

修改为

#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER) || (CUDART_VERSION>=8000)

参考https://blog.csdn.net/yhaolpz/article/details/71375762

(2)在python中import cv2时出现

ImportError: No module named cv2

解决:由于安装过程中没有报错,所以认为问题不在于安装错了,而在于系统未准确找到opencv的lib文件,因此配置pkg-config。

mkdir /home/xiongcx/files/pkgconfig

cp /home/xiongcx/files/opencv_install/lib/pkgconfig/opencv.pc /home/xiongcx/files/pkgconfig/opencv3.1.0.pc

vi ~/.bashrc

#添加环境变量
export PKG_CONFIG_PATH="/home/xiongcx/files/pkgconfig:$PKG_CONFIG_PATH"

source ~/.bashrc

此时测试pkgconfig

pkg-config --libs opencv3.1.0

发现可以正常显示opencv的信息,再在python中import cv2时不再出现上面的错误,而是错误3

参考https://www.cnblogs.com/wycBlog/p/7217498.html

(3)在python中import cv2时出现

>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: /home/xiongcx/anaconda2/bin/../lib/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by ./cv2.so)

解决:

先查看libstdc++.so.6是什么

>>> cd /home/xiongcx/anaconda2/bin/../lib/

>>> ls -l | grep libstdc++.so.6

lrwxrwxrwx  1 xiongcx xiongcx       19 10月 15 21:36 libstdc++.so -> libstdc++.so.6.0.19
-rwxrwxr-x  1 xiongcx xiongcx  1055192 10月 30 14:57 libstdc++.so.6
-rwxrwxrwx  2 xiongcx xiongcx  1055192 9月  19  2015 libstdc++.so.6.0.19
-rw-r--r--  1 xiongcx xiongcx  1566440 10月 30 14:49 libstdc++.so.6.0.21

然后

>>> strings libstdc++.so.6 | grep GLIBCXX

GLIBCXX_3.4
GLIBCXX_3.4.1
GLIBCXX_3.4.2
GLIBCXX_3.4.3
GLIBCXX_3.4.4
GLIBCXX_3.4.5
GLIBCXX_3.4.6
GLIBCXX_3.4.7
GLIBCXX_3.4.8
GLIBCXX_3.4.9
GLIBCXX_3.4.10
GLIBCXX_3.4.11
GLIBCXX_3.4.12
GLIBCXX_3.4.13
GLIBCXX_3.4.14
GLIBCXX_3.4.15
GLIBCXX_3.4.16
GLIBCXX_3.4.17
GLIBCXX_3.4.18
GLIBCXX_3.4.19
GLIBCXX_FORCE_NEW
GLIBCXX_DEBUG_MESSAGE_LENGTH

发现这里面没有GLIBCXX_3.4.21

通过locate指令查找系统中是否存在其他libstdc++.so.6

>>> locate libstdc++.so.6

/usr/lib/x86_64-linux-gnu/libstdc++.so.6
/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21

再查看这里的libstdc++.so.6是否符合要求

>>> strings /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21 | grep GLIBCXX

GLIBCXX_3.4
GLIBCXX_3.4.1
GLIBCXX_3.4.2
GLIBCXX_3.4.3
GLIBCXX_3.4.4
GLIBCXX_3.4.5
GLIBCXX_3.4.6
GLIBCXX_3.4.7
GLIBCXX_3.4.8
GLIBCXX_3.4.9
GLIBCXX_3.4.10
GLIBCXX_3.4.11
GLIBCXX_3.4.12
GLIBCXX_3.4.13
GLIBCXX_3.4.14
GLIBCXX_3.4.15
GLIBCXX_3.4.16
GLIBCXX_3.4.17
GLIBCXX_3.4.18
GLIBCXX_3.4.19
GLIBCXX_3.4.20
GLIBCXX_3.4.21
GLIBCXX_DEBUG_MESSAGE_LENGTH

可以看到是符合要求的,因此将这个libstdc++.so.6.0.21复制到/home/xiongcx/anaconda2/bin/../lib/中,然后删除原来的软链接libstdc++.so.6,再建立新的指向libstdc++.so.6.0.21的软链接

cp /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21 ~/anaconda2/lib

rm libstdc++.so.6

ln -s libstdc++.so.6.0.21 libstdc++.so.6

再在python中import cv2时不再出现上面的错误,而是错误(4)

(4)报错如下:

>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: libgomp.so.1: version `GOMP_4.0' not found (required by /usr/lib/x86_64-linux-gnu/libsoxr.so.0)

解决:这个报错与(3)有些相似,猜测也是anaconda2中部分链接需要更新的原因。先在anaconda2/lib中

>>> strings libgomp.so.1 | grep "GOMP"

GOMP_barrier
GOMP_critical_start
GOMP_critical_end
GOMP_critical_name_start
GOMP_critical_name_end
GOMP_atomic_start
GOMP_atomic_end
GOMP_loop_static_start
GOMP_loop_dynamic_start
GOMP_loop_guided_start
GOMP_loop_runtime_start
GOMP_loop_ordered_static_start
GOMP_loop_ordered_dynamic_start
GOMP_loop_ordered_guided_start
GOMP_loop_ordered_runtime_start
GOMP_loop_static_next
GOMP_loop_dynamic_next
GOMP_loop_guided_next
GOMP_loop_runtime_next
GOMP_loop_ordered_static_next
GOMP_loop_ordered_dynamic_next
GOMP_loop_ordered_guided_next
GOMP_loop_ordered_runtime_next
GOMP_parallel_loop_static_start
GOMP_parallel_loop_dynamic_start
GOMP_parallel_loop_guided_start
GOMP_parallel_loop_runtime_start
GOMP_loop_end
GOMP_loop_end_nowait
GOMP_loop_ull_static_start
GOMP_loop_ull_dynamic_start
GOMP_loop_ull_guided_start
GOMP_loop_ull_runtime_start
GOMP_loop_ull_ordered_static_start
GOMP_loop_ull_ordered_dynamic_start
GOMP_loop_ull_ordered_guided_start
GOMP_loop_ull_ordered_runtime_start
GOMP_loop_ull_static_next
GOMP_loop_ull_dynamic_next
GOMP_loop_ull_guided_next
GOMP_loop_ull_runtime_next
GOMP_loop_ull_ordered_static_next
GOMP_loop_ull_ordered_dynamic_next
GOMP_loop_ull_ordered_guided_next
GOMP_loop_ull_ordered_runtime_next
GOMP_ordered_start
GOMP_ordered_end
GOMP_parallel_start
GOMP_parallel_end
GOMP_sections_start
GOMP_sections_next
GOMP_parallel_sections_start
GOMP_sections_end
GOMP_sections_end_nowait
GOMP_single_start
GOMP_single_copy_start
GOMP_single_copy_end
GOMP_task
GOMP_taskwait
GOMP_taskyield
GOMP_1.0
GOMP_2.0
GOMP_3.0
GOMP_CPU_AFFINITY
GOMP_SPINCOUNT
GOMP_STACKSIZE
not enough memory to store GOMP_CPU_AFFINITY list
Invalid value for enviroment variable GOMP_CPU_AFFINITY

发现的确没有GOMP_4.0,在系统中查找libgomp.so.1发现了/usr/lib/x86_64-linux-gnu/libgomp.so.1,于是

>>> strings /usr/lib/x86_64-linux-gnu/libgomp.so.1 | grep "GOMP"

GOMP_barrier
GOMP_barrier_cancel
GOMP_critical_start
GOMP_critical_end
GOMP_critical_name_start
GOMP_critical_name_end
GOMP_atomic_start
GOMP_atomic_end
GOMP_loop_static_start
GOMP_loop_dynamic_start
GOMP_loop_guided_start
GOMP_loop_runtime_start
GOMP_loop_ordered_static_start
GOMP_loop_ordered_dynamic_start
GOMP_loop_ordered_guided_start
GOMP_loop_ordered_runtime_start
GOMP_loop_static_next
GOMP_loop_dynamic_next
GOMP_loop_guided_next
GOMP_loop_runtime_next
GOMP_loop_ordered_static_next
GOMP_loop_ordered_dynamic_next
GOMP_loop_ordered_guided_next
GOMP_loop_ordered_runtime_next
GOMP_parallel_loop_static_start
GOMP_parallel_loop_dynamic_start
GOMP_parallel_loop_guided_start
GOMP_parallel_loop_runtime_start
GOMP_parallel_loop_static
GOMP_parallel_loop_dynamic
GOMP_parallel_loop_guided
GOMP_parallel_loop_runtime
GOMP_loop_end
GOMP_loop_end_cancel
GOMP_loop_end_nowait
GOMP_loop_ull_static_start
GOMP_loop_ull_dynamic_start
GOMP_loop_ull_guided_start
GOMP_loop_ull_runtime_start
GOMP_loop_ull_ordered_static_start
GOMP_loop_ull_ordered_dynamic_start
GOMP_loop_ull_ordered_guided_start
GOMP_loop_ull_ordered_runtime_start
GOMP_loop_ull_static_next
GOMP_loop_ull_dynamic_next
GOMP_loop_ull_guided_next
GOMP_loop_ull_runtime_next
GOMP_loop_ull_ordered_static_next
GOMP_loop_ull_ordered_dynamic_next
GOMP_loop_ull_ordered_guided_next
GOMP_loop_ull_ordered_runtime_next
GOMP_ordered_start
GOMP_ordered_end
GOMP_parallel_start
GOMP_parallel_end
GOMP_parallel
GOMP_cancellation_point
GOMP_cancel
GOMP_sections_start
GOMP_sections_next
GOMP_parallel_sections_start
GOMP_parallel_sections
GOMP_sections_end
GOMP_sections_end_cancel
GOMP_sections_end_nowait
GOMP_single_start
GOMP_single_copy_start
GOMP_single_copy_end
GOMP_task
GOMP_taskwait
GOMP_taskyield
GOMP_taskgroup_start
GOMP_taskgroup_end
GOMP_offload_register
GOMP_offload_unregister
GOMP_target
GOMP_target_data
GOMP_target_end_data
GOMP_target_update
GOMP_teams
GOMP_PLUGIN_malloc
GOMP_PLUGIN_malloc_cleared
GOMP_PLUGIN_realloc
GOMP_PLUGIN_debug
GOMP_PLUGIN_error
GOMP_PLUGIN_fatal
GOMP_PLUGIN_async_unmap_vars
GOMP_PLUGIN_acc_thread
GOMP_1.0
GOMP_2.0
GOMP_3.0
GOMP_4.0
GOMP_4.0.1
GOMP_PLUGIN_1.0
GOMP_DEBUG
GOMP_CPU_AFFINITY
GOMP_SPINCOUNT
GOMP_STACKSIZE
  GOMP_CPU_AFFINITY = ''
  GOMP_STACKSIZE = '%lu'
  GOMP_SPINCOUNT = '%lu'
Invalid value for enviroment variable GOMP_CPU_AFFINITY
GOMP_OFFLOAD_get_name
GOMP_OFFLOAD_get_caps
GOMP_OFFLOAD_get_type
GOMP_OFFLOAD_get_num_devices
GOMP_OFFLOAD_init_device
GOMP_OFFLOAD_fini_device
GOMP_OFFLOAD_load_image
GOMP_OFFLOAD_unload_image
GOMP_OFFLOAD_alloc
GOMP_OFFLOAD_free
GOMP_OFFLOAD_dev2host
GOMP_OFFLOAD_host2dev
GOMP_OFFLOAD_run
GOMP_OFFLOAD_openacc_parallel
GOMP_OFFLOAD_openacc_register_async_cleanup
GOMP_OFFLOAD_openacc_async_test
GOMP_OFFLOAD_openacc_async_test_all
GOMP_OFFLOAD_openacc_async_wait
GOMP_OFFLOAD_openacc_async_wait_async
GOMP_OFFLOAD_openacc_async_wait_all
GOMP_OFFLOAD_openacc_async_wait_all_async
GOMP_OFFLOAD_openacc_async_set_async
GOMP_OFFLOAD_openacc_create_thread_data
GOMP_OFFLOAD_openacc_destroy_thread_data
GOMP_OFFLOAD_openacc_get_current_cuda_device
GOMP_OFFLOAD_openacc_get_current_cuda_context
GOMP_OFFLOAD_openacc_get_cuda_stream
GOMP_OFFLOAD_openacc_set_cuda_stream

存在GOMP_4.0,符合要求,于是删除anaconda2/lib中原有的libgomp.so.1,将/usr/lib/x86_64-linux-gnu/libgomp.so.1复制过来。然后成功了。

>>> import cv2
>>> cv2.__version__
'3.1.0'

参考http://www.voidcn.com/article/p-qlcwabfp-bnv.html

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值