[tip]Обновил для Qt 5.14, OpenCV 4.1 и Ubuntu 18.04[/tip]

Ставим нужные пакеты:

sudo apt update 

sudo apt install build-essential checkinstall git cmake libfaac-dev libjack-jackd2-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libsdl1.2-dev libtheora-dev libva-dev libvdpau-dev libvorbis-dev libx11-dev libxfixes-dev libxvidcore-dev texi2html yasm zlib1g-dev 

sudo apt install libgstreamer1.0-0 libgstreamer1.0-dev gstreamer1.0-tools gstreamer1.0-plugins-base libgstreamer-plugins-base1.0-dev gstreamer1.0-plugins-good gstreamer1.0-plugins-ugly gstreamer1.0-plugins-bad  gstreamer1.0-libav

sudo apt install libgtkglext1 libgtkglext1-dev

Добавим x264 и ffmpeg по вкусу:

sudo apt install nasm
git clone https://code.videolan.org/videolan/x264
cd x264
./configure --enable-shared --enable-pic
make
sudo make install
cd ..
sudo apt-get install libgtk2.0-0 libgtk2.0-dev
sudo apt-get install libjpeg62 libjpeg62-dev libjpeg9 libjpeg9-dev 

git clone https://github.com/FFmpeg/FFmpeg
cd FFmpeg

./configure --enable-gpl --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libx264 --enable-nonfree --enable-postproc --enable-version3 --enable-shared

make
sudo make install
cd ..

Ну и теперь клонируем наш OpenCV, настраиваем:

git clone https://github.com/Itseez/opencv.git OpenCV
cd OpenCV
mkdir build
cd build
export PATH=/home/pavelk/Qt5.14.0/5.14.0/gcc_64/bin:$PATH
cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local/opencv/ -DWITH_OPENGL=ON -DWITH_QT=ON

В PATH только путь на свой замените.

Выхлоп должен быть примерно таким
— Detected processor: x86_64
— Looking for ccache — not found
— Found ZLIB: /usr/lib/x86_64-linux-gnu/libz.so (found suitable version «1.2.11», minimum required is «1.2.3»)
— Could NOT find TIFF (missing: TIFF_LIBRARY TIFF_INCLUDE_DIR)
— Could NOT find Jasper (missing: JASPER_LIBRARIES JASPER_INCLUDE_DIR)
— Found ZLIB: /usr/lib/x86_64-linux-gnu/libz.so (found version «1.2.11»)
— found Intel IPP (ICV version): 2019.0.0 [2019.0.0 Gold] — at: /home/pavelk/OpenCV/build/3rdparty/ippicv/ippicv_lnx/icv
— found Intel IPP Integration Wrappers sources: 2019.0.0
— at: /home/pavelk/OpenCV/build/3rdparty/ippicv/ippicv_lnx/iw
— Could not find OpenBLAS include. Turning OpenBLAS_FOUND off
— Could not find OpenBLAS lib. Turning OpenBLAS_FOUND off
— Could NOT find Atlas (missing: Atlas_CBLAS_INCLUDE_DIR Atlas_CLAPACK_INCLUDE_DIR Atlas_CBLAS_LIBRARY Atlas_BLAS_LIBRARY Atlas_LAPACK_LIBRARY)
— A library with BLAS API not found. Please specify library location.
— LAPACK requires BLAS
— A library with LAPACK API not found. Please specify library location.
— Could NOT find JNI (missing: JAVA_AWT_LIBRARY JAVA_JVM_LIBRARY JAVA_INCLUDE_PATH JAVA_INCLUDE_PATH2 JAVA_AWT_INCLUDE_PATH)
— VTK is not found. Please set -DVTK_DIR in CMake to VTK build directory, or to VTK install subdirectory with VTKConfig.cmake file
— OpenCV Python: during development append to PYTHONPATH: /home/pavelk/OpenCV/build/python_loader
— Checking for module ‘libavresample’
— No package ‘libavresample’ found
— Checking for module ‘libdc1394-2’
— No package ‘libdc1394-2’ found
— Caffe: NO
— Protobuf: NO
— Glog: NO
— freetype2: YES (ver 21.0.15)
— harfbuzz: YES (ver 1.7.2)
— Could NOT find HDF5 (missing: HDF5_LIBRARIES HDF5_INCLUDE_DIRS) (found version «»)
— Module opencv_ovis disabled because OGRE3D was not found
— No preference for use of exported gflags CMake configuration set, and no hints for include/library directories provided. Defaulting to preferring an installed/exported gflags CMake configuration if available.
— Failed to find installed gflags CMake configuration, searching for gflags build directories exported with CMake.
— Failed to find gflags — Failed to find an installed/exported CMake configuration for gflags, will perform search for installed gflags components.
— Failed to find gflags — Could not find gflags include directory, set GFLAGS_INCLUDE_DIR to directory containing gflags/gflags.h
— Failed to find glog — Could not find glog include directory, set GLOG_INCLUDE_DIR to directory containing glog/logging.h
— Module opencv_sfm disabled because the following dependencies are not found: Eigen Glog/Gflags
— Checking for module ‘tesseract’
— No package ‘tesseract’ found
— Tesseract: NO
— Registering hook ‘INIT_MODULE_SOURCES_opencv_dnn’: /home/pavelk/OpenCV/modules/dnn/cmake/hooks/INIT_MODULE_SOURCES_opencv_dnn.cmake
— opencv_dnn: filter out cuda4dnn source code

