源码包的获取
源码获取有两种方式,一种是通过编译时在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.mk
PKG_NAME:=opencv
PKG_VERSION:=4.9.0
PKG_RELEASE:=1
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.xz
PKG_SOURCE_URL:=http://sourceforge.net/projects/opencvlibrary/files/$(PKG_VERSION)/
PKG_MD5SUM:=b3e72c58db9b08a1f497f220a71e9332
PKG_BUILD_DIR = $(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION)
PKG_LICENSE:=BSD-3-Clause
PKG_LICENSE_FILES:=LICENSE
PKG_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:=0
PKG_BUILD_DEPENDS:= \
+OPENCV_PYTHON:python3/host
include $(INCLUDE_DIR)/package.mk
include $(INCLUDE_DIR)/cmake.mk
include $(TOPDIR)/feeds/packages/lang/python/python3-package.mk
define Package/opencv/Default/description
OpenCV (Open Source Computer Vision Library) is an open source computer
vision and machine learning software library. OpenCV was built to provide
a common infrastructure for computer vision applications and to accelerate
the use of machine perception in the commercial products. Being a
BSD-licensed product, OpenCV makes it easy for businesses to utilize
and modify the code.
endef
define Package/opencv/default
SECTION:=libs
CATEGORY:=Libraries
TITLE:=opencv
URL:=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
endef
define Package/opencv
$(call Package/opencv/default)
endef
ifeq ($(CONFIG_OPENCV_FFMPEG),y)
CMAKE_OPTIONS += -DWITH_FFMPEG=ON
else
CMAKE_OPTIONS += -DWITH_FFMPEG=OFF
endif
ifeq ($(CONFIG_OPENCV_JPEG),y)
CMAKE_OPTIONS += \
-DWITH_JPEG=ON \
-DBUILD_OPENJPEG=ON \
-DBUILD_JPEG=ON
else
CMAKE_OPTIONS += \
-DWITH_JPEG=OFF \
-DBUILD_OPENJPEG=OFF \
-DBUILD_JPEG=OFF
endif
ifeq ($(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
else
CMAKE_OPTIONS += \
-DBUILD_opencv_python2=OFF \
-DBUILD_opencv_python3=OFF
endif
ifeq ($(CONFIG_OPENCV_V4L),y)
CMAKE_OPTIONS += \
-DWITH_LIBV4L=ON \
-DWITH_v4L=ON
else
CMAKE_OPTIONS += \
-DWITH_LIBV4L=OFF \
-DWITH_v4L=OFF
endif
ifeq ($(CONFIG_OPENCV_TIFF),y)
CMAKE_OPTIONS += \
-DWITH_TIFF=ON
else
CMAKE_OPTIONS += \
-DWITH_TIFF=OFF
endif
ifeq ($(CONFIG_OPENCV_PNG),y)
CMAKE_OPTIONS += \
-DWITH_PNG=ON
else
CMAKE_OPTIONS += \
-DWITH_PNG=OFF
endif
ifeq ($(CONFIG_OPENCV_GSTREAMER),y)
CMAKE_OPTIONS += \
-DWITH_GSTREAMER=ON
else
CMAKE_OPTIONS += \
-DWITH_GSTREAMER=OFF
endif
ifeq ($(CONFIG_OPENCV_PROTOBUF),y)
CMAKE_OPTIONS += \
-DBUILD_PROTOBUF=ON \
-DWITH_PROTOBUF=ON
else
CMAKE_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=OFF
TARGET_LDFLAGS += -latomic
define 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/
endef
define 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)/cv2
mv $(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