|
| 1 | +import cv2 |
| 2 | +import numpy as np |
| 3 | +import tkinter as tk |
| 4 | +from tkinter.filedialog import * |
| 5 | + |
| 6 | +photo = askopenfilename() # reading the input image |
| 7 | +img = cv2.imread(photo) |
| 8 | +img = cv2.resize(img,(500,500)) |
| 9 | + |
| 10 | +window = tk.Tk() |
| 11 | +window.title("Perspective transform") |
| 12 | +window.geometry('350x200') |
| 13 | + |
| 14 | + |
| 15 | +# pts1 is an array storing coordinates of 4 points on the original image |
| 16 | +pts1 = np.float32([[103,97],[390,93],[85,351],[412,352]]) |
| 17 | +# pts2 is an array storing coordinates of 4 positions where the above points should be after the transformation |
| 18 | +pts2 = np.float32([[103,97],[390,93],[133,400],[390,400]]) |
| 19 | + |
| 20 | +Mat = cv2.getPerspectiveTransform(pts1,pts2) |
| 21 | +dst = cv2.warpPerspective(img, Mat, (500,500)) |
| 22 | + |
| 23 | +label = tk.Label(window, text="Points chosen on original image: " + str(pts1)).grid(row=0, column=1) |
| 24 | +label = tk.Label(window, text="Points on transformed image: " + str(pts2)).grid(row=1, column=1) |
| 25 | +label = tk.Label(window, text="The coordinates can be changed in the code ").grid(row=2, column=1) |
| 26 | +# displaying the images |
| 27 | +cv2.imshow("Original Image", img) |
| 28 | +cv2.imshow("Transformed Image", dst) |
| 29 | +cv2.waitKey(0) |
| 30 | +cv2.destroyAllWindows() |
| 31 | + |
| 32 | +window.mainloop() |
0 commit comments