— General configuration for OpenCV 4.2.0-dev =====================================
— Version control: 4.2.0-32-gf6ec467f19

— Extra modules:
— Location (extra): /home/pavelk/OpenCV/opencv_contrib/modules
— Version control (extra): 4.2.0-4-g648db235

— Platform:
— Timestamp: 2020-01-03T13:12:12Z
— Host: Linux 5.0.0-37-generic x86_64
— CMake: 3.10.2
— CMake generator: Unix Makefiles
— CMake build tool: /usr/bin/make
— Configuration: Release

— CPU/HW features:
— Baseline: SSE SSE2 SSE3
— requested: SSE3
— Dispatched code generation: SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
— requested: SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
— SSE4_1 (16 files): + SSSE3 SSE4_1
— SSE4_2 (2 files): + SSSE3 SSE4_1 POPCNT SSE4_2
— FP16 (1 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
— AVX (5 files): + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
— AVX2 (29 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
— AVX512_SKX (6 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX

— C/C++:
— Built as dynamic libs?: YES
— C++ Compiler: /usr/bin/c++ (ver 7.4.0)
— C++ flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG
— C++ flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG
— C Compiler: /usr/bin/cc
— C flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG -DNDEBUG
— C flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -g -O0 -DDEBUG -D_DEBUG
— Linker flags (Release): -Wl,—gc-sections
— Linker flags (Debug): -Wl,—gc-sections
— ccache: NO
— Precompiled headers: NO
— Extra dependencies: dl m pthread rt /usr/lib/x86_64-linux-gnu/libGL.so /usr/lib/x86_64-linux-gnu/libGLU.so
— 3rdparty dependencies:

— OpenCV modules:
— To be built: aruco bgsegm bioinspired calib3d ccalib core cvv datasets dnn dnn_objdetect dnn_superres dpm face features2d flann freetype fuzzy gapi hfs highgui img_hash imgcodecs imgproc line_descriptor ml objdetect optflow phase_unwrapping photo plot quality rapid reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab xfeatures2d ximgproc xobjdetect xphoto
— Disabled: world
— Disabled by dependency: —
— Unavailable: cnn_3dobj cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev hdf java js matlab ovis python2 python3 sfm viz
— Applications: tests perf_tests apps
— Documentation: NO
— Non-free algorithms: NO

— GUI:
— QT: YES (ver 5.14.0)
— QT OpenGL support: YES (Qt5::OpenGL 5.14.0)
— GTK+: NO
— OpenGL support: YES (/usr/lib/x86_64-linux-gnu/libGL.so /usr/lib/x86_64-linux-gnu/libGLU.so)
— VTK support: NO

— Media I/O:
— ZLib: /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.11)
— JPEG: /usr/lib/x86_64-linux-gnu/libjpeg.so (ver 90)
— WEBP: build (ver encoder: 0x020e)
— PNG: /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.6.34)
— TIFF: build (ver 42 — 4.0.10)
— JPEG 2000: build (ver 1.900.1)
— OpenEXR: build (ver 2.3.0)
— HDR: YES
— SUNRASTER: YES
— PXM: YES
— PFM: YES

— Video I/O:
— DC1394: NO
— FFMPEG: YES
— avcodec: YES (58.65.102)
— avformat: YES (58.35.101)
— avutil: YES (56.38.100)
— swscale: YES (5.6.100)
— avresample: NO
— GStreamer: YES (1.14.5)
— v4l/v4l2: YES (linux/videodev2.h)

— Parallel framework: pthreads

— Trace: YES (with Intel ITT)

— Other third-party libraries:
— Intel IPP: 2019.0.0 Gold [2019.0.0] — at: /home/pavelk/OpenCV/build/3rdparty/ippicv/ippicv_lnx/icv
— Intel IPP IW: sources (2019.0.0)
— at: /home/pavelk/OpenCV/build/3rdparty/ippicv/ippicv_lnx/iw
— Lapack: NO
— Eigen: NO
— Custom HAL: NO
— Protobuf: build (3.5.1)

— OpenCL: YES (no extra features)
— Include path: /home/pavelk/OpenCV/3rdparty/include/opencl/1.2
— Link libraries: Dynamic load

— Python (for build): /usr/bin/python2.7

— Java:
— ant: NO
— JNI: NO
— Java wrappers: NO
— Java tests: NO

— Install to: /usr/local/opencv
— ——————————————————————

— Configuring done
— Generating done
— Build files have been written to: /home/pavelk/OpenCV/build

наконец-то собираем и ставим:

make -j5
make install
[tip]Кстати, «-j5» отвечает за количество потоков сборки, можете увеличить, если больше есть. [/tip]

Если нужны дополнительные модули, например xfeatures2d для работы со SURF, то нужно их скачать отдельно

cd OpenCV
git clone https://github.com/opencv/opencv_contrib

и в параметры компиляции у cmake добавить:

-DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules
-DOPENCV_ENABLE_NONFREE:BOOL=ON
[tip]Если какой-либо модуль при сборке будет ебать мозг, то к cmake дописать -DBUILD_opencv_modulename=OFF[/tip]

Все библиотеки будут в папке /usr/local/opencv/

Простенький пример для Qt Creator: Создаём консольное приложение, в *.pro файл проекта добавляем:

QT += widgets
INCLUDEPATH +="/usr/local/opencv/include/opencv4" 
LIBS += -L"/usr/local/opencv/lib/" 
LIBS += \
   -lopencv_core \ 
   -lopencv_features2d \ 
   -lopencv_highgui \ 
   -lopencv_imgcodecs \ 
   -lopencv_imgproc \ 
   -lopencv_video \ 
   -lopencv_videoio \ 
   -lopencv_videostab
#include <QApplication>

#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
using namespace std;
using namespace cv;

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    namedWindow("TestVideo", WINDOW_AUTOSIZE);

    VideoCapture cap("/home/pavelk/bumpy.mp4");

    if ( !cap.isOpened() ) {
        cout << "Cannot open the video file."<<endl;
        return -1;
    }

    while( true ) {
        Mat frame;

        if (!cap.read(frame)) {
            cout<<"Cannot read the video file."<<endl;
            break;
        }

        imshow("TestVideo", frame);

        if( waitKey(30)==27 ) { break; }
    }

    return a.exec();
}

Вот как-то так…

4 комментария

zombotar 8 октября 2017 - 12:31

Действительно, проблема была в отсутствии qt.conf правильной структуры.

Reply
zombotar 5 апреля 2017 - 0:54

Всё замечательно и прекрасно работает. Собирает и debug и release. Вот только есть беда одна: собираюсь использовать мою программу без QtCreator. Кинул в директорию с исполняемым файлом нужные либы и получаю следующую ошибку: «error: (-215) !empty() in function detectMultiScale. line 1681». xml файлы haarcascade’ы имеются и пути к ним правильные. В директории с исполняемым файлом также есть нужные динамические библиотеки (хотя тут вопрос спорный: те ли это библиотеки). Сталкивались ли вы с подобным поведением?

Reply
Pavelk 22 апреля 2017 - 21:29

Тут по-моему нужно копать в сторону qt.conf http://doc.qt.io/qt-5/qt-conf.html

Reply
nik 18 августа 2012 - 12:47

ПАСИБО БОЛЬШОЕ…

Reply

Leave a Comment

Этот сайт использует Akismet для борьбы со спамом. Узнайте, как обрабатываются ваши данные комментариев.

You may also like