分类 OpenCV 下的文章

“OpenCV的全称是Open Source Computer Vision Library,是一个跨平台的计算机视觉库。OpenCV是由英特尔公司发起并参与开发,以BSD许可证授权发行。”

下载或者git clone opencv

https://github.com/opencv/opencv.git

https://github.com/opencv/opencv/archive/refs/tags/4.5.3.zip

下载或者git clone opencv_contrib

https://github.com/opencv/opencv_contrib.git

https://github.com/opencv/opencv_contrib/archive/refs/tags/4.5.3.zip

将opencv_contrib4.5.3放到opencv4.5.3目录下

目录结构
tree -L 1
.
├── 3rdparty
├── apps
├── build
├── cmake
├── CMakeLists.txt
├── CONTRIBUTING.md
├── COPYRIGHT
├── data
├── doc
├── include
├── LICENSE
├── modules
├── opencv_contrib-4.5.3
├── platforms
├── README.md
├── samples
└── SECURITY.md

make build && cd build

cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_EXTRA_MODULES=../opencv_contrib/modules ..

make -j4

sudo make install

如果要打开world
cmake -D CMAKE_BUILD_TYPE=Release -D BUILD_opencv_world=ON -D OPENCV_EXTRA_MODULES=../opencv_contrib/modules ..

出现错误:

CMake Error at modules/world/CMakeLists.txt:62 (if):
  if given arguments:
    ";opencv_core;opencv_flann;opencv_imgproc;opencv_ml;opencv_photo;opencv_python_tests;opencv_dnn;opencv_features2d;opencv_imgcodecs;opencv_videoio;opencv_calib3d;opencv_highgui;opencv_objdetect;opencv_stitching;opencv_video;opencv_gapi;opencv_world;opencv_java_bindings_generator;opencv_js_bindings_generator;opencv_objc_bindings_generator;opencv_python_bindings_generator;opencv_ts;" "MATCHES" ";opencv_viz;" "AND" "OPENCV_MODULE_opencv_viz_IS_PART_OF_WORLD" "AND" "VTK_VERSION" "VERSION_GREATER_EQUAL" "8.90.0"

  Unknown arguments specified
Configuring incomplete, errors occurred!

下载编译安装VTK试试
https://www.vtk.org/files/release/9.0/VTK-9.0.3.tar.gz
tar xvf VTK-9.0.3.tar.gz
cd VTK-9.0.3
mkdir build && cd build
cmake ..
make -j4
sudo make install

-- Installing: /usr/local/lib/libvtkDomainsChemistry-9.0.so.9.0.3
-- Installing: /usr/local/lib/libvtkDomainsChemistry-9.0.so.1
-- Set runtime path of "/usr/local/lib/libvtkDomainsChemistry-9.0.so.9.0.3" to ""
-- Installing: /usr/local/lib/libvtkDomainsChemistry-9.0.so
-- Installing: /usr/local/include/vtk-9.0/vtkOpenGLMoleculeMapper.h
-- Installing: /usr/local/include/vtk-9.0/vtkDomainsChemistryOpenGL2Module.h
-- Installing: /usr/local/lib/vtk/hierarchy/VTK/vtkDomainsChemistryOpenGL2-hierarchy.txt
-- Installing: /usr/local/lib/libvtkDomainsChemistryOpenGL2-9.0.so.9.0.3
-- Installing: /usr/local/lib/libvtkDomainsChemistryOpenGL2-9.0.so.1
-- Set runtime path of "/usr/local/lib/libvtkDomainsChemistryOpenGL2-9.0.so.9.0.3" to ""
-- Installing: /usr/local/lib/libvtkDomainsChemistryOpenGL2-9.0.so

静态库没有fPIC编译, 只要在configure时加上--enable-shared即可, 即: ./configure --enable-shared

编译opencv4.5.3的时候出现错误

