feat: add symlink sh
parent
97158ca5f7
commit
5b7cc14e76
93
README.md
93
README.md
|
@ -0,0 +1,93 @@
|
||||||
|
|
||||||
|
# 在ROS中重新编译和配置OpenCV 4.2版本
|
||||||
|
|
||||||
|
## 背景问题
|
||||||
|
1. GTK的依赖与ORB中的GTK依赖有冲突。
|
||||||
|
2. 如果需要新增加模块且不希望系统中有多份OpenCV,可以按照以下方法重新编译OpenCV并重置系统中的OpenCV。
|
||||||
|
|
||||||
|
## 步骤
|
||||||
|
|
||||||
|
### 1. 下载OpenCV源码和OpenCV-contrib源码
|
||||||
|
- OpenCV: [opencv 4.2.0](https://opencv.org/blog/opencv-4-2-0/) 或者 [GitHub](https://github.com/opencv/opencv/releases/tag/4.2.0)
|
||||||
|
- OpenCV-contrib: [GitHub](https://github.com/opencv/opencv_contrib/releases/tag/4.2.0)
|
||||||
|
|
||||||
|
### 2. 编译OpenCV和contrib库
|
||||||
|
推荐安装cmake-gui:
|
||||||
|
```sh
|
||||||
|
sudo apt-get install cmake-gui
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 2.1 更改`opencv/3rdparty/carotene/CMakeList.txt`
|
||||||
|
将以下内容添加到文件中:
|
||||||
|
```cmake
|
||||||
|
set_source_files_properties(${carotene_sources} COMPILE_FLAGS "--param ipa-cp-unit-growth=100000 --param inline-unit-growth=100000 --param large-stack-frame-growth=5000")
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 2.2 配置CMake编译选项
|
||||||
|
选择安装模块和安装路径,注意路径不要设置为系统路径,后面会手动拷贝到系统目录中:
|
||||||
|
```sh
|
||||||
|
cmake -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules-4.2.0 \
|
||||||
|
-DWITH_CUDA=ON \
|
||||||
|
-DOPENCV_GENERATE_PKGCONFIG=TRUE \
|
||||||
|
-DCMAKE_INSTALL_PREFIX=/home/autolabor/ThirdParty/opencv-build \
|
||||||
|
-DBUILD_TESTS=OFF \
|
||||||
|
-DBUILD_opencv_sfm=OFF ..
|
||||||
|
```
|
||||||
|
|
||||||
|
或者通过cmake-gui选择这些编译选项,但在generate阶段可能会报错,可以作为参考使用。
|
||||||
|
|
||||||
|
#### 2.3 编译和安装
|
||||||
|
```sh
|
||||||
|
sudo make install -j8
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. 覆盖系统OpenCV库
|
||||||
|
|
||||||
|
#### 3.1 创建软链接脚本
|
||||||
|
编译完成后会出现几个文件夹,创建一个软链接的脚本,脚本内容如下:
|
||||||
|
|
||||||
|
#### 3.2 拷贝文件夹内容
|
||||||
|
分别将右侧的`bin`、`include`、`lib`、`share`中的内容拷贝到:
|
||||||
|
- `/build/bin` -> `/usr/bin`
|
||||||
|
- `/build/include/opencv4` -> `/usr/include/opencv4`
|
||||||
|
- `/build/share/opencv4` -> `/usr/share/opencv4`
|
||||||
|
- `/build/lib/*.4.2.0` -> `/usr/lib/aarch64-linux-gnu/`
|
||||||
|
|
||||||
|
拷贝完成后,执行`create_link.sh`脚本,添加so库的链接 `*.so -> *.so.4.2`, `*.so.4.2 -> *.so.4.2.0`
|
||||||
|
|
||||||
|
#### 3.3 配置pkgconfig
|
||||||
|
将`/build/lib/pkgconfig/opencv4.pc` 拷贝到 `/usr/lib/aarch64-linux-gnu/pkgconfig`中。
|
||||||
|
|
||||||
|
这样使用`pkg-config --modversion opencv4`,就可以看到4.2.0的输出了。
|
||||||
|
|
||||||
|
#### 3.4 配置CMake文件
|
||||||
|
将`/build/lib/cmake/opencv4/*.cmake` 拷贝到 `/usr/lib/aarch64-linux-gnu/cmake/opencv4/`中。
|
||||||
|
|
||||||
|
修改`OpenCVConfig.cmake`文件,将`OpenCV_INSTALL_PATH`目录多添加一层`../`,即指向`/usr`。
|
||||||
|
|
||||||
|
修改`OpenCVModules-release.cmake`文件,将所有的`/lib/`替换为`/aarch64-linux-gnu/`。
|
||||||
|
|
||||||
|
这样就完成了CMake的配置,在调用`find_package(OpenCV)`时,会调用这几个CMake文件。
|
||||||
|
|
||||||
|
#### 3.5 配置Python
|
||||||
|
将`/build/lib/python3.8/dist-package/cv2/python3.8`中的`*.so`拷贝到
|
||||||
|
`/usr/lib/python3.8/dist-package/cv2/python3.8`中。
|
||||||
|
|
||||||
|
同样,将Python2.7的上述路径进行覆盖。
|
||||||
|
|
||||||
|
此时,就可以在Python中调用`import cv2`了。
|
||||||
|
|
||||||
|
|
||||||
|
### 示例代码
|
||||||
|
```sh
|
||||||
|
# 创建软链接的脚本 (create_link.sh)
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
ln -sf /usr/lib/aarch64-linux-gnu/libopencv_core.so.4.2 /usr/lib/aarch64-linux-gnu/libopencv_core.so
|
||||||
|
ln -sf /usr/lib/aarch64-linux-gnu/libopencv_imgproc.so.4.2 /usr/lib/aarch64-linux-gnu/libopencv_imgproc.so
|
||||||
|
# 添加其他需要的软链接
|
||||||
|
|
||||||
|
# 运行脚本
|
||||||
|
chmod +x create_link.sh
|
||||||
|
sudo ./create_link.sh
|
||||||
|
```
|
|
@ -0,0 +1,21 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# 目标目录
|
||||||
|
TARGET_DIR="/usr/lib/aarch64-linux-gnu"
|
||||||
|
|
||||||
|
# 遍历所有 .so.4.2.0 文件并创建相应的符号链接
|
||||||
|
for file in ${TARGET_DIR}/libopencv_*.so.4.2.0; do
|
||||||
|
base_name=$(basename ${file} .4.2.0)
|
||||||
|
|
||||||
|
# 创建 .so.4.2 的符号链接
|
||||||
|
if [ ! -L "${TARGET_DIR}/${base_name}.4.2" ]; then
|
||||||
|
ln -s ${file} ${TARGET_DIR}/${base_name}.4.2
|
||||||
|
echo "Created link: ${TARGET_DIR}/${base_name}.4.2 -> ${file}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 创建 .so 的符号链接
|
||||||
|
if [ ! -L "${TARGET_DIR}/${base_name}" ]; then
|
||||||
|
ln -s ${base_name}.4.2 ${TARGET_DIR}/${base_name}
|
||||||
|
echo "Created link: ${TARGET_DIR}/${base_name} -> ${TARGET_DIR}/${base_name}.4.2"
|
||||||
|
fi
|
||||||
|
done
|
Loading…
Reference in New Issue