源码包的获取
源码获取有两种方式,一种是通过编译时在makefile指定它的git地址,在编译时下载,这种很依赖网速,网速不好时,编译会失败。另一种是我们将源码的压缩包下载到本地,放到我们的SDK中,只需要解压编译就可以了,这里我们用第二种方式,选择我们需要的版本即可。
源码的下载链接: link
将我们下载好的opencv-4.9.0.tar.xz的压缩包放在SDK的dl/目录下
修改Makefile
SDK/feeds/packages/libs/opencv/Makefile,没有目录或者文件则新建
# Copyright (C) 2024 Fusion CCTV, Inc.
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#include $(TOPDIR)/rules.mkPKG_NAME:=opencv
PKG_VERSION:=4.9.0
PKG_RELEASE:=1PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.xz
PKG_SOURCE_URL:=http://sourceforge.net/projects/opencvlibrary/files/$(PKG_VERSION)/
PKG_MD5SUM:=b3e72c58db9b08a1f497f220a71e9332PKG_BUILD_DIR = $(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION)PKG_LICENSE:=BSD-3-Clause
PKG_LICENSE_FILES:=LICENSEPKG_INSTALL:=1
CMAKE_INSTALL:=1
CMAKE_BINARY_SUBDIR:=build
PKG_BUILD_PARALLEL:=0
PKG_USE_MIPS16:=0# We need to include python3-package.mk for the python variables
# but we are not a python package, so set PYTHON3_PKG_BUILD.
PYTHON3_PKG_BUILD:=0PKG_BUILD_DEPENDS:= \+OPENCV_PYTHON:python3/hostinclude $(INCLUDE_DIR)/package.mk
include $(INCLUDE_DIR)/cmake.mk
include $(TOPDIR)/feeds/packages/lang/python/python3-package.mkdefine Package/opencv/Default/descriptionOpenCV (Open Source Computer Vision Library) is an open source computervision and machine learning software library. OpenCV was built to providea common infrastructure for computer vision applications and to acceleratethe use of machine perception in the commercial products. Being aBSD-licensed product, OpenCV makes it easy for businesses to utilizeand modify the code.
endefdefine Package/opencv/defaultSECTION:=libsCATEGORY:=LibrariesTITLE:=opencvURL:=https://opencv.org/DEPENDS:= \+libpthread +librt +libatomic +libstdcpp +zlib +libjpeg +libwebp \+OPENCV_FFMPEG:ffmpeg \+OPENCV_FFMPEG:bzip2 \+OPENCV_FFMPEG:libffmpeg-full \+OPENCV_JPEG:libjpeg \+OPENCV_PYTHON:python3-numpy \+OPENCV_PYTHON:python3-numpy-src \+OPENCV_V4L:libv4l \+OPENCV_V4L:libv4l \+OPENCV_TIFF:libtiff \+OPENCV_PNG:libpng \+OPENCV_GSTREAMER:gstreamer1 \+OPENCV_GSTREAMER:gstreamer1-libs
endefdefine Package/opencv$(call Package/opencv/default)
endefifeq ($(CONFIG_OPENCV_FFMPEG),y)CMAKE_OPTIONS += -DWITH_FFMPEG=ON
elseCMAKE_OPTIONS += -DWITH_FFMPEG=OFF
endififeq ($(CONFIG_OPENCV_JPEG),y)CMAKE_OPTIONS += \-DWITH_JPEG=ON \-DBUILD_OPENJPEG=ON \-DBUILD_JPEG=ON
elseCMAKE_OPTIONS += \-DWITH_JPEG=OFF \-DBUILD_OPENJPEG=OFF \-DBUILD_JPEG=OFF
endififeq ($(CONFIG_OPENCV_PYTHON),y)CMAKE_OPTIONS += \-DBUILD_opencv_python2=OFF \-DBUILD_opencv_python3=ON \-DPYTHON3_INCLUDE_PATH=$(STAGING_DIR)/usr/include/python$(PYTHON3_VERSION) \-DPYTHON3_LIBRARIES=$(STAGING_DIR)/usr/lib/libpython$(PYTHON3_VERSION).so \-DPYTHON3_NUMPY_INCLUDE_DIRS=$(STAGING_DIR_ROOT)/usr/lib/python$(PYTHON3_VERSION)/site-packages/numpy/core/include \-DPYTHON3_PACKAGES_PATH=/usr/lib/python$(PYTHON3_VERSION)/site-packages
elseCMAKE_OPTIONS += \-DBUILD_opencv_python2=OFF \-DBUILD_opencv_python3=OFF
endififeq ($(CONFIG_OPENCV_V4L),y)CMAKE_OPTIONS += \-DWITH_LIBV4L=ON \-DWITH_v4L=ON
elseCMAKE_OPTIONS += \-DWITH_LIBV4L=OFF \-DWITH_v4L=OFF
endififeq ($(CONFIG_OPENCV_TIFF),y)CMAKE_OPTIONS += \-DWITH_TIFF=ON
elseCMAKE_OPTIONS += \-DWITH_TIFF=OFF
endififeq ($(CONFIG_OPENCV_PNG),y)CMAKE_OPTIONS += \-DWITH_PNG=ON
elseCMAKE_OPTIONS += \-DWITH_PNG=OFF
endififeq ($(CONFIG_OPENCV_GSTREAMER),y)CMAKE_OPTIONS += \-DWITH_GSTREAMER=ON
elseCMAKE_OPTIONS += \-DWITH_GSTREAMER=OFF
endififeq ($(CONFIG_OPENCV_PROTOBUF),y)CMAKE_OPTIONS += \-DBUILD_PROTOBUF=ON \-DWITH_PROTOBUF=ON
elseCMAKE_OPTIONS += \-DBUILD_PROTOBUF=OFF \-DWITH_PROTOBUF=OFF
endif# OpenCV component options
CMAKE_OPTIONS += \-DBUILD_opencv_freetype=ON \-DBUILD_DOCS=OFF \-DBUILD_PERF_TESTS=OFF \-DBUILD_TESTS=OFF \-DBUILD_WITH_DEBUG_INFO=OFF \-DDOWNLOAD_EXTERNAL_TEST_DATA=OFF \-DOPENCV_GENERATE_PKGCONFIG=ON \-DCMAKE_VERBOSE=OFF \-DOPENCV_ENABLE_PKG_CONFIG=ON# OpenCV build options
CMAKE_OPTIONS += \-DBUILD_WITH_STATIC_CRT=OFF \-DENABLE_CCACHE=OFF \-DENABLE_COVERAGE=OFF \-DENABLE_FAST_MATH=ON \-DENABLE_IMPL_COLLECTION=OFF \-DENABLE_NOISY_WARNINGS=OFF \-DENABLE_OMIT_FRAME_POINTER=ON \-DENABLE_PRECOMPILED_HEADERS=OFF \-DENABLE_PROFILING=OFF \-DOPENCV_WARNINGS_ARE_ERRORS=OFF \-DBUILD_opencv_gpu:BOOL=OFF \-DWITH_1394:BOOL=OFF -DBUILD_opencv_stitching=OFF \-DBUILD_opencv_superres:BOOL=OFF -DBUILD_opencv_ts:BOOL=OFF \-DBUILD_opencv_highgui=ON \-DBUILD_opencv_videostab=ON \-DWITH_GTK=OFF# OpenCV link options
CMAKE_OPTIONS += \-DCMAKE_INSTALL_RPATH_USE_LINK_PATH=OFF \-DCMAKE_SKIP_RPATH=OFF \-DCMAKE_USE_RELATIVE_PATHS=OFF# OpenCV packaging options:
CMAKE_OPTIONS += \-DBUILD_PACKAGE=OFF \-DENABLE_SOLUTION_FOLDERS=OFF \-DINSTALL_CREATE_DISTRIB=OFF# Cuda stuff
CMAKE_OPTIONS += \-DBUILD_CUDA_STUBS=OFF \-DBUILD_opencv_cudaarithm=OFF \-DBUILD_opencv_cudabgsegm=OFF \-DBUILD_opencv_cudacodec=OFF \-DBUILD_opencv_cudafeatures2d=OFF \-DBUILD_opencv_cudafilters=OFF \-DBUILD_opencv_cudaimgproc=OFF \-DBUILD_opencv_cudalegacy=OFF \-DBUILD_opencv_cudaobjdetect=OFF \-DBUILD_opencv_cudaoptflow=OFF \-DBUILD_opencv_cudastereo=OFF \-DBUILD_opencv_cudawarping=OFF \-DBUILD_opencv_cudev=OFF \-DWITH_CUBLAS=OFF \-DWITH_CUDA=OFF \-DWITH_CUFFT=OFF# NVidia stuff
CMAKE_OPTIONS += \-DWITH_NVCUVID=OFF \-DWITH_CAROTENE=OFF# AMD stuff
CMAKE_OPTIONS += \-DWITH_OPENCLAMDBLAS=OFF \-DWITH_OPENCLAMDFFT=OFF# Intel stuff
CMAKE_OPTIONS += \-DBUILD_WITH_DYNAMIC_IPP=OFF \-DWITH_INTELPERC=OFF \-DWITH_IPP=OFF \-DWITH_IPP_A=OFF# Smartek stuff
CMAKE_OPTIONS += -DWITH_GIGEAPI=OFF# Prosilica stuff
CMAKE_OPTIONS += -DWITH_PVAPI=OFF# Ximea stuff
CMAKE_OPTIONS += -DWITH_XIMEA=OFF# Non-Linux support (Android options) must remain OFF:
CMAKE_OPTIONS += \-DANDROID=OFF \-DBUILD_ANDROID_CAMERA_WRAPPER=OFF \-DBUILD_ANDROID_EXAMPLES=OFF \-DBUILD_ANDROID_SERVICE=OFF \-DBUILD_FAT_JAVA_LIB=OFF \-DINSTALL_ANDROID_EXAMPLES=OFF \-DWITH_ANDROID_CAMERA=OFF# Non-Linux support (Mac OSX options) must remain OFF:
CMAKE_OPTIONS += \-DWITH_AVFOUNDATION=OFF \-DWITH_CARBON=OFF \-DWITH_QUICKTIME=OFF# Non-Linux support (Windows options) must remain OFF:
CMAKE_OPTIONS += \-DWITH_CSTRIPES=OFF \-DWITH_DSHOW=OFF \-DWITH_MSMF=OFF \-DWITH_VFW=OFF \-DWITH_VIDEOINPUT=OFF \-DWITH_WIN32UI=OFF# Software/3rd-party support options:
CMAKE_OPTIONS += \-DBUILD_EXAMPLES=OFF \-DBUILD_JASPER=OFF \-DBUILD_OPENEXR=OFF \-DBUILD_PNG=OFF \-DBUILD_TIFF=OFF \-DBUILD_ZLIB=OFF \-DINSTALL_C_EXAMPLES=OFF \-DINSTALL_PYTHON_EXAMPLES=OFF \-DINSTALL_TO_MANGLED_PATHS=OFF# Disabled features (mostly because they are not available in OpenWRT)
CMAKE_OPTIONS += \-DWITH_CLP=OFF \-DWITH_GDAL=OFF \-DWITH_GPHOTO2=OFF \-DWITH_GSTREAMER_0_10=OFF \-DWITH_LAPACK=OFF \-DWITH_MATLAB=OFF \-DWITH_OPENCL=OFF \-DWITH_OPENCL_SVM=OFF \-DWITH_OPENEXR=OFF \-DWITH_OPENNI2=OFF \-DWITH_OPENNI=OFF \-DWITH_UNICAP=OFF \-DWITH_VA=OFF \-DWITH_VA_INTEL=OFF \-DWITH_VTK=OFF \-DWITH_XINE=OFFTARGET_LDFLAGS += -latomicdefine Build/InstallDev$(INSTALL_DIR) $(1)/usr/include$(CP) $(PKG_INSTALL_DIR)/usr/include/opencv4/ $(1)/usr/include/#$(CP) $(PKG_INSTALL_DIR)/usr/include/opencv2 $(1)/usr/include/$(INSTALL_DIR) $(1)/usr/lib$(CP) $(PKG_INSTALL_DIR)/usr/lib/libopencv* $(1)/usr/lib/
endefdefine Package/opencv/install$(INSTALL_DIR) $(1)/usr/lib$(CP) $(PKG_INSTALL_DIR)/usr/lib/libopencv* $(1)/usr/lib/ifeq ($(CONFIG_OPENCV_PYTHON),y)$(INSTALL_DIR) $(1)$(PYTHON3_PKG_DIR)cp -r $(PKG_INSTALL_DIR)$(PYTHON3_PKG_DIR)/cv2 $(1)$(PYTHON3_PKG_DIR)/cv2mv $(1)$(PYTHON3_PKG_DIR)/cv2/python-$(PYTHON3_VERSION)/cv2.cpython-*.so \$(1)$(PYTHON3_PKG_DIR)/cv2/python-$(PYTHON3_VERSION)/cv2.cpython-$(PYTHON3_VERSION).so
endif
endef$(eval $(call BuildPackage,opencv))
更新扩展包
这些都准备就绪后,我们需要将我们的opencv在编译时勾选,先更新我们的扩展包,使其在make menuconfig时出现
./scripts/feeds update -a
./scripts/feeds install -a
make menuconfig
在make menuconfig后勾选我们的opencv扩展,在libraries->opencv
全编
make V=s
编译成功后查看目录和生成文件
解压编译后的开源包:
SDK/build_dir/target-mipsel_24kc_musl/opencv-4.9.0/
生成的库文件目录:
SDK/staging_dir/target-mipsel_24kc_musl/usr/lib