Make the OpenCV libraries that the binary program really needs available to the customer - opencv

I have installed the OpenCV package in Ubuntu22.04 by sudo apt-get.
And the said OpenCV is one of the dependencies for my project.
The problem is that the target computer does not have installed OpenCV and it's impossible to use sudo permission.
How to make the OpenCV that I am using portable by the install command of Cmake? I intend to copy the OpenCV libraries when make install is invoked.
What I have tried is seen below.
Here is a portion of the CMakeLists.txt:
find_package( OpenCV REQUIRED )
message("OpenCV_LIBS:")
foreach(lib ${OpenCV_LIBS})
message("${lib}")
endforeach()
install(FILES ${OpenCV_LIBS} TYPE LIB)
Here is the output when cmake .. is invoked:
OpenCV_LIBS:
opencv_calib3d
opencv_core
opencv_dnn
opencv_features2d
opencv_flann
opencv_highgui
//omits some similar output
Here is the error message when make install is called:
CMake Error at cmake_install.cmake:130 (file):
file INSTALL cannot find "/home/john/myproject/opencv_calib3d":
No such file or directory.
And what's more, how to only make the libraries that the binary program really needs available to the customer other than sending all the libraries of OpenCV to the customer?
Tips: the customer uses the same OS.

Related

fatal error: opencv2/xfeatures2d.hpp: No such file or directory

I am trying to build this code:
https://docs.opencv.org/3.2.0/d5/d6f/tutorial_feature_flann_matcher.html
I am using Ubuntu 16.04 with CLion 2017.3 and have OpenCV 3.4 installed.
xfeatures2d.hpp can't be found on the system.
I have looked at many different problems on the internet, but couldn't find a solution.
Any help?
You have to include cmake comiplation flag to opencv OPENCV_EXTRA_MODULES_PATH and set it to the opencv_contrib/modules.
cmake -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules
Here's what I had to do to compile OpenCV with xfeatures2d:
Download opencv_contrib (I took a release from the releases page). This step is what gives us xfeatures2d.
Unpack the tarball somewhere, e.g., ~/src/opencv_contrib-4.5.5
When configuring OpenCV (the main OpenCV source like opencv-4.5.2) with CMake, add the following option to cmake command line (adjusting the value to be your actual path to the modules subdirectory of the opencv_contrib tarball):
-DOPENCV_EXTRA_MODULES_PATH=$HOME/src/opencv_contrib-4.5.5/modules/
Now just build as you normally would and install.
The above actions gave me the expected file (among others): /usr/include/opencv4/opencv2/xfeatures2d.hpp.

Opencv headers different version and libraries different version: ubuntu

