diff options
author | Anthony Wang | 2024-04-28 21:35:46 -0400 |
---|---|---|
committer | Anthony Wang | 2024-04-28 21:35:46 -0400 |
commit | 6dacec96754a493171d1d45dd4f5b7b683de6ccb (patch) | |
tree | b796ab74c60cb18d67571a5dee7a7a04fdad243d /decoder.py | |
parent | 80b6b746b82be2046847a658711b6845c39bc52f (diff) |
Use cv2.warpPerspective instead of dumb interpolation
https://docs.opencv.org/4.9.0/da/d6e/tutorial_py_geometric_transformations.html
Diffstat (limited to 'decoder.py')
-rw-r--r-- | decoder.py | 51 |
1 files changed, 12 insertions, 39 deletions
@@ -1,15 +1,14 @@ import argparse -import sys import traceback import cv2 -import matplotlib.pyplot as plt +# import matplotlib.pyplot as plt import numpy as np from creedsolo import RSCodec from raptorq import Decoder parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument("-i", "--input", help="camera device index or input video file", default=0) -parser.add_argument("-o", "--output", help="output file for decoded data") +parser.add_argument("-o", "--output", help="output file for decoded data", default="out") parser.add_argument("-x", "--height", help="grid height", default=100, type=int) parser.add_argument("-y", "--width", help="grid width", default=100, type=int) parser.add_argument("-f", "--fps", help="frame rate", default=30, type=int) @@ -38,7 +37,7 @@ def find_corner(A, f): mask = cv2.floodFill( A, np.empty(0), - tuple(reversed(guess)), + tuple(np.flip(guess)), 0, (100, 100, 100), (100, 100, 100), @@ -83,42 +82,16 @@ while data is None: bcol -= origin F = 255 * np.linalg.inv(np.stack((rcol, gcol, bcol)).T) - # Dumb perspective transform - xv = np.linspace( - -(cheight / 2 - 1) / (args.height - cheight + 1), - 1 + (cheight / 2 - 1) / (args.height - cheight + 1), - args.height, + cch = cheight / 2 - 1 + ccw = cwidth / 2 - 1 + M = cv2.getPerspectiveTransform( + np.float32([np.flip(widx), np.flip(ridx), np.flip(gidx), np.flip(bidx)]), + np.float32([[ccw, cch], [args.width - ccw - 1, cch], [ccw, args.height - ccw - 1], [args.width - ccw - 1, args.height - cch - 1]]), ) - yv = np.linspace( - -(cwidth / 2 - 1) / (args.width - cwidth + 1), - 1 + (cwidth / 2 - 1) / (args.width - cwidth + 1), - args.width, - ) - xp = ( - np.outer(1 - xv, 1 - yv) * widx[0] - + np.outer(1 - xv, yv) * ridx[0] - + np.outer(xv, 1 - yv) * gidx[0] - + np.outer(xv, yv) * bidx[0] - ) - yp = ( - np.outer(1 - xv, 1 - yv) * widx[1] - + np.outer(1 - xv, yv) * ridx[1] - + np.outer(xv, 1 - yv) * gidx[1] - + np.outer(xv, yv) * bidx[1] - ) - - # plt.scatter(widx[1], widx[0]) - # plt.scatter(ridx[1], ridx[0]) - # plt.scatter(gidx[1], gidx[0]) - # plt.scatter(bidx[1], bidx[0]) - # plt.scatter(yp, xp) - # plt.imshow(raw_frame.astype(np.uint8)) - # plt.show() - - frame = raw_frame[ - np.clip(np.round(xp).astype(np.int64), 0, X - 1), np.clip(np.round(yp).astype(np.int64), 0, Y - 1), : - ] + frame = cv2.warpPerspective(raw_frame, M, (args.width, args.height)) frame = (np.squeeze(F @ (frame - origin)[..., np.newaxis]) >= 128).astype(np.uint8) + # plt.imshow(frame * 255) + # plt.show() frame = np.concatenate( ( frame[:cheight, cwidth : args.width - cwidth].flatten(), @@ -130,7 +103,7 @@ while data is None: data = decoder.decode(bytes(rsc.decode(bytearray(np.packbits(frame) ^ frame_xor))[0][: args.psize])) print("Decoded frame") except KeyboardInterrupt: - sys.exit() + break except: traceback.print_exc() with open(args.output, "wb") as f: |