【教程】CentOS7 編譯安裝opencv3.0.0
阿新 • • 發佈:2018-11-25
《CentOS7 編譯安裝opencv3.0.0》
開發環境
- CentOS7 x64
- gcc 4.8.5
- vmware10虛擬機器
注意:本教程均在root許可權下進行!
安裝教程
- 安裝依賴庫
yum -y install gcc gcc-c++ gtk2-devel gimp-devel gimp-devel-tools gimp-help-browser zlib-devel libtiff-devel libjpeg-devel libpng-devel gstreamer-devel libavc1394-devel libraw1394-devel libdc1394-devel jasper-devel jasper-utils swig python libtool nasm numpy
- 安裝cmake
yum -y install cmake-gui
- 配置
下載opencv3.0.0原始碼包:https://opencv.org/releases.html
- 建立安裝目錄
mkdir /opt/opencv-3.0.0
cd /opt/opencv-3.0.0
然後將opencv原始碼包放到該目錄下。 - 解壓
unzip opencv-3.0.0.zip
cd opencv-3.0.0
- 配置
- 在opencv目錄下建立編譯目錄
mkdir build
- 啟動cmake
cmake-gui
- 選中目錄後點擊config
- 選擇Makefiles工程
- 下載圖中顯示的檔案時很慢,如果長時間卡住或者出錯,手動下載該檔案放入到目錄:
opencv-3.0.0\3rdparty\ippicv\downloads\linux-8b449a536a2157bcad08a2b9f266828b
下,然後重新Configure即可。
下載地址:https://sourceforge.net/projects/opencvlibrary/files/3rdparty/ippicv/
- 配置
- 取消勾選:WITH_CUDA和WITH_IPP
- 勾選:WITH_OPENGL
- 修改CMAKE_BUILD_TYPE:選擇Release,這是編譯的opencv版本
- 修改CMAKE_INSTALL_PREFIE:改為/opt/opencv-3.0.0,這是opencv編譯完成後的安裝目錄
- 再次config
- 現在沒有了紅色條目,點選Generate生成Makefile工程
- 修改編譯選項檔案,防止編譯出錯
gedit ./cmake/OpenCVCompilerOptions.cmake
- 在opencv目錄下建立編譯目錄
- 建立安裝目錄
- 編譯
cd build
make && make install
等待編譯和安裝完成… - 設定環境變數
echo "/opt/opencv-3.0.0/lib" > /etc/ld.so.conf.d/opencv-3.0.0.conf ldconfig gedit /etc/profile 在最後一行加入:export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/opt/opencv-3.0.0/lib/pkgconfig/ source /etc/profile
- 測試
mkdir /test cd /test touch main.cpp Makefile make ./all
【main.cpp】內容如下
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
Mat img(Size(300, 300), CV_8U, Scalar(255, 0, 0));
imshow("", img);
waitKey(0);
return 0;
}
【Makefile】內容如下:
SRC := $(wildcard *.cpp)
OBJ := $(patsubst %.cpp, %.o, $(SRC))
CC = gcc
CCLINK = g++
CFLAGS = -g -Wall
LIBS = $(shell pkg-config opencv --cflags --libs)
all: $(OBJ)
$(CCLINK) $(CFLAGS) $^ $(LIBS) -o [email protected]
$(OBJ): %.o:%.cpp
$(CC) $(CFLAGS) -c $^ $(LIBS) -o [email protected]
.PHONY:clean
clean:
rm *.o all
ends…