Building opencv from source in Mac m1 - c++

I'm using the following Make to build OpenCV from source,
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=/Users/Tools/opencv_contrib/modules \
-D PYTHON3_EXECUTABLE=/miniforge/base/envs/envname/bin/python3 \
-D BUILD_opencv_python2=OFF \
-D BUILD_opencv_python3=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D WITH_TBB=ON \
-D WITH_V4L=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_ENABLE_NONFREE=ON \
-D WITH_OPENGL=ON \
-DBUILD_ZLIB=OFF \
-D OPENCV_EXTRA_MODULES_PATH=/Users/Tools/opencv_contrib/modules \
-D BUILD_EXAMPLES=ON ..
Unfortunately it doesn't build. It throws the following error,
: error: unknown type name 'AVBSFContext'; did you mean 'AVIOContext'?
I have of course installed FFmpeg. what am I still missing?

I think that you've installed ffmpeg 5 and OpenCV is not yet compatible with it. Please try the following:
brew install ffmpeg#4
brew unlink ffmpeg
brew link ffmpeg#4
and then recompile OpenCV again.

Related

CMake Cross compile Arm64 error “unrecognised command line option ‘-msse’”

I am trying to cross-compile openCV for an Arm64 linux platform and am getting the following errors
aarch64-linux-gnu-gcc: error: unrecognised command line option ‘-msse’; did you mean ‘-fdse’?
aarch64-linux-gnu-gcc: error: unrecognised command line option ‘-msse2’
aarch64-linux-gnu-gcc: error: unrecognised command line option ‘-msse3’
I understand that the -msse flags are not compatible with arm64, yet when I generate my makefile from CMake, these flags are include.
My CMake command is
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-DCUDA_NVCC_EXECUTABLE=/usr/local/cuda-10/bin/nvcc \
-DCUDA_HOST_COMPILER=/opt/l4t-gcc-toolchain-64-bit-32-7.1/bin/aarch64-linux-gnu-gcc \
-DCUDA_INCLUDE_DIRS=/usr/local/cuda-10/targets/aarch64-linux/include \
-DCUDA_CUDART_LIBRARY=/usr/local/cuda-10/targets/aarch64-linux/lib/libcudart.so \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D BUILD_EXAMPLES=OFF \
-D WITH_CUDA=ON \
-D CUDA_ARCH_BIN="6.2" \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.4.0/modules/ \
-D ENABLE_CXX11=ON ..
I am building on linux ubuntu 18.4.
How to do I tell CMake that the platform is Arm64?
Thanks
I resolved the issue, the missing flag I was looking for was
-D CMAKE_TOOLCHAIN_FILE=../platforms/linux/aarch64-gnu.toolchain.cmake

Link error when compiling opencv from source when including ffmpeg compiled from source

