OpenCV 2.4.10を仮想マシン上のUbuntu 14.04 LTS 32bitにインストールしました. OpenCVをLinux環境にインストールしたのは初めてで,ちゃんとインストールできるまでにかなり時間がかかってしまいました…. 作業手順をメモしておきます. 掲載内容に間違いがありましたら,コメントにてご指摘いただければ幸いです.
インストール前の準備
- 既存環境を最新状態に更新する
- OpenCVのソースファイルをダウンロードする
- ソースにパッチを適用する(32bit環境のみ)
下記コマンドを実行しました.
1 2 | ~$ sudo apt-get update ~$ sudo apt-get upgrade |
ホームディレクトリに「OpenCV」ディレクトリを作成し,OpenCVのソースをダウンロードしました. ダウンロードした圧縮ファイルは,展開しておきました.
1 2 3 4 | ~$ mkdir OpenCV ~$ cd OpenCV/ OpenCV$ wget http: //sourceforge .net /projects/opencvlibrary/files/opencv-unix/2 .4.10 /opencv-2 .4.10.zip OpenCV$ unzip opencv-2.4.10.zip |
Ubuntu 14.04 LTS 32bitではmakeがエラー終了してしまいました. 対処方法は,core: fix x86 PIC code compilationで紹介されています. 「opencv-2.4.10_system.cpp.patch」というファイル名で,下記コードをホームディレクトリに保存してください.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | *** ./OpenCV/opencv-2.4.10/modules/core/src/system.cpp 2014-10-01 16:33:36.000000000 +0900 --- ./system.cpp 2014-12-08 08:25:38.178802775 +0900 *************** *** 273,280 **** "movl $7,%%eax\n\t" "movl $0,%%ecx\n\t" "cpuid\n\t" "popl %%ebx\n\t" ! : "=a"(cpuid_data[0]), "=b"(cpuid_data[1]), "=c"(cpuid_data[2]), "=d"(cpuid_data[3]) : : "cc" ); --- 273,281 ---- "movl $7,%%eax\n\t" "movl $0,%%ecx\n\t" "cpuid\n\t" + "movl %%ebx,%1\n\t" "popl %%ebx\n\t" ! : "=a"(cpuid_data[0]), "=r"(cpuid_data[1]), "=c"(cpuid_data[2]), "=d"(cpuid_data[3]) : : "cc" ); |
ホームディレクトリに戻り,パッチを適用してください.
1 2 | OpenCV$ cd ~ ~$ patch -p0 < opencv-2.4.10_system.cpp.patch |
インストール
シェルスクリプトの準備
作業自動化のため,下記のWebサイトを参考に,シェルスクリプトを作成しました. 下記コードを,「opencv-2.4.10.sh」というファイル名で,ホームディレクトリに保存してください.
[変更 2015/03/18] cmakeコマンドのオプション記述を変更しました.改行位置を変更したのみで,シェルスクリプトの動作に影響はありません.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #!/bin/sh echo "Installing Dependenices" sudo apt-get update # Build Tools sudo apt-get -y install build-essential checkinstall cmake pkg-config yasm # GUI sudo apt-get -y install libqt4-dev libgtk2.0-dev # Media & Video sudo apt-get -y install libjpeg-dev libpng-dev libtiff5-dev libjasper-dev sudo apt-get -y install libavcodec-dev libavformat-dev libswscale-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev libxine-dev sudo apt-get -y install libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev sudo apt-get -y install x264 v4l-utils # Parallelism and linear algebra libraries sudo apt-get -y install libtbb-dev libeigen3-dev # Python sudo apt-get -y install python-dev python-numpy echo "Installing OpenCV 2.4.10" cd ~ /OpenCV/opencv-2 .4.10 mkdir release cd release cmake \ -DCMAKE_BUILD_TYPE=RELEASE \ -DCMAKE_INSTALL_PREFIX= /usr/local \ -DBUILD_EXAMPLES=ON \ -DINSTALL_C_EXAMPLES=ON \ -DINSTALL_PYTHON_EXAMPLES=ON \ -DWITH_1394=OFF \ -DWITH_OPENGL=ON \ -DWITH_QT=ON \ -DWITH_TBB=ON \ -DWITH_V4L=ON \ -DWITH_XINE=ON \ .. make sudo make install sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf' sudo ldconfig echo "OpenCV 2.4.10 ready to be used" |
シェルスクリプトの実行
シェルスクリプトを実行しました. 実行中,パスワードの入力を求められる場合があります. 私の環境では,実行完了まで1時間弱かかりました.
1 2 | ~$ chmod +x opencv-2.4.10.sh ~$ . /opencv-2 .4.10.sh 2>&1 | tee . /opencv-2 .4.10_install.log |
(参考)cmakeの実行結果
私の環境でのcmakeの実行結果は下記のとおりです. ホームディレクトリに作成されるログファイル「opencv-2.4.10_install.log」で確認できます.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | -- General configuration for OpenCV 2.4.10 ===================================== -- Version control: unknown -- -- Platform: -- Host: Linux 3.13.0-24-generic i686 -- CMake: 2.8.12.2 -- CMake generator: Unix Makefiles -- CMake build tool: /usr/bin/make -- Configuration: RELEASE -- -- C/C++: -- Built as dynamic libs?: YES -- C++ Compiler: /usr/bin/c++ (ver 4.8.2) -- C++ flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -Wno-delete-non-virtual-dtor -fdiagnostics-show-option -pthread -march=i686 -fomit-frame-pointer -msse -msse2 -msse3 -mfpmath=sse -ffunction-sections -O2 -DNDEBUG -DNDEBUG -- C++ flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -Wno-delete-non-virtual-dtor -fdiagnostics-show-option -pthread -march=i686 -fomit-frame-pointer -msse -msse2 -msse3 -mfpmath=sse -ffunction-sections -g -O0 -DDEBUG -D_DEBUG -- C Compiler: /usr/bin/cc -- C flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wno-narrowing -fdiagnostics-show-option -pthread -march=i686 -fomit-frame-pointer -msse -msse2 -msse3 -mfpmath=sse -ffunction-sections -O2 -DNDEBUG -DNDEBUG -- C flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wno-narrowing -fdiagnostics-show-option -pthread -march=i686 -fomit-frame-pointer -msse -msse2 -msse3 -mfpmath=sse -ffunction-sections -g -O0 -DDEBUG -D_DEBUG -- Linker flags (Release): -- Linker flags (Debug): -- Precompiled headers: YES -- -- OpenCV modules: -- To be built: core flann imgproc highgui features2d calib3d ml video legacy objdetect photo gpu ocl nonfree contrib python stitching superres ts videostab -- Disabled: world -- Disabled by dependency: - -- Unavailable: androidcamera dynamicuda java viz -- -- GUI: -- QT 4.x: YES (ver 4.8.6 EDITION = OpenSource) -- QT OpenGL support: YES (/usr/lib/i386-linux-gnu/libQtOpenGL.so) -- OpenGL support: YES (/usr/lib/i386-linux-gnu/libGLU.so /usr/lib/i386-linux-gnu/libGL.so /usr/lib/i386-linux-gnu/libSM.so /usr/lib/i386-linux-gnu/libICE.so /usr/lib/i386-linux-gnu/libX11.so /usr/lib/i386-linux-gnu/libXext.so) -- VTK support: NO -- -- Media I/O: -- ZLib: /usr/lib/i386-linux-gnu/libz.so (ver 1.2.8) -- JPEG: /usr/lib/i386-linux-gnu/libjpeg.so (ver ) -- PNG: /usr/lib/i386-linux-gnu/libpng.so (ver 1.2.50) -- TIFF: /usr/lib/i386-linux-gnu/libtiff.so (ver 42 - 4.0.3) -- JPEG 2000: /usr/lib/i386-linux-gnu/libjasper.so (ver 1.900.1) -- OpenEXR: build (ver 1.7.1) -- -- Video I/O: -- DC1394 1.x: NO -- DC1394 2.x: NO -- FFMPEG: YES -- codec: YES (ver 54.35.0) -- format: YES (ver 54.20.4) -- util: YES (ver 52.3.0) -- swscale: YES (ver 2.1.1) -- gentoo-style: YES -- GStreamer: -- base: YES (ver 0.10.36) -- video: YES (ver 0.10.36) -- app: YES (ver 0.10.36) -- riff: YES (ver 0.10.36) -- pbutils: YES (ver 0.10.36) -- OpenNI: NO -- OpenNI PrimeSensor Modules: NO -- PvAPI: NO -- GigEVisionSDK: NO -- UniCap: NO -- UniCap ucil: NO -- V4L/V4L2: Using libv4l1 (ver 1.0.1) / libv4l2 (ver 1.0.1) -- XIMEA: NO -- Xine: YES (ver 1.1.21) -- -- Other third-party libraries: -- Use IPP: NO -- Use Eigen: YES (ver 3.2.0) -- Use TBB: YES (ver 4.2 interface 7000) -- Use OpenMP: NO -- Use GCD NO -- Use Concurrency NO -- Use C=: NO -- Use Cuda: NO -- Use OpenCL: YES -- -- OpenCL: -- Version: dynamic -- Include path: /home/省略/OpenCV/opencv-2.4.10/3rdparty/include/opencl/1.2 -- Use AMD FFT: NO -- Use AMD BLAS: NO -- -- Python: -- Interpreter: /usr/bin/python2 (ver 2.7.6) -- Libraries: /usr/lib/i386-linux-gnu/libpython2.7.so (ver 2.7.6) -- numpy: /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.8.2) -- packages path: lib/python2.7/dist-packages -- -- Java: -- ant: NO -- JNI: NO -- Java tests: NO -- -- Documentation: -- Build Documentation: NO -- Sphinx: NO -- PdfLaTeX compiler: NO -- -- Tests and samples: -- Tests: YES -- Performance tests: YES -- C/C++ Examples: YES -- -- Install path: /usr/local -- -- cvconfig.h is in: /home/省略/OpenCV/opencv-2.4.10/release -- ----------------------------------------------------------------- |
動作確認
Webカメラの映像を表示するプログラムをコンパイルし,実行してみました. 私の環境では,14~16行目のようにフレームサイズとフレームレートを設定しなかった場合,端末に「select timeout」と表示されてWebカメラの映像を表示できませんでした. Webカメラは720p,30fpsにも対応している,Logicool HD Webcam C270です. 仮想マシン上で実行していることなど,環境依存の問題なのではないかと考えていますが,表示できなかった理由はわかっていません….
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include <iostream> #include <opencv2/opencv.hpp> using namespace std; using namespace cv; int key = -1; int main( int argc, char *argv[]) { VideoCapture cap(0); Mat frame; cap.set(CV_CAP_PROP_FRAME_WIDTH, 320); cap.set(CV_CAP_PROP_FRAME_HEIGHT, 240); cap.set(CV_CAP_PROP_FPS, 5); if (!cap.isOpened()){ cout << "webcam is not detected" << endl; return 1; } cout << "webcam is detected" << endl; while (key != 'q' ){ namedWindow( "webcam" , 1); cap >> frame; imshow( "webcam" , frame); key = waitKey(30); } return 0; } |
上記コードを「webcam.cpp」というファイル名で保存し,コンパイルと実行を行いました.
1 2 | work$ g++ -o webcam webcam.cpp `pkg-config --cflags opencv` `pkg-config --libs opencv` work$ . /webcam |
実行例です.ウィンドウがMac OS X風ですが,Ubuntuです.
以上,参考になれば幸いです.
製品紹介
売り上げランキング: 736
0 件のコメント:
コメントを投稿