【opencv】さくらのレンタルサーバーにopencv-4.5.1をインストール

OpenCV

概要

以下を見ながらイントールしました。

OpenCV: Installation in Linux

インストール

設定

opencvのディレクトリを作成して、掲載されている順にインストールします。

wget -O opencv.zip https://github.com/opencv/opencv/archive/master.zip
unzip opencv.zip

# Create build directory
mkdir -p build && cd build

# Configure
cmake  ../opencv-master

# Build
cmake --build .

以下のメッセージが出力され、設定が出来ます。

~ 省略 ~

--   OpenCV modules:
--     To be built:                 calib3d core dnn features2d flann gapi highgui imgcodecs imgproc ml objdetect photo stitching ts video videoio
--     Disabled:                    world
--     Disabled by dependency:      -
--     Unavailable:                 java python2 python3
--     Applications:                tests perf_tests apps
--     Documentation:               NO
--     Non-free algorithms:         NO
--
--   GUI:
--     GTK+:                        NO
--     VTK support:                 NO
--
--   Media I/O:
--     ZLib:                        /usr/lib/libz.so (ver 1.2.11)
--     JPEG:                        /usr/local/lib/libjpeg.so (ver 80)
--     WEBP:                        /usr/local/lib/libwebp.so (ver encoder: 0x020f)
--     PNG:                         /usr/local/lib/libpng.so (ver 1.6.37)
--     TIFF:                        /usr/local/lib/libtiff.so (ver 42 / 4.1.0)
--     JPEG 2000:                   OpenJPEG (ver 2.3.1)
--     OpenEXR:                     build (ver 2.3.0)
--     HDR:                         YES
--     SUNRASTER:                   YES
--     PXM:                         YES
--     PFM:                         YES
--
--   Video I/O:
--     DC1394:                      NO
--     FFMPEG:                      NO
--       avcodec:                   NO
--       avformat:                  NO
--       avutil:                    NO
--       swscale:                   NO
--       avresample:                NO
--     GStreamer:                   NO
--     v4l/v4l2:                    NO
--
--   Parallel framework:            pthreads

~ 省略 ~

コンパイル

make

コンパイルすると、さっそく、エラーが出力されます。

リンクの際、memalignという関数が無いようです。memalignは古い関数で、使用されなくなっていました。

[ 26%] Built target IlmImf
[ 28%] Built target opencv_imgcodecs
[ 29%] Built target opencv_videoio
[ 29%] Built target opencv_highgui
[ 30%] Built target opencv_ts
[ 30%] Linking CXX executable ../../bin/opencv_perf_core
../../lib/libopencv_imgproc.so.4.5.2: undefined reference to `memalign'
c++: error: linker command failed with exit code 1 (use -v to see invocation)
*** Error code 1

コンパイル時にWarnigが出でいたので、ソースをエディターで修正します。

emacs /home/opencv/src/build/3rdparty/ippicv/ippicv_lnx/iw/src/iw_own.c

以下のように、<stdlib.h>を追加し、posix_memalignを呼び出すように変更し、再度、コンパイルします。コンパイルには、しばらく時間がかかります。

/* ファイルの先頭あたり */
#include <stdlib.h>

/* 84行目あたり */
#else
    void *ptr;
    posix_memalign(&ptr, iAlign, iSize);
    return ptr;
    //return memalign(iAlign, iSize);
#endif

無事、コンパイルが終了しました。

[ 99%] Linking CXX executable ../../bin/opencv_visualisation
[ 99%] Built target opencv_visualisation
[ 99%] Linking CXX executable ../../bin/opencv_interactive-calibration
[100%] Built target opencv_interactive-calibration
[100%] Linking CXX executable ../../bin/opencv_version
[100%] Built target opencv_version

make intallでインストールされるのですが、デフォルトでは、以下の場所にインストールされてしまいます。

By default OpenCV will be installed to the /usr/local directory, all files will be copied to following locations:

/usr/local/bin - executable files
/usr/local/lib - libraries (.so)
/usr/local/cmake/opencv4 - cmake package
/usr/local/include/opencv4 - headers
/usr/local/share/opencv4 - other files (e.g. trained cascades in XML format)

さくらのレンタルサーバーでは、ローカルのディレクトリ(/home/userName/local)にインストールします。cmakeで以下のコマンドを実行します。

プログラムが /home/userName/local にインストールされます。

cmake -DCMAKE_INSTALL_PREFIX=/home/userName/local ../opencv-master/

make install
スポンサーリンク
OpenCVサクラのレンタルサーバー
Engineerをフォローする
レンサバ