I've been stuck on this problem for weeks.
I'm trying to build opencv on a raspberry pi 4 with x264 support. To do this I need to include ffmpeg, and also build that from source.
However when compiling opencv, I'm consistently getting these linker errors:
/usr/bin/ld: ../../lib/libopencv_videoio.so.4.5.3: undefined reference to `avcodec_get_context_defaults3'
/usr/bin/ld: ../../lib/libopencv_videoio.so.4.5.3: undefined reference to `av_lockmgr_register'
/usr/bin/ld: ../../lib/libopencv_videoio.so.4.5.3: undefined reference to `av_register_all'
I'm new to Linux, so I'm not exactly sure how to get started troubleshooting this. I believe I have ffmpeg correctly compiled and installed.
Here's the steps I use:
Configure ffmpeg:
sudo ./configure \
--prefix=/usr \
--extra-ldflags="-latomic" \
--enable-shared \
--extra-libs='-lpthread -lm' \
--ld=g++ \
--enable-gpl \
--disable-debug \
--enable-nonfree \
--enable-libx264 \
--enable-omx \
--enable-omx-rpi \
--enable-gnutls \
--enable-libfreetype \
--enable-libmp3lame
Then build it:
sudo make -j4
Then install it as a package: (so opencv cmake will detect it)
sudo checkinstall -y --deldoc=yes --pkgversion=9999 --pkgname=ffmpeg
Then configure opencv (ffmpeg is detected)
sudo cmake ../opencv_sources -D CMAKE_BUILD_TYPE=RELEASE \
-D OPENCV_EXTRA_MODULES_PATH= $PWD/../opencv_contrib/modules \
-D ENABLE_NEON=ON \
-D ENABLE_VFPV3=ON \
-D BUILD_TESTS=ON \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D OPENCV_ENABLE_NONFREE=ON \
-D CMAKE_SHARED_LINKER_FLAGS='-latomic -L/usr/lib' \
-D WITH_V4L=ON \
-D WITH_QT=OFF \
-D BUILD_EXAMPLES=OFF \
-D CPU_BASELINE=NATIVE \
-D CMAKE_INSTALL_PREFIX="$HOME/opencv_build" \
-D BUILD_opencv_apps=OFF \
-D BUILD_opencv_python2=OFF \
-D BUILD_SHARED_LIBS=ON \
-D WITH_FFMPEG=ON
Then build opencv:
sudo make -j4
And this is where I get linking errors.
I checked in /usr/lib and the .so files appear to be there:
ls /usr/lib | grep libav
libavcodec.a
libavcodec.so
libavcodec.so.59
libavcodec.so.59.4.101
libavdevice.a
libavdevice.so
libavdevice.so.59
libavdevice.so.59.0.100
libavfilter.a
libavfilter.so
libavfilter.so.8
libavfilter.so.8.1.103
libavformat.a
libavformat.so
libavformat.so.59
libavformat.so.59.4.101
libavutil.a
libavutil.so
libavutil.so.57
libavutil.so.57.3.100
How do check where opencv is looking when linking? And is there a way I can check the shared libraries that ffmpeg generated to make sure they work?
The ffmpeg version is: git-2021-08-10-c245963
And opencv is 4.5.3-dev
TL;DR:
I was compiling the master branch of ffmpeg. This version removed some deprecated functions and was incompatible with my opencv version. Or at least the build tests failed. By switching to the release/4.4 branch and repeating my previous build steps I was able to successfully build opencv.
Details:
I was able to determine that it's not a linking problem. I wrote a short program that calls avcodec_get_context_defaults3(...), as well as avcodec_version(), which is another function that should be in there. Upon linking libavcodec.so, the avcodec_version() is found, but avcodec_get_context_defaults3() is not. Therefore I believe this is a version compatibility issue between ffmpeg and opencv.
I then confirmed that the function was removed from ffmpeg. The latest version does not have it, old versions do. I used git's search function and found this commit:
Author: Andreas Rheinhardt <andreas.rheinhardt#gmail.com>
Date: Thu Feb 25 20:37:24 2021 +0100
avcodec: Remove deprecated avcodec_get_context_defaults3
Deprecated in 04fc8e24a091ed1d77d7a3c0cbcfe60baec19a9f.
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt#gmail.com>
Signed-off-by: James Almer <jamrial#gmail.com>
I switched to the release/3.4 branch of FFMPEG and repeated the steps. But that was missing some other things. I repeated the steps a 3rd time with release/4.4 and that worked.
The command I used to download the correct branch was this:
sudo git clone https://github.com/FFmpeg/FFmpeg --branch release/4.4 --depth 1 ffmpeg_sources

Compiling OpenCV with Contrib and C++11 Support?

I want to compile opencv from source on ubuntu 16.04.
I already did this successfully a couple of times before.
I am following this tutorial from pyimagesearch.
Normally this works very well, but this time I am getting the following error for the contrib module text.
[ 27%] Generating precomp.hpp.gch/opencv_text_RELEASE.gch In file
included from /usr/include/c++/5/cinttypes:35:0,
from /usr/local/include/tesseract/host.h:30,
from /usr/local/include/tesseract/serialis.h:26,
from /usr/local/include/tesseract/baseapi.h:37,
from /home/rvq/github/opencv-3.2.0/build/modules/text/precomp.hpp:51:
/usr/include/c++/5/bits/c++0x_warning.h:32:2: error: #error This file
requires compiler and library support for the ISO C++ 2011 standard.
This support must be enabled with the -std=c++11 or -std=gnu++11
compiler options. #error This file requires compiler and library
support \ ^
[ 27%] Built target pch_Generate_opencv_saliency
Does somebody know how to resolve this?
Adding -D ENABLE_PRECOMPILED_HEADERS=OFF \ to CMake command resolved the issue.
Complete CMake Command:
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_EXTRA_MODULES_PATH=~/github/opencv_contrib-3.2.0/modules \
-D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python \
-D ENABLE_PRECOMPILED_HEADERS=OFF \
-D BUILD_EXAMPLES=ON ..

OpenCV make on Ubuntu with opencv_contrib fails due to opencv_xphoto

I'm trying to build OpenCV on Ubuntu 15.10 with opencv_contrib (to use the nonfree modules). I did the following:
cmake -D CMAKE_BUILD_TYPE=RELEASE -DOPENCV_EXTRA_MODULES_PATH=/home/myname/Downloads/opencv_contrib-master/modules -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=OFF -D WITH_OPENGL=ON -D BUILD_opencv_ximgproc=ON ..
but I tried also with
BUILD_opencv_ximgproc=OFF
CMake goes well but it fails during the make step:
CMakeFiles/Makefile2:8403: recipe for target 'modules/xphoto/CMakeFiles/opencv_xphoto.dir/all' failed
make[1]: *** [modules/xphoto/CMakeFiles/opencv_xphoto.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
and then:
Makefile:136: recipe for target 'all' failed
make: *** [all] Error 2
Do you have any suggestions?
Thanks.
I solved it by deleting and re-downloading both opencv and opencv_contrib. Then I built everything again:
git clone https://github.com/Itseez/opencv.git
git clone https://github.com/Itseez/opencv_contrib.git
cd opencv
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -DOPENCV_EXTRA_MODULES_PATH=/home/myname/Downloads/opencv_contrib/modules -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=OFF -D WITH_OPENGL=ON -D BUILD_opencv_ximgproc=ON ..
make -j7
sudo make install

error in cmake opencv: Parse error in command line argument: -D

it is a while I am trying to install opencv but each time I have some problems with the configuration of cmake.
this is the cmake I am trying to use:
cmake -D CMAKE_BUILD_TYPE=RELEASE -D BUILD_PYTHON_SUPPORT=ON
-D WITH_XINE=ON -D WITH_OPENGL=ON -D INSTALL_C_EXAMPLES=ON
-D INSTALL_PYTHON_EXAMPLES=ON -D WITH_TBB=ON -D BUILD_EXAMPLES=ON
-D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON
-D CMAKE_INSTALL_PREFIX=/home/username/main-env/opencv3.0
-D WITH_GTK = ON -D MATLAB_ROOT_DIR=/home/MATLAB/R2015a ..
,but got this error
Parse error in command line argument: -D
Should be: VAR:type=value
CMake Error: No cmake script provided.
CMake Error: Problem processing arguments. Aborting.
P.S. I want to put it in main-env folder which is the virtual environment folder to avoid any problems with system files and less struggling with various errors encountered in previous times. not sure possible maybe it is totally wrong. please let me know if you have some knowledge about it.
the all terms should be separated by -D and shouldn't be any attached terms with -D