1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-29 15:23:05 +04:00

Merge pull request #29281 from pranavkaushik1:improve-qrcode-colors

samples: add color-coded bounding boxes for multi QR code detection #29281

Improves the existing qrcode.py sample by adding color-coded bounding boxes when multiple QR codes are detected simultaneously.

Previously all QR codes were drawn with the same green color, making it hard to distinguish between them visually.

This change adds a QR_COLORS palette so each detected QR code gets a unique color, improving visual clarity for multi-QR detection.

Co-authored-by: Ayush Gupta <gupta.ayushg@gmail.com>
This commit is contained in:
Pranav Kaushik
2026-06-11 08:43:27 -07:00
committed by GitHub
parent a77474f3b1
commit 4dee742731
+16 -4
View File
@@ -21,6 +21,18 @@ PY3 = sys.version_info[0] == 3
if PY3:
xrange = range
# Colors for distinguishing multiple QR codes visually
QR_COLORS = [
(0, 255, 0), # green
(255, 0, 0), # blue
(0, 0, 255), # red
(255, 255, 0), # cyan
(0, 255, 255), # yellow
(255, 0, 255), # magenta
(128, 255, 0), # lime
(255, 128, 0), # orange
]
class QrSample:
def __init__(self, args):
@@ -46,15 +58,14 @@ class QrSample:
cv.putText(result, message, (20, 20), 1,
cv.FONT_HERSHEY_DUPLEX, (0, 0, 255))
def drawQRCodeContours(self, image, cnt):
def drawQRCodeContours(self, image, cnt, color=(0, 255, 0)):
if cnt.size != 0:
rows, cols, _ = image.shape
show_radius = 2.813 * ((rows / cols) if rows > cols else (cols / rows))
contour_radius = show_radius * 0.4
cv.drawContours(image, [cnt], 0, (0, 255, 0), int(round(contour_radius)))
cv.drawContours(image, [cnt], 0, color, int(round(contour_radius)))
tpl = cnt.reshape((-1, 2))
for x in tuple(tpl.tolist()):
color = (255, 0, 0)
cv.circle(image, tuple(x), int(round(contour_radius)), color, -1)
def drawQRCodeResults(self, result, points, decode_info, fps):
@@ -64,7 +75,8 @@ class QrSample:
if n > 0:
for i in range(n):
cnt = np.array(points[i]).reshape((-1, 1, 2)).astype(np.int32)
self.drawQRCodeContours(result, cnt)
color = QR_COLORS[i % len(QR_COLORS)]
self.drawQRCodeContours(result, cnt, color)
msg = 'QR[{:d}]@{} : '.format(i, *(cnt.reshape(1, -1).tolist()))
print(msg, end="")
if len(decode_info) > i: