Skip to content

Commit 8f0a8a0

Browse files
committed
Add qrcode detect and decode
1 parent 9b77773 commit 8f0a8a0

File tree

5 files changed

+107
-0
lines changed

5 files changed

+107
-0
lines changed

python/code_120/README.md

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# 二维码检测和识别
2+
3+
## 概述
4+
根据Opencv的描述,OpenCV3.4.4以上版本支持二维码检测和识别!
5+
6+
## 函数
7+
8+
Opencv在对象检测模块中 `QRCodeDetector` 有两个相关API分别实现二维码检测与二维码解析。
9+
10+
1. 检测API
11+
```
12+
points = QRCodeDetector.detect(img)
13+
```
14+
其中:
15+
- img为输入图像,灰度或者彩色图像;
16+
- points输出得到的二维码四个点的坐标信息;
17+
18+
2. 识别API
19+
```
20+
straight_qrcode = QRCodeDetector.decode(img, points)
21+
```
22+
其中:
23+
- img为输入图像,灰度或者彩色图像;
24+
- points是二维码ROI最小外接矩形顶点坐标;
25+
- straight_qrcode输出的是二维码区域ROI图像信息
26+
返回的二维码utf-8字符串;
27+
28+
3. 结合检测识别的API
29+
```
30+
points,straight_qrcode = QRCodeDetector.detectAndDecode(img)
31+
```
32+
其中:
33+
- img为输入图像,灰度或者彩色图像;
34+
- points输出二维码ROI最小外接矩形顶点坐标;
35+
- straight_qrcode输出的是二维码区域ROI图像信息
36+
返回的二维码utf-8字符串;
37+
38+
## 代码
39+
40+
整体检测识别的代码如下:
41+
```python
42+
import cv2
43+
import numpy as np
44+
45+
# 读取二维码
46+
src = cv2.imread("qrcode.png")
47+
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
48+
# 设置检测器
49+
qrcoder = cv2.QRCodeDetector()
50+
# 检测识别二维码
51+
codeinfo, points, straight_qrcode = qrcoder.detectAndDecode(gray)
52+
result = np.copy(src)
53+
cv2.drawContours(result, [np.int32(points)], 0, (0, 0, 255), 2)
54+
# 输出识别二维码的信息
55+
print("qrcode information is : \n%s"% codeinfo)
56+
# 显示图片
57+
cv2.imshow("result", result)
58+
cv2.imshow("qrcode roi", np.uint8(straight_qrcode))
59+
cv2.waitKey(0)
60+
cv2.destroyAllWindows()
61+
```
62+
输出:
63+
```
64+
qrcode information is :
65+
Hello, this is Jimmy, thanks for your little star~
66+
```
67+
原图:
68+
69+
<img src=https://i.loli.net/2020/02/24/Lv4TuaMqPehZzFQ.png width=200>
70+
71+
检测:
72+
73+
<img src=https://i.loli.net/2020/02/24/3bY6AgUSCN75QFR.png width=200>
74+
75+
ROI图:
76+
77+
<img src=https://i.loli.net/2020/02/24/Wf2JVXcFbMaOQu6.png width=100>

python/code_120/opencv_120.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
"""
4+
@Date: 2020-02-24 18:06:06
5+
6+
@author: JimmyHua
7+
"""
8+
9+
import cv2
10+
import numpy as np
11+
12+
# load the qrcode
13+
src = cv2.imread("qrcode.png")
14+
cv2.imshow("image", src)
15+
# gray image
16+
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
17+
# set qrcoder
18+
qrcoder = cv2.QRCodeDetector()
19+
codeinfo, points, straight_qrcode = qrcoder.detectAndDecode(gray)
20+
print(points)
21+
result = np.copy(src)
22+
cv2.drawContours(result, [np.int32(points)], 0, (0, 0, 255), 2)
23+
print("qrcode information is :\n%s"% codeinfo)
24+
cv2.imshow("result", result)
25+
cv2.imwrite("result.png", result)
26+
code_roi = np.uint8(straight_qrcode)
27+
cv2.imshow("qrcode roi", code_roi)
28+
cv2.imwrite("qrcode_roi.png", code_roi)
29+
cv2.waitKey(0)
30+
cv2.destroyAllWindows()

python/code_120/qrcode.png

8.49 KB
Loading

python/code_120/qrcode_roi.png

278 Bytes
Loading

python/code_120/result.png

10.2 KB
Loading

0 commit comments

Comments
 (0)