CentOS 7: OpenCV 3.1をビルドする

Table of Contents

1 OpenCVのダウンロード


sudo yum-builddep -y opencv-develgit clone opencvgit checkout 3.1.0 -b 3.1.0

2 CMakeFileの変更


[ 24%] Built target pch_Generate_opencv_videoioLinking CXX shared library ../../lib/libopencv_videoio.so/usr/bin/ld: /usr/local/include/../lib/libswscale.a(swscale.o):relocation R_X86_64_PC32 against symbol `ff_M24A’ can not be used whenmaking a shared object; recompile with -fPIC/usr/bin/ld: final link failed: Bad valuecollect2: error: ld returned 1 exit statusmake[2]: *** [lib/libopencv_videoio.so.3.1.0] Error 1make[1]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/all] Error 2make: *** [all] Error 2


$ sed -i -e ‘s/libavformat.a/libavformat.so/g’ -e ‘s/libavutil.a/libavutil.so/g’ -e ‘s/libswscale.a/libswscale.so/g’ -e ‘s/libavresample.a/libavresample.so/g’ -e ‘s/libavcodec.a/libavcodec.so/g’ cmake/OpenCVFindLibsVideo.cmake

3 OpenCVのビルド


$ cmake -G ‘Unix Makefiles’ -DWITH_FFMPEG=ON -DWITH_GSTREAMER=OFF -DCMAKE_BUILD_TYPE=RELEASE$ make && sudo make install

4 動作確認


#!/usr/bin/env pythonimport sysimport cv2def create_thumbnail(movie_name, image_name): capture = cv2.VideoCapture(movie_name) max_frame = int(capture.get(cv2.CAP_PROP_FRAME_COUNT)) half_frame = max_frame // 2 capture.set(cv2.CAP_PROP_POS_FRAMES, half_frame) retval, image = capture.read() if not retval: return False cv2.imwrite(image_name, image) return Trueif __name__ == ‘__main__’: argv = sys.argv if len(argv) != 3: print(‘Create thumbnail of movie at half position.’) print(‘usage: %s [Input movie] [Output thumbnail]’ % argv[0]) exit(1) create_thumbnail(argv[1], argv[2])


$ PYTHONPATH=/usr/local/lib/python2.7/site-packages python create-thumbnail.py input.mkv output.png$ file output.pngoutput.png: PNG image data, 640 x 360, 8-bit/color RGB, non-interlaced

Android | Linux | SDL - Narrow Escape