DeepFlowest

[Docker] Ubuntu18.04에서 Opencv 3.4.2설치 본문

Docker

[Docker] Ubuntu18.04에서 Opencv 3.4.2설치

Orange57 2020. 12. 8. 10:37
728x90
반응형
SMALL

OpenCV 설치 전 준비


1. sudo 설치

OpenCV 설치할 때 첫 단계로 

$ sudo apt-get update 를 해야 하는데,

sudo가 설치가 안 되어있기 때문에 다음과 같은 오류가 뜬다.

==> sudo 설치 및 사용자 설정이 필요하다.

# sudo 설치
$ apt-get update && apt-get install -y sudo

# 사용자 계정 추가 (사용자명 : user)
$ adduser --disabled-password --gecos "" user \ && echo 'user:user' | chpasswd \ && adduser user sudo \ && echo 'user ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers

2. wget 설치

$ sudo apt-get install wget

 

 


OpenCV 3.4.2 설치


1. Prerequisites 설치

$ sudo apt-get update && \
sudo apt-get upgrade && \
sudo apt-get install build-essential cmake unzip pkg-config && \
sudo apt-get install libjpeg-dev libpng-dev libtiff-dev && \
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev v4l-utils libxvidcore-dev libx264-dev libxine2-dev && \
sudo apt-get install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev && \
sudo apt-get install libgtk-3-dev && \
sudo apt-get install mesa-utils libgl1-mesa-dri libgtkgl2.0-dev libgtkglext1-dev && \
sudo apt-get install libatlas-base-dev gfortran libeigen3-dev && \
sudo apt-get install python2.7-dev python3-dev python-numpy python3-numpy

2. OpenCV 3.4.2 컴파일 및 설치

$ mkdir opencv
$ cd opencv

$ wget -O opencv.zip https://github.com/opencv/opencv/archive/3.4.2.zip && \
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/3.4.2.zip

$ unzip opencv.zip && \
unzip opencv_contrib.zip

$ cd opencv-3.4.2
$ mkdir build && cd build

방법1) 파이썬 경로 설정 X

 

$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=OFF -D WITH_IPP=OFF -D WITH_1394=OFF -D BUILD_WITH_DEBUG_INFO=OFF -D BUILD_DOCS=OFF -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=OFF -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D WITH_QT=OFF -D WITH_GTK=ON -D WITH_OPENGL=ON -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.4.2/modules -D WITH_V4L=ON  -D WITH_FFMPEG=ON -D WITH_XINE=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D OPENCV_GENERATE_PKGCONFIG=ON ../

 

방법2) 파이썬 경로 설정 O

 

아래 코드는 
$ which python 결과 나온 경로를 입력해준 코드.
/opt/conda/bin/python

 

$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=OFF -D WITH_IPP=OFF -D WITH_1394=OFF -D BUILD_WITH_DEBUG_INFO=OFF -D BUILD_DOCS=OFF -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=OFF -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D WITH_QT=OFF -D WITH_GTK=ON -D WITH_OPENGL=ON -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.4.2/modules -D WITH_V4L=ON  -D WITH_FFMPEG=ON -D WITH_XINE=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D OPENCV_GENERATE_PKGCONFIG=ON -D PYTHON3_EXECUTABLE=/opt/conda/bin/python -D PYTHON3_INCLUDE_DIR=/opt/conda/include/python3.8 -D PYTHON3_PACKAGES_PATH=/opt/conda/lib/python3.8/site-packages -D PYTHON3_LIBRARY=/opt/conda/lib/libpython3.8.so ../

아니면 우분투에서 다음과 같이 파이썬 경로 설정하고 할 수 있음.

cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D WITH_TBB=OFF \
-D WITH_IPP=OFF \
-D WITH_1394=OFF \
-D BUILD_WITH_DEBUG_INFO=OFF \
-D BUILD_DOCS=OFF \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D BUILD_EXAMPLES=OFF \
-D BUILD_TESTS=OFF \
-D BUILD_PERF_TESTS=OFF \
-D WITH_QT=OFF \
-D WITH_GTK=ON \
-D WITH_OPENGL=ON \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.4.2/modules \
-D WITH_V4L=ON  \
-D WITH_FFMPEG=ON \
-D WITH_XINE=ON \
-D BUILD_NEW_PYTHON_SUPPORT=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D PYTHON2_INCLUDE_DIR=/usr/include/python2.7 \
-D PYTHON2_NUMPY_INCLUDE_DIRS=/usr/lib/python2.7/dist-packages/numpy/core/include/ \
-D PYTHON2_PACKAGES_PATH=/usr/lib/python2.7/dist-packages \
-D PYTHON2_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython2.7.so \
-D PYTHON3_INCLUDE_DIR=/usr/include/python3.6m \
-D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/lib/python3/dist-packages/numpy/core/include/  \
-D PYTHON3_PACKAGES_PATH=/usr/lib/python3/dist-packages \
-D PYTHON3_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.6m.so \
../

 

 

# 컴퓨터의 CPU 코어수 확인

$ cat /proc/cpuinfo | grep processor | wc -l

# 컴파일

(코어 수  : 8)

$ time make -j8

# 컴파일 결과물 설치

$ sudo make install

# /etc/ld.so.conf.d/ 디렉토리에 /usr/local/lib를 포함하는 설정파일이 있는지 확인

$ cat /etc/ld.so.conf.d/*

* /usr/local/lib이 출력되지 않았다면 다음 명령을 추가로 실행

$ sudo sh -c 'echo '/usr/local/lib' > /etc/ld.so.conf.d/opencv.conf'

# 컴파일시 opencv  라이브러리를 찾을 수 있도록 다음 명령을 실행

$ sudo ldconfig

3. OpenCV 3.4.2 설치 결과 확인

$ pkg-config --modversion opencv

 

※ 만약, python 안에서 import cv2가 안 된다면, 다음 명령어로 파이썬 용 설치하기

$ pip3 install opencv-contrib-python==3.4.2

$ pip install opencv-contrib-python==3.4.2


4. 컴파일에 사용했던 opencv 소스코드 디렉토리 삭제

$ cd
$ rm -rf opencv

 

 

 

참고 : webnautes.tistory.com/1186?category=704653

728x90
반응형
LIST
Comments