提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
ubuntu20.04安装opencv4库
- 一、安装步骤
-
- 1.下载源码并解压
- 2.进入刚刚下载回来的源码包
- 3.配置环境变量
- 4.验证
- 二、编译中出现的错误
-
- (1)AssertionError: Apache Ant报错
-
- 安装Java
- 下载安装Apache Ant
-
- 1.访问Apache Ant官网下载页面[https://ant.apache.org/bindownload.cgi](https://ant.apache.org/bindownload.cgi)
- 2.移动解压后的文件到/usr/local目录下:
- 3.配置环境变量:
- 4.验证安装:
- (2)python文件的AssertionError
-
- 1.修改源文件:
- 2.检查python版本:
-
- (*非必要*)若存在Python 2.7,需要进行卸载
-
- 1.检查系统中的Python版本
- 2.卸载Python 2.7
- 3.验证Python已成功卸载
- 4.卸载Python 2.7的备份文件
- 清理残余文件和目录
-
- 原因
- 解决办法
- 三、参考链接
一、安装步骤
1.下载源码并解压
opencv官方网站:https://opencv.org/releases/
2.进入刚刚下载回来的源码包
mkdir build cd build cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D OPENCV_GENERATE_PKGCONFIG=YES .. make -j4 sudo make install
3.配置环境变量
终端输入:
sudo gedit ~/.bashrc
在最下面添加:
export CPLUS_INCLUDE_PATH="/usr/include/opencv4:$CPLUS_INCLUDE_PATH" export LIBRARY_PATH="/usr/local/lib:$LIBRARY_PATH"
若显示如下,则成功安装。
4.验证
上述步骤运行无误后,基本完成了
pkg-config --modversion opencv4
二、编译中出现的错误
(1)AssertionError: Apache Ant报错
问题解析: 未安装apache ant
解决步骤:
安装Java
首先,安装Java JDK,在终端中输入以下命令:
sudo apt-get update sudo apt-get install openjdk-8-jdk
下载安装Apache Ant
1.访问Apache Ant官网下载页面https://ant.apache.org/bindownload.cgi
2.移动解压后的文件到/usr/local目录下:
bash cd ~ tar -zxvf apache-ant-1.9.16-bin.tar.gz sudo mv apache-ant-1.9.16 /usr/local/
3.配置环境变量:
sudo gedit ~/.bashrc
在bashrc文件里写入:
export ANT_HOME=/usr/local/apache-ant-1.9.16 export PATH=${PATH}:${ANT_HOME}/bin
4.验证安装:
bash ant -version
如果出现以下类似信息,即表明安装成功:
Apache Ant(TM) version 1.9.16 compiled on July 10 2021
(2)python文件的AssertionError
[ 6%] Generate files for Java bindings Traceback (most recent call last): File "/home/gu/文档/opencv-4.5.0/modules/java/generator/../generator/gen_java.py", line 1093, in <module> copy_java_files(java_files_dir, target_path) File "/home/gu/文档/opencv-4.5.0/modules/java/generator/../generator/gen_java.py", line 1032, in copy_java_files src = checkFileRemap(java_file) File "/home/gu/文档/opencv-4.5.0/modules/java/generator/../generator/gen_java.py", line 31, in checkFileRemap assert path[-3:] != '.in', path AssertionError: /home/darkchii/下载/opencv-3.4.1/modules/java/generator/src/java/org/opencv/osgi/OpenCVNativeLoader.java.in make[2]: *** [CMakeFiles/dephelper/gen_opencv_java_source] 错误 1 make[1]: *** [modules/java_bindings_generator/CMakeFiles/gen_opencv_java_source.dir/all] 错误 2 make: *** [all] 错误 2
查看错误信息,发现错误出现在gen_java.py文件处,解决办法:
将gen_java.py源文件中的31行:
assert path[-3:] != '.in', path
修改为:
assert path[-4:] != '.in', path
即可,然后重新make,在同样文件里出现了编码错误:
[ 7%] Generate files for Java bindings JAVA: Processing OpenCV modules: 12 duplicated: CLASS cv::.Algorithm : SKIP:void read(FileNode fn) due to ARG typeFileNode/I SKIP:void write(Ptr_FileStorage fs, String name = String()) due to ARG typePtr_FileStorage/I Traceback (most recent call last): File "/home/gu/文档/opencv-4.5.0/modules/java/generator/../generator/gen_java.py", line 1164, in <module> generator.gen(srcfiles, module, dstdir, jni_path, java_path, common_headers) File "/home/gu/文档/opencv-4.5.0/modules/java/generator/../generator/gen_java.py", line 492, in gen self.save(cpp_file, T_CPP_MODULE.substitute(m = module, M = module.upper(), code = moduleCppCode.getvalue(), includes = " ".join(includes))) File "/home/gu/文档/opencv-4.5.0/modules/java/generator/../generator/gen_java.py", line 514, in save f.write(buf) UnicodeEncodeError: 'ascii' codec can't encode characters in position 215-216: ordinal not in range(128) make[2]: *** [CMakeFiles/dephelper/gen_opencv_java_source] 错误 1 make[1]: *** [modules/java_bindings_generator/CMakeFiles/gen_opencv_java_source.dir/all] 错误 2 make: *** [all] 错误 2
1.修改源文件:
将gen_java.py文件的514行
f.write(buf)
修改为:
f.write(buf.encode("utf-8").decode('utf-8'))
2.检查python版本:
确保正在使用Python3运行该脚本,可以在终端下运行以下命令:
python --version python3 --version
如果有Python 2 和Python 3,确保使用
(非必要)若存在Python 2.7,需要进行卸载
1.检查系统中的Python版本
首先,检查是否已安装
python --version
2.卸载Python 2.7
sudo apt-get remove python2.7 sudo apt-get autoremove
3.验证Python已成功卸载
为了确认
python --version
4.卸载Python 2.7的备份文件
在卸载过程中,有些Python 2.7的备份文件可能还残留在系统中。为了彻底卸载
sudo find / -name python2.7
这条命令将会搜索系统中所有以
sudo rm -rf /path/to/file_or_directory
请将
清理残余文件和目录
现在,我们已经从系统中成功地卸载了
sudo updatedb sudo locate -b '.pyc' | xargs sudo rm sudo rm -rf /usr/lib/python2.7/ sudo rm -rf /usr/include/python2.7/ sudo rm -rf /usr/share/python-wheels/ sudo rm -rf /usr/local/lib/python2.7/ sudo rm -rf /usr/local/include/python2.7/ sudo rm -rf /usr/local/bin/easy_install* sudo rm -rf /usr/local/bin/pip* sudo rm -rf /usr/local/bin/pydoc* sudo rm -rf /usr/local/bin/python* sudo rm -rf /usr/local/share/python/ sudo rm -rf /usr/bin/python2.7*
这样,我们就成功卸载了Python 2.7。但是,又出现了新的问题,因为在编译
/bin/sh: python: not found
原因
python原本映射的是
解决办法:
解决办法
命令行执行:
ls -l /usr/bin/python*
然后会出现:
lrwxrwxrwx 1 root root 9 12月 27 19:10 /usr/bin/python3 -> python3.8 -rwxr-xr-x 1 root root 5465880 11月 22 18:22 /usr/bin/python3.8 lrwxrwxrwx 1 root root 33 11月 22 18:22 /usr/bin/python3.8-config -> x86_64-linux-gnu-python3.8-config lrwxrwxrwx 1 root root 16 3月 13 2020 /usr/bin/python3-config -> python3.8-config -rwxr-xr-x 1 root root 384 1月 25 2023 /usr/bin/python3-futurize -rwxr-xr-x 1 root root 388 1月 25 2023 /usr/bin/python3-pasteurize
我们发现确实没有
我们执行:
sudo ln -s /usr/bin/python3 /usr/bin/python
然后使用
lrwxrwxrwx 1 root root 16 12月 28 22:02 /usr/bin/python -> /usr/bin/python3 lrwxrwxrwx 1 root root 9 12月 27 19:10 /usr/bin/python3 -> python3.8 -rwxr-xr-x 1 root root 5465880 11月 22 18:22 /usr/bin/python3.8 lrwxrwxrwx 1 root root 33 11月 22 18:22 /usr/bin/python3.8-config -> x86_64-linux-gnu-python3.8-config lrwxrwxrwx 1 root root 16 3月 13 2020 /usr/bin/python3-config -> python3.8-config -rwxr-xr-x 1 root root 384 1月 25 2023 /usr/bin/python3-futurize -rwxr-xr-x 1 root root 388 1月 25 2023 /usr/bin/python3-pasteurize
回到
/usr/local/bin/python3:解释器错误: 没有那个文件或目录
因此再重新链接python 3:
sudo ln -sf /usr/bin/python3 /usr/local/bin/python3
问题解决。
三、参考链接
https://blog.csdn.net/m0_58235748/article/details/132165883
https://pythonjishu.com/zbriglwwgmeiiim/
https://tool.4xseo.com/a/15653.html
https://www.python100.com/html/72G0U877HGYA.html
https://blog.51cto.com/u_14558366/5178939