通过超分辨率重构来提高二维码的对比度
1 問題描述
(1)圖像分辨率小。例如一些嵌入在海報(如圖1)或遠距離拍攝的碼,其分辨率遠小于通常情況下的碼圖像。
圖1.海報中的二維碼占比很小
(2)圖像質量較低。有很多是經過了多次的壓縮和轉發,存在嚴重的人工效應(如振鈴效應)干擾。
(3)由于光照等原因,導致二維碼亮度不均勻、圖像模糊等。
2 微信超分辨率重構的框架
通常情況下,相比于低分辨率圖像,高分辨率圖像能夠提供更豐富的細節,呈現出良好的視覺效果,對于二維碼識別這種以圖像作為輸入的后續流程,也有助于提高系統的整體表現。然而,受成像設備、存儲空間和網絡資源的限制,我們常常只能獲得較低分辨率的圖像。于是,超分辨率技術便派上了用場。
超分辨率技術以低分辨率圖像作為輸入,重建高分辨率輸出,是計算機視覺領域的一個經典問題。傳統的基于像素插值的超分算法以其計算簡單、速度快的優勢,被廣泛采用,如最近鄰(nearest)插值,雙線性(bilinear)插值以及雙三次(bicubic)插值等。但它們無法適應多樣的圖像內容,感知能力較差,難以重建出圖像的高頻信息,輸出圖像過于模糊,應用于壓縮圖像時,甚至會放大人工效應干擾。
近年來,計算機視覺技術和深度學習飛速發展,學術界開始關注利用深度學習解決圖像重建問題。我們根據掃碼圖像的特點,設計了適用于移動端的超分網絡,實現了快速高效的碼圖像重建,并改善圖像質量,大大提高了掃碼算法的識別成功率。網絡結構如圖所示。
圖1 碼圖像重建網絡結構
3 API
基于OpenCV開發了相應的API。
3.1 cv.wechat_qrcode_WeChatQRCode
cv.wechat_qrcode_WeChatQRCode(detector_prototxt_path, detector_caffe_model_path, super_resolution_prototxt_path, super_resolution_caffe_model_path) -> <wechat_qrcode_WeChatQRCode object>初始化微信 QR 碼檢測模塊,包括兩個基于 CNN 的模型:對象檢測模型和超分辨率模型。對象檢測模型用于檢測帶有邊界框的二維碼。當 QR 碼較小時,采用超分辨率模型對其進行放大。
文件地址:https://github.com/WeChatCV/opencv_3rdparty
3.1.1 參數說明:
| detector_prototxt_path | 對象檢測模型的prototex文件路徑 |
| detector_caffe_model_path | 對象檢測模型的caffe模型文件路徑 |
| super_resolution_prototxt_path | 超分辨率模型的prototex文件路徑 |
| super_resolution_caffe_model_path | 超分辨率模型的caffe模型文件路徑 |
3.1.2 返回值:
wechat_qrcode_WeChatQRCode 對象
3.2 cv.wechat_qrcode_WeChatQRCode.detectAndDecode
cv.wechat_qrcode_WeChatQRCode.detectAndDecode(img[, points]) ->retval, points檢測和解析二維碼
3.2.1 參數說明:
| img | opencv 讀取的灰度圖或 BGR 圖 |
| points | 檢測到 QR 碼四邊形頂點后,將其位置輸出到該數組 |
3.2.2 返回值:
retval: 解析結果
points: QR 碼四邊形頂點位置數組,如果找不到則為空
4 二維碼提升對比度測試
4.1 測試代碼:
import cv2 import osif __name__ == '__main__':# 初始化對象detector = cv2.wechat_qrcode_WeChatQRCode("detect.prototxt","detect.caffemodel","sr.prototxt","sr.caffemodel")results = {"image": [], "res": [], "points": []}image_dir = "src"files = os.listdir(image_dir)# 遍歷圖片目錄for f in files:image_file = os.path.join(image_dir, f)img = cv2.imread(image_file) # 讀取圖像res, points = detector.detectAndDecode(img) # 檢測圖像if res: # 非空表示檢測成功results["image"].append(image_file)results["res"].append(res)results["points"].append(points)image_num = len(files)detected_num = len(results['res'])print("Image dir:", image_dir)print(f"Processed images: {image_num} , detected images: {detected_num}",f"detection success rate : {detected_num / image_num * 100}%. ")print("Detected images are:")for i in results['image']:print(i, end=',')4.2 輸出結果
Image dir: src
Processed images: 134 , detected images: 19 detection success rate : 14.17910447761194%.
Dected images are:
src/4_217.jpg,src/4_135.jpg,src/10_144.jpg,src/4_171.jpg,src/19_146.jpg,src/4_148.jpg,src/19_164.jpg,src/6_29.jpg,src/19_171.jpg,src/4_39.jpg,src/2.jpg,src/6_41.jpg,src/19_162.jpg,src/19_154.jpg,src/19_181.jpg,src/4_228.jpg,src/4_140.jpg,src/4_26.jpg,src/4_38.jpg
Image dir: detect
Processed images: 137 , detected images: 22 detection success rate : 16.05839416058394%.
Detected images are:
detect/4_217.jpg,detect/4_171.jpg,detect/19_146.jpg,detect/4_148.jpg,detect/19_164.jpg,detect/4_196.jpg,detect/19_151.jpg,detect/4_76.jpg,detect/4_81.jpg,detect/4_39.jpg,detect/19_159.jpg,detect/2.jpg,detect/19_165.jpg,detect/6_41.jpg,detect/19_162.jpg,detect/19_154.jpg,detect/19_137.jpg,detect/5_13.jpg,detect/4_132.jpg,detect/4_228.jpg,detect/4_26.jpg,detect/4_38.jpg
5 簡單分析
以未能成功檢測的src/5_50.jpg為例:
該圖片通過微信掃碼可以成功檢測,檢測成功時攝像頭離圖片較近,因此考慮在 QR 檢測程序中對圖片進行縮放預處理:
x, y = img.shape[0:2] img = cv2.resize(img, (int(y * 1.5), int(x * 1.5)))結果如下:
Image dir: detect
Processed images: 137 , detected images: 33 detection success rate : 24.087591240875913%.
Detected images are:
detect/4_217.jpg,detect/4_135.jpg,detect/10_144.jpg,detect/4_171.jpg,detect/4_133.jpg,detect/4_69.jpg,detect/19_164.jpg,detect/19_151.jpg,detect/6_29.jpg,detect/4_76.jpg,detect/6_2.jpg,detect/7_47.jpg,detect/4_81.jpg,detect/4_39.jpg,detect/2.jpg,detect/19_165.jpg,detect/15_75.jpg,detect/7_44.jpg,detect/7_7.jpg,detect/6_12.jpg,detect/19_162.jpg,detect/11_99.jpg,detect/4_100.jpg,detect/19_154.jpg,detect/19_137.jpg,detect/5_50.jpg,detect/5_13.jpg,detect/19_181.jpg,detect/14_11.jpg,detect/4_228.jpg,detect/6_30.jpg,detect/4_26.jpg,detect/4_38.jpg
Image dir: src
Processed images: 134 , detected images: 32 detection success rate : 23.88059701492537%.
Detected images are:
src/4_217.jpg,src/4_135.jpg,src/10_144.jpg,src/4_171.jpg,src/19_146.jpg,src/4_69.jpg,src/19_164.jpg,src/4_196.jpg,src/6_29.jpg,src/4_76.jpg,src/19_171.jpg,src/6_2.jpg,src/4_220.jpg,src/7_47.jpg,src/4_81.jpg,src/7_29.jpg,src/4_39.jpg,src/19_159.jpg,src/4_237.jpg,src/2.jpg,src/19_165.jpg,src/15_75.jpg,src/4_115.jpg,src/19_162.jpg,src/19_154.jpg,src/5_50.jpg,src/5_13.jpg,src/19_181.jpg,src/4_228.jpg,src/6_30.jpg,src/4_26.jpg,src/4_38.jpg
測試結果表明 opencv 中的 QR 碼識別 API 主要針對理想位置和大小的 QR 碼圖像進行識別,而簡單的圖像預處理可以提高 QR 碼識別成功率。
6 總結及展望
我們目前通過調用微信API來實現檢測和超分辨率重構,下一步工作將搭建圖1所示的網絡,利用微信已經訓練好的模型,來看看通過超分辨率重構后的效果。
參考文獻
https://blog.csdn.net/qq_35054151/article/details/113647111
總結
以上是生活随笔為你收集整理的通过超分辨率重构来提高二维码的对比度的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (pytorch-深度学习系列)pyto
- 下一篇: 《碟中谍5》中惊鸿一瞥的步态识别技术,究