I am new to Ubuntu and OpenCV. Apologies for my amateurishness.
I think I messed up OpenCV installation.
I had installed OpenCV 2.4 months back. The installation was successful (built openCV from source using CMake)
Couple of days back I had also installed OpenCV 3.0 using the same procedure.
The problem began when one of my code gave a segmentation fault for seemingly no reason. On suggestion by a friend on Caffe Neural Network issues forum (https://github.com/BVLC/caffe/issues/3416), it seems that I am compiling with 3.0 headers but linking against the 2.4 libraries.
I checked /usr/local/include. It has the standard openCv header files ( I didn't understand what 'compiling with 3.0 header means' )
In /usr/local/lib I have OpenCV 3.0 libraries ( no 2.4.8 at all).
Now compilation was successful, but when I try to run it, I get the following error.
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff76c49bc in cv::merge(cv::_InputArray const&,
cv::_OutputArray const&) ()
from /usr/lib/x86_64-linux-gnu/libopencv_core.so.2.4
Just to add
"pkg-config --modversion opencv"
3.0.0
whereas dpkg -l | grep libopencv
gave out
i libopencv-calib3d-dev:amd64 2.4.8+dfsg1-2ubuntu1 amd64 development files for libopencv-calib3d
ii libopencv-calib3d2.4:amd64 2.4.8+dfsg1-2ubuntu1 amd64 computer vision Camera Calibration library
ii libopencv-contrib-dev:amd64 2.4.8+dfsg1-2ubuntu1 amd64 development files for libopencv-contrib
.......
q.1> why does my code link to libopencv_core.so.2.4 instead of 3.0. I have checked in /usr/local/lib and did not see a single 2.4 library. I have tried manually defining links to header files and libraries in CMake but still same result.
q.2> Do OpenCV headers have a version number? I don't understand how they are called '2.4 headers'. I have checked the source code but did not see any specific requirement for a version of library. Then what dictates to link to 2.4 version libraries?
Sorry if this is theoretical but this would be highly beneficial to beginners like me. Please do comment for a suitable question title if the current one is not appropriate.
Thanks
Your opencv2.4 libraries are located in /usr/lib/x86_64-linux-gnu/ while the new opencv3 libraries are most likely located in /usr/local/lib. The linker is first looking in the /usr/lib/x86_64-linux-gnu/ directory, and thus finding the opencv2.4 libs first. To fix this you can tell the linker to look in /usr/local/lib first by adding the following to your ~/.bashrc file
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
Reload the bashrc file in the terminal as follows (or simply close and re-open the terminal):
source ~/.bashrc
And update the library database:
sudo ldconfig
To check the order in which the linker will search for libraries, you can use the following:
ld -lopencv_core --verbose | grep attempt

Installing OpenCV in Ubuntu 14.10

I'm trying to install OpenCV in Ubuntu 14.10 according to instruction.
I installed all mentioned dependencies, but when I'm trying to run make I get such errors:
/home/ilia/opencv-2.4.8/modules/highgui/src/ffmpeg_codecs.hpp:114:7: error: ‘CODEC_ID_H261’ was not declared in this scope
{ CODEC_ID_H261, MKTAG('H', '2', '6', '1') }
for all codecs, as I think.
And these errors:
In file included from /home/ilia/opencv-2.4.8/modules/highgui/src/cap_ffmpeg.cpp:45:0:
/home/ilia/opencv-2.4.8/modules/highgui/src/cap_ffmpeg_impl.hpp: In member function ‘double CvCapture_FFMPEG::getProperty(int)’:
/home/ilia/opencv-2.4.8/modules/highgui/src/cap_ffmpeg_impl.hpp:773:33: error: ‘AVStream’ has no member named ‘r_frame_rate’
return av_q2d(video_st->r_frame_rate);
^
/home/ilia/opencv-2.4.8/modules/highgui/src/cap_ffmpeg_impl.hpp: In member function ‘double CvCapture_FFMPEG::get_fps()’:
/home/ilia/opencv-2.4.8/modules/highgui/src/cap_ffmpeg_impl.hpp:820:49: error: ‘AVStream’ has no member named ‘r_frame_rate’
double fps = r2d(ic->streams[video_stream]->r_frame_rate);
^
In file included from /home/ilia/opencv-2.4.8/modules/highgui/src/cap_ffmpeg.cpp:45:0:
/home/ilia/opencv-2.4.8/modules/highgui/src/cap_ffmpeg_impl.hpp: In function ‘int icv_av_write_frame_FFMPEG(AVFormatContext*, AVStream*, uint8_t*, uint32_t, AVFrame*)’:
/home/ilia/opencv-2.4.8/modules/highgui/src/cap_ffmpeg_impl.hpp:1236:72: error: ‘avcodec_encode_video’ was not declared in this scope
out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
^
It looks like it can not find some header files, but I installed all necessary dev packages
libswscale-dev, libavdevice-dev, libavfilter-dev, libavformat-dev, libavcodec-dev. What should I do to resolve these problems?
Installing OpenCV from the Ubuntu repositories is a good choice for the most cases, but sometimes you need build OpenCV from sources yourself.
For example, if you need OpenCV's non-free functionality, or want to contribute to this project (you should use the latest version to create pull request), or want to change something (yes, OpenCV can also contain bugs).
Possible solution is building ffmpeg (it's rather simple) - I really don't understand why Debian/Ubuntu prefers libav without alternative.
For installing ffmpeg you should download its sources from official site or clone GIT repository (git://source.ffmpeg.org/ffmpeg.git), then enter source directory and run
./configure --enable-shared --disable-static
make
sudo make install
you can also add other parameters to configure.
You can build static libraries too, but OpenCV can't be built with static ffmpeg libraries (now I don't know why).
After this you can download OpenCV sources from OpenCV site or clone GitHub repository (OpenCV repository), create build folder and run from it the following:
cmake PATH_TO_SOURCES -DCMAKE_BUILD_TYPE=Release
make
sudo make install
Of course, PATH_TO_SOURCES must be actual path to your OpenCV sources.
After these steps you have working latest OpenCV build in your system.
Unless you have special reasons I would suggest installing the OpenCV that are already in the Ubuntu repository: sudo apt-get install libopencv-dev
For video codecs I suggest simply trying to install all ffmpeg and gstreamer related codec packages.
You can try to build without a ffmpeg:
cmake -DCMAKE_BUILD_TYPE=RELEASE -DWITH_FFMPEG=OFF ..
make
sudo make install

Compiling using cmake with dependencies installed using macport

I'm trying to build project that has dependency to OpenCV. I installed Opencv using macports and when I try to build project, cmake gives the following output:
CMake Error at CMakeLists.txt:47 (FIND_PACKAGE):
By not providing "FindOpenCV.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "OpenCV", but
CMake did not find one.
Could not find a package configuration file provided by "OpenCV" with any
of the following names:
OpenCVConfig.cmake
opencv-config.cmake
Add the installation prefix of "OpenCV" to CMAKE_PREFIX_PATH or set
"OpenCV_DIR" to a directory containing one of the above files. If "OpenCV"
provides a separate development package or SDK, be sure it has been
installed.
I searched a little bit about this problem and added the following env. variables to my $HOME/.profile file
export DYLD_LIBRARY_PATH=/opt/local/lib:$DYLD_LIBRARY_PATH
export CMAKE_PREFIX_PATH=/opt/local
without success. I checked and I have all opencv files istalled in /opt/local/lib and /opt/local/include/opencv directories. There is also OpenCVConfig.cmake in the following path:
/opt/local/lib/cmake/OpenCVConfig.cmake
How to make cmake know the path where opencv is installed? Previously I've build OpenCV on my own using cmake and installed into /usr/local and then it worked fine without any other fix. However I had some problems with ffmpeg and right now I switched to using macports.
Another option that works for me was set the env value OpenCV_DIR at the cmake opencv dir:
export OpenCV_DIR=/opt/local/lib/cmake/
macports traditionally installs OpenCV to /opt/local/ instead of the standard /usr/local/.
The solution to your problem is stated at:
Add the installation prefix of "OpenCV" to CMAKE_PREFIX_PATH or set
"OpenCV_DIR" to a directory containing one of the above files.
When building your project in the command-line, make sure you execute:
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/opt/local/lib/pkgconfig
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/local/lib
And then set the flag CMAKE_PREFIX_PATH for cmake:
cmake -D CMAKE_PREFIX_PATH=/opt/local ../
Not a MACPORT problem, but someone may find this helpful. Followed #hugh-pearse 's and #leszek-hanusz 's answers in this question, with a little tweak. I had installed opencv from ubuntu 12.10 repository (libopencv-)* and had the same problem. Couldn't solve it with export OpenCV_DIR=/usr/share/OpenCV/ (since my OpenCVConfig.cmake whas there). It was solved when I changed some lines on the OpenCVConfig.cmake file:
# ======================================================
# Include directories to add to the user project:
# ======================================================
# Provide the include directories to the caller
#SET(OpenCV_INCLUDE_DIRS "${OpenCV_INSTALL_PATH}/include/opencv;${OpenCV_INSTALL_PATH}/include")
SET(OpenCV_INCLUDE_DIRS "/usr/include/opencv;/usr/include/opencv2")
INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIRS})
# ======================================================
# Link directories to add to the user project:
# ======================================================
# Provide the libs directory anyway, it may be needed in some cases.
#SET(OpenCV_LIB_DIR "${OpenCV_INSTALL_PATH}/lib")
SET(OpenCV_LIB_DIR "/usr/lib")
LINK_DIRECTORIES(${OpenCV_LIB_DIR})
And that worked on my Ubuntu 12.10. Remember to add the target_link_libraries(yourprojectname ${OpenCV_LIBS}) in your CMakeLists.txt.
I finally read the header of the OpenCVConfig.cmake file.
It instructs to include these lines to use from an external project:
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(MY_TARGET_NAME ${OpenCV_LIBS})
(adding include_directories to CMakeLists.txt fixed it for me)
Make sure you have compiled OpenCV, once you compiled, OpenCVConfig.cmake will be generated in build directory.
follow these steps to compile
then , export OpenCV_DIR=<path to build directory with OpenCVConfig.cmake>
It should work now !
I tried all the above ideas in vain.
I eventually found a way to have the compilation work: Besides using #kengregson steps, I simply renamed the folder /usr/local/include/opencv2 so that it is not chosen when compiling my cpp file.
Since I compile my own OpenCV in a multiple-users server, so I can't install the OpenCV libs in /usr/local directly, but in my home folder instead, then exactly the same issue occurs to me.
Below is how I fix it:
Flowing the steps in OpenCV official doc: Using OpenCV with gcc and CMake
Add displayImage test program, and use this CMake file
cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV)
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
The same error happened, so add one environment variable in my .tcshrc
setenv OpenCV_DIR "${folder where contains OpenCVConfig.cmake }"
\# mine is:
setenv OpenCV_DIR "~/local/OpenCV2.4.13/share/OpenCV"

