opencv底层默认使用libjpeg做图像解码,libjpeg-turbo也是一种JPEG图像编解码器,解码速度通常是libjpeg的2-6倍,在条件相同的系统上,libjpeg-turbo可以在很大程度上优于libjpeg,这得益于其高度优化的Huffman编码。libjpeg-turbo的性能可以与专有的高速JPEG编解码器媲美,多线程的libjpeg-turbo解码速度与GPU加速的Nvidia nvJPEG硬解码相当。
opencv3.4.3下3rdparty下自带libjpeg和libjpeg-turbo库,可通过cmakelist选择编译,默认为libjpeg。即使选择libjpeg-turbo,效率也不是很高,可能该版本较老。另外下载编译最新的libjpeg-turbo-2.0.1库。编译libjpeg-turbo-2.0.1与编译libjpeg-turbo-1.5.6之前的库方式不同,本文做下记录。
1、编译cmake
下载最新的cmake-3.13.2.tar.gz
tar -xf cmake-3.13.2.tar.gz
cd cmake-3.13.2
sudo ./bootstrap
sudo make -j12
sudo make install
2、编译libjpeg-turbo-2.0.1
https://sourceforge.net/projects/libjpeg-turbo/
下载最新的libjpeg-turbo-2.0.1
apt-get install nasm
cd libjpeg-turbo-2.0.1
mkdir build
cd build
cmake -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE=RELEASE -DENABLE_STATIC=FALSE -DCMAKE_INSTALL_DOCDIR=/usr/share/doc/libjpeg-turbo-2.0.1 -DCMAKE_INSTALL_DEFAULT_LIBDIR=lib ..
make -j12
rm -f /usr/lib/libjpeg.so*
make install
3、编译opencv
修改cmakelist.txt
修改CMAKE_INSTALL_PREFIX到安装目录下,避免opencv多版本冲突
set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" CACHE PATH "Installation Directory")
打开BUILD_JPEG开关
OCV_OPTION(BUILD_JPEG "Build libjpeg from source" ON)
后续测试cuda 预处理,打开CUDA开关
OCV_OPTION(WITH_CUDA "Include NVidia Cuda Runtime support" ON )
OCV_OPTION(BUILD_CUDA_STUBS "Build CUDA modules stubs when no CUDA SDK" ON )
末尾增加message信息
message("****${JPEG_LIBRARY}")
修改cmake/OpenCVFindLibsGrfmt.cmake
通过JPEG_FOUND、BUILD_JPEG_TURBO_DISABLE选择解码库
# --- libjpeg (optional) ---
if(WITH_JPEG)
# if(BUILD_JPEG)
# ocv_clear_vars(JPEG_FOUND)
# else()
# include(FindJPEG)
# endif()
set(JPEG_FOUND ON)
set(BUILD_JPEG_TURBO_DISABLE ON)
if(NOT JPEG_FOUND)
ocv_clear_vars(JPEG_LIBRARY JPEG_LIBRARIES JPEG_INCLUDE_DIR)
if(NOT BUILD_JPEG_TURBO_DISABLE)
set(JPEG_LIBRARY libjpeg-turbo)
set(JPEG_LIBRARIES ${JPEG_LIBRARY})
add_subdirectory("${OpenCV_SOURCE_DIR}/3rdparty/libjpeg-turbo")
set(JPEG_INCLUDE_DIR "${${JPEG_LIBRARY}_SOURCE_DIR}/src")
set(JPEG_TYPE "libjpeg-turbo")
else()
message("********libjpeg")
set(JPEG_LIBRARY libjpeg)
set(JPEG_LIBRARIES ${JPEG_LIBRARY})
add_subdirectory("${OpenCV_SOURCE_DIR}/3rdparty/libjpeg")
set(JPEG_INCLUDE_DIR "${${JPEG_LIBRARY}_SOURCE_DIR}")
set(JPEG_TYPE "libjpeg")
endif()
endif()
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=../install -DWITH_JPEG=ON -DBUILD_JPEG=OFF -DJPEG_INCLUDE_DIR=/home/libjpeg-turbo-2.0.1 -DJPEG_LIBRARY=/home/libjpeg-turbo-2.0.1/build/libturbojpeg.so ..
通过最后的message("****${JPEG_LIBRARY}")信息查看是否编的libjpeg-turbo-2.0.1.so
make -j12
make install