Skip to content

Instantly share code, notes, and snippets.

@shirou
Last active March 9, 2020 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shirou/1bdf41967eab2aee9a0bab3aefce6e78 to your computer and use it in GitHub Desktop.
Save shirou/1bdf41967eab2aee9a0bab3aefce6e78 to your computer and use it in GitHub Desktop.
import sys
import cv2
def sliding_window(image, stepSize, windowSize):
for y in range(0, image.shape[0], stepSize):
for x in range(0, image.shape[1], stepSize):
# yield the current window
yield (x, y, image[y : y + windowSize[1], x : x + windowSize[0]])
img = cv2.imread(sys.argv[1])
qr = cv2.QRCodeDetector()
winW = int(img.shape[1] / 4)
winH = int(img.shape[0] / 4)
step = int(img.shape[1] / 5)
print("image size: {} x {} ".format(img.shape[1], img.shape[0]))
print("window size: {} x {}, step={} ".format(winW, winH, step))
for (x, y, window) in sliding_window(img, stepSize=step, windowSize=(winW, winH)):
print(x, y, window.shape)
if window.shape[0] != winH or window.shape[1] != winW:
continue
data, points, straight_qrcode = qr.detectAndDecode(window)
cv2.imwrite("cut-{}-{}.jpg".format(x, y), window)
if data:
print("detected: {}".format(data))
break
import sys
from itertools import product
import cv2
def split(n: int, d: int):
i = int(n / d)
ret = [(x, x + i) for x in range(0, n, i)]
return ret
def detect(img, divide):
i = 0
height = img.shape[0]
width = img.shape[1]
for (sx, ex), (sy, ey) in product(split(height, divide), split(width, divide)):
print(divide, int(sx), int(ex), int(sy), int(ey))
dst = img[int(sx) : int(ex), int(sy) : int(ey)]
cv2.imwrite("cut-{}.jpg".format(i), dst)
data, points, straight_qrcode = qr.detectAndDecode(dst)
if data:
return data
i += 1
img = cv2.imread(sys.argv[1])
qr = cv2.QRCodeDetector()
for divide in range(1, 4):
data = detect(img, divide)
if data:
print("データ:", data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment