Back to Opencvsharp

Building OpenCV and OpenCvSharpExtern for your Embedded Platform

docs/embedded-builds.md

latest8.5 KB
Original Source

Building OpenCV and OpenCvSharpExtern for your Embedded Platform

Using a Raspberry Pi with a 64-bit build of Debian Trixie (Lite is fine)

If you want a build of OpenCV that minimizes the on-disk footprint, you can turn off features (i.e. omit them from the build) for a custom OpenCV build. If you do this, you must also build a custom OpenCvSharpExtern library that matches your configuration.

Feature Sets

The following table shows the available OpenCV modules and their corresponding OpenCvSharpExtern exclusion flags:

OpenCV ModuleOpenCV CMake FlagOpenCvSharpExtern FlagDescription
core(always included)(always included)Core functionality (Mat, basic operations)
imgprocBUILD_opencv_imgproc(always included)Image processing (resize, filters, color conversion)
imgcodecsBUILD_opencv_imgcodecs(always included)Image file reading/writing
videoioBUILD_opencv_videoioNO_VIDEOIOVideo capture and writing
highguiBUILD_opencv_highguiNO_HIGHGUIGUI windows and trackbars
videoBUILD_opencv_videoNO_VIDEOVideo analysis (optical flow, background subtraction)
calib3dBUILD_opencv_calib3dNO_CALIB3DCamera calibration, 3D reconstruction
features2dBUILD_opencv_features2dNO_FEATURES2D2D feature detection (ORB, SIFT, etc.)
flannBUILD_opencv_flannNO_FLANNFast approximate nearest neighbor searches
dnnBUILD_opencv_dnnNO_DNNDeep neural network inference
mlBUILD_opencv_mlNO_MLMachine learning (SVM, decision trees, etc.)
objdetectBUILD_opencv_objdetectNO_OBJDETECTObject detection (cascade classifiers, QR codes)
photoBUILD_opencv_photoNO_PHOTOComputational photography (inpainting, HDR)
stitchingBUILD_opencv_stitchingNO_STITCHINGImage stitching and panorama creation
shapeBUILD_opencv_shapeNO_CONTRIBShape matching
superresBUILD_opencv_superresNO_CONTRIBSuper resolution
contrib modulesOPENCV_EXTRA_MODULES_PATHNO_CONTRIBAll opencv_contrib modules (aruco, face, tracking, etc.)
barcode(part of contrib)NO_BARCODEBarcode detection

Example: USB Camera Frame Capture

Let's say your app needs to connect to a USB camera and simply capture frames from it. You'll need these features:

  • core - Basic Mat operations
  • imgproc - Image processing (color conversion, resizing)
  • imgcodecs - Saving captured frames to disk
  • videoio - Camera capture via V4L2/FFMPEG
  • highgui - Optional, for displaying frames in a window

You do NOT need: video, calib3d, features2d, flann, dnn, ml, objdetect, photo, stitching, or any contrib modules.

So you'll need to build a custom set of OpenCV libraries first. Directly on your Raspberry Pi, do the following (Recommend a Pi 5 for compiling speed, but earlier versions work just fine as well).

Build Scripts

Two build scripts are provided for convenience:

ScriptDescriptionOutput Size
build-opencvsharp-arm64.shFull build with all features and contrib modules~140MB
build-opencvsharp-minimal-arm64.shMinimal build for basic camera capture~25MB

To use either script:

bash
cd ~
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git  # only needed for full build
git clone https://github.com/shimat/opencvsharp.git

cd ~/opencv
git fetch --tags
git checkout 4.10.0

# Only for full build:
cd ~/opencv_contrib
git fetch --tags
git checkout 4.10.0

# Run the desired build script
cd ~
chmod +x ~/opencvsharp/src/tools/build-opencvsharp-minimal-arm64.sh
~/opencvsharp/src/tools/build-opencvsharp-minimal-arm64.sh

Building a full OpenCV feature set - large (~140MB) output

First, you can run with a full-featured set of binaries. It's large, and contains probably things you don't need, but it works.

A script to do the full-up build for linux-arm64 is available here, but below are the manual instructions.

bash
cd ~
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git

cd ~/opencv
git fetch --tags
git checkout 4.10.0
cd ~/opencv_contrib
git fetch --tags
git checkout 4.10.0

Building a subset

You can build a subset, but it takes some work. The way Linux libraries link, you have to handle all of the dependencies. You can't simply rebuild OpenCV with fewer features and have it work. OpenCvSharpExtern is linked to the endpoints of all of the features in OpenCV, so if some of them are missing, loading libOpenCvSharpExtern.so will fail even if you aren't using the missing features. You must rebuild both, and the original OpenCVSharpExtern is not created to make this friendly.

This repository attempts to address that by providing better support for minimal, configurable builds.

A script to do a minimal build for linux-arm64 is available here, but below are the manual instructions.

Building OpenCV from Source

First, you have to clone and build OpenCV with your desired features. This is supported and fairly well documented for OpenCV.

Make sure you have all of the dev tools installed:

bash
sudo apt update
sudo apt install -y \
  build-essential \
  cmake \
  git \
  pkg-config \
  libgtk-3-dev \
  libavcodec-dev \
  libavformat-dev \
  libswscale-dev \
  libv4l-dev

Now clone the OpenCV code:

bash
cd ~
git clone https://github.com/opencv/opencv.git

cd ~/opencv
git fetch --tags
git checkout 4.10.0

Now you need to configure the cmake build for your desired feature set:

bash
cd ~/opencv
mkdir build
cd build
cmake .. \
  -DCMAKE_BUILD_TYPE=Release \
  -DCMAKE_INSTALL_PREFIX=/opt/opencv-nocontrib \
  -DBUILD_SHARED_LIBS=ON \
  -DBUILD_TESTS=OFF \
  -DBUILD_PERF_TESTS=OFF \
  -DBUILD_EXAMPLES=OFF \
  -DBUILD_opencv_apps=OFF \
  -DBUILD_opencv_dnn=OFF \
  -DBUILD_opencv_ml=OFF \
  -DBUILD_opencv_objdetect=OFF \
  -DBUILD_opencv_photo=OFF \
  -DBUILD_opencv_stitching=OFF \
  -DBUILD_opencv_video=OFF \
  -DBUILD_opencv_calib3d=OFF \
  -DBUILD_opencv_features2d=OFF \
  -DBUILD_opencv_flann=OFF \
  -DBUILD_opencv_shape=OFF \
  -DBUILD_opencv_superres=OFF \
  -DBUILD_opencv_xphoto=OFF \
  -DBUILD_opencv_highgui=ON \
  -DBUILD_opencv_imgproc=ON \
  -DBUILD_opencv_imgcodecs=ON \
  -DBUILD_opencv_videoio=ON \
  -DWITH_GSTREAMER=ON \
  -DWITH_FFMPEG=ON \
  -DWITH_V4L=ON

And now build the libraries. This will take a while. Note that the config above puts the output into /opt/opencv-nocontrib. You can adjust that as you see fit, but you will need it for the config process for OpenCvSharpExtern.

bash
make -j$(nproc)
sudo make install

Building OpenCvSharpExtern from Source

Here we continue the build process from above. You must have already built OpenCV above and you will need the install path from above if you adjusted it.

First, clone the OpenCvSharp repository that contains the matching build configuration.

bash
cd ~
git clone https://github.com/shimat/opencvsharp.git
cd opencvsharp/src

Now configure this build to match the features you included in OpenCV. This is absolutely a manual process, so the list below only matches the build from above. Refer to the earlier feature table for your specific needs.

bash
mkdir ~/opencvsharp/src/build
cd ~/opencvsharp/src/build

cmake .. \
  -DCMAKE_BUILD_TYPE=Release \
  -DOpenCV_DIR=/opt/opencv-nocontrib/lib/cmake/opencv4 \
  -DNO_CONTRIB=ON \
  -DNO_STITCHING=ON \
  -DNO_CALIB3D=ON \
  -DNO_VIDEO=ON \
  -DNO_FEATURES2D=ON \
  -DNO_FLANN=ON \
  -DNO_DNN=ON \
  -DNO_ML=ON \
  -DNO_OBJDETECT=ON \
  -DNO_PHOTO=ON \
  -DNO_BARCODE=ON

And now build the libOpenCvSharpExtern binary:

bash
make -j$(nproc)

Collecting the Output

After building, you'll find:

  • OpenCV libraries in /opt/opencv-nocontrib/lib/
  • OpenCvSharpExtern in ~/opencvsharp/src/build/OpenCvSharpExtern/libOpenCvSharpExtern.so

Copy all required .so files to your application's runtime directory:

bash
mkdir -p ~/myapp/runtimes/linux-arm64/native
cp /opt/opencv-nocontrib/lib/libopencv_*.so* ~/myapp/runtimes/linux-arm64/native/
cp ~/opencvsharp/src/build/OpenCvSharpExtern/libOpenCvSharpExtern.so ~/myapp/runtimes/linux-arm64/native/

Verify the dependencies are satisfied:

bash
ldd ~/myapp/runtimes/linux-arm64/native/libOpenCvSharpExtern.so | grep "not found"

If no output, all dependencies are satisfied.