首先宣告,我是在樹莓派4B全新安裝的,之前沒有安裝過舊的opencv,使用的樹莓派鏡像是
2021-10-30-raspios-bullseye-armhf.img 這個鏡像,剛安裝好CMake,安裝cmake的步驟簡單說下:
下載了cmake原始碼包后,就是在終端運行以下幾個命令:
mv /home/pi/Downloads/cmake-3.22.0.tar.gz /home/pi/cppProjects/cmake_src/
sudo apt install openssl libssl-dev
tar -zxvf cmake-3.22.0.tar.gz
cd cmake-3.22-0
./bootstrap
make
sudo make install
在樹莓派安裝OpenCV有些復雜,網上也有很多種安裝OpenCV的方法,比較混亂,經過我的搜索和梳理,找到了一種比較簡單的安裝方法,網址為:
https://qengineering.eu/install-opencv-4.5-on-raspberry-pi-4.html
大家最好先看看上面這個網頁,我也是完全按照它的建議,從github下載他提供的安裝腳本安裝的,直接按照上面網頁的步驟做即可,首先要更改GPU Memory, 然后要更新樹莓派的EEPROM,接著作者說了不要在python的PIP中安裝opencv等,確實都是好建議,
然后我們要運行unname -a確認下當前是64位系統還是32位系統,經過我的驗證,我下載的2021-10-30-raspios-bullseye-armhf.img 這個鏡像是32位系統,因此可以直接運行作者提供的shell腳本程式,接下來關鍵的一步是要設定Swap memory,因為opencv很吃記憶體,編譯程序要占用很大記憶體,必須要多提供些硬碟空間用于臨時記憶體,按照作者的要求修改 /sbin/dphys-swapfile檔案,下面最后一行有2048那個就是檔案要修改的地方,
restrict size (computed and absolute!) to maximally this limit
can be set to empty for no limit, but beware of filled partitions!
this is/was a (outdated?) 32bit kernel limit (in MBytes), do not overrun it
but is also sensible on 64bit to prevent filling /var or even / partition
CONF_MAXSWAP=2048 --------> 2048改成4096,
然后重啟系統,
接著去作者的github網站下載他的shell腳本檔案:
https://github.com/Qengineering/Install-OpenCV-Raspberry-Pi-32-bits
下載了該資料庫后,

下載后如上圖,隨便挑一個比如將OpenCV-4-5-0.sh檔案用WinSCP拷貝到樹莓派中,在終端直接運行下面兩句即可:
$ sudo chmod 755 ./OpenCV-4-5-0.sh
$ ./OpenCV-4-5-0.sh
一個shell腳本,完成了60多個指令,這就是shell腳本程式的好處,安裝CMake的程序也可以使用類似的shell腳本來實作,省去了打字的麻煩!注意,為了加快速度,我沒有使能QT功能,大家有需要自己修改OpenCV-4-5-x.sh腳本即可,
整個opencv安裝程序大約一個半小時,安裝完成后的截圖如下,你會看到
Congratulations!
You’ve successfully installed OpenCV 4.5.0 on your Raspberry Pi 32-bit OS
這兩句話,代表安裝完畢了!

最后修改 /sbin/dphys-swapfile檔案,將CONF_MAXSWAP=4096改回成2048,
restrict size (computed and absolute!) to maximally this limit
can be set to empty for no limit, but beware of filled partitions!
this is/was a (outdated?) 32bit kernel limit (in MBytes), do not overrun it
but is also sensible on 64bit to prevent filling /var or even / partition
CONF_MAXSWAP=4096 --------> 4096改成2048
找到了這篇文章,的確大大降低了安裝難度,大家可以看看這位CSDN朋友的安裝程序:
https://blog.csdn.net/qq_42642142/article/details/112474678
復雜得多,實際上就是將腳本程式一步一步地執行,不過經過這么折騰和總結,我相信他的軟體水平也會提高不少呢!
二、驗證OpenCV的安裝
1、使用Python驗證
如下:
import cv2
print(cv2.version)
截圖如下:

從上圖可知我剛安裝的opencv版本是4.5.0,
2、做一個OpenCV C++小程式驗證OpenCV的C++介面
打開我之前做的opencv驗證小程式,ch2_1

打開第一個CmakeLists.txt,如下:
cmake_minimum_required (VERSION 3.0)
cmake_policy(SET CMP0012 NEW)
PROJECT(Ch2_1)
find_package(OpenCV REQUIRED)
message(" opencv version: ${OpenCV_VERSION}")
include_directories(${OpenCV_INCLUDE_DIRS}) # Not needed for CMake >= 2.8.11
ADD_EXECUTABLE( sample1 sample1.cpp )
TARGET_LINK_LIBRARIES(sample1 ${OpenCV_LIBS})
我在第六行添加了一個message可以看到當前的opencv版本號,
第二個檔案就是影像處理領域大名鼎鼎的lena頭像了,大家隨便找個圖就行,我還是把圖貼上吧…

第三個sample1.cpp檔案也很簡單,就是將彩色圖轉換成灰度圖,并在終端顯示指定位置的像素值,
sample1.cpp檔案如下:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
// OpenCV includes
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
using namespace cv;
int main( int argc, const char** argv )
{
// Read images
Mat color= imread("../lena.jpg");
Mat gray= imread("../lena.jpg", IMREAD_GRAYSCALE);
if(! color.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
// Write images
imwrite("lenaGray.jpg", gray);
// Get same pixel with opencv function
int myRow=color.cols-1;
int myCol=color.rows-1;
auto pixel= color.at<Vec3b>(myRow, myCol);
cout << "Pixel value (B,G,R): (" << (int)pixel[0] << "," << (int)pixel[1] << "," << (int)pixel[2] << ")" << endl;
// show images
imshow("Lena BGR", color);
imshow("Lena Gray", gray);
// wait for any key press
waitKey(0);
return 0;
}
用WinSCP把ch2_1檔案夾拷到樹莓派中,然后在終端運行下列命令:
$mkdir build; cd build
$cmake …/
$cmake --build .
$./sample1
下面的截圖就是運行情況:

從上圖可看到CMake檢測到了opencv的版本為4.5.0

運行sample1檔案后,當正常看到了兩張美麗性感的Lena圖后,代表opencv的C++介面也可以正常使用了,

至此,opencv4.5.0就成功安裝到樹莓派上了,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/376048.html
標籤:其他
