|
| 1 | +import cv2 |
| 2 | +import numpy as np |
| 3 | +import argparse |
| 4 | + |
| 5 | +# global vars |
| 6 | +arr = [] |
| 7 | +count = 0 |
| 8 | + |
| 9 | +# setup argparse |
| 10 | +parser = argparse.ArgumentParser() |
| 11 | +parser.add_argument("-p", "--path", help = "path to image") |
| 12 | +args = vars(parser.parse_args()) |
| 13 | + |
| 14 | +# transform image to perspective form using warpPerspective |
| 15 | +def transform(img, arr): |
| 16 | + (tl, tr, br, bl) = arr |
| 17 | + |
| 18 | + # find the maximum width of selected object |
| 19 | + widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2)) |
| 20 | + widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2)) |
| 21 | + w = max(int(widthA), int(widthB)) |
| 22 | + |
| 23 | + # find the maximum height of selected object |
| 24 | + heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2)) |
| 25 | + heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2)) |
| 26 | + h = max(int(heightA), int(heightB)) |
| 27 | + |
| 28 | + print("New Image Dimensions: \nWidth = {}\nHeight = {}".format(w,h)) |
| 29 | + # Create source and destination points |
| 30 | + inp_pts = np.float32(arr) |
| 31 | + op_pts = np.float32([[0,0],[w-1,0],[w-1,h-1],[0,h-1]]) |
| 32 | + |
| 33 | + # Create Perspective Transform and perform warp Perspective |
| 34 | + M = cv2.getPerspectiveTransform(inp_pts,op_pts) |
| 35 | + out = cv2.warpPerspective(img, M, (w, h)) |
| 36 | + |
| 37 | + return out |
| 38 | + |
| 39 | +# read image |
| 40 | +img = cv2.imread(args["path"]) |
| 41 | + |
| 42 | +# resize image |
| 43 | +width = 500 |
| 44 | +height = int((img.shape[0] * 500)/(img.shape[1])) |
| 45 | +dim = (width, height) |
| 46 | +img = cv2.resize(img, dim, interpolation = cv2.INTER_AREA) |
| 47 | + |
| 48 | +cv2.imshow('image', img) |
| 49 | + |
| 50 | +# helper function to append coordinates |
| 51 | +def points(x,y): |
| 52 | + if len(arr) <= 4: |
| 53 | + arr.append((x,y)) |
| 54 | + return len(arr) |
| 55 | + |
| 56 | +# helper function to draw connector lines |
| 57 | +def draw(count, img): |
| 58 | + if(count == 2): |
| 59 | + cv2.line(img, arr[0], arr[1], (255,255,255), 2) |
| 60 | + if(count == 3): |
| 61 | + cv2.line(img, arr[1], arr[2], (255,255,255), 2) |
| 62 | + if(count == 4): |
| 63 | + cv2.line(img, arr[2], arr[3], (255,255,255), 2) |
| 64 | + cv2.line(img, arr[3], arr[0], (255,255,255), 2) |
| 65 | + |
| 66 | +# mouseclick events |
| 67 | +def onClick(event, x, y, flags, param): |
| 68 | + if event == cv2.EVENT_LBUTTONDOWN: |
| 69 | + count = points(x,y) |
| 70 | + if count <= 4: |
| 71 | + cv2.circle(img, (x,y), 2, (255,255,0), 10) |
| 72 | + draw(count, img) |
| 73 | + cv2.imshow('image', img) |
| 74 | + |
| 75 | +cv2.setMouseCallback('image', onClick) |
| 76 | +cv2.waitKey(0) |
| 77 | + |
| 78 | +# transform image with mentioned coordinates |
| 79 | +out = transform(img, arr) |
| 80 | + |
| 81 | +cv2.imshow('final', out) |
| 82 | +cv2.waitKey(0) |
| 83 | +cv2.destroyAllWindows() |
0 commit comments