1. Agregar módulo contrib
Nota: El módulo de contribución variante debe completarse en la carpeta de módulos bajo opencv_contrib en OPENCV_EXTRA_MODULES_PATH.
2. Marque OPENCV_GENERATE_PKGCONFIG
Si no lo marca, no lo encontrará cuando vea la configuración más tarde:
$ pkg-config --cflags --libs opencv4
Package opencv4 was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv4.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv4' found
3. Problemas de compilación y soluciones
Consulte el blog a continuación, la compilación aún encontró algunos errores, estos errores pueden no ser encontrados por todos.
Blog de referencia: Ubuntu18.04 instala OpenCV4.2.0 + biblioteca OpenCV_Contrib4.2.0
[ 85%] Built target opencv_perf_video
[ 85%] Linking CXX executable ../../bin/opencv_test_gapi
[ 85%] Built target opencv_test_gapi
Makefile:162: recipe for target 'all' failed
make: *** [all] Error 2
Verifique el error:
/home/wmz/Downloads/opencv/opencv_contrib/modules/xfeatures2d/src/boostdesc.cpp:654:20: fatal error: boostdesc_bgm.i: No such file or directory
#include "boostdesc_bgm.i"
^~~~~~~~~~~~~~~~~
compilation terminated.
modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/build.make:91: recipe for target 'modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/src/boostdesc.cpp.o' failed
make[2]: *** [modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/src/boostdesc.cpp.o] Error 1
CMakeFiles/Makefile2:6648: recipe for target 'modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/all' failed
make[1]: *** [modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
Referencia de la solución: compile los mensajes de OpenCV y openc_contrib que faltan solución de error de archivo boostdesc_bgm.i
Referencia: https://github.com/opencv/opencv_contrib/issues/1301
Descarga de referencia: al instalar OpenCV, solicita la solución del problema del archivo boostdesc_bgm.i faltante (con recursos en la nube de Baidu)
Vuelva a compilar después de resolver el problema:
sudo make clean
sudo make -j8
Sin embargo, todavía no tuvo éxito, pero se modificó el error:
/home/wmz/Downloads/opencv/opencv_contrib/modules/xfeatures2d/test/test_features2d.cpp:51:10: fatal error: features2d/test/test_detectors_regression.impl.hpp: No such file or directory
#include "features2d/test/test_detectors_regression.impl.hpp"
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
modules/xfeatures2d/CMakeFiles/opencv_test_xfeatures2d.dir/build.make:86: recipe for target 'modules/xfeatures2d/CMakeFiles/opencv_test_xfeatures2d.dir/test/test_features2d.cpp.o' failed
make[2]: *** [modules/xfeatures2d/CMakeFiles/opencv_test_xfeatures2d.dir/test/test_features2d.cpp.o] Error 1
CMakeFiles/Makefile2:6746: recipe for target 'modules/xfeatures2d/CMakeFiles/opencv_test_xfeatures2d.dir/all' failed
make[1]: *** [modules/xfeatures2d/CMakeFiles/opencv_test_xfeatures2d.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
Referencia: opencv no puede encontrar el archivo feature2d / test / test_detectors_regression.impl.hpp
Pruébelo aquí abajo y agregue en la línea 559:
include_directories("modules")
Después del cambio:
Bueno, la compilación tiene éxito.
Continuar ejecución:
sudo make install
4. Configure las variables de entorno después de la instalación
Ingrese en la terminal o abra con gedit (reemplace vim):
sudo vim /etc/ld.so.conf.d/opencv.conf
Agregar adentro:
/usr/local/lib
Guarde y salga, configure la biblioteca:
sudo ldconfig
Cambiar las variables de entorno:
sudo gedit /etc/bash.bashrc
Guarde y salga, pruébelo ahora, ingrese:
pkg-config --cflags --libs opencv4
Nota: pkg-config --cflags --libs opencv4 solo es opencv4 y superior, la versión anterior es pkg-config --cflags --libs opencv.
Ver resultados:
$ pkg-config --cflags --libs opencv4
-I/usr/local/include/opencv4/opencv -I/usr/local/include/opencv4 -L/usr/local/lib -lopencv_gapi -lopencv_stitching -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_cudabgsegm -lopencv_cudafeatures2d -lopencv_cudaobjdetect -lopencv_cudastereo -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_highgui -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_quality -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_cudacodec -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_videostab -lopencv_cudaoptflow -lopencv_optflow -lopencv_cudalegacy -lopencv_videoio -lopencv_cudawarping -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_video -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_cudaimgproc -lopencv_cudafilters -lopencv_imgproc -lopencv_cudaarithm -lopencv_core -lopencv_cudev
5. Escribe un programa de prueba
(1) Procedimiento de prueba 1
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat img=imread("test.jpg");//
imshow("test",img);
waitKey(5000);
return 0;
}
Compilar:
g++ -std=c++11 test.cpp -o test `pkg-config --cflags --libs opencv4`
Error:
/usr/local/lib/libopencv_core.a(cuda_host_mem.cpp.o): In function `cv::cuda::registerPageLocked(cv::Mat&)':
cuda_host_mem.cpp:(.text._ZN2cv4cuda18registerPageLockedERNS_3MatE+0x31): undefined reference to `cudaHostRegister'
cuda_host_mem.cpp:(.text._ZN2cv4cuda18registerPageLockedERNS_3MatE+0xab): undefined reference to `cudaGetErrorString'
/usr/local/lib/libopencv_core.a(cuda_host_mem.cpp.o): In function `cv::cuda::unregisterPageLocked(cv::Mat&)':
cuda_host_mem.cpp:(.text._ZN2cv4cuda20unregisterPageLockedERNS_3MatE+0x1b): undefined reference to `cudaHostUnregister'
cuda_host_mem.cpp:(.text._ZN2cv4cuda20unregisterPageLockedERNS_3MatE+0x40): undefined reference to `cudaGetErrorString'
collect2: error: ld returned 1 exit status
Continúe resolviendo mañana.
(2) Procedimiento de prueba 2
Nombre de archivo: DisplayImage.cpp
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv )
{
if ( argc != 2 )
{
printf("usage: DisplayImage.out <Image_Path>\n");
return -1;
}
Mat image;
image = imread( argv[1], 1 );
if ( !image.data )
{
printf("No image data \n");
return -1;
}
namedWindow("Display Image", WINDOW_AUTOSIZE );
imshow("Display Image", image);
waitKey(0);
return 0;
}
Cree un archivo CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
Ejecutar en la ruta actual:
mkdir build
cd build
cmake ..
make
./DisplayImg lena.jpg
Necesita copiar la imagen lena.jpg a la carpeta de compilación.
El efecto de implementación es el siguiente:
wmz@wmz:~/Documents/test/opencv-t2$ mkdir build
wmz@wmz:~/Documents/test/opencv-t2$ cd build
wmz@wmz:~/Documents/test/opencv-t2/build$ cmake ..
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Found CUDA: /usr/local/cuda (found suitable exact version "10.2")
-- Found OpenCV: /usr/local (found version "4.2.0")
-- Configuring done
-- Generating done
-- Build files have been written to: /home/wmz/Documents/test/opencv-t2/build
wmz@wmz:~/Documents/test/opencv-t2/build$ make
Scanning dependencies of target DisplayImage
[ 50%] Building CXX object CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o
[100%] Linking CXX executable DisplayImage
[100%] Built target DisplayImage
wmz@wmz:~/Documents/test/opencv-t2/build$ ./DisplayImage lena.jpg
wmz@wmz:~/Documents/test/opencv-t2/build$
Referencia: instalación de Opencv y prueba simple en linux
También puede ir directamente al directorio samples / cpp / example_cmake y ejecutarlo en orden
cmake . #这个是以字母C开头的
make
./opencv_example
La cámara se llamará y emitirá "Hola mundo".