1. libcamera and libcamera-apps工具簡介
- I. libcamera 是一款用來支持用戶基於Linux 操作系統控制複雜相機的應用程序,用戶可以通過libcamera直接操作樹莓派芯片集成的GPU模塊。
- II. libcamera 開放了基於C++語言的應用程序接口用於配置相機並從相機獲取圖像的功能API,同時libcamera管理的圖像緩衝buffer能夠直接輸入到圖像編碼器或者直接輸出到顯示模塊中。
- III. libcamera-app 是基於libcamera完成的相機控制例程,其代碼設計主要是模擬基於博通專有GPU代碼的傳統堆棧的功能,具體提供的功能如下:
- libcamera-hello: 輸出從相機中獲取的預覽圖像功能
- libcamera-jpeg: 通過相機獲取高分辨率的JPEG圖像
- libcamera-still: 一個功能類似於raspistill的相機視頻獲取程序
- libcamera-vid: 視頻捕獲程序
- libcamera-raw: 直接從相機中獲取未壓縮的原始RAW圖像程序
- libcamera-detect: 這個程序默認是不編譯的,如果用戶在樹莓派上安裝了TensorFlow Lite軟件則可以使用此程序進行目標檢測圖像捕獲。
- IV. libcamera工具基於樹莓派提供的圖像pipeline處理流程,能夠完成ISP圖像預處理功能,所有的功能均在樹莓派的ARM核心下完成,包括的功能如下:
- AEC/AGC (Auto Exposure/Gain Control)
- AWB (Auto White Balance)
- ALSC (Auto Lens Shading Correction)
- V. libcamera能夠支持的相機:OV5647 ,IMX219 ,IMX477 ,IMX708 ,IMX290, IMX327, OV9281, IMX378
- VI. Raspberry支持了用戶可編輯的 IQ Tunning File,從而能夠讓用戶進行圖像預處理ISP功能的調試優化,具體參考 官方 Tuning Guide for the Raspberry Pi cameras and libcamera,樹莓派相機Tuning實戰。
2. libcamera 使用測試
I. libcamera-hello 一般功能測試
libcamera-hello # 獲取相機設備並展示5s的實時視頻
libcamera-hello -t 0 # 一直獲取相機設備並展示,永不停止(使用Ctrl+C終止)
libcamera-hello --tuning-file /usr/share/libcamera/ipa/raspberrypi/imx219_noir.json -t 0 # 使用imx219無紅外濾波片模組的tuning文件進行ISP預處理
libcamera-hello --info-text "red gain %rg, blue gain %bg, Lens Focus %focus" # 直接獲取Camera當前紅色通道以及藍色通道的增益信息,相機鏡頭焦距(僅支持HQ Camera)
libcamera-hello --roi 0.25,0.25,0.5,0.5 # --roi Select a crop (region of interest) from the camera <x,y,w,h> 設置ROI起點和寬度&高度
libcamera-still --hdr -o hdr.jpg # 開啓相機的HDR模式
- 相機信息的查看 Tips:
--info-text
Directive Substitution %frame The sequence number of the frame %fps The instantaneous frame rate %exp The shutter speed used to capture the image, in microseconds %ag The analogue gain applied to the image in the sensor %dg The digital gain applied to the image by the ISP %rg The gain applied to the red component of each pixel %bg The gain applied to the blue component of each pixel %focus The focus metric for the image, where a larger value implies a sharper image %lp The current lens position in dioptres (1 / distance in metres). %afstate The autofocus algorithm state (one of idle, scanning, focused or failed).
II. libcamera-jpeg 圖像採集測試
libcamera-jpeg -o test.jpg # 獲取一張全分辨率的JPG圖片
libcamera-jpeg -o test.jpg -t 2000 --width 640 --height 480 # 預覽2000ms後在拍照,拍攝VGA照片分辨率爲 640*480
libcamera-jpeg -o test.jpg -t 2000 --shutter 20000 --gain 1.5 # 預覽2000ms後在拍照,拍攝曝光快門參數爲 20ms,增益爲1.5x(需要注意的是,當增益在CMOS模擬增益範圍內時,該參數控制的是模擬增益,當增益超出模擬增益最大值時,啓用數字增益)
libcamera-jpeg --ev -0.5 -o darker.jpg # 配置AEC/AGC 算法參數控制曝光補償,能夠通過設置該參數使得圖片明暗變化
libcamera-jpeg --ev 0 -o normal.jpg
libcamera-jpeg --ev 0.5 -o brighter.jpg
- 關於數字增益Tips:
- 數字增益實際上是ISP(image Signal Processor)的功能,而不是Sensor本身的功能,數字增益一般都爲1除非以下特殊情況
- 使用
--gain
參數項配置的增益值超出了Sensor自身能夠提供的模擬增益最大值的情況,數字增益ISP提供超出部分的增益功能 - 色彩增益也是數字增益的功能之一,對除了綠色通道以外的其他兩個通道 RB 通道進行 \(1 / min(red_{gain}, blue_{gain})\) 增益,從而歸一化色彩信息
- 當 AEC/AGC 參數發生變化時,數字增益將會發生變化以保證波動的平滑
III. libcamera-still 圖像獲取測試
libcamera-still -o test.jpg # 獲取一張全分辨率jpg圖像
libcamera-still -e png -o test.png # 編碼功能:輸出PNG格式圖片 -e ~ --encoding,如果未設定圖片輸出格式,將按照文件名稱自動確認
libcamera-still -e bmp -o test.bmp # 編碼功能:輸出bmp格式圖片
libcamera-still -e rgb -o test.data # 二進制數據直接輸出功能:輸出rgb RAW圖
libcamera-still -e yuv420 -o test.data # 二進制數據直接輸出功能:輸出 yuv420 RAW圖
# RAW圖可以被保存爲jpg,dng格式,而其存儲格式爲 DNG(Adobe Digital Negative) 格式,能夠兼容多種軟件: dcraw or RawTherapee
libcamera-still -r -o test.jpg # Raw Image Capture 相機原始RAW圖像捕獲輸出 libcamera-still --raw -o test.jpg
libcamera-still -o long_exposure.jpg --shutter 100000000 --gain 1 --awbgains 1,1 --immediate # 100s超長時間曝光,在使用超長時曝光功能之前,需要停止使能 AEC/AGC 和 AWB 的功能,這是由於這兩個功能將會強制等待若干幀圖像數據
- DNG file Tips:
- DNG文件信息中包括了圖像捕獲過程中的原始信息,包括了黑點平數據,白平衡信息,CCM顏色矯正矩陣信息等ISP需要使用的參數,這樣將大大方便後期人員手動進行RAW圖處理流程的實施
- 使用
exiftool
工具讀取 DNG 文件中的原始數據信息如下:
File Name : test.dng Directory : . File Size : 24 MB File Modification Date/Time : 2021:08:17 16:36:18+01:00 File Access Date/Time : 2021:08:17 16:36:18+01:00 File Inode Change Date/Time : 2021:08:17 16:36:18+01:00 File Permissions : rw-r--r-- File Type : DNG File Type Extension : dng MIME Type : image/x-adobe-dng Exif Byte Order : Little-endian (Intel, II) Make : Raspberry Pi Camera Model Name : /base/soc/i2c0mux/i2c@1/imx477@1a Orientation : Horizontal (normal) Software : libcamera-still Subfile Type : Full-resolution Image Image Width : 4056 Image Height : 3040 Bits Per Sample : 16 Compression : Uncompressed Photometric Interpretation : Color Filter Array Samples Per Pixel : 1 Planar Configuration : Chunky CFA Repeat Pattern Dim : 2 2 CFA Pattern 2 : 2 1 1 0 Black Level Repeat Dim : 2 2 Black Level : 256 256 256 256 White Level : 4095 DNG Version : 1.1.0.0 DNG Backward Version : 1.0.0.0 Unique Camera Model : /base/soc/i2c0mux/i2c@1/imx477@1a Color Matrix 1 : 0.8545269369 -0.2382823821 -0.09044229197 -0.1890484985 1.063961506 0.1062747385 -0.01334283455 0.1440163847 0.2593136724 As Shot Neutral : 0.4754476844 1 0.413686484 Calibration Illuminant 1 : D65 Strip Offsets : 0 Strip Byte Counts : 0 Exposure Time : 1/20 ISO : 400 CFA Pattern : [Blue,Green][Green,Red] Image Size : 4056x3040 Megapixels : 12.3 Shutter Speed : 1/20
IV. libcamera-vid 視頻功能測試
- 基本功能測試
libcamera-vid -t 10000 -o test.h264 # 保存10s的h264格式視頻,在不指定格式的情況下,vid默認保存格式也爲 h264,該保存的視頻可以使用 vlc來播放 vlc test.h264 # 播放視頻 libcamera-vid -o test.h264 --save-pts timestamps.txt # 輸出當前視頻對應的 timestamps 時間間隔文件 mkvmerge -o test.mkv --timecodes 0:timestamps.txt test.h264 # 使用 mkvmerge 工具,通過 timestamps 文件和原始視頻轉碼爲其他格式視頻數據 libcamera-vid -t 10000 --codec mjpeg -o test.mjpeg # 視頻錄製的保存格式設置功能 mjpeg 格式 libcamera-vid -t 10000 --codec yuv420 -o test.data # 視頻錄製的保存格式設置功能 yuv420 格式 libcamera-vid -t 10000 --codec mjpeg --segment 1 -o test%05d.jpeg # 錄製 mjpeg 格式的視頻,並將該視頻按照 1ms 的格式分割爲圖片保存
- 網絡流功能測試
# UDP 方式 libcamera-vid -t 0 --inline -o udp://<ip-addr>:<port> # Server視頻流服務端,ip-addr:port 爲客戶端IP地址:端口號 vlc udp://@:<port> :demux=h264 # 客戶端獲取Server端視頻流數據 # TCP 方式,30FPS條件下延時較低,樹莓派將等待客戶端的鏈接,然後才啓動視頻流服務 libcamera-vid -t 0 --inline --listen -o tcp://0.0.0.0:<port> # Server視頻流服務端,ip-addr:port 爲客戶端IP地址:端口號 vlc tcp/h264://<ip-addr-of-server>:<port> # 客戶端獲取Server端視頻流數據 # RTSP 網絡廣播視頻流方式 libcamera-vid -t 0 --inline -o - | cvlc stream:///dev/stdin --sout '#rtp{sdp=rtsp://:8554/stream1}' :demux=h264 # Server視頻流服務端 vlc rtsp://<ip-addr-of-server>:8554/stream1 # 客戶端獲取Server端視頻流數據
- 高幀率獲取圖像
- 需要配置 H.264 的功能版本爲
--level 4.2
- ISP軟件色彩去噪功能需要關閉,提高CPU能力
--denoise cdn_off
- 對於超出100FPS的應用需求,需要
-n
選項關閉CPU一些輔助的不必要的資源消耗,從而減少丟幀的情況 - 需要在
/boot/config.txt
文件中設置force_turbo=1
功能,保證CPU主頻不被限制 - 配置ISP處理的圖像像素尺寸小於等於
--width 1280 --height 720
- 通過
/boot/config.txt
文件中配置GPU超頻功能gpu_freq=550
- 1280x720 120fps 視頻編碼命令樣例:
libcamera-vid --level 4.2 --framerate 120 --width 1280 --height 720 --save-pts timestamp.pts -o video.264 -t 10000 --denoise cdn_off -n
- 需要配置 H.264 的功能版本爲
V. libcamera-raw RAW圖功能測試
libcamera-raw -t 2000 -o test.raw # 保存原始的RAW圖像到當前目錄下,保存過程中不會展示預覽視頻窗口,運行指令打印了當前保存RAW圖像的像素尺寸和Bayer的格式,從而方便用戶解析RAW數據
libcamera-raw -t 2000 --segment 1 -o test%05d.raw # 2s時間的圖像切片1ms保存一張,在存儲條件好的條件下, 例如SSD存儲器時,能夠在12MP的高清圖像以10FPS保存下來
libcamera-raw -t 5000 --width 4056 --height 3040 -o test.raw --framerate 8 # 配置圖像保存時間,圖像像素寬度&高度,保存的幀率
3. Post-Processing 圖像數據流處理
post-processing 作爲一種後處理框架,能夠有效地從Camera中獲取的數據輸送到用戶定製化的 圖像處理/神經網絡 算法流程當中,而具體完成算法的流程及算法當中涉及到的參數都通過 JSON 文件進行了傳遞,用戶可以脫離圖像處理算法本身,從而更爲便捷地完成圖像處理的功能。
a) 將圖像進行色彩亮度翻轉功能
libcamera-hello --post-process-file /path/to/negate.json # 其中negate.json爲圖像色彩翻轉算法的JSON文件
使用的 JSON 文件內容,由於色彩翻轉算法不需要任何的處理參數,所以僅僅需要 negate 即可。
{
"negate":
{
}
}
b)HDR 高動態功能
HDR算法中包括了HDR (high dynamic range) imaging 和 DRC (dynamic range compression)兩種算法,HDR 法實際上是基於多幀圖像的 DRC 算法。
算法的基本流程如下:
- 通過邊緣保持的平滑濾波器(edge-preserving smoothing filter)獲得一個低通圖像(LP).
- 將原始圖像與低通圖像進行差值獲得高通圖像(HP).
- 使用全局色調圖除了低通圖像數據 LP,並將處理後的結果加在HP數據上,從而保證最終的圖像保留了顯著的特徵
HDR模塊控制的參數如下表所示:
使用命令
libcamera-still -o test.jpg --post-process-file drc.json
上述處理流程在樹莓派4上運行的時間大約爲4s@12MP,
{
"hdr" :
{
"num_frames" : 1,
"lp_filter_strength" : 0.2,
"lp_filter_threshold" : [ 0, 10.0 , 2048, 205.0, 4095, 205.0 ],
"global_tonemap_points" :
[
{ "q": 0.1, "width": 0.05, "target": 0.15, "max_up": 1.5, "max_down": 0.7 },
{ "q": 0.5, "width": 0.05, "target": 0.5, "max_up": 1.5, "max_down": 0.7 },
{ "q": 0.8, "width": 0.05, "target": 0.8, "max_up": 1.5, "max_down": 0.7 }
],
"global_tonemap_strength" : 1.0,
"local_pos_strength" : [ 0, 6.0, 1024, 2.0, 4095, 2.0 ],
"local_neg_strength" : [ 0, 4.0, 1024, 1.5, 4095, 1.5 ],
"local_tonemap_strength" : 1.0,
"local_colour_scale" : 0.9
}
}
c) 複合的多流程 post-processing 功能
libcamera-hello --post-process-file /path/to/multiple.json # 其中multiple.json爲圖像Sobel邊緣提取 + 色彩翻轉算法的JSON文件
multiple.json文件中包括了第一位置的 Sobel 邊緣檢測算子,其算子的核矩陣大小爲 5x5,然後第二步進行了 圖像色彩翻轉功能
{
"sobel_cv":
{
"ksize": 5
},
"negate":
{
}
}
d) Opencv Post-Processing
-
sobel_cv
{ "sobel_cv": { "ksize": 5 } }
-
face_detect_cv
{ "face_detect_cv": { "cascade_name" : "/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml", "scaling_factor" : 1.1, "min_neighbors" : 2, "min_size" : 32, "max_size" : 256, "refresh_rate" : 1, "draw_features" : 1 } }
-
annotate_cv
{ "annotate_cv" : { "text" : "Frame %frame exp %exp ag %ag dg %dg", "fg" : 255, "bg" : 0, "scale" : 1.0, "thickness" : 2, "alpha" : 0.3 } }
e) Tensorflow Lite Post-Processing
在使用樹莓派平臺的TensorFlow Lite功能之前,需要對libcamera進行重新編譯以使能該功能,下面距離介紹基於TenlsorFlow的中間神經網絡處理流程。
- pose_estimation_tf
姿態估計識別框架,使用了 Google MobileNet v1 modelposenet_mobilenet_v1_100_257x257_multi_kpt_stripped.tflite
下載
基本命令:{ "pose_estimation_tf": { "refresh_rate" : 5, "model_file" : "posenet_mobilenet_v1_100_257x257_multi_kpt_stripped.tflite" }, "plot_pose_cv" : { "confidence_threshold" : -0.5 } }
libcamera-hello --post-process-file pose_estimation_tf.json --lores-width 258 --lores-height 258
運行結果:
其他圖像中間處理算法 JSON 參考: CVAlgJSONExample.json