From 4dee742731bb4b773942595d4ed4838d64916d15 Mon Sep 17 00:00:00 2001 From: Pranav Kaushik Date: Thu, 11 Jun 2026 08:43:27 -0700 Subject: [PATCH] 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 --- samples/python/qrcode.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/samples/python/qrcode.py b/samples/python/qrcode.py index 73c6cd3bd2..c7de5d141e 100644 --- a/samples/python/qrcode.py +++ b/samples/python/qrcode.py @@ -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: