?一、卸載 OpenCV 4.5.4?
清除已安裝的 OpenCV 庫?
sudo apt-get purge libopencv* python3-opencv # 卸載所有APT安裝的OpenCV包?:ml-citation{ref="1,3" data="citationList"}sudo apt autoremove # 清理殘留依賴?:ml-citation{ref="1,4" data="citationList"}
手動清理殘留文件?
sudo rm -rf /usr/local/include/opencv* # 刪除頭文件
sudo rm -rf /usr/local/lib/libopencv* # 刪除庫文件
?二、安裝 OpenCV 4.2.0?
?安裝依賴項?
sudo apt updatesudo apt install -y build-essential cmake git pkg-config libgtk2.0-dev \\libavcodec-dev libavformat-dev libswscale-dev libtbb2 libtbb-dev \\libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev # 基礎依賴?:ml-citation{ref="2,4" data="citationList"}
下載源碼?
wget https://github.com/opencv/opencv/archive/4.2.0.zip -O opencv-4.2.0.zip
wget https://github.com/opencv/opencv_contrib/archive/4.2.0.zip -O opencv_contrib-4.2.0.zip
unzip opencv-4.2.0.zip && unzip opencv_contrib-4.2.0.zip # 解壓源碼?:ml-citation{ref="2,4" data="citationList"}
配置 CMake?
cd opencv-4.2.0 && mkdir build && cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \\-D CMAKE_INSTALL_PREFIX=/usr/local \\-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.2.0/modules \\-D WITH_CUDA=ON \\-D CUDA_ARCH_BIN=5.3 # Jetson Xavier NX 需設為7.2?:ml-citation{ref="4,6" data="citationList"} \\-D ENABLE_NEON=ON \\-D BUILD_TESTS=OFF .. # 禁用測試以加速編譯?:ml-citation{ref="2,4" data="citationList"}
編譯與安裝?
make -j8 # 啟用多線程編譯
sudo make install # 安裝到系統目錄?:ml-citation{ref="4,6" data="citationList"}
?三、驗證安裝?
檢查版本?
pkg-config --modversion opencv # 應輸出4.2.0?:ml-citation{ref="2,4" data="citationList"}
Python 綁定驗證?
python3 -c "import cv2; print(cv2.__version__)" # 確認Python環境版本?:ml-citation{ref="2,6" data="citationList"}