centos7上安装opencv便于java使用
时间: 2024-09-21 10:05:56 浏览: 47
在CentOS 7上安装OpenCV以便用于Java,通常涉及几个步骤:
1. **更新系统**:
执行 `sudo yum update` 更新系统并安装必要的开发工具。
2. **安装依赖**:
由于OpenCV本身包含一些依赖项,首先安装它们:
```bash
sudo yum install -y gcc-c++ cmake libjpeg-turbo-devel libtiff-devel libjasper-devel libpng-devel libavcodec-devel libavformat-devel libswscale-devel libavutil-devel openexr-devel freetype-devel gstreamer-plugins-base-devel gstreamer1-plugins-good-devel
```
3. **下载OpenCV源码**:
下载最新版的OpenCV源码 (https://opencv.org/releases/) 或者克隆GitHub仓库:
```bash
wget https://github.com/opencv/opencv/archive/<opencv_version>.tar.gz
tar -zxvf <opencv_version>.tar.gz
cd opencv-<opencv_version>
```
4. **构建OpenCV**:
创建一个build目录,并进入:
```bash
mkdir build && cd build
cmake ..
```
接下来选择合适的构建选项,如 `-DCMAKE_BUILD_TYPE=RELEASE` 缩减编译时间。然后编译和安装:
```bash
make && sudo make install
```
5. **配置Java支持**:
安装`javacv`库,它是OpenCV与Java交互的桥梁:
```bash
git clone https://github.com/bytedeco/javacv.git
cd javacv
mvn install
```
6. **添加环境变量**:
将OpenCV的include路径和库路径添加到`JAVA_OPTS`环境变量中:
```bash
echo 'export JAVA_OPTS="$JAVA_OPTS -Djava.library.path=/usr/local/lib" -Djava.ext.dirs=/usr/local/lib/jni' >> /etc/profile.d/opencv.sh
source /etc/profile.d/opencv.sh
```
现在OpenCV已经准备好供Java使用了。记得导入适当的`javacv`包并在Java项目中引用它。
阅读全文