/usr/bin/ld: /usr/local/lib/libavcodec.a(vc1dsp_mmx.o): relocation R_X86_64_PC32 against symbol `ff_pw_9' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: 最后的链结失败: bad value
collect2: error: ld returned 1 exit status
make[2]: * [modules/world/CMakeFiles/opencv_world.dir/build.make:8801:lib/libopencv_world.so.4.5.3] 错误 1
make[1]: * [CMakeFiles/Makefile2:2093:modules/world/CMakeFiles/opencv_world.dir/all] 错误 2
make: * [Makefile:163:all] 错误 2

将ffmpeg换成动态库编译就可以了。

cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_CONTRIB_MODULES=../opencv_contrib-4.5.3/modules -DBUILD_opencv_world=ON -DCMAKE_INSTALL_PREFIX=/usr/local/opencv-4.5.3 -DBUILD_EXAMPLES=ON ..

配置情况说明

-- General configuration for OpenCV 4.5.3 =====================================
--   Version control:               unknown
-- 
--   Platform:
--     Timestamp:                   2021-09-29T02:55:59Z
--     Host:                        Linux 5.11.0-36-generic x86_64
--     CMake:                       3.16.3
--     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 (17 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 (31 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
--       AVX512_SKX (7 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++ standard:                11
--     C++ Compiler:                /usr/bin/c++  (ver 9.3.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 -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 -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=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -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=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -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,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a   -Wl,--gc-sections -Wl,--as-needed  
--     Linker flags (Debug):        -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a   -Wl,--gc-sections -Wl,--as-needed  
--     ccache:                      NO
--     Precompiled headers:         NO
--     Extra dependencies:          dl m pthread rt
--     3rdparty dependencies:
-- 
--   OpenCV modules:
--     To be built:                 calib3d core dnn features2d flann gapi highgui imgcodecs imgproc ml objdetect photo stitching ts video videoio world
--     Disabled:                    -
--     Disabled by dependency:      -
--     Unavailable:                 java python2 python3
--     Applications:                tests perf_tests examples apps
--     Documentation:               NO
--     Non-free algorithms:         NO
-- 
--   GUI: 
--     GTK+:                        YES (ver 2.24.32)
--       GThread :                  YES (ver 2.64.6)
--       GtkGlExt:                  NO
--     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 80)
--     WEBP:                        build (ver encoder: 0x020f)
--     PNG:                         /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.6.37)
--     TIFF:                        /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 / 4.1.0)
--     JPEG 2000:                   build (ver 2.4.0)
--     OpenEXR:                     build (ver 2.3.0)
--     HDR:                         YES
--     SUNRASTER:                   YES
--     PXM:                         YES
--     PFM:                         YES
-- 
--   Video I/O:
--     DC1394:                      YES (2.2.5)
--     FFMPEG:                      YES
--       avcodec:                   YES (58.35.100)
--       avformat:                  YES (58.20.100)
--       avutil:                    YES (56.22.100)
--       swscale:                   YES (5.3.100)
--       avresample:                NO
--     GStreamer:                   NO
--     v4l/v4l2:                    YES (linux/videodev2.h)
-- 
--   Parallel framework:            pthreads
-- 
--   Trace:                         YES (with Intel ITT)
-- 
--   Other third-party libraries:
--     Intel IPP:                   2020.0.0 Gold [2020.0.0]
--            at:                   /home/hesy/Desktop/temp/opencv-4.5.3/build/3rdparty/ippicv/ippicv_lnx/icv
--     Intel IPP IW:                sources (2020.0.0)
--               at:                /home/hesy/Desktop/temp/opencv-4.5.3/build/3rdparty/ippicv/ippicv_lnx/iw
--     VA:                          YES
--     Lapack:                      NO
--     Eigen:                       YES (ver 3.3.7)
--     Custom HAL:                  NO
--     Protobuf:                    build (3.5.1)
-- 
--   OpenCL:                        YES (INTELVA)
--     Include path:                /home/hesy/Desktop/temp/opencv-4.5.3/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-4.5.3
-- -----------------------------------------------------------------
-- 
-- Configuring done
-- Generating done

make -j8
sudo make install

-- Installing: /usr/local/opencv-4.5.3/bin/opencv_annotation
-- Set runtime path of "/usr/local/opencv-4.5.3/bin/opencv_annotation" to "/usr/local/opencv-4.5.3/lib"
-- Installing: /usr/local/opencv-4.5.3/bin/opencv_visualisation
-- Set runtime path of "/usr/local/opencv-4.5.3/bin/opencv_visualisation" to "/usr/local/opencv-4.5.3/lib"
-- Installing: /usr/local/opencv-4.5.3/bin/opencv_interactive-calibration
-- Set runtime path of "/usr/local/opencv-4.5.3/bin/opencv_interactive-calibration" to "/usr/local/opencv-4.5.3/lib"
-- Installing: /usr/local/opencv-4.5.3/bin/opencv_version
-- Set runtime path of "/usr/local/opencv-4.5.3/bin/opencv_version" to "/usr/local/opencv-4.5.3/lib"
-- Installing: /usr/local/opencv-4.5.3/bin/opencv_model_diagnostics
-- Set runtime path of "/usr/local/opencv-4.5.3/bin/opencv_model_diagnostics" to "/usr/local/opencv-4.5.3/lib"

下载x264并编译

git clone git://git.videolan.org/x264.git
./configure --enable-shared --enable-static --prefix=/usr
make -j8
make install
解释一下,指定/usr的话,安装好,不用配置路径。
j8的目的是编译起来,用8路进行make,节省编译时间

2 把libx264编译到ffmpeg

./configure --enable-gpl --enable-libx264 --prefix=/root/extest/webMP4/ffmpeg
make -j8
make instal

就可以了
Referenced from:https://blog.csdn.net/dong_beijing/article/details/78366732

使用bash执行就可以了。

export OPENCV_LOG_LEVEL=DEBUG
export OPENCV_VIDEOIO_DEBUG=1

示例:

./opencv-demo
OpenCV4 Example.
OpenCV Version = 4.5.3
[DEBUG:0] global /home/const/opencv-4.5.3/modules/videoio/src/videoio_registry.cpp (191) VideoBackendRegistry VIDEOIO: Builtin backends(8): FFMPEG(1000); GSTREAMER(990); INTEL_MFX(980); V4L2(970); CV_IMAGES(960); CV_MJPEG(950); FIREWIRE(940); UEYE(930)
[DEBUG:0] global /home/const/opencv-4.5.3/modules/videoio/src/videoio_registry.cpp (215) VideoBackendRegistry VIDEOIO: Available backends(8): FFMPEG(1000); GSTREAMER(990); INTEL_MFX(980); V4L2(970); CV_IMAGES(960); CV_MJPEG(950); FIREWIRE(940); UEYE(930)
[ INFO:0] global /home/const/opencv-4.5.3/modules/videoio/src/videoio_registry.cpp (217) VideoBackendRegistry VIDEOIO: Enabled backends(8, sorted by priority): FFMPEG(1000); GSTREAMER(990); INTEL_MFX(980); V4L2(970); CV_IMAGES(960); CV_MJPEG(950); FIREWIRE(940); UEYE(930)
[ WARN:0] global /home/const/opencv-4.5.3/modules/videoio/src/cap.cpp (126) open VIDEOIO(FFMPEG): trying capture filename='1.mp4' ...
[DEBUG:0] global /home/const/opencv-4.5.3/modules/videoio/src/cap_ffmpeg_impl.hpp (1022) open FFMPEG: stream[0] is video stream with codecID=27 width=2560 height=1440
[DEBUG:0] global /home/const/opencv-4.5.3/modules/videoio/src/cap_ffmpeg_hw.hpp (929) HWAccelIterator FFMPEG: allowed acceleration types (vaapi): 'vaapi.iHD'
[DEBUG:0] global /home/const/opencv-4.5.3/modules/videoio/src/cap_ffmpeg_hw.hpp (947) HWAccelIterator FFMPEG: disabled codecs: 'av1.vaapi,av1_qsv,vp8.vaapi,vp8_qsv'
[DEBUG:0] global /home/const/opencv-4.5.3/modules/videoio/src/cap_ffmpeg_impl.hpp (1054) open FFMPEG: trying to configure H/W acceleration: 'vaapi.iHD'
[ INFO:0] global /home/const/opencv-4.5.3/modules/videoio/src/cap_ffmpeg_hw.hpp (265) hw_check_device FFMPEG: Skipping 'vaapi' video acceleration on the following device name as not matching substring 'iHD':
[DEBUG:0] global /home/const/opencv-4.5.3/modules/videoio/src/cap_ffmpeg_impl.hpp (1063) open FFMPEG: ... can't create H/W device: 'vaapi.iHD'
[ERROR:0] global /home/const/opencv-4.5.3/modules/videoio/src/cap_ffmpeg_impl.hpp (1109) open VIDEOIO/FFMPEG: Failed to initialize VideoCapture
[ WARN:0] global /home/const/opencv-4.5.3/modules/videoio/src/cap.cpp (149) open VIDEOIO(FFMPEG): can't create capture
ERROR! Unable to open camera