CMake can't find FFMPEG in custom install path

I am compiling a dependency for a project on Ubuntu 10.10, and instead of having it install to /usr/local by default, I am instead installing it to /tmp/stage/usr/local. How do I go about informing CMake of the location of this custom installed dependency when I call it to generate the build files for said project.
I am running CMake 2.8.1, and I have tried to set CMAKE_PREFIX_PATH on the cmake command line, like so
cmake -D CMAKE_PREFIX_PATH=/tmp/stage/usr/local
but this doesn't seem to make a difference - the project doesn't seem to detect the dependency.
Also, if it matters, the project in question is OpenCV 2.2, and the dependency in question is FFMPEG...
I figured out how to fix my problem, and trying to point CMake at the appropriate install location isn't the issue.
Apparently, CMake is unable to find the pkg-config files for FFMPEG (i.e. libavcodec.pc, libavdevice.pc, etc.) that tell it where the FFMPEG headers and libraries are located. In a typical install scenario, these files would be located at /usr/lib/pkgconfig. However because of the custom install location, they are instead located at /tmp/stage/usr/local/lib/pkgconfig.
So that CMake could find these files, I had to add the following environment variable:
export PKG_CONFIG_PATH=/tmp/stage/usr/local/lib/pkgconfig
After which point, OpenCV built against FFMPEG as expected